blob: 0046b0e4a8bf7ae96447fbe24535af8e4dbae402 [file] [log] [blame]
Kenny Root15a4d2f2010-03-11 18:20:12 -08001/*
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
Tobias Haamel53332882010-02-18 16:15:43 -080017package android.app;
18
Alan Viverette5794a5b2016-01-12 15:04:18 -050019import android.annotation.IntDef;
Keun-young Park078f9b92016-03-02 02:18:19 +000020import android.annotation.TestApi;
Dianne Hackborn7299c412010-03-04 18:41:49 -080021import android.content.Context;
22import android.content.res.Configuration;
Tobias Haamel53332882010-02-18 16:15:43 -080023import android.os.RemoteException;
24import android.os.ServiceManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060025import android.os.ServiceManager.ServiceNotFoundException;
Tobias Haamel53332882010-02-18 16:15:43 -080026import android.util.Log;
27
Alan Viverette5794a5b2016-01-12 15:04:18 -050028import java.lang.annotation.Retention;
29import java.lang.annotation.RetentionPolicy;
30
Tobias Haamel53332882010-02-18 16:15:43 -080031/**
32 * This class provides access to the system uimode services. These services
33 * allow applications to control UI modes of the device.
34 * It provides functionality to disable the car mode and it gives access to the
35 * night mode settings.
Dianne Hackborn7299c412010-03-04 18:41:49 -080036 *
37 * <p>These facilities are built on top of the underlying
38 * {@link android.content.Intent#ACTION_DOCK_EVENT} broadcasts that are sent when the user
39 * physical places the device into and out of a dock. When that happens,
40 * the UiModeManager switches the system {@link android.content.res.Configuration}
41 * to the appropriate UI mode, sends broadcasts about the mode switch, and
42 * starts the corresponding mode activity if appropriate. See the
43 * broadcasts {@link #ACTION_ENTER_CAR_MODE} and
44 * {@link #ACTION_ENTER_DESK_MODE} for more information.
45 *
46 * <p>In addition, the user may manually switch the system to car mode without
47 * physically being in a dock. While in car mode -- whether by manual action
48 * from the user or being physically placed in a dock -- a notification is
49 * displayed allowing the user to exit dock mode. Thus the dock mode
50 * represented here may be different than the current state of the underlying
51 * dock event broadcast.
Tobias Haamel53332882010-02-18 16:15:43 -080052 *
53 * <p>You do not instantiate this class directly; instead, retrieve it through
54 * {@link android.content.Context#getSystemService
Tobias Haamel29274dc2010-02-22 22:25:33 +010055 * Context.getSystemService(Context.UI_MODE_SERVICE)}.
Tobias Haamel53332882010-02-18 16:15:43 -080056 */
57public class UiModeManager {
58 private static final String TAG = "UiModeManager";
59
Dianne Hackborn7299c412010-03-04 18:41:49 -080060 /**
61 * Broadcast sent when the device's UI has switched to car mode, either
62 * by being placed in a car dock or explicit action of the user. After
63 * sending the broadcast, the system will start the intent
64 * {@link android.content.Intent#ACTION_MAIN} with category
65 * {@link android.content.Intent#CATEGORY_CAR_DOCK}
66 * to display the car UI, which typically what an application would
67 * implement to provide their own interface. However, applications can
68 * also monitor this Intent in order to be informed of mode changes or
69 * prevent the normal car UI from being displayed by setting the result
70 * of the broadcast to {@link Activity#RESULT_CANCELED}.
71 */
72 public static String ACTION_ENTER_CAR_MODE = "android.app.action.ENTER_CAR_MODE";
73
74 /**
75 * Broadcast sent when the device's UI has switch away from car mode back
76 * to normal mode. Typically used by a car mode app, to dismiss itself
77 * when the user exits car mode.
78 */
79 public static String ACTION_EXIT_CAR_MODE = "android.app.action.EXIT_CAR_MODE";
80
81 /**
82 * Broadcast sent when the device's UI has switched to desk mode,
83 * by being placed in a desk dock. After
84 * sending the broadcast, the system will start the intent
85 * {@link android.content.Intent#ACTION_MAIN} with category
86 * {@link android.content.Intent#CATEGORY_DESK_DOCK}
87 * to display the desk UI, which typically what an application would
88 * implement to provide their own interface. However, applications can
89 * also monitor this Intent in order to be informed of mode changes or
90 * prevent the normal desk UI from being displayed by setting the result
91 * of the broadcast to {@link Activity#RESULT_CANCELED}.
92 */
93 public static String ACTION_ENTER_DESK_MODE = "android.app.action.ENTER_DESK_MODE";
94
95 /**
Daniel Sandlerb999abc2010-03-11 15:19:53 -050096 * Broadcast sent when the device's UI has switched away from desk mode back
97 * to normal mode. Typically used by a desk mode app, to dismiss itself
98 * when the user exits desk mode.
Dianne Hackborn7299c412010-03-04 18:41:49 -080099 */
100 public static String ACTION_EXIT_DESK_MODE = "android.app.action.EXIT_DESK_MODE";
Alan Viverette5794a5b2016-01-12 15:04:18 -0500101
102 /** @hide */
103 @IntDef({MODE_NIGHT_AUTO, MODE_NIGHT_NO, MODE_NIGHT_YES})
104 @Retention(RetentionPolicy.SOURCE)
105 public @interface NightMode {}
106
107 /**
108 * Constant for {@link #setNightMode(int)} and {@link #getNightMode()}:
Dianne Hackborn7299c412010-03-04 18:41:49 -0800109 * automatically switch night mode on and off based on the time.
110 */
111 public static final int MODE_NIGHT_AUTO = Configuration.UI_MODE_NIGHT_UNDEFINED >> 4;
112
Alan Viverette5794a5b2016-01-12 15:04:18 -0500113 /**
114 * Constant for {@link #setNightMode(int)} and {@link #getNightMode()}:
Dianne Hackborn7299c412010-03-04 18:41:49 -0800115 * never run in night mode.
116 */
117 public static final int MODE_NIGHT_NO = Configuration.UI_MODE_NIGHT_NO >> 4;
118
Alan Viverette5794a5b2016-01-12 15:04:18 -0500119 /**
120 * Constant for {@link #setNightMode(int)} and {@link #getNightMode()}:
Dianne Hackborn7299c412010-03-04 18:41:49 -0800121 * always run in night mode.
122 */
123 public static final int MODE_NIGHT_YES = Configuration.UI_MODE_NIGHT_YES >> 4;
Tobias Haamel53332882010-02-18 16:15:43 -0800124
125 private IUiModeManager mService;
126
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600127 /*package*/ UiModeManager() throws ServiceNotFoundException {
Tobias Haamel53332882010-02-18 16:15:43 -0800128 mService = IUiModeManager.Stub.asInterface(
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600129 ServiceManager.getServiceOrThrow(Context.UI_MODE_SERVICE));
Tobias Haamel53332882010-02-18 16:15:43 -0800130 }
131
132 /**
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700133 * Flag for use with {@link #enableCarMode(int)}: go to the car
134 * home activity as part of the enable. Enabling this way ensures
135 * a clean transition between the current activity (in non-car-mode) and
136 * the car home activity that will serve as home while in car mode. This
137 * will switch to the car home activity even if we are already in car mode.
Tobias Haamel53332882010-02-18 16:15:43 -0800138 */
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700139 public static final int ENABLE_CAR_MODE_GO_CAR_HOME = 0x0001;
keunyoung1d0a7cc2014-07-28 13:12:50 -0700140
141 /**
keunyoungc093bf22014-08-11 18:51:15 -0700142 * Flag for use with {@link #enableCarMode(int)}: allow sleep mode while in car mode.
143 * By default, when this flag is not set, the system may hold a full wake lock to keep the
144 * screen turned on and prevent the system from entering sleep mode while in car mode.
145 * Setting this flag disables such behavior and the system may enter sleep mode
146 * if there is no other user activity and no other wake lock held.
keunyoung1d0a7cc2014-07-28 13:12:50 -0700147 * Setting this flag can be relevant for a car dock application that does not require the
148 * screen kept on.
149 */
keunyoungc093bf22014-08-11 18:51:15 -0700150 public static final int ENABLE_CAR_MODE_ALLOW_SLEEP = 0x0002;
keunyoung1d0a7cc2014-07-28 13:12:50 -0700151
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700152 /**
Dianne Hackborn9c9c5322010-03-30 23:12:22 -0700153 * Force device into car mode, like it had been placed in the car dock.
154 * This will cause the device to switch to the car home UI as part of
155 * the mode switch.
156 * @param flags Must be 0.
157 */
158 public void enableCarMode(int flags) {
159 if (mService != null) {
160 try {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700161 mService.enableCarMode(flags);
Dianne Hackborn9c9c5322010-03-30 23:12:22 -0700162 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700163 throw e.rethrowFromSystemServer();
Dianne Hackborn9c9c5322010-03-30 23:12:22 -0700164 }
165 }
166 }
167
168 /**
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700169 * Flag for use with {@link #disableCarMode(int)}: go to the normal
170 * home activity as part of the disable. Disabling this way ensures
171 * a clean transition between the current activity (in car mode) and
172 * the original home activity (which was typically last running without
173 * being in car mode).
174 */
175 public static final int DISABLE_CAR_MODE_GO_HOME = 0x0001;
176
177 /**
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700178 * Turn off special mode if currently in car mode.
179 * @param flags May be 0 or {@link #DISABLE_CAR_MODE_GO_HOME}.
180 */
181 public void disableCarMode(int flags) {
Tobias Haamel53332882010-02-18 16:15:43 -0800182 if (mService != null) {
183 try {
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700184 mService.disableCarMode(flags);
Tobias Haamel53332882010-02-18 16:15:43 -0800185 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700186 throw e.rethrowFromSystemServer();
Tobias Haamel53332882010-02-18 16:15:43 -0800187 }
188 }
189 }
190
191 /**
Dianne Hackborn7299c412010-03-04 18:41:49 -0800192 * Return the current running mode type. May be one of
193 * {@link Configuration#UI_MODE_TYPE_NORMAL Configuration.UI_MODE_TYPE_NORMAL},
John Spurlock6c191292014-04-03 16:37:27 -0400194 * {@link Configuration#UI_MODE_TYPE_DESK Configuration.UI_MODE_TYPE_DESK},
195 * {@link Configuration#UI_MODE_TYPE_CAR Configuration.UI_MODE_TYPE_CAR},
196 * {@link Configuration#UI_MODE_TYPE_TELEVISION Configuration.UI_MODE_TYPE_TELEVISION},
197 * {@link Configuration#UI_MODE_TYPE_APPLIANCE Configuration.UI_MODE_TYPE_APPLIANCE}, or
198 * {@link Configuration#UI_MODE_TYPE_WATCH Configuration.UI_MODE_TYPE_WATCH}.
Dianne Hackborn7299c412010-03-04 18:41:49 -0800199 */
200 public int getCurrentModeType() {
201 if (mService != null) {
202 try {
203 return mService.getCurrentModeType();
204 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700205 throw e.rethrowFromSystemServer();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800206 }
207 }
208 return Configuration.UI_MODE_TYPE_NORMAL;
209 }
210
211 /**
Alan Viverette5794a5b2016-01-12 15:04:18 -0500212 * Sets the night mode.
213 * <p>
214 * The mode can be one of:
Tobias Haamel53332882010-02-18 16:15:43 -0800215 * <ul>
Alan Viverette5794a5b2016-01-12 15:04:18 -0500216 * <li><em>{@link #MODE_NIGHT_NO}<em> sets the device into
217 * {@code notnight} mode</li>
218 * <li><em>{@link #MODE_NIGHT_YES}</em> sets the device into
219 * {@code night} mode</li>
220 * <li><em>{@link #MODE_NIGHT_AUTO}</em> automatically switches between
221 * {@code night} and {@code notnight} based on the device's current
222 * location and certain other sensors</li>
Dianne Hackborn7299c412010-03-04 18:41:49 -0800223 * </ul>
Alan Viverette5794a5b2016-01-12 15:04:18 -0500224 * <p>
225 * <strong>Note:</strong> On API 22 and below, changes to the night mode
226 * are only effective when the {@link Configuration#UI_MODE_TYPE_CAR car}
227 * or {@link Configuration#UI_MODE_TYPE_DESK desk} mode is enabled on a
228 * device. Starting in API 23, changes to night mode are always effective.
229 *
230 * @param mode the night mode to set
231 * @see #getNightMode()
Tobias Haamel53332882010-02-18 16:15:43 -0800232 */
Alan Viverette5794a5b2016-01-12 15:04:18 -0500233 public void setNightMode(@NightMode int mode) {
Tobias Haamel53332882010-02-18 16:15:43 -0800234 if (mService != null) {
235 try {
236 mService.setNightMode(mode);
237 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700238 throw e.rethrowFromSystemServer();
Tobias Haamel53332882010-02-18 16:15:43 -0800239 }
240 }
241 }
242
243 /**
Alan Viverette5794a5b2016-01-12 15:04:18 -0500244 * Returns the currently configured night mode.
245 * <p>
246 * May be one of:
247 * <ul>
248 * <li>{@link #MODE_NIGHT_NO}</li>
249 * <li>{@link #MODE_NIGHT_YES}</li>
250 * <li>{@link #MODE_NIGHT_AUTO}</li>
251 * <li>{@code -1} on error</li>
252 * </ul>
253 *
254 * @return the current night mode, or {@code -1} on error
255 * @see #setNightMode(int)
Tobias Haamel53332882010-02-18 16:15:43 -0800256 */
Alan Viverette5794a5b2016-01-12 15:04:18 -0500257 public @NightMode int getNightMode() {
Tobias Haamel53332882010-02-18 16:15:43 -0800258 if (mService != null) {
259 try {
260 return mService.getNightMode();
261 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700262 throw e.rethrowFromSystemServer();
Tobias Haamel53332882010-02-18 16:15:43 -0800263 }
264 }
265 return -1;
266 }
keunyounga7710492015-09-23 11:33:58 -0700267
268 /**
269 * @return If UI mode is locked or not. When UI mode is locked, calls to change UI mode
270 * like {@link #enableCarMode(int)} will silently fail.
Keun-young Park078f9b92016-03-02 02:18:19 +0000271 * @hide
keunyounga7710492015-09-23 11:33:58 -0700272 */
Keun-young Park078f9b92016-03-02 02:18:19 +0000273 @TestApi
keunyounga7710492015-09-23 11:33:58 -0700274 public boolean isUiModeLocked() {
275 if (mService != null) {
276 try {
277 return mService.isUiModeLocked();
278 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700279 throw e.rethrowFromSystemServer();
keunyounga7710492015-09-23 11:33:58 -0700280 }
281 }
282 return true;
283 }
284
285 /**
Alan Viverette5794a5b2016-01-12 15:04:18 -0500286 * Returns whether night mode is locked or not.
287 * <p>
288 * When night mode is locked, only privileged system components may change
289 * night mode and calls from non-privileged applications to change night
290 * mode will fail silently.
291 *
292 * @return {@code true} if night mode is locked or {@code false} otherwise
Keun-young Park078f9b92016-03-02 02:18:19 +0000293 * @hide
keunyounga7710492015-09-23 11:33:58 -0700294 */
Keun-young Park078f9b92016-03-02 02:18:19 +0000295 @TestApi
keunyounga7710492015-09-23 11:33:58 -0700296 public boolean isNightModeLocked() {
297 if (mService != null) {
298 try {
299 return mService.isNightModeLocked();
300 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700301 throw e.rethrowFromSystemServer();
keunyounga7710492015-09-23 11:33:58 -0700302 }
303 }
304 return true;
305 }
Tobias Haamel53332882010-02-18 16:15:43 -0800306}