Skip to content

Commit 9e5c68c

Browse files
cybersphinxAudricV
authored andcommitted
Add check for input devices with cursor.
1 parent 88eed6c commit 9e5c68c

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

app/src/main/java/org/schabi/newpipe/util/DeviceUtils.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
import android.content.pm.PackageManager;
77
import android.content.res.Configuration;
88
import android.graphics.Point;
9+
import android.hardware.input.InputManager;
910
import android.os.BatteryManager;
1011
import android.os.Build;
1112
import android.provider.Settings;
1213
import android.util.TypedValue;
14+
import android.view.InputDevice;
1315
import android.view.KeyEvent;
1416
import android.view.WindowInsets;
1517
import android.view.WindowManager;
@@ -26,6 +28,8 @@
2628
import java.lang.reflect.InvocationTargetException;
2729
import java.lang.reflect.Method;
2830

31+
import static android.content.Context.INPUT_SERVICE;
32+
2933
public final class DeviceUtils {
3034

3135
private static final String AMAZON_FEATURE_FIRE_TV = "amazon.hardware.fire_tv";
@@ -95,6 +99,25 @@ public static boolean isTv(final Context context) {
9599
* @return true if the Android device is in desktop mode or using DeX.
96100
*/
97101
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+
98121
if (ContextCompat.getSystemService(context, UiModeManager.class)
99122
.getCurrentModeType() == Configuration.UI_MODE_TYPE_DESK) {
100123
return true;

0 commit comments

Comments
 (0)