Java Code Examples for android.app.UiModeManager#getCurrentModeType()

The following examples show how to use android.app.UiModeManager#getCurrentModeType() . 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: 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 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: 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 5
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 6
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 7
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 8
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 9
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 10
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 11
Source File: Utils.java    From BottomSheetPickers with Apache License 2.0 4 votes vote down vote up
public static boolean isTv(Context context) {
    UiModeManager uiModeManager =
            (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
    return uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}
 
Example 12
Source File: OpenVPNService.java    From EasyVPN-Free with GNU General Public License v3.0 4 votes vote down vote up
private boolean runningOnAndroidTV() {
    UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);
    return uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}
 
Example 13
Source File: AppUtils.java    From CumulusTV with MIT License 4 votes vote down vote up
public static boolean isTV(@NonNull Context context) {
    UiModeManager uiModeManager =
            (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
    return uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}
 
Example 14
Source File: LockDeviceService.java    From SecondScreen with Apache License 2.0 4 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
    super.onHandleIntent(intent);

    // Close the notification drawer
    Intent closeDrawer = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
    sendBroadcast(closeDrawer);

    // Determine current charging status
    IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = registerReceiver(null, ifilter);
    int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
            status == BatteryManager.BATTERY_STATUS_FULL;

    // Get current UI mode
    UiModeManager mUiModeManager = (UiModeManager) getSystemService(Context.UI_MODE_SERVICE);
    int uiMode = mUiModeManager.getCurrentModeType();

    // Determine current dock state, based on the current UI mode
    boolean isDocked;
    switch(uiMode) {
        case Configuration.UI_MODE_TYPE_DESK:
            isDocked = true;
            break;
        case Configuration.UI_MODE_TYPE_CAR:
            isDocked = true;
            break;
        default:
            isDocked = false;
    }

    // In order to ensure that the device locks itself when the following code is run,
    // we need to temporarily set the lock screen lock after timeout value.
    // For a smooth transition into the daydream, we set this value to one millisecond,
    // locking the device at the soonest opportunity after the transition completes.
    int timeout = Settings.Secure.getInt(getContentResolver(), "lock_screen_lock_after_timeout", 5000);
    if(timeout != 1) {
        SharedPreferences prefMain = U.getPrefMain(this);
        SharedPreferences.Editor editor = prefMain.edit();
        editor.putInt("timeout", Settings.Secure.getInt(getContentResolver(), "lock_screen_lock_after_timeout", 5000));
        editor.apply();

        U.runCommand(this, U.timeoutCommand + "1");
    }

    // Schedule TimeoutService to reset lock screen timeout to original value
    Intent timeoutService = new Intent(this, TimeoutService.class);
    PendingIntent pendingIntent = PendingIntent.getService(this, 123456, timeoutService, PendingIntent.FLAG_CANCEL_CURRENT);

    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    manager.set(AlarmManager.RTC, System.currentTimeMillis() + 1000, pendingIntent);

    // If Daydreams is enabled and the device is charging, then lock the device by launching the daydream.
    if(isCharging
            && !U.castScreenActive(this)
            && Settings.Secure.getInt(getContentResolver(), "screensaver_enabled", 0) == 1
            && ((Settings.Secure.getInt(getContentResolver(), "screensaver_activate_on_dock", 0) == 1 && isDocked)
            || Settings.Secure.getInt(getContentResolver(), "screensaver_activate_on_sleep", 0) == 1)) {
        // Send intent to launch the current daydream manually
        Intent lockIntent = new Intent(Intent.ACTION_MAIN);
        lockIntent.setComponent(ComponentName.unflattenFromString("com.android.systemui/.Somnambulator"));
        lockIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        try {
            startActivity(lockIntent);
        } catch (ActivityNotFoundException e) { /* Gracefully fail */ }
    } else
        // Otherwise, send a power button keystroke to lock the device normally
        U.lockDevice(this);
}
 
Example 15
Source File: OpenVPNService.java    From bitmask_android with GNU General Public License v3.0 4 votes vote down vote up
private boolean runningOnAndroidTV() {
    UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);
    return uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}
 
Example 16
Source File: Utils.java    From SAI with GNU General Public License v3.0 4 votes vote down vote up
public static boolean isTv(Context c) {
    UiModeManager uiModeManager = (UiModeManager) c.getSystemService(Context.UI_MODE_SERVICE);
    return uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}
 
Example 17
Source File: MyUtil.java    From android-tv-leanback with Apache License 2.0 4 votes vote down vote up
public static boolean isRunningInTvMode(Context ctx) {
    UiModeManager uiModeManager = (UiModeManager) ctx.getSystemService(Context.UI_MODE_SERVICE);
    return uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}
 
Example 18
Source File: OpenVPNService.java    From Cake-VPN with GNU General Public License v2.0 4 votes vote down vote up
private boolean runningOnAndroidTV() {
    UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);
    return uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}
 
Example 19
Source File: ActivityUtil.java    From xipl with Apache License 2.0 2 votes vote down vote up
/**
 * Verifies if the current user interface (UI) mode is for television (Mostly if we're in
 * Android TV)
 *
 * @param activity the activity verifying the UI mode.
 * @return true if the application is running in Android TV.
 */
public static boolean isTvMode(Activity activity) {
    UiModeManager uiModeManager = (UiModeManager) activity.getSystemService(UI_MODE_SERVICE);
    return (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION);
}
 
Example 20
Source File: SpecialSupport.java    From rebootmenu with GNU General Public License v3.0 2 votes vote down vote up
/**
 * 检测是否在Android TV环境下
 *
 * @param context {@link PackageManager}
 * @return boolean
 * @see <a href="https://developer.android.com/training/tv/start/hardware.html#check-features">处理 TV 硬件 - 检查硬件功能</a>
 */
public static boolean isAndroidTV(Context context) {
    UiModeManager uiModeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
    return uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
}