|
1 | 1 | package org.schabi.newpipe.util; |
2 | 2 |
|
| 3 | +import android.annotation.SuppressLint; |
3 | 4 | import android.app.UiModeManager; |
4 | 5 | import android.content.Context; |
5 | 6 | import android.content.pm.PackageManager; |
6 | 7 | import android.content.res.Configuration; |
7 | 8 | import android.graphics.Point; |
| 9 | +import android.hardware.input.InputManager; |
8 | 10 | import android.os.BatteryManager; |
9 | 11 | import android.os.Build; |
10 | 12 | import android.provider.Settings; |
11 | 13 | import android.util.TypedValue; |
| 14 | +import android.view.InputDevice; |
12 | 15 | import android.view.KeyEvent; |
13 | 16 | import android.view.WindowInsets; |
14 | 17 | import android.view.WindowManager; |
|
22 | 25 | import org.schabi.newpipe.App; |
23 | 26 | import org.schabi.newpipe.R; |
24 | 27 |
|
| 28 | +import java.lang.reflect.Method; |
| 29 | + |
| 30 | +import static android.content.Context.INPUT_SERVICE; |
| 31 | + |
25 | 32 | public final class DeviceUtils { |
26 | 33 |
|
27 | 34 | private static final String AMAZON_FEATURE_FIRE_TV = "amazon.hardware.fire_tv"; |
@@ -84,6 +91,78 @@ public static boolean isTv(final Context context) { |
84 | 91 | return DeviceUtils.isTV; |
85 | 92 | } |
86 | 93 |
|
| 94 | + /** |
| 95 | + * Checks if the device is in desktop or DeX mode. This function should only |
| 96 | + * be invoked once on view load as it is using reflection for the DeX checks. |
| 97 | + * @param context the context to use for services and config. |
| 98 | + * @return true if the Android device is in desktop mode or using DeX. |
| 99 | + */ |
| 100 | + @SuppressWarnings("JavaReflectionMemberAccess") |
| 101 | + public static boolean isDesktopMode(@NonNull final Context context) { |
| 102 | + // Adapted from https://stackoverflow.com/a/64615568 |
| 103 | + // to check for all input devices that have an active cursor |
| 104 | + final InputManager im = (InputManager) context.getSystemService(INPUT_SERVICE); |
| 105 | + for (final int id : im.getInputDeviceIds()) { |
| 106 | + final InputDevice inputDevice = im.getInputDevice(id); |
| 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)) { |
| 112 | + return true; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + final UiModeManager uiModeManager = |
| 117 | + ContextCompat.getSystemService(context, UiModeManager.class); |
| 118 | + if (uiModeManager != null |
| 119 | + && uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_DESK) { |
| 120 | + return true; |
| 121 | + } |
| 122 | + |
| 123 | + // DeX check for standalone and multi-window mode, from: |
| 124 | + // https://developer.samsung.com/samsung-dex/modify-optimizing.html |
| 125 | + try { |
| 126 | + final Configuration config = context.getResources().getConfiguration(); |
| 127 | + final Class<?> configClass = config.getClass(); |
| 128 | + final int semDesktopModeEnabledConst = |
| 129 | + configClass.getField("SEM_DESKTOP_MODE_ENABLED").getInt(configClass); |
| 130 | + final int currentMode = |
| 131 | + configClass.getField("semDesktopModeEnabled").getInt(config); |
| 132 | + if (semDesktopModeEnabledConst == currentMode) { |
| 133 | + return true; |
| 134 | + } |
| 135 | + } catch (final NoSuchFieldException | IllegalAccessException ignored) { |
| 136 | + // Device doesn't seem to support DeX |
| 137 | + } |
| 138 | + |
| 139 | + @SuppressLint("WrongConstant") final Object desktopModeManager = context |
| 140 | + .getApplicationContext() |
| 141 | + .getSystemService("desktopmode"); |
| 142 | + |
| 143 | + if (desktopModeManager != null) { |
| 144 | + try { |
| 145 | + final Method getDesktopModeStateMethod = desktopModeManager.getClass() |
| 146 | + .getDeclaredMethod("getDesktopModeState"); |
| 147 | + final Object desktopModeState = getDesktopModeStateMethod |
| 148 | + .invoke(desktopModeManager); |
| 149 | + final Class<?> desktopModeStateClass = desktopModeState.getClass(); |
| 150 | + final Method getEnabledMethod = desktopModeStateClass |
| 151 | + .getDeclaredMethod("getEnabled"); |
| 152 | + final int enabledStatus = (int) getEnabledMethod.invoke(desktopModeState); |
| 153 | + if (enabledStatus == desktopModeStateClass |
| 154 | + .getDeclaredField("ENABLED").getInt(desktopModeStateClass)) { |
| 155 | + return true; |
| 156 | + } |
| 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 |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + return false; |
| 164 | + } |
| 165 | + |
87 | 166 | public static boolean isTablet(@NonNull final Context context) { |
88 | 167 | final String tabletModeSetting = PreferenceManager.getDefaultSharedPreferences(context) |
89 | 168 | .getString(context.getString(R.string.tablet_mode_key), ""); |
|
0 commit comments