android.app.UiModeManager Java Examples

The following examples show how to use android.app.UiModeManager. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AndroidInfoModule.java    From react-native-GPay with MIT License 8 votes vote down vote up
/**
 * See: https://developer.android.com/reference/android/app/UiModeManager.html#getCurrentModeType()
 */
private String uiMode() {
  UiModeManager uiModeManager = (UiModeManager) getReactApplicationContext().getSystemService(UI_MODE_SERVICE);
  switch (uiModeManager.getCurrentModeType()) {
    case Configuration.UI_MODE_TYPE_TELEVISION:
      return "tv";
    case Configuration.UI_MODE_TYPE_CAR:
      return "car";
    case Configuration.UI_MODE_TYPE_DESK:
      return "desk";
    case Configuration.UI_MODE_TYPE_WATCH:
      return "watch";
    case Configuration.UI_MODE_TYPE_NORMAL:
      return "normal";
    default:
      return "unknown";
  }
}
 
Example #2
Source File: MainActivity.java    From AnotherRSS with The Unlicense 6 votes vote down vote up
@Override
protected void onResume() {
    Log.d(AnotherRSS.TAG, "onResume");
    AnotherRSS.withGui = true;
    new DbExpunge().execute();

    boolean night = mPreferences.getBoolean("nightmode_use", false);
    if (night) {
        int startH = mPreferences.getInt("nightmode_use_start", AnotherRSS.Config.DEFAULT_NIGHT_START);
        int stopH = mPreferences.getInt("nightmode_use_stop", AnotherRSS.Config.DEFAULT_NIGHT_STOP);
        if (AnotherRSS.inTimeSpan(startH, stopH) && umm.getNightMode() != UiModeManager.MODE_NIGHT_YES) {
            umm.setNightMode(UiModeManager.MODE_NIGHT_YES);
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        }
        if (!AnotherRSS.inTimeSpan(startH, stopH) && umm.getNightMode() != UiModeManager.MODE_NIGHT_NO) {
            umm.setNightMode(UiModeManager.MODE_NIGHT_NO);
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }
    } else {
        if (umm.getNightMode() == UiModeManager.MODE_NIGHT_YES) {
            umm.setNightMode(UiModeManager.MODE_NIGHT_NO);
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        }
    }
    super.onResume();
}
 
Example #3
Source File: DeviceTypeResolver.java    From react-native-device-info with MIT License 6 votes vote down vote up
public DeviceType getDeviceType() {
  // Detect TVs via ui mode (Android TVs) or system features (Fire TV).
  if (context.getPackageManager().hasSystemFeature("amazon.hardware.fire_tv")) {
    return DeviceType.TV;
  }

  UiModeManager uiManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
  if (uiManager != null && uiManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
    return DeviceType.TV;
  }

  DeviceType deviceTypeFromConfig = getDeviceTypeFromResourceConfiguration();

  if (deviceTypeFromConfig != null && deviceTypeFromConfig != DeviceType.UNKNOWN) {
    return deviceTypeFromConfig;
  }

  return getDeviceTypeFromPhysicalSize();
}
 
Example #4
Source File: Util.java    From MediaSDK with Apache License 2.0 6 votes vote down vote up
/**
 * Returns whether the app is running on a TV device.
 *
 * @param context Any context.
 * @return Whether the app is running on a TV device.
 */
public static boolean isTv(Context context) {
  // See https://developer.android.com/training/tv/start/hardware.html#runtime-check.
  UiModeManager uiModeManager =
      (UiModeManager) context.getApplicationContext().getSystemService(UI_MODE_SERVICE);
  return uiModeManager != null
      && uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}
 
Example #5
Source File: Utils.java    From FireFiles with Apache License 2.0 6 votes vote down vote up
/**
 * Returns true when running Android TV
 *
 * @param c Context to detect UI Mode.
 * @return true when device is running in tv mode, false otherwise.
 */
public static String getDeviceType(Context c) {
    UiModeManager uiModeManager = (UiModeManager) c.getSystemService(Context.UI_MODE_SERVICE);
    int modeType = uiModeManager.getCurrentModeType();
    switch (modeType){
        case Configuration.UI_MODE_TYPE_TELEVISION:
            return "TELEVISION";
        case Configuration.UI_MODE_TYPE_WATCH:
            return "WATCH";
        case Configuration.UI_MODE_TYPE_NORMAL:
            String type = isTablet(c) ? "TABLET" : "PHONE";
            return type;
        case Configuration.UI_MODE_TYPE_UNDEFINED:
            return "UNKOWN";
        default:
            return "";
    }
}
 
Example #6
Source File: Utils.java    From FireFiles with Apache License 2.0 6 votes vote down vote up
/**
 * Returns true when running Android TV
 *
 * @param c Context to detect UI Mode.
 * @return true when device is running in tv mode, false otherwise.
 */
public static String getDeviceType(Context c) {
    UiModeManager uiModeManager = (UiModeManager) c.getSystemService(Context.UI_MODE_SERVICE);
    int modeType = uiModeManager.getCurrentModeType();
    switch (modeType){
        case Configuration.UI_MODE_TYPE_TELEVISION:
            return "TELEVISION";
        case Configuration.UI_MODE_TYPE_WATCH:
            return "WATCH";
        case Configuration.UI_MODE_TYPE_NORMAL:
            String type = isTablet(c) ? "TABLET" : "PHONE";
            return type;
        case Configuration.UI_MODE_TYPE_UNDEFINED:
            return "UNKOWN";
        default:
            return "";
    }
}
 
Example #7
Source File: UiModeManagerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) {
    // Launch a dock activity
    String category = null;
    if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
        // Only launch car home when car mode is enabled and the caller
        // has asked us to switch to it.
        if (mEnableCarDockLaunch
                && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
            category = Intent.CATEGORY_CAR_DOCK;
        }
    } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) {
        // Only launch car home when desk mode is enabled and the caller
        // has asked us to switch to it.  Currently re-using the car
        // mode flag since we don't have a formal API for "desk mode".
        if (ENABLE_LAUNCH_DESK_DOCK_APP
                && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) {
            category = Intent.CATEGORY_DESK_DOCK;
        }
    } else {
        // Launch the standard home app if requested.
        if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) {
            category = Intent.CATEGORY_HOME;
        }
    }

    if (LOG) {
        Slog.v(TAG, String.format(
            "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, "
                + "category=%s",
            action, enableFlags, disableFlags, category));
    }

    sendConfigurationAndStartDreamOrDockAppLocked(category);
}
 
Example #8
Source File: PlayerDeviceUtils.java    From TubiPlayer with MIT License 5 votes vote down vote up
public static boolean isTVDevice(final Context context) {
    if (sIsTVDevice == null) {
        UiModeManager uiModeManager = (UiModeManager) context.getSystemService(UI_MODE_SERVICE);
        sIsTVDevice = uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;

        if (!sIsTVDevice) { // We also check fire tv
            sIsTVDevice = context.getPackageManager().hasSystemFeature(AMAZON_FEATURE_FIRE_TV);
        }
    }
    return sIsTVDevice;
}
 
Example #9
Source File: UiModeManagerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void onTwilightStateChanged(@Nullable TwilightState state) {
    synchronized (mLock) {
        if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
            updateComputedNightModeLocked();
            updateLocked(0, 0);
        }
    }
}
 
Example #10
Source File: UiModeManagerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void setNightMode(int mode) {
    if (isNightModeLocked() &&  (getContext().checkCallingOrSelfPermission(
            android.Manifest.permission.MODIFY_DAY_NIGHT_MODE)
            != PackageManager.PERMISSION_GRANTED)) {
        Slog.e(TAG,
                "Night mode locked, requires MODIFY_DAY_NIGHT_MODE permission");
        return;
    }
    switch (mode) {
        case UiModeManager.MODE_NIGHT_NO:
        case UiModeManager.MODE_NIGHT_YES:
        case UiModeManager.MODE_NIGHT_AUTO:
            break;
        default:
            throw new IllegalArgumentException("Unknown mode: " + mode);
    }

    final long ident = Binder.clearCallingIdentity();
    try {
        synchronized (mLock) {
            if (mNightMode != mode) {
                Settings.Secure.putInt(getContext().getContentResolver(),
                        Settings.Secure.UI_NIGHT_MODE, mode);
                mNightMode = mode;
                updateLocked(0, 0);
            }
        }
    } finally {
        Binder.restoreCallingIdentity(ident);
    }
}
 
Example #11
Source File: UiModeManagerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private void updateDockState(int newState) {
    synchronized (mLock) {
        if (newState != mDockState) {
            mDockState = newState;
            setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR, 0);
            if (mSystemReady) {
                updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0);
            }
        }
    }
}
 
Example #12
Source File: Util.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns whether the app is running on a TV device.
 *
 * @param context Any context.
 * @return Whether the app is running on a TV device.
 */
public static boolean isTv(Context context) {
  // See https://developer.android.com/training/tv/start/hardware.html#runtime-check.
  UiModeManager uiModeManager =
      (UiModeManager) context.getApplicationContext().getSystemService(UI_MODE_SERVICE);
  return uiModeManager != null
      && uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}
 
Example #13
Source File: OptimizelyClientEngine.java    From android-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Get client engine value for current UI mode type
 *
 * @param context any valid Android {@link Context}
 * @return String value of client engine
 */
public static EventBatch.ClientEngine getClientEngineFromContext(@NonNull Context context) {
    UiModeManager uiModeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);

    if (uiModeManager != null && uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
        return EventBatch.ClientEngine.ANDROID_TV_SDK;
    }

    return EventBatch.ClientEngine.ANDROID_SDK;
}
 
Example #14
Source File: UiModeManagerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private static String nightModeToStr(int mode) {
    switch (mode) {
        case UiModeManager.MODE_NIGHT_YES:
            return NIGHT_MODE_STR_YES;
        case UiModeManager.MODE_NIGHT_NO:
            return NIGHT_MODE_STR_NO;
        case UiModeManager.MODE_NIGHT_AUTO:
            return NIGHT_MODE_STR_AUTO;
        default:
            return NIGHT_MODE_STR_UNKNOWN;
    }
}
 
Example #15
Source File: UiModeManagerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
private static int strToNightMode(String modeStr) {
    switch (modeStr) {
        case NIGHT_MODE_STR_YES:
            return UiModeManager.MODE_NIGHT_YES;
        case NIGHT_MODE_STR_NO:
            return UiModeManager.MODE_NIGHT_NO;
        case NIGHT_MODE_STR_AUTO:
            return UiModeManager.MODE_NIGHT_AUTO;
        default:
            return -1;
    }
}
 
Example #16
Source File: Util.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns whether the app is running on a TV device.
 *
 * @param context Any context.
 * @return Whether the app is running on a TV device.
 */
public static boolean isTv(Context context) {
  // See https://developer.android.com/training/tv/start/hardware.html#runtime-check.
  UiModeManager uiModeManager =
      (UiModeManager) context.getApplicationContext().getSystemService(UI_MODE_SERVICE);
  return uiModeManager != null
      && uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}
 
Example #17
Source File: QManager.java    From aptoide-client-v8 with GNU General Public License v3.0 5 votes vote down vote up
public QManager(Resources resources, ActivityManager activityManager, WindowManager windowManager,
    UiModeManager uiModeManager) {
  this.resources = resources;
  this.activityManager = activityManager;
  this.windowManager = windowManager;
  this.uiModeManager = uiModeManager;
}
 
Example #18
Source File: TopicSearchFragment.java    From NGA-CLIENT-VER-OPEN-SOURCE with GNU General Public License v2.0 5 votes vote down vote up
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    int layoutId;

    UiModeManager uiModeManager = (UiModeManager) getContext().getSystemService(Context.UI_MODE_SERVICE);
    if (DeviceUtils.isGreaterEqual_9_0() && uiModeManager.getNightMode() == UiModeManager.MODE_NIGHT_YES) {
        layoutId = R.layout.fragment_topic_list_night;
    } else {
        layoutId = R.layout.fragment_topic_list;
    }
    return inflater.inflate(layoutId, container, false);
}
 
Example #19
Source File: MyPreferenceFragment.java    From AnotherRSS with The Unlicense 5 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
    umm = (UiModeManager) getActivity().getSystemService(Context.UI_MODE_SERVICE);
    mp = new MediaPlayer();
}
 
Example #20
Source File: DexCollectionService.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
@SuppressLint("ObsoleteSdkInt")
private static boolean shouldServiceRun() {
    if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) return false;
    final boolean result = (DexCollectionType.hasXbridgeWixel() || DexCollectionType.hasBtWixel())
            && ((!Home.get_forced_wear() && (((UiModeManager) xdrip.getAppContext().getSystemService(UI_MODE_SERVICE)).getCurrentModeType() != Configuration.UI_MODE_TYPE_WATCH))
            || PersistentStore.getBoolean(CollectionServiceStarter.pref_run_wear_collector));
    if (d) Log.d(TAG, "shouldServiceRun() returning: " + result);
    return result;
}
 
Example #21
Source File: DexCollectionService.java    From xDrip-plus with GNU General Public License v3.0 5 votes vote down vote up
@SuppressLint("ObsoleteSdkInt")
private static boolean shouldServiceRun() {
    if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) return false;
    final boolean result = (DexCollectionType.hasXbridgeWixel() || DexCollectionType.hasBtWixel())
            && ((!Home.get_forced_wear() && (((UiModeManager) xdrip.getAppContext().getSystemService(UI_MODE_SERVICE)).getCurrentModeType() != Configuration.UI_MODE_TYPE_WATCH))
            || PersistentStore.getBoolean(CollectionServiceStarter.pref_run_wear_collector));
    if (d) Log.d(TAG, "shouldServiceRun() returning: " + result);
    return result;
}
 
Example #22
Source File: DeviceUtil.java    From ExoMedia with Apache License 2.0 5 votes vote down vote up
/**
 * Determines if the current device is a TV.
 *
 * @param context The context to use for determining the device information
 * @return True if the current device is a TV
 */
public boolean isDeviceTV(Context context) {
    //Since Android TV is only API 21+ that is the only time we will compare configurations
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        UiModeManager uiManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
        return uiManager != null && uiManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
    }

    return false;
}
 
Example #23
Source File: KeyAssignmentUtils.java    From talkback with Apache License 2.0 5 votes vote down vote up
public static boolean isKeyCodeToIgnore(Context context, int keyCode) {
  // If we're not on Android TV, don't ignore any keys.
  UiModeManager uiModeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
  if ((uiModeManager == null)
      || (uiModeManager.getCurrentModeType() != Configuration.UI_MODE_TYPE_TELEVISION)) {
    return false;
  }

  return ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
      || (keyCode == KeyEvent.KEYCODE_DPAD_DOWN)
      || (keyCode == KeyEvent.KEYCODE_DPAD_UP)
      || (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)
      || (keyCode == KeyEvent.KEYCODE_BACK)
      || (keyCode == KeyEvent.KEYCODE_DPAD_LEFT));
}
 
Example #24
Source File: Utils.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true when running Android TV
 *
 * @param c Context to detect UI Mode.
 * @return true when device is running in tv mode, false otherwise.
 */
public static String getDeviceType(Context c) {
    UiModeManager uiModeManager = (UiModeManager) c.getSystemService(Context.UI_MODE_SERVICE);
    int modeType = uiModeManager.getCurrentModeType();
    switch (modeType){
        case Configuration.UI_MODE_TYPE_TELEVISION:
            return "TELEVISION";
        case Configuration.UI_MODE_TYPE_WATCH:
            return "WATCH";
        case Configuration.UI_MODE_TYPE_NORMAL:
            String type = isTablet(c) ? "TABLET" : "PHONE";
            return type;
        case Configuration.UI_MODE_TYPE_UNDEFINED:
            return "UNKOWN";
        default:
            return "";
    }
}
 
Example #25
Source File: FeatureSupport.java    From talkback with Apache License 2.0 5 votes vote down vote up
public static boolean isTv(Context context) {
  if (context == null) {
    return false;
  }

  UiModeManager modeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
  return ((modeManager != null)
      && (modeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION));
}
 
Example #26
Source File: DexCollectionService.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
@SuppressLint("ObsoleteSdkInt")
private static boolean shouldServiceRun() {
    if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) return false;
    final boolean result = (DexCollectionType.hasXbridgeWixel() || DexCollectionType.hasBtWixel())
            && ((!Home.get_forced_wear() && (((UiModeManager) xdrip.getAppContext().getSystemService(UI_MODE_SERVICE)).getCurrentModeType() != Configuration.UI_MODE_TYPE_WATCH))
            || PersistentStore.getBoolean(CollectionServiceStarter.pref_run_wear_collector));
    if (d) Log.d(TAG, "shouldServiceRun() returning: " + result);
    return result;
}
 
Example #27
Source File: Tools.java    From dtube-mobile-unofficial with Apache License 2.0 5 votes vote down vote up
static boolean deviceSupportsPIPMode(Context c){
    PackageManager packageManager = c.getApplicationContext().getPackageManager();
    boolean supportsPIP = false;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
        supportsPIP = packageManager.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE);
    }

    UiModeManager uiModeManager = (UiModeManager) c.getSystemService(UI_MODE_SERVICE);
    boolean runningOnTV = uiModeManager.getCurrentModeType()== Configuration.UI_MODE_TYPE_TELEVISION;

    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && !runningOnTV && supportsPIP;
}
 
Example #28
Source File: DexCollectionService.java    From xDrip with GNU General Public License v3.0 5 votes vote down vote up
@SuppressLint("ObsoleteSdkInt")
private static boolean shouldServiceRun() {
    if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) return false;
    final boolean result = (DexCollectionType.hasXbridgeWixel() || DexCollectionType.hasBtWixel())
            && ((!Home.get_forced_wear() && (((UiModeManager) xdrip.getAppContext().getSystemService(UI_MODE_SERVICE)).getCurrentModeType() != Configuration.UI_MODE_TYPE_WATCH))
            || PersistentStore.getBoolean(CollectionServiceStarter.pref_run_wear_collector));
    if (d) Log.d(TAG, "shouldServiceRun() returning: " + result);
    return result;
}
 
Example #29
Source File: OptimizelyClientEngineTest.java    From android-sdk with Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB_MR2)
@Test
public void testGetClientEngineFromContextAndroid() {
    Context context = mock(Context.class);
    UiModeManager uiModeManager = mock(UiModeManager.class);
    when(context.getSystemService(Context.UI_MODE_SERVICE)).thenReturn(uiModeManager);
    when(uiModeManager.getCurrentModeType()).thenReturn(Configuration.UI_MODE_TYPE_NORMAL);
    assertEquals(EventBatch.ClientEngine.ANDROID_SDK, OptimizelyClientEngine.getClientEngineFromContext(context));
}
 
Example #30
Source File: OptimizelyClientEngineTest.java    From android-sdk with Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.HONEYCOMB_MR2)
@Test
public void testGetClientEngineFromContextAndroidTV() {
    Context context = mock(Context.class);
    UiModeManager uiModeManager = mock(UiModeManager.class);
    when(context.getSystemService(Context.UI_MODE_SERVICE)).thenReturn(uiModeManager);
    when(uiModeManager.getCurrentModeType()).thenReturn(Configuration.UI_MODE_TYPE_TELEVISION);
    assertEquals(EventBatch.ClientEngine.ANDROID_TV_SDK, OptimizelyClientEngine.getClientEngineFromContext(context));
}