mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2024-11-05 17:46:24 +00:00
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
This commit is contained in:
parent
c38f150562
commit
abf1cc536d
@ -25,7 +25,6 @@ import androidx.preference.PreferenceManager;
|
|||||||
import org.schabi.newpipe.App;
|
import org.schabi.newpipe.App;
|
||||||
import org.schabi.newpipe.R;
|
import org.schabi.newpipe.R;
|
||||||
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
import static android.content.Context.INPUT_SERVICE;
|
import static android.content.Context.INPUT_SERVICE;
|
||||||
@ -98,27 +97,29 @@ public final class DeviceUtils {
|
|||||||
* @param context the context to use for services and config.
|
* @param context the context to use for services and config.
|
||||||
* @return true if the Android device is in desktop mode or using DeX.
|
* @return true if the Android device is in desktop mode or using DeX.
|
||||||
*/
|
*/
|
||||||
public static boolean isDesktopMode(final Context context) {
|
@SuppressWarnings("JavaReflectionMemberAccess")
|
||||||
|
public static boolean isDesktopMode(@NonNull final Context context) {
|
||||||
// Adapted from https://stackoverflow.com/a/64615568
|
// Adapted from https://stackoverflow.com/a/64615568
|
||||||
// to check for all input devices that have an active cursor
|
// to check for all input devices that have an active cursor
|
||||||
final InputManager im = (InputManager) context.getSystemService(INPUT_SERVICE);
|
final InputManager im = (InputManager) context.getSystemService(INPUT_SERVICE);
|
||||||
for (final int id : im.getInputDeviceIds()) {
|
for (final int id : im.getInputDeviceIds()) {
|
||||||
final InputDevice inputDevice = im.getInputDevice(id);
|
final InputDevice inputDevice = im.getInputDevice(id);
|
||||||
if (
|
if (inputDevice.supportsSource(InputDevice.SOURCE_BLUETOOTH_STYLUS)
|
||||||
inputDevice.supportsSource(InputDevice.SOURCE_BLUETOOTH_STYLUS)
|
|
||||||
|| inputDevice.supportsSource(InputDevice.SOURCE_MOUSE)
|
|| inputDevice.supportsSource(InputDevice.SOURCE_MOUSE)
|
||||||
|| inputDevice.supportsSource(InputDevice.SOURCE_STYLUS)
|
|| inputDevice.supportsSource(InputDevice.SOURCE_STYLUS)
|
||||||
|| inputDevice.supportsSource(InputDevice.SOURCE_TOUCHPAD)
|
|| inputDevice.supportsSource(InputDevice.SOURCE_TOUCHPAD)
|
||||||
|| inputDevice.supportsSource(InputDevice.SOURCE_TRACKBALL)
|
|| inputDevice.supportsSource(InputDevice.SOURCE_TRACKBALL)) {
|
||||||
) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ContextCompat.getSystemService(context, UiModeManager.class)
|
final UiModeManager uiModeManager =
|
||||||
.getCurrentModeType() == Configuration.UI_MODE_TYPE_DESK) {
|
ContextCompat.getSystemService(context, UiModeManager.class);
|
||||||
|
if (uiModeManager != null
|
||||||
|
&& uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_DESK) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeX check for standalone and multi-window mode, from:
|
// DeX check for standalone and multi-window mode, from:
|
||||||
// https://developer.samsung.com/samsung-dex/modify-optimizing.html
|
// https://developer.samsung.com/samsung-dex/modify-optimizing.html
|
||||||
try {
|
try {
|
||||||
@ -131,12 +132,14 @@ public final class DeviceUtils {
|
|||||||
if (semDesktopModeEnabledConst == currentMode) {
|
if (semDesktopModeEnabledConst == currentMode) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} catch (final NoSuchFieldException | IllegalAccessException e) {
|
} catch (final NoSuchFieldException | IllegalAccessException ignored) {
|
||||||
// empty
|
// Device doesn't seem to support DeX
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("WrongConstant") final Object desktopModeManager = context
|
@SuppressLint("WrongConstant") final Object desktopModeManager = context
|
||||||
.getApplicationContext()
|
.getApplicationContext()
|
||||||
.getSystemService("desktopmode");
|
.getSystemService("desktopmode");
|
||||||
|
|
||||||
if (desktopModeManager != null) {
|
if (desktopModeManager != null) {
|
||||||
try {
|
try {
|
||||||
final Method getDesktopModeStateMethod = desktopModeManager.getClass()
|
final Method getDesktopModeStateMethod = desktopModeManager.getClass()
|
||||||
@ -151,11 +154,12 @@ public final class DeviceUtils {
|
|||||||
.getDeclaredField("ENABLED").getInt(desktopModeStateClass)) {
|
.getDeclaredField("ENABLED").getInt(desktopModeStateClass)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} catch (NoSuchFieldException | NoSuchMethodException
|
} catch (final Exception ignored) {
|
||||||
| IllegalAccessException | InvocationTargetException e) {
|
// Device does not support DeX 3.0 or something went wrong when trying to determine
|
||||||
// Device does not support DeX 3.0
|
// if it supports this feature
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user