2
2
3
3
import android .content .Context ;
4
4
import android .media .MediaRouter ;
5
+ import android .os .Build ;
5
6
import android .util .Log ;
6
7
import android .hardware .display .DisplayManager ;
7
8
import android .view .Display ;
@@ -13,14 +14,29 @@ public static boolean isScreenSharingActive(Context context) {
13
14
MediaRouter mediaRouter = (MediaRouter ) context .getSystemService (Context .MEDIA_ROUTER_SERVICE );
14
15
MediaRouter .RouteInfo route = mediaRouter .getSelectedRoute (MediaRouter .ROUTE_TYPE_LIVE_VIDEO );
15
16
16
- boolean isScreenSharing = route != null && route .isEnabled () &&
17
- route .getDeviceType () == MediaRouter .RouteInfo .DEVICE_TYPE_TV ;
17
+ boolean isScreenSharing = false ;
18
+
19
+ if (route != null && route .isEnabled ()) {
20
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .N ) {
21
+ // Only call getDeviceType() on API 24 and above
22
+ isScreenSharing = route .getDeviceType () == MediaRouter .RouteInfo .DEVICE_TYPE_TV ;
23
+ } else {
24
+ // Fallback logic if needed for lower APIs — or assume no screen sharing
25
+ Log .w (TAG , "getDeviceType() not supported on API < 24" );
26
+ }
27
+ }
28
+
18
29
Log .d (TAG , "Screen Sharing Active: " + isScreenSharing );
19
30
return isScreenSharing ;
31
+
20
32
}
21
33
22
34
public static boolean isScreenMirrored (Context context ) {
23
35
DisplayManager displayManager = (DisplayManager ) context .getSystemService (Context .DISPLAY_SERVICE );
36
+ if (displayManager == null ) {
37
+ Log .w (TAG , "DisplayManager not available. Cannot check for screen mirroring." );
38
+ return false ;
39
+ }
24
40
Display [] displays = displayManager .getDisplays ();
25
41
26
42
for (Display display : displays ) {
0 commit comments