UiModeManager.java revision 53332883543868fb83e111a07306368b7772b340
1package android.app;
2
3import android.os.RemoteException;
4import android.os.ServiceManager;
5import android.util.Log;
6
7/**
8 * This class provides access to the system uimode services.  These services
9 * allow applications to control UI modes of the device.
10 * It provides functionality to disable the car mode and it gives access to the
11 * night mode settings.
12 *
13 * <p>You do not instantiate this class directly; instead, retrieve it through
14 * {@link android.content.Context#getSystemService
15 * Context.getSystemService(Context.UIMODE_SERVICE)}.
16 */
17public class UiModeManager {
18    private static final String TAG = "UiModeManager";
19
20    public static final int MODE_NOTNIGHT = 1;
21    public static final int MODE_NIGHT = 2;
22    public static final int MODE_AUTO = 3;
23
24    private IUiModeManager mService;
25
26    /*package*/ UiModeManager() {
27        mService = IUiModeManager.Stub.asInterface(
28                ServiceManager.getService("uimode"));
29    }
30
31    /**
32     * Disables the car mode.
33     */
34    public void disableCarMode() {
35        if (mService != null) {
36            try {
37                mService.disableCarMode();
38            } catch (RemoteException e) {
39                Log.e(TAG, "disableCarMode: RemoteException", e);
40            }
41        }
42    }
43
44    /**
45     * Sets the night mode.  Changes to the night mode are only effective when
46     * the car mode is enabled on a device.
47     *
48     * <p>The mode can be one of:
49     * <ul>
50     *   <li><em>{@link #MODE_NOTNIGHT}<em> - sets the device into notnight
51     *       mode.</li>
52     *   <li><em>{@link #MODE_NIGHT}</em> - sets the device into night mode.
53     *   </li>
54     *   <li><em>{@link #MODE_AUTO}</em> - automatic night/notnight switching
55     *       depending on the location and certain other sensors.</li>
56     */
57    public void setNightMode(int mode) {
58        if (mService != null) {
59            try {
60                mService.setNightMode(mode);
61            } catch (RemoteException e) {
62                Log.e(TAG, "setNightMode: RemoteException", e);
63            }
64        }
65    }
66
67    /**
68     * Returns the currently configured night mode.
69     *
70     * @return {@link #MODE_NOTNIGHT}, {@link #MODE_NIGHT} or {@link #MODE_AUTO}
71     *         When an error occurred -1 is returned.
72     */
73    public int getNightMode() {
74        if (mService != null) {
75            try {
76                return mService.getNightMode();
77            } catch (RemoteException e) {
78                Log.e(TAG, "getNightMode: RemoteException", e);
79            }
80        }
81        return -1;
82    }
83}
84