1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.app;
18 
19 import android.annotation.IntDef;
20 import android.annotation.SystemService;
21 import android.annotation.TestApi;
22 import android.content.Context;
23 import android.content.res.Configuration;
24 import android.os.RemoteException;
25 import android.os.ServiceManager;
26 import android.os.ServiceManager.ServiceNotFoundException;
27 import android.util.Log;
28 
29 import java.lang.annotation.Retention;
30 import java.lang.annotation.RetentionPolicy;
31 
32 /**
33  * This class provides access to the system uimode services.  These services
34  * allow applications to control UI modes of the device.
35  * It provides functionality to disable the car mode and it gives access to the
36  * night mode settings.
37  *
38  * <p>These facilities are built on top of the underlying
39  * {@link android.content.Intent#ACTION_DOCK_EVENT} broadcasts that are sent when the user
40  * physical places the device into and out of a dock.  When that happens,
41  * the UiModeManager switches the system {@link android.content.res.Configuration}
42  * to the appropriate UI mode, sends broadcasts about the mode switch, and
43  * starts the corresponding mode activity if appropriate.  See the
44  * broadcasts {@link #ACTION_ENTER_CAR_MODE} and
45  * {@link #ACTION_ENTER_DESK_MODE} for more information.
46  *
47  * <p>In addition, the user may manually switch the system to car mode without
48  * physically being in a dock.  While in car mode -- whether by manual action
49  * from the user or being physically placed in a dock -- a notification is
50  * displayed allowing the user to exit dock mode.  Thus the dock mode
51  * represented here may be different than the current state of the underlying
52  * dock event broadcast.
53  */
54 @SystemService(Context.UI_MODE_SERVICE)
55 public class UiModeManager {
56     private static final String TAG = "UiModeManager";
57 
58     /**
59      * Broadcast sent when the device's UI has switched to car mode, either
60      * by being placed in a car dock or explicit action of the user.  After
61      * sending the broadcast, the system will start the intent
62      * {@link android.content.Intent#ACTION_MAIN} with category
63      * {@link android.content.Intent#CATEGORY_CAR_DOCK}
64      * to display the car UI, which typically what an application would
65      * implement to provide their own interface.  However, applications can
66      * also monitor this Intent in order to be informed of mode changes or
67      * prevent the normal car UI from being displayed by setting the result
68      * of the broadcast to {@link Activity#RESULT_CANCELED}.
69      */
70     public static String ACTION_ENTER_CAR_MODE = "android.app.action.ENTER_CAR_MODE";
71 
72     /**
73      * Broadcast sent when the device's UI has switch away from car mode back
74      * to normal mode.  Typically used by a car mode app, to dismiss itself
75      * when the user exits car mode.
76      */
77     public static String ACTION_EXIT_CAR_MODE = "android.app.action.EXIT_CAR_MODE";
78 
79     /**
80      * Broadcast sent when the device's UI has switched to desk mode,
81      * by being placed in a desk dock.  After
82      * sending the broadcast, the system will start the intent
83      * {@link android.content.Intent#ACTION_MAIN} with category
84      * {@link android.content.Intent#CATEGORY_DESK_DOCK}
85      * to display the desk UI, which typically what an application would
86      * implement to provide their own interface.  However, applications can
87      * also monitor this Intent in order to be informed of mode changes or
88      * prevent the normal desk UI from being displayed by setting the result
89      * of the broadcast to {@link Activity#RESULT_CANCELED}.
90      */
91     public static String ACTION_ENTER_DESK_MODE = "android.app.action.ENTER_DESK_MODE";
92 
93     /**
94      * Broadcast sent when the device's UI has switched away from desk mode back
95      * to normal mode.  Typically used by a desk mode app, to dismiss itself
96      * when the user exits desk mode.
97      */
98     public static String ACTION_EXIT_DESK_MODE = "android.app.action.EXIT_DESK_MODE";
99 
100     /** @hide */
101     @IntDef(prefix = { "MODE_" }, value = {
102             MODE_NIGHT_AUTO,
103             MODE_NIGHT_NO,
104             MODE_NIGHT_YES
105     })
106     @Retention(RetentionPolicy.SOURCE)
107     public @interface NightMode {}
108 
109     /**
110      * Constant for {@link #setNightMode(int)} and {@link #getNightMode()}:
111      * automatically switch night mode on and off based on the time.
112      */
113     public static final int MODE_NIGHT_AUTO = Configuration.UI_MODE_NIGHT_UNDEFINED >> 4;
114 
115     /**
116      * Constant for {@link #setNightMode(int)} and {@link #getNightMode()}:
117      * never run in night mode.
118      */
119     public static final int MODE_NIGHT_NO = Configuration.UI_MODE_NIGHT_NO >> 4;
120 
121     /**
122      * Constant for {@link #setNightMode(int)} and {@link #getNightMode()}:
123      * always run in night mode.
124      */
125     public static final int MODE_NIGHT_YES = Configuration.UI_MODE_NIGHT_YES >> 4;
126 
127     private IUiModeManager mService;
128 
UiModeManager()129     /*package*/ UiModeManager() throws ServiceNotFoundException {
130         mService = IUiModeManager.Stub.asInterface(
131                 ServiceManager.getServiceOrThrow(Context.UI_MODE_SERVICE));
132     }
133 
134     /**
135      * Flag for use with {@link #enableCarMode(int)}: go to the car
136      * home activity as part of the enable.  Enabling this way ensures
137      * a clean transition between the current activity (in non-car-mode) and
138      * the car home activity that will serve as home while in car mode.  This
139      * will switch to the car home activity even if we are already in car mode.
140      */
141     public static final int ENABLE_CAR_MODE_GO_CAR_HOME = 0x0001;
142 
143     /**
144      * Flag for use with {@link #enableCarMode(int)}: allow sleep mode while in car mode.
145      * By default, when this flag is not set, the system may hold a full wake lock to keep the
146      * screen turned on and prevent the system from entering sleep mode while in car mode.
147      * Setting this flag disables such behavior and the system may enter sleep mode
148      * if there is no other user activity and no other wake lock held.
149      * Setting this flag can be relevant for a car dock application that does not require the
150      * screen kept on.
151      */
152     public static final int ENABLE_CAR_MODE_ALLOW_SLEEP = 0x0002;
153 
154     /**
155      * Force device into car mode, like it had been placed in the car dock.
156      * This will cause the device to switch to the car home UI as part of
157      * the mode switch.
158      * @param flags Must be 0.
159      */
enableCarMode(int flags)160     public void enableCarMode(int flags) {
161         if (mService != null) {
162             try {
163                 mService.enableCarMode(flags);
164             } catch (RemoteException e) {
165                 throw e.rethrowFromSystemServer();
166             }
167         }
168     }
169 
170     /**
171      * Flag for use with {@link #disableCarMode(int)}: go to the normal
172      * home activity as part of the disable.  Disabling this way ensures
173      * a clean transition between the current activity (in car mode) and
174      * the original home activity (which was typically last running without
175      * being in car mode).
176      */
177     public static final int DISABLE_CAR_MODE_GO_HOME = 0x0001;
178 
179     /**
180      * Turn off special mode if currently in car mode.
181      * @param flags May be 0 or {@link #DISABLE_CAR_MODE_GO_HOME}.
182      */
disableCarMode(int flags)183     public void disableCarMode(int flags) {
184         if (mService != null) {
185             try {
186                 mService.disableCarMode(flags);
187             } catch (RemoteException e) {
188                 throw e.rethrowFromSystemServer();
189             }
190         }
191     }
192 
193     /**
194      * Return the current running mode type.  May be one of
195      * {@link Configuration#UI_MODE_TYPE_NORMAL Configuration.UI_MODE_TYPE_NORMAL},
196      * {@link Configuration#UI_MODE_TYPE_DESK Configuration.UI_MODE_TYPE_DESK},
197      * {@link Configuration#UI_MODE_TYPE_CAR Configuration.UI_MODE_TYPE_CAR},
198      * {@link Configuration#UI_MODE_TYPE_TELEVISION Configuration.UI_MODE_TYPE_TELEVISION},
199      * {@link Configuration#UI_MODE_TYPE_APPLIANCE Configuration.UI_MODE_TYPE_APPLIANCE},
200      * {@link Configuration#UI_MODE_TYPE_WATCH Configuration.UI_MODE_TYPE_WATCH}, or
201      * {@link Configuration#UI_MODE_TYPE_VR_HEADSET Configuration.UI_MODE_TYPE_VR_HEADSET}.
202      */
getCurrentModeType()203     public int getCurrentModeType() {
204         if (mService != null) {
205             try {
206                 return mService.getCurrentModeType();
207             } catch (RemoteException e) {
208                 throw e.rethrowFromSystemServer();
209             }
210         }
211         return Configuration.UI_MODE_TYPE_NORMAL;
212     }
213 
214     /**
215      * Sets the night mode.
216      * <p>
217      * The mode can be one of:
218      * <ul>
219      *   <li><em>{@link #MODE_NIGHT_NO}<em> sets the device into
220      *       {@code notnight} mode</li>
221      *   <li><em>{@link #MODE_NIGHT_YES}</em> sets the device into
222      *       {@code night} mode</li>
223      *   <li><em>{@link #MODE_NIGHT_AUTO}</em> automatically switches between
224      *       {@code night} and {@code notnight} based on the device's current
225      *       location and certain other sensors</li>
226      * </ul>
227      * <p>
228      * <strong>Note:</strong> On API 22 and below, changes to the night mode
229      * are only effective when the {@link Configuration#UI_MODE_TYPE_CAR car}
230      * or {@link Configuration#UI_MODE_TYPE_DESK desk} mode is enabled on a
231      * device. Starting in API 23, changes to night mode are always effective.
232      *
233      * @param mode the night mode to set
234      * @see #getNightMode()
235      */
setNightMode(@ightMode int mode)236     public void setNightMode(@NightMode int mode) {
237         if (mService != null) {
238             try {
239                 mService.setNightMode(mode);
240             } catch (RemoteException e) {
241                 throw e.rethrowFromSystemServer();
242             }
243         }
244     }
245 
246     /**
247      * Returns the currently configured night mode.
248      * <p>
249      * May be one of:
250      * <ul>
251      *   <li>{@link #MODE_NIGHT_NO}</li>
252      *   <li>{@link #MODE_NIGHT_YES}</li>
253      *   <li>{@link #MODE_NIGHT_AUTO}</li>
254      *   <li>{@code -1} on error</li>
255      * </ul>
256      *
257      * @return the current night mode, or {@code -1} on error
258      * @see #setNightMode(int)
259      */
getNightMode()260     public @NightMode int getNightMode() {
261         if (mService != null) {
262             try {
263                 return mService.getNightMode();
264             } catch (RemoteException e) {
265                 throw e.rethrowFromSystemServer();
266             }
267         }
268         return -1;
269     }
270 
271     /**
272      * @return If UI mode is locked or not. When UI mode is locked, calls to change UI mode
273      *         like {@link #enableCarMode(int)} will silently fail.
274      * @hide
275      */
276     @TestApi
isUiModeLocked()277     public boolean isUiModeLocked() {
278         if (mService != null) {
279             try {
280                 return mService.isUiModeLocked();
281             } catch (RemoteException e) {
282                 throw e.rethrowFromSystemServer();
283             }
284         }
285         return true;
286     }
287 
288     /**
289      * Returns whether night mode is locked or not.
290      * <p>
291      * When night mode is locked, only privileged system components may change
292      * night mode and calls from non-privileged applications to change night
293      * mode will fail silently.
294      *
295      * @return {@code true} if night mode is locked or {@code false} otherwise
296      * @hide
297      */
298     @TestApi
isNightModeLocked()299     public boolean isNightModeLocked() {
300         if (mService != null) {
301             try {
302                 return mService.isNightModeLocked();
303             } catch (RemoteException e) {
304                 throw e.rethrowFromSystemServer();
305             }
306         }
307         return true;
308     }
309 }
310