下面列出了怎么用android.app.UiModeManager的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 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";
}
}
@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();
}
/**
* 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 "";
}
}
/**
* 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 "";
}
}
/**
* 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 "";
}
}
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();
}
/**
* 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;
}
@Override
public void onTwilightStateChanged(@Nullable TwilightState state) {
synchronized (mLock) {
if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) {
updateComputedNightModeLocked();
updateLocked(0, 0);
}
}
}
@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);
}
}
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);
}
}
}
}
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);
}
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;
}
}
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;
}
}
private void start(String host, int port) {
Log.d(TAG, "start host: " + host + ", port: " + port);
try {
//https://code.google.com/p/android/issues/detail?id=22763
if (running)
return;
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
UiModeManager uiModeManager = (UiModeManager) getSystemService(UI_MODE_SERVICE);
if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) {
Log.d(TAG, "Running on a TV Device");
wakeLock = powerManager.newWakeLock(FULL_WAKE_LOCK, "snapcast:SnapcastFullWakeLock");
} else {
Log.d(TAG, "Running on a non-TV Device");
wakeLock = powerManager.newWakeLock(PARTIAL_WAKE_LOCK, "snapcast:SnapcastPartialWakeLock");
}
wakeLock.acquire();
WifiManager wm = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
wifiWakeLock = wm.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF, "snapcast:SnapcastWifiWakeLock");
wifiWakeLock.acquire();
this.host = host;
this.port = port;
startProcess();
} catch (Exception e) {
e.printStackTrace();
if (listener != null)
listener.onError(this, e.getMessage(), e);
stop();
}
}
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;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
umm = (UiModeManager) getActivity().getSystemService(Context.UI_MODE_SERVICE);
mp = new MediaPlayer();
}
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;
}
@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;
}
@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;
}
@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;
}
@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;
}
public QManager(Resources resources, ActivityManager activityManager, WindowManager windowManager,
UiModeManager uiModeManager) {
this.resources = resources;
this.activityManager = activityManager;
this.windowManager = windowManager;
this.uiModeManager = uiModeManager;
}
@Override
public void _load(@ValidData @NonNull UiModeOperationData data, @NonNull OnResultCallback callback) {
UiModeManager uiModeManager = (UiModeManager) context.getSystemService(Context.UI_MODE_SERVICE);
if (uiModeManager == null) {
Logger.wtf("Can't get UiModeManager???");
callback.onResult(false);
return;
}
if (data.ui_mode == UiModeOperationData.UiMode.car) {
uiModeManager.enableCarMode(0);
} else { // if (data.ui_mode == UiModeOperationData.UiMode.normal) {
uiModeManager.disableCarMode(0);
}
callback.onResult(true);
}
/**
* 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;
}
@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));
}
@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));
}
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));
}
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));
}
/**
* 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;
}
@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);
}