|
6 | 6 | import android.content.pm.PackageManager; |
7 | 7 | import android.content.res.Configuration; |
8 | 8 | import android.graphics.Point; |
| 9 | +import android.hardware.input.InputManager; |
9 | 10 | import android.os.BatteryManager; |
10 | 11 | import android.os.Build; |
11 | 12 | import android.provider.Settings; |
12 | 13 | import android.util.TypedValue; |
| 14 | +import android.view.InputDevice; |
13 | 15 | import android.view.KeyEvent; |
14 | 16 | import android.view.WindowInsets; |
15 | 17 | import android.view.WindowManager; |
|
26 | 28 | import java.lang.reflect.InvocationTargetException; |
27 | 29 | import java.lang.reflect.Method; |
28 | 30 |
|
| 31 | +import static android.content.Context.INPUT_SERVICE; |
| 32 | + |
29 | 33 | public final class DeviceUtils { |
30 | 34 |
|
31 | 35 | private static final String AMAZON_FEATURE_FIRE_TV = "amazon.hardware.fire_tv"; |
@@ -95,6 +99,25 @@ public static boolean isTv(final Context context) { |
95 | 99 | * @return true if the Android device is in desktop mode or using DeX. |
96 | 100 | */ |
97 | 101 | public static boolean isDesktopMode(final Context context) { |
| 102 | + // Adapted from https://stackoverflow.com/a/64615568 |
| 103 | + // to check for all devices that have an active cursor |
| 104 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| 105 | + final InputManager im = (InputManager) context.getSystemService(INPUT_SERVICE); |
| 106 | + for (final int id : im.getInputDeviceIds()) { |
| 107 | + final InputDevice inputDevice = im.getInputDevice(id); |
| 108 | + if ( |
| 109 | + inputDevice.supportsSource(InputDevice.SOURCE_BLUETOOTH_STYLUS) |
| 110 | + || inputDevice.supportsSource(InputDevice.SOURCE_MOUSE) |
| 111 | + || inputDevice.supportsSource(InputDevice.SOURCE_STYLUS) |
| 112 | + || inputDevice.supportsSource(InputDevice.SOURCE_TOUCHPAD) |
| 113 | + || inputDevice.supportsSource(InputDevice.SOURCE_TRACKBALL) |
| 114 | + ) { |
| 115 | + return true; |
| 116 | + } |
| 117 | + } |
| 118 | + return false; |
| 119 | + } |
| 120 | + |
98 | 121 | if (ContextCompat.getSystemService(context, UiModeManager.class) |
99 | 122 | .getCurrentModeType() == Configuration.UI_MODE_TYPE_DESK) { |
100 | 123 | return true; |
|
0 commit comments