Add JSDoc comment and a performance note for `isDesktopMode`

This commit is contained in:
Hanif Shersy 2022-05-04 18:39:08 +10:00 committed by AudricV
parent a1773d166f
commit 88eed6cc23
No known key found for this signature in database
GPG Key ID: DA92EC7905614198
1 changed files with 10 additions and 3 deletions

View File

@ -88,12 +88,19 @@ public final class DeviceUtils {
return DeviceUtils.isTV; return DeviceUtils.isTV;
} }
/**
* Checks if the device is in desktop or DeX mode. This function should only
* be invoked once on view load as it is using reflection for the DeX checks.
* @param context the context to use for services and config.
* @return true if the Android device is in desktop mode or using DeX.
*/
public static boolean isDesktopMode(final Context context) { public static boolean isDesktopMode(final Context context) {
if (ContextCompat.getSystemService(context, UiModeManager.class) if (ContextCompat.getSystemService(context, UiModeManager.class)
.getCurrentModeType() == Configuration.UI_MODE_TYPE_DESK) { .getCurrentModeType() == Configuration.UI_MODE_TYPE_DESK) {
return true; return true;
} }
// DeX check for standalone and multi-window mode // DeX check for standalone and multi-window mode, from:
// https://developer.samsung.com/samsung-dex/modify-optimizing.html
try { try {
final Configuration config = context.getResources().getConfiguration(); final Configuration config = context.getResources().getConfiguration();
final Class<?> configClass = config.getClass(); final Class<?> configClass = config.getClass();
@ -119,8 +126,8 @@ public final class DeviceUtils {
final Class<?> desktopModeStateClass = desktopModeState.getClass(); final Class<?> desktopModeStateClass = desktopModeState.getClass();
final Method getEnabledMethod = desktopModeStateClass final Method getEnabledMethod = desktopModeStateClass
.getDeclaredMethod("getEnabled"); .getDeclaredMethod("getEnabled");
final int enabled = (int) getEnabledMethod.invoke(desktopModeState); final int enabledStatus = (int) getEnabledMethod.invoke(desktopModeState);
if (enabled == desktopModeStateClass if (enabledStatus == desktopModeStateClass
.getDeclaredField("ENABLED").getInt(desktopModeStateClass)) { .getDeclaredField("ENABLED").getInt(desktopModeStateClass)) {
return true; return true;
} }