Skip to content

Commit abf1cc5

Browse files
committed
Improve code of DeviceUtils.isDesktopMode
- Avoid NullPointerException crashes if there is no UiModeManager or desktop system service mode - Use final for every exception - Suppress missing fields warnings - Add missing NonNull annotation
1 parent c38f150 commit abf1cc5

1 file changed

Lines changed: 20 additions & 16 deletions

File tree

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

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.schabi.newpipe.App;
2626
import org.schabi.newpipe.R;
2727

28-
import java.lang.reflect.InvocationTargetException;
2928
import java.lang.reflect.Method;
3029

3130
import static android.content.Context.INPUT_SERVICE;
@@ -98,27 +97,29 @@ public static boolean isTv(final Context context) {
9897
* @param context the context to use for services and config.
9998
* @return true if the Android device is in desktop mode or using DeX.
10099
*/
101-
public static boolean isDesktopMode(final Context context) {
100+
@SuppressWarnings("JavaReflectionMemberAccess")
101+
public static boolean isDesktopMode(@NonNull final Context context) {
102102
// Adapted from https://stackoverflow.com/a/64615568
103103
// to check for all input devices that have an active cursor
104104
final InputManager im = (InputManager) context.getSystemService(INPUT_SERVICE);
105105
for (final int id : im.getInputDeviceIds()) {
106106
final InputDevice inputDevice = im.getInputDevice(id);
107-
if (
108-
inputDevice.supportsSource(InputDevice.SOURCE_BLUETOOTH_STYLUS)
109-
|| inputDevice.supportsSource(InputDevice.SOURCE_MOUSE)
110-
|| inputDevice.supportsSource(InputDevice.SOURCE_STYLUS)
111-
|| inputDevice.supportsSource(InputDevice.SOURCE_TOUCHPAD)
112-
|| inputDevice.supportsSource(InputDevice.SOURCE_TRACKBALL)
113-
) {
107+
if (inputDevice.supportsSource(InputDevice.SOURCE_BLUETOOTH_STYLUS)
108+
|| inputDevice.supportsSource(InputDevice.SOURCE_MOUSE)
109+
|| inputDevice.supportsSource(InputDevice.SOURCE_STYLUS)
110+
|| inputDevice.supportsSource(InputDevice.SOURCE_TOUCHPAD)
111+
|| inputDevice.supportsSource(InputDevice.SOURCE_TRACKBALL)) {
114112
return true;
115113
}
116114
}
117115

118-
if (ContextCompat.getSystemService(context, UiModeManager.class)
119-
.getCurrentModeType() == Configuration.UI_MODE_TYPE_DESK) {
116+
final UiModeManager uiModeManager =
117+
ContextCompat.getSystemService(context, UiModeManager.class);
118+
if (uiModeManager != null
119+
&& uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_DESK) {
120120
return true;
121121
}
122+
122123
// DeX check for standalone and multi-window mode, from:
123124
// https://developer.samsung.com/samsung-dex/modify-optimizing.html
124125
try {
@@ -131,12 +132,14 @@ public static boolean isDesktopMode(final Context context) {
131132
if (semDesktopModeEnabledConst == currentMode) {
132133
return true;
133134
}
134-
} catch (final NoSuchFieldException | IllegalAccessException e) {
135-
// empty
135+
} catch (final NoSuchFieldException | IllegalAccessException ignored) {
136+
// Device doesn't seem to support DeX
136137
}
138+
137139
@SuppressLint("WrongConstant") final Object desktopModeManager = context
138140
.getApplicationContext()
139141
.getSystemService("desktopmode");
142+
140143
if (desktopModeManager != null) {
141144
try {
142145
final Method getDesktopModeStateMethod = desktopModeManager.getClass()
@@ -151,11 +154,12 @@ public static boolean isDesktopMode(final Context context) {
151154
.getDeclaredField("ENABLED").getInt(desktopModeStateClass)) {
152155
return true;
153156
}
154-
} catch (NoSuchFieldException | NoSuchMethodException
155-
| IllegalAccessException | InvocationTargetException e) {
156-
// Device does not support DeX 3.0
157+
} catch (final Exception ignored) {
158+
// Device does not support DeX 3.0 or something went wrong when trying to determine
159+
// if it supports this feature
157160
}
158161
}
162+
159163
return false;
160164
}
161165

0 commit comments

Comments
 (0)