UiModeManager.java revision 7299c41630935a2b106e73e5603579a7747f7535
153332883543868fb83e111a07306368b7772b340Tobias Haamelpackage android.app;
253332883543868fb83e111a07306368b7772b340Tobias Haamel
37299c41630935a2b106e73e5603579a7747f7535Dianne Hackbornimport android.content.Context;
47299c41630935a2b106e73e5603579a7747f7535Dianne Hackbornimport android.content.res.Configuration;
553332883543868fb83e111a07306368b7772b340Tobias Haamelimport android.os.RemoteException;
653332883543868fb83e111a07306368b7772b340Tobias Haamelimport android.os.ServiceManager;
753332883543868fb83e111a07306368b7772b340Tobias Haamelimport android.util.Log;
853332883543868fb83e111a07306368b7772b340Tobias Haamel
953332883543868fb83e111a07306368b7772b340Tobias Haamel/**
1053332883543868fb83e111a07306368b7772b340Tobias Haamel * This class provides access to the system uimode services.  These services
1153332883543868fb83e111a07306368b7772b340Tobias Haamel * allow applications to control UI modes of the device.
1253332883543868fb83e111a07306368b7772b340Tobias Haamel * It provides functionality to disable the car mode and it gives access to the
1353332883543868fb83e111a07306368b7772b340Tobias Haamel * night mode settings.
147299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn *
157299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn * <p>These facilities are built on top of the underlying
167299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn * {@link android.content.Intent#ACTION_DOCK_EVENT} broadcasts that are sent when the user
177299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn * physical places the device into and out of a dock.  When that happens,
187299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn * the UiModeManager switches the system {@link android.content.res.Configuration}
197299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn * to the appropriate UI mode, sends broadcasts about the mode switch, and
207299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn * starts the corresponding mode activity if appropriate.  See the
217299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn * broadcasts {@link #ACTION_ENTER_CAR_MODE} and
227299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn * {@link #ACTION_ENTER_DESK_MODE} for more information.
237299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn *
247299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn * <p>In addition, the user may manually switch the system to car mode without
257299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn * physically being in a dock.  While in car mode -- whether by manual action
267299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn * from the user or being physically placed in a dock -- a notification is
277299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn * displayed allowing the user to exit dock mode.  Thus the dock mode
287299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn * represented here may be different than the current state of the underlying
297299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn * dock event broadcast.
3053332883543868fb83e111a07306368b7772b340Tobias Haamel *
3153332883543868fb83e111a07306368b7772b340Tobias Haamel * <p>You do not instantiate this class directly; instead, retrieve it through
3253332883543868fb83e111a07306368b7772b340Tobias Haamel * {@link android.content.Context#getSystemService
3329274dc363229a3a4f18c7341f90bd04e39c2c07Tobias Haamel * Context.getSystemService(Context.UI_MODE_SERVICE)}.
3453332883543868fb83e111a07306368b7772b340Tobias Haamel */
3553332883543868fb83e111a07306368b7772b340Tobias Haamelpublic class UiModeManager {
3653332883543868fb83e111a07306368b7772b340Tobias Haamel    private static final String TAG = "UiModeManager";
3753332883543868fb83e111a07306368b7772b340Tobias Haamel
387299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn    /**
397299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * Broadcast sent when the device's UI has switched to car mode, either
407299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * by being placed in a car dock or explicit action of the user.  After
417299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * sending the broadcast, the system will start the intent
427299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * {@link android.content.Intent#ACTION_MAIN} with category
437299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * {@link android.content.Intent#CATEGORY_CAR_DOCK}
447299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * to display the car UI, which typically what an application would
457299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * implement to provide their own interface.  However, applications can
467299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * also monitor this Intent in order to be informed of mode changes or
477299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * prevent the normal car UI from being displayed by setting the result
487299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * of the broadcast to {@link Activity#RESULT_CANCELED}.
497299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     */
507299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn    public static String ACTION_ENTER_CAR_MODE = "android.app.action.ENTER_CAR_MODE";
517299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn
527299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn    /**
537299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * Broadcast sent when the device's UI has switch away from car mode back
547299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * to normal mode.  Typically used by a car mode app, to dismiss itself
557299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * when the user exits car mode.
567299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     */
577299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn    public static String ACTION_EXIT_CAR_MODE = "android.app.action.EXIT_CAR_MODE";
587299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn
597299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn    /**
607299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * Broadcast sent when the device's UI has switched to desk mode,
617299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * by being placed in a desk dock.  After
627299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * sending the broadcast, the system will start the intent
637299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * {@link android.content.Intent#ACTION_MAIN} with category
647299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * {@link android.content.Intent#CATEGORY_DESK_DOCK}
657299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * to display the desk UI, which typically what an application would
667299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * implement to provide their own interface.  However, applications can
677299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * also monitor this Intent in order to be informed of mode changes or
687299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * prevent the normal desk UI from being displayed by setting the result
697299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * of the broadcast to {@link Activity#RESULT_CANCELED}.
707299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     */
717299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn    public static String ACTION_ENTER_DESK_MODE = "android.app.action.ENTER_DESK_MODE";
727299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn
737299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn    /**
747299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * Broadcast sent when the device's UI has switch away from car mode back
757299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * to normal mode.  Typically used by a car mode app, to dismiss itself
767299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * when the user exits car mode.
777299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     */
787299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn    public static String ACTION_EXIT_DESK_MODE = "android.app.action.EXIT_DESK_MODE";
797299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn
807299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn    /** Constant for {@link #setNightMode(int)} and {@link #getNightMode()}:
817299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * automatically switch night mode on and off based on the time.
827299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     */
837299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn    public static final int MODE_NIGHT_AUTO = Configuration.UI_MODE_NIGHT_UNDEFINED >> 4;
847299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn
857299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn    /** Constant for {@link #setNightMode(int)} and {@link #getNightMode()}:
867299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * never run in night mode.
877299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     */
887299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn    public static final int MODE_NIGHT_NO = Configuration.UI_MODE_NIGHT_NO >> 4;
897299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn
907299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn    /** Constant for {@link #setNightMode(int)} and {@link #getNightMode()}:
917299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * always run in night mode.
927299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     */
937299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn    public static final int MODE_NIGHT_YES = Configuration.UI_MODE_NIGHT_YES >> 4;
9453332883543868fb83e111a07306368b7772b340Tobias Haamel
9553332883543868fb83e111a07306368b7772b340Tobias Haamel    private IUiModeManager mService;
9653332883543868fb83e111a07306368b7772b340Tobias Haamel
9753332883543868fb83e111a07306368b7772b340Tobias Haamel    /*package*/ UiModeManager() {
9853332883543868fb83e111a07306368b7772b340Tobias Haamel        mService = IUiModeManager.Stub.asInterface(
997299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn                ServiceManager.getService(Context.UI_MODE_SERVICE));
10053332883543868fb83e111a07306368b7772b340Tobias Haamel    }
10153332883543868fb83e111a07306368b7772b340Tobias Haamel
10253332883543868fb83e111a07306368b7772b340Tobias Haamel    /**
1037299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * Turn off special mode if currently in car mode.
10453332883543868fb83e111a07306368b7772b340Tobias Haamel     */
10553332883543868fb83e111a07306368b7772b340Tobias Haamel    public void disableCarMode() {
10653332883543868fb83e111a07306368b7772b340Tobias Haamel        if (mService != null) {
10753332883543868fb83e111a07306368b7772b340Tobias Haamel            try {
10853332883543868fb83e111a07306368b7772b340Tobias Haamel                mService.disableCarMode();
10953332883543868fb83e111a07306368b7772b340Tobias Haamel            } catch (RemoteException e) {
11053332883543868fb83e111a07306368b7772b340Tobias Haamel                Log.e(TAG, "disableCarMode: RemoteException", e);
11153332883543868fb83e111a07306368b7772b340Tobias Haamel            }
11253332883543868fb83e111a07306368b7772b340Tobias Haamel        }
11353332883543868fb83e111a07306368b7772b340Tobias Haamel    }
11453332883543868fb83e111a07306368b7772b340Tobias Haamel
11553332883543868fb83e111a07306368b7772b340Tobias Haamel    /**
1167299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * Return the current running mode type.  May be one of
1177299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * {@link Configuration#UI_MODE_TYPE_NORMAL Configuration.UI_MODE_TYPE_NORMAL},
1187299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * {@link Configuration#UI_MODE_TYPE_DESK Configuration.UI_MODE_TYPE_DESK}, or
1197299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * {@link Configuration#UI_MODE_TYPE_CAR Configuration.UI_MODE_TYPE_CAR},
1207299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     */
1217299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn    public int getCurrentModeType() {
1227299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn        if (mService != null) {
1237299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn            try {
1247299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn                return mService.getCurrentModeType();
1257299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn            } catch (RemoteException e) {
1267299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn                Log.e(TAG, "getCurrentModeType: RemoteException", e);
1277299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn            }
1287299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn        }
1297299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn        return Configuration.UI_MODE_TYPE_NORMAL;
1307299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn    }
1317299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn
1327299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn    /**
13353332883543868fb83e111a07306368b7772b340Tobias Haamel     * Sets the night mode.  Changes to the night mode are only effective when
1347299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * the car or desk mode is enabled on a device.
13553332883543868fb83e111a07306368b7772b340Tobias Haamel     *
13653332883543868fb83e111a07306368b7772b340Tobias Haamel     * <p>The mode can be one of:
13753332883543868fb83e111a07306368b7772b340Tobias Haamel     * <ul>
1387299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     *   <li><em>{@link #MODE_NIGHT_NO}<em> - sets the device into notnight
13953332883543868fb83e111a07306368b7772b340Tobias Haamel     *       mode.</li>
1407299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     *   <li><em>{@link #MODE_NIGHT_YES}</em> - sets the device into night mode.
14153332883543868fb83e111a07306368b7772b340Tobias Haamel     *   </li>
1427299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     *   <li><em>{@link #MODE_NIGHT_AUTO}</em> - automatic night/notnight switching
14353332883543868fb83e111a07306368b7772b340Tobias Haamel     *       depending on the location and certain other sensors.</li>
1447299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * </ul>
14553332883543868fb83e111a07306368b7772b340Tobias Haamel     */
14653332883543868fb83e111a07306368b7772b340Tobias Haamel    public void setNightMode(int mode) {
14753332883543868fb83e111a07306368b7772b340Tobias Haamel        if (mService != null) {
14853332883543868fb83e111a07306368b7772b340Tobias Haamel            try {
14953332883543868fb83e111a07306368b7772b340Tobias Haamel                mService.setNightMode(mode);
15053332883543868fb83e111a07306368b7772b340Tobias Haamel            } catch (RemoteException e) {
15153332883543868fb83e111a07306368b7772b340Tobias Haamel                Log.e(TAG, "setNightMode: RemoteException", e);
15253332883543868fb83e111a07306368b7772b340Tobias Haamel            }
15353332883543868fb83e111a07306368b7772b340Tobias Haamel        }
15453332883543868fb83e111a07306368b7772b340Tobias Haamel    }
15553332883543868fb83e111a07306368b7772b340Tobias Haamel
15653332883543868fb83e111a07306368b7772b340Tobias Haamel    /**
15753332883543868fb83e111a07306368b7772b340Tobias Haamel     * Returns the currently configured night mode.
15853332883543868fb83e111a07306368b7772b340Tobias Haamel     *
1597299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     * @return {@link #MODE_NIGHT_NO}, {@link #MODE_NIGHT_YES}, or
1607299c41630935a2b106e73e5603579a7747f7535Dianne Hackborn     *  {@link #MODE_NIGHT_AUTO}.  When an error occurred -1 is returned.
16153332883543868fb83e111a07306368b7772b340Tobias Haamel     */
16253332883543868fb83e111a07306368b7772b340Tobias Haamel    public int getNightMode() {
16353332883543868fb83e111a07306368b7772b340Tobias Haamel        if (mService != null) {
16453332883543868fb83e111a07306368b7772b340Tobias Haamel            try {
16553332883543868fb83e111a07306368b7772b340Tobias Haamel                return mService.getNightMode();
16653332883543868fb83e111a07306368b7772b340Tobias Haamel            } catch (RemoteException e) {
16753332883543868fb83e111a07306368b7772b340Tobias Haamel                Log.e(TAG, "getNightMode: RemoteException", e);
16853332883543868fb83e111a07306368b7772b340Tobias Haamel            }
16953332883543868fb83e111a07306368b7772b340Tobias Haamel        }
17053332883543868fb83e111a07306368b7772b340Tobias Haamel        return -1;
17153332883543868fb83e111a07306368b7772b340Tobias Haamel    }
17253332883543868fb83e111a07306368b7772b340Tobias Haamel}
173