|
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; |
|
22 | 23 | import org.schabi.newpipe.App; |
23 | 24 | import org.schabi.newpipe.R; |
24 | 25 |
|
| 26 | +import java.lang.reflect.InvocationTargetException; |
| 27 | +import java.lang.reflect.Method; |
| 28 | + |
25 | 29 | public final class DeviceUtils { |
26 | 30 |
|
27 | 31 | private static final String AMAZON_FEATURE_FIRE_TV = "amazon.hardware.fire_tv"; |
@@ -84,6 +88,50 @@ public static boolean isTv(final Context context) { |
84 | 88 | return DeviceUtils.isTV; |
85 | 89 | } |
86 | 90 |
|
| 91 | + public static boolean isDesktopMode(final Context context) { |
| 92 | + final boolean isDesktopMode = ContextCompat.getSystemService(context, UiModeManager.class) |
| 93 | + .getCurrentModeType() == Configuration.UI_MODE_TYPE_DESK; |
| 94 | + |
| 95 | + // DeX check for standalone and multi-window mode |
| 96 | + boolean isDeXMode = false; |
| 97 | + try { |
| 98 | + final Configuration config = context.getResources().getConfiguration(); |
| 99 | + final Class<?> configClass = config.getClass(); |
| 100 | + final int semDesktopModeEnabledConst = |
| 101 | + configClass.getField("SEM_DESKTOP_MODE_ENABLED").getInt(configClass); |
| 102 | + final int currentMode = |
| 103 | + configClass.getField("semDesktopModeEnabled").getInt(config); |
| 104 | + if (semDesktopModeEnabledConst == currentMode) { |
| 105 | + isDeXMode = true; |
| 106 | + } |
| 107 | + } catch (final NoSuchFieldException | IllegalAccessException e) { |
| 108 | + // empty |
| 109 | + } |
| 110 | + @SuppressLint("WrongConstant") final Object desktopModeManager = context |
| 111 | + .getApplicationContext() |
| 112 | + .getSystemService("desktopmode"); |
| 113 | + if (desktopModeManager != null) { |
| 114 | + try { |
| 115 | + final Method getDesktopModeStateMethod = desktopModeManager.getClass() |
| 116 | + .getDeclaredMethod("getDesktopModeState"); |
| 117 | + final Object desktopModeState = getDesktopModeStateMethod |
| 118 | + .invoke(desktopModeManager); |
| 119 | + final Class<?> desktopModeStateClass = desktopModeState.getClass(); |
| 120 | + final Method getEnabledMethod = desktopModeStateClass |
| 121 | + .getDeclaredMethod("getEnabled"); |
| 122 | + final int enabled = (int) getEnabledMethod.invoke(desktopModeState); |
| 123 | + final boolean isEnabled = enabled == desktopModeStateClass |
| 124 | + .getDeclaredField("ENABLED").getInt(desktopModeStateClass); |
| 125 | + |
| 126 | + isDeXMode = isEnabled; |
| 127 | + } catch (NoSuchFieldException | NoSuchMethodException |
| 128 | + | IllegalAccessException | InvocationTargetException e) { |
| 129 | + // Device does not support DeX 3.0 |
| 130 | + } |
| 131 | + } |
| 132 | + return isDesktopMode || isDeXMode; |
| 133 | + } |
| 134 | + |
87 | 135 | public static boolean isTablet(@NonNull final Context context) { |
88 | 136 | final String tabletModeSetting = PreferenceManager.getDefaultSharedPreferences(context) |
89 | 137 | .getString(context.getString(R.string.tablet_mode_key), ""); |
|
0 commit comments