FileDocCategorySizeDatePackage
UiModeManager.javaAPI DocAndroid 5.1 API9996Thu Mar 12 22:22:10 GMT 2015android.app

UiModeManager

public class UiModeManager extends Object
This class provides access to the system uimode services. These services allow applications to control UI modes of the device. It provides functionality to disable the car mode and it gives access to the night mode settings.

These facilities are built on top of the underlying {@link android.content.Intent#ACTION_DOCK_EVENT} broadcasts that are sent when the user physical places the device into and out of a dock. When that happens, the UiModeManager switches the system {@link android.content.res.Configuration} to the appropriate UI mode, sends broadcasts about the mode switch, and starts the corresponding mode activity if appropriate. See the broadcasts {@link #ACTION_ENTER_CAR_MODE} and {@link #ACTION_ENTER_DESK_MODE} for more information.

In addition, the user may manually switch the system to car mode without physically being in a dock. While in car mode -- whether by manual action from the user or being physically placed in a dock -- a notification is displayed allowing the user to exit dock mode. Thus the dock mode represented here may be different than the current state of the underlying dock event broadcast.

You do not instantiate this class directly; instead, retrieve it through {@link android.content.Context#getSystemService Context.getSystemService(Context.UI_MODE_SERVICE)}.

Fields Summary
private static final String
TAG
public static String
ACTION_ENTER_CAR_MODE
Broadcast sent when the device's UI has switched to car mode, either by being placed in a car dock or explicit action of the user. After sending the broadcast, the system will start the intent {@link android.content.Intent#ACTION_MAIN} with category {@link android.content.Intent#CATEGORY_CAR_DOCK} to display the car UI, which typically what an application would implement to provide their own interface. However, applications can also monitor this Intent in order to be informed of mode changes or prevent the normal car UI from being displayed by setting the result of the broadcast to {@link Activity#RESULT_CANCELED}.
public static String
ACTION_EXIT_CAR_MODE
Broadcast sent when the device's UI has switch away from car mode back to normal mode. Typically used by a car mode app, to dismiss itself when the user exits car mode.
public static String
ACTION_ENTER_DESK_MODE
Broadcast sent when the device's UI has switched to desk mode, by being placed in a desk dock. After sending the broadcast, the system will start the intent {@link android.content.Intent#ACTION_MAIN} with category {@link android.content.Intent#CATEGORY_DESK_DOCK} to display the desk UI, which typically what an application would implement to provide their own interface. However, applications can also monitor this Intent in order to be informed of mode changes or prevent the normal desk UI from being displayed by setting the result of the broadcast to {@link Activity#RESULT_CANCELED}.
public static String
ACTION_EXIT_DESK_MODE
Broadcast sent when the device's UI has switched away from desk mode back to normal mode. Typically used by a desk mode app, to dismiss itself when the user exits desk mode.
public static final int
MODE_NIGHT_AUTO
Constant for {@link #setNightMode(int)} and {@link #getNightMode()}: automatically switch night mode on and off based on the time.
public static final int
MODE_NIGHT_NO
Constant for {@link #setNightMode(int)} and {@link #getNightMode()}: never run in night mode.
public static final int
MODE_NIGHT_YES
Constant for {@link #setNightMode(int)} and {@link #getNightMode()}: always run in night mode.
private IUiModeManager
mService
public static final int
ENABLE_CAR_MODE_GO_CAR_HOME
Flag for use with {@link #enableCarMode(int)}: go to the car home activity as part of the enable. Enabling this way ensures a clean transition between the current activity (in non-car-mode) and the car home activity that will serve as home while in car mode. This will switch to the car home activity even if we are already in car mode.
public static final int
ENABLE_CAR_MODE_ALLOW_SLEEP
Flag for use with {@link #enableCarMode(int)}: allow sleep mode while in car mode. By default, when this flag is not set, the system may hold a full wake lock to keep the screen turned on and prevent the system from entering sleep mode while in car mode. Setting this flag disables such behavior and the system may enter sleep mode if there is no other user activity and no other wake lock held. Setting this flag can be relevant for a car dock application that does not require the screen kept on.
public static final int
DISABLE_CAR_MODE_GO_HOME
Flag for use with {@link #disableCarMode(int)}: go to the normal home activity as part of the disable. Disabling this way ensures a clean transition between the current activity (in car mode) and the original home activity (which was typically last running without being in car mode).
Constructors Summary
UiModeManager()


    /*package*/  
        mService = IUiModeManager.Stub.asInterface(
                ServiceManager.getService(Context.UI_MODE_SERVICE));
    
Methods Summary
public voiddisableCarMode(int flags)
Turn off special mode if currently in car mode.

param
flags May be 0 or {@link #DISABLE_CAR_MODE_GO_HOME}.

    
                          
        
        if (mService != null) {
            try {
                mService.disableCarMode(flags);
            } catch (RemoteException e) {
                Log.e(TAG, "disableCarMode: RemoteException", e);
            }
        }
    
public voidenableCarMode(int flags)
Force device into car mode, like it had been placed in the car dock. This will cause the device to switch to the car home UI as part of the mode switch.

param
flags Must be 0.


                                              
        
        if (mService != null) {
            try {
                mService.enableCarMode(flags);
            } catch (RemoteException e) {
                Log.e(TAG, "disableCarMode: RemoteException", e);
            }
        }
    
public intgetCurrentModeType()
Return the current running mode type. May be one of {@link Configuration#UI_MODE_TYPE_NORMAL Configuration.UI_MODE_TYPE_NORMAL}, {@link Configuration#UI_MODE_TYPE_DESK Configuration.UI_MODE_TYPE_DESK}, {@link Configuration#UI_MODE_TYPE_CAR Configuration.UI_MODE_TYPE_CAR}, {@link Configuration#UI_MODE_TYPE_TELEVISION Configuration.UI_MODE_TYPE_TELEVISION}, {@link Configuration#UI_MODE_TYPE_APPLIANCE Configuration.UI_MODE_TYPE_APPLIANCE}, or {@link Configuration#UI_MODE_TYPE_WATCH Configuration.UI_MODE_TYPE_WATCH}.

        if (mService != null) {
            try {
                return mService.getCurrentModeType();
            } catch (RemoteException e) {
                Log.e(TAG, "getCurrentModeType: RemoteException", e);
            }
        }
        return Configuration.UI_MODE_TYPE_NORMAL;
    
public intgetNightMode()
Returns the currently configured night mode.

return
{@link #MODE_NIGHT_NO}, {@link #MODE_NIGHT_YES}, or {@link #MODE_NIGHT_AUTO}. When an error occurred -1 is returned.

        if (mService != null) {
            try {
                return mService.getNightMode();
            } catch (RemoteException e) {
                Log.e(TAG, "getNightMode: RemoteException", e);
            }
        }
        return -1;
    
public voidsetNightMode(int mode)
Sets the night mode. Changes to the night mode are only effective when the car or desk mode is enabled on a device.

The mode can be one of:

  • {@link #MODE_NIGHT_NO} - sets the device into notnight mode.
  • {@link #MODE_NIGHT_YES} - sets the device into night mode.
  • {@link #MODE_NIGHT_AUTO} - automatic night/notnight switching depending on the location and certain other sensors.

        if (mService != null) {
            try {
                mService.setNightMode(mode);
            } catch (RemoteException e) {
                Log.e(TAG, "setNightMode: RemoteException", e);
            }
        }