blob: 4f172a4251d441c5f73e4b14cc14f65644baf364 [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;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060020import android.annotation.SystemService;
Keun-young Park078f9b92016-03-02 02:18:19 +000021import android.annotation.TestApi;
Mathew Inwood4fb17d12018-08-14 14:25:44 +010022import android.annotation.UnsupportedAppUsage;
Dianne Hackborn7299c412010-03-04 18:41:49 -080023import android.content.Context;
24import android.content.res.Configuration;
Tobias Haamel53332882010-02-18 16:15:43 -080025import android.os.RemoteException;
26import android.os.ServiceManager;
Jeff Sharkey49ca5292016-05-10 12:54:45 -060027import android.os.ServiceManager.ServiceNotFoundException;
Tobias Haamel53332882010-02-18 16:15:43 -080028import android.util.Log;
29
Alan Viverette5794a5b2016-01-12 15:04:18 -050030import java.lang.annotation.Retention;
31import java.lang.annotation.RetentionPolicy;
32
Tobias Haamel53332882010-02-18 16:15:43 -080033/**
34 * This class provides access to the system uimode services. These services
35 * allow applications to control UI modes of the device.
36 * It provides functionality to disable the car mode and it gives access to the
37 * night mode settings.
Dianne Hackborn7299c412010-03-04 18:41:49 -080038 *
39 * <p>These facilities are built on top of the underlying
40 * {@link android.content.Intent#ACTION_DOCK_EVENT} broadcasts that are sent when the user
41 * physical places the device into and out of a dock. When that happens,
42 * the UiModeManager switches the system {@link android.content.res.Configuration}
43 * to the appropriate UI mode, sends broadcasts about the mode switch, and
44 * starts the corresponding mode activity if appropriate. See the
45 * broadcasts {@link #ACTION_ENTER_CAR_MODE} and
46 * {@link #ACTION_ENTER_DESK_MODE} for more information.
47 *
48 * <p>In addition, the user may manually switch the system to car mode without
49 * physically being in a dock. While in car mode -- whether by manual action
50 * from the user or being physically placed in a dock -- a notification is
51 * displayed allowing the user to exit dock mode. Thus the dock mode
52 * represented here may be different than the current state of the underlying
53 * dock event broadcast.
Tobias Haamel53332882010-02-18 16:15:43 -080054 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060055@SystemService(Context.UI_MODE_SERVICE)
Tobias Haamel53332882010-02-18 16:15:43 -080056public class UiModeManager {
57 private static final String TAG = "UiModeManager";
58
Dianne Hackborn7299c412010-03-04 18:41:49 -080059 /**
60 * Broadcast sent when the device's UI has switched to car mode, either
61 * by being placed in a car dock or explicit action of the user. After
62 * sending the broadcast, the system will start the intent
63 * {@link android.content.Intent#ACTION_MAIN} with category
64 * {@link android.content.Intent#CATEGORY_CAR_DOCK}
65 * to display the car UI, which typically what an application would
66 * implement to provide their own interface. However, applications can
67 * also monitor this Intent in order to be informed of mode changes or
68 * prevent the normal car UI from being displayed by setting the result
69 * of the broadcast to {@link Activity#RESULT_CANCELED}.
70 */
71 public static String ACTION_ENTER_CAR_MODE = "android.app.action.ENTER_CAR_MODE";
72
73 /**
74 * Broadcast sent when the device's UI has switch away from car mode back
75 * to normal mode. Typically used by a car mode app, to dismiss itself
76 * when the user exits car mode.
77 */
78 public static String ACTION_EXIT_CAR_MODE = "android.app.action.EXIT_CAR_MODE";
79
80 /**
81 * Broadcast sent when the device's UI has switched to desk mode,
82 * by being placed in a desk dock. After
83 * sending the broadcast, the system will start the intent
84 * {@link android.content.Intent#ACTION_MAIN} with category
85 * {@link android.content.Intent#CATEGORY_DESK_DOCK}
86 * to display the desk UI, which typically what an application would
87 * implement to provide their own interface. However, applications can
88 * also monitor this Intent in order to be informed of mode changes or
89 * prevent the normal desk UI from being displayed by setting the result
90 * of the broadcast to {@link Activity#RESULT_CANCELED}.
91 */
92 public static String ACTION_ENTER_DESK_MODE = "android.app.action.ENTER_DESK_MODE";
93
94 /**
Daniel Sandlerb999abc2010-03-11 15:19:53 -050095 * Broadcast sent when the device's UI has switched away from desk mode back
96 * to normal mode. Typically used by a desk mode app, to dismiss itself
97 * when the user exits desk mode.
Dianne Hackborn7299c412010-03-04 18:41:49 -080098 */
99 public static String ACTION_EXIT_DESK_MODE = "android.app.action.EXIT_DESK_MODE";
Alan Viverette5794a5b2016-01-12 15:04:18 -0500100
101 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700102 @IntDef(prefix = { "MODE_" }, value = {
103 MODE_NIGHT_AUTO,
104 MODE_NIGHT_NO,
105 MODE_NIGHT_YES
106 })
Alan Viverette5794a5b2016-01-12 15:04:18 -0500107 @Retention(RetentionPolicy.SOURCE)
108 public @interface NightMode {}
109
110 /**
111 * Constant for {@link #setNightMode(int)} and {@link #getNightMode()}:
Dianne Hackborn7299c412010-03-04 18:41:49 -0800112 * automatically switch night mode on and off based on the time.
113 */
114 public static final int MODE_NIGHT_AUTO = Configuration.UI_MODE_NIGHT_UNDEFINED >> 4;
115
Alan Viverette5794a5b2016-01-12 15:04:18 -0500116 /**
117 * Constant for {@link #setNightMode(int)} and {@link #getNightMode()}:
Dianne Hackborn7299c412010-03-04 18:41:49 -0800118 * never run in night mode.
119 */
120 public static final int MODE_NIGHT_NO = Configuration.UI_MODE_NIGHT_NO >> 4;
121
Alan Viverette5794a5b2016-01-12 15:04:18 -0500122 /**
123 * Constant for {@link #setNightMode(int)} and {@link #getNightMode()}:
Dianne Hackborn7299c412010-03-04 18:41:49 -0800124 * always run in night mode.
125 */
126 public static final int MODE_NIGHT_YES = Configuration.UI_MODE_NIGHT_YES >> 4;
Tobias Haamel53332882010-02-18 16:15:43 -0800127
128 private IUiModeManager mService;
129
Mathew Inwood4fb17d12018-08-14 14:25:44 +0100130 @UnsupportedAppUsage
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600131 /*package*/ UiModeManager() throws ServiceNotFoundException {
Tobias Haamel53332882010-02-18 16:15:43 -0800132 mService = IUiModeManager.Stub.asInterface(
Jeff Sharkey49ca5292016-05-10 12:54:45 -0600133 ServiceManager.getServiceOrThrow(Context.UI_MODE_SERVICE));
Tobias Haamel53332882010-02-18 16:15:43 -0800134 }
135
136 /**
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700137 * Flag for use with {@link #enableCarMode(int)}: go to the car
138 * home activity as part of the enable. Enabling this way ensures
139 * a clean transition between the current activity (in non-car-mode) and
140 * the car home activity that will serve as home while in car mode. This
141 * will switch to the car home activity even if we are already in car mode.
Tobias Haamel53332882010-02-18 16:15:43 -0800142 */
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700143 public static final int ENABLE_CAR_MODE_GO_CAR_HOME = 0x0001;
keunyoung1d0a7cc2014-07-28 13:12:50 -0700144
145 /**
keunyoungc093bf22014-08-11 18:51:15 -0700146 * Flag for use with {@link #enableCarMode(int)}: allow sleep mode while in car mode.
147 * By default, when this flag is not set, the system may hold a full wake lock to keep the
148 * screen turned on and prevent the system from entering sleep mode while in car mode.
149 * Setting this flag disables such behavior and the system may enter sleep mode
150 * if there is no other user activity and no other wake lock held.
keunyoung1d0a7cc2014-07-28 13:12:50 -0700151 * Setting this flag can be relevant for a car dock application that does not require the
152 * screen kept on.
153 */
keunyoungc093bf22014-08-11 18:51:15 -0700154 public static final int ENABLE_CAR_MODE_ALLOW_SLEEP = 0x0002;
keunyoung1d0a7cc2014-07-28 13:12:50 -0700155
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700156 /**
Dianne Hackborn9c9c5322010-03-30 23:12:22 -0700157 * Force device into car mode, like it had been placed in the car dock.
158 * This will cause the device to switch to the car home UI as part of
159 * the mode switch.
160 * @param flags Must be 0.
161 */
162 public void enableCarMode(int flags) {
163 if (mService != null) {
164 try {
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700165 mService.enableCarMode(flags);
Dianne Hackborn9c9c5322010-03-30 23:12:22 -0700166 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700167 throw e.rethrowFromSystemServer();
Dianne Hackborn9c9c5322010-03-30 23:12:22 -0700168 }
169 }
170 }
171
172 /**
Dianne Hackbornf5c5d222010-04-09 13:14:48 -0700173 * Flag for use with {@link #disableCarMode(int)}: go to the normal
174 * home activity as part of the disable. Disabling this way ensures
175 * a clean transition between the current activity (in car mode) and
176 * the original home activity (which was typically last running without
177 * being in car mode).
178 */
179 public static final int DISABLE_CAR_MODE_GO_HOME = 0x0001;
180
181 /**
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700182 * Turn off special mode if currently in car mode.
183 * @param flags May be 0 or {@link #DISABLE_CAR_MODE_GO_HOME}.
184 */
185 public void disableCarMode(int flags) {
Tobias Haamel53332882010-02-18 16:15:43 -0800186 if (mService != null) {
187 try {
Dianne Hackbornd49258f2010-03-26 00:44:29 -0700188 mService.disableCarMode(flags);
Tobias Haamel53332882010-02-18 16:15:43 -0800189 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700190 throw e.rethrowFromSystemServer();
Tobias Haamel53332882010-02-18 16:15:43 -0800191 }
192 }
193 }
194
195 /**
Dianne Hackborn7299c412010-03-04 18:41:49 -0800196 * Return the current running mode type. May be one of
197 * {@link Configuration#UI_MODE_TYPE_NORMAL Configuration.UI_MODE_TYPE_NORMAL},
John Spurlock6c191292014-04-03 16:37:27 -0400198 * {@link Configuration#UI_MODE_TYPE_DESK Configuration.UI_MODE_TYPE_DESK},
199 * {@link Configuration#UI_MODE_TYPE_CAR Configuration.UI_MODE_TYPE_CAR},
200 * {@link Configuration#UI_MODE_TYPE_TELEVISION Configuration.UI_MODE_TYPE_TELEVISION},
Zak Cohen1a6acdb2016-12-12 15:21:21 -0800201 * {@link Configuration#UI_MODE_TYPE_APPLIANCE Configuration.UI_MODE_TYPE_APPLIANCE},
202 * {@link Configuration#UI_MODE_TYPE_WATCH Configuration.UI_MODE_TYPE_WATCH}, or
203 * {@link Configuration#UI_MODE_TYPE_VR_HEADSET Configuration.UI_MODE_TYPE_VR_HEADSET}.
Dianne Hackborn7299c412010-03-04 18:41:49 -0800204 */
205 public int getCurrentModeType() {
206 if (mService != null) {
207 try {
208 return mService.getCurrentModeType();
209 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700210 throw e.rethrowFromSystemServer();
Dianne Hackborn7299c412010-03-04 18:41:49 -0800211 }
212 }
213 return Configuration.UI_MODE_TYPE_NORMAL;
214 }
215
216 /**
Alan Viverette5794a5b2016-01-12 15:04:18 -0500217 * Sets the night mode.
218 * <p>
219 * The mode can be one of:
Tobias Haamel53332882010-02-18 16:15:43 -0800220 * <ul>
Alan Viverette5794a5b2016-01-12 15:04:18 -0500221 * <li><em>{@link #MODE_NIGHT_NO}<em> sets the device into
222 * {@code notnight} mode</li>
223 * <li><em>{@link #MODE_NIGHT_YES}</em> sets the device into
224 * {@code night} mode</li>
225 * <li><em>{@link #MODE_NIGHT_AUTO}</em> automatically switches between
226 * {@code night} and {@code notnight} based on the device's current
227 * location and certain other sensors</li>
Dianne Hackborn7299c412010-03-04 18:41:49 -0800228 * </ul>
Alan Viverette5794a5b2016-01-12 15:04:18 -0500229 * <p>
230 * <strong>Note:</strong> On API 22 and below, changes to the night mode
231 * are only effective when the {@link Configuration#UI_MODE_TYPE_CAR car}
232 * or {@link Configuration#UI_MODE_TYPE_DESK desk} mode is enabled on a
233 * device. Starting in API 23, changes to night mode are always effective.
234 *
235 * @param mode the night mode to set
236 * @see #getNightMode()
Tobias Haamel53332882010-02-18 16:15:43 -0800237 */
Alan Viverette5794a5b2016-01-12 15:04:18 -0500238 public void setNightMode(@NightMode int mode) {
Tobias Haamel53332882010-02-18 16:15:43 -0800239 if (mService != null) {
240 try {
241 mService.setNightMode(mode);
242 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700243 throw e.rethrowFromSystemServer();
Tobias Haamel53332882010-02-18 16:15:43 -0800244 }
245 }
246 }
247
248 /**
Alan Viverette5794a5b2016-01-12 15:04:18 -0500249 * Returns the currently configured night mode.
250 * <p>
251 * May be one of:
252 * <ul>
253 * <li>{@link #MODE_NIGHT_NO}</li>
254 * <li>{@link #MODE_NIGHT_YES}</li>
255 * <li>{@link #MODE_NIGHT_AUTO}</li>
256 * <li>{@code -1} on error</li>
257 * </ul>
258 *
259 * @return the current night mode, or {@code -1} on error
260 * @see #setNightMode(int)
Tobias Haamel53332882010-02-18 16:15:43 -0800261 */
Alan Viverette5794a5b2016-01-12 15:04:18 -0500262 public @NightMode int getNightMode() {
Tobias Haamel53332882010-02-18 16:15:43 -0800263 if (mService != null) {
264 try {
265 return mService.getNightMode();
266 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700267 throw e.rethrowFromSystemServer();
Tobias Haamel53332882010-02-18 16:15:43 -0800268 }
269 }
270 return -1;
271 }
keunyounga7710492015-09-23 11:33:58 -0700272
273 /**
274 * @return If UI mode is locked or not. When UI mode is locked, calls to change UI mode
275 * like {@link #enableCarMode(int)} will silently fail.
Keun-young Park078f9b92016-03-02 02:18:19 +0000276 * @hide
keunyounga7710492015-09-23 11:33:58 -0700277 */
Keun-young Park078f9b92016-03-02 02:18:19 +0000278 @TestApi
keunyounga7710492015-09-23 11:33:58 -0700279 public boolean isUiModeLocked() {
280 if (mService != null) {
281 try {
282 return mService.isUiModeLocked();
283 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700284 throw e.rethrowFromSystemServer();
keunyounga7710492015-09-23 11:33:58 -0700285 }
286 }
287 return true;
288 }
289
290 /**
Alan Viverette5794a5b2016-01-12 15:04:18 -0500291 * Returns whether night mode is locked or not.
292 * <p>
293 * When night mode is locked, only privileged system components may change
294 * night mode and calls from non-privileged applications to change night
295 * mode will fail silently.
296 *
297 * @return {@code true} if night mode is locked or {@code false} otherwise
Keun-young Park078f9b92016-03-02 02:18:19 +0000298 * @hide
keunyounga7710492015-09-23 11:33:58 -0700299 */
Keun-young Park078f9b92016-03-02 02:18:19 +0000300 @TestApi
keunyounga7710492015-09-23 11:33:58 -0700301 public boolean isNightModeLocked() {
302 if (mService != null) {
303 try {
304 return mService.isNightModeLocked();
305 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700306 throw e.rethrowFromSystemServer();
keunyounga7710492015-09-23 11:33:58 -0700307 }
308 }
309 return true;
310 }
Tobias Haamel53332882010-02-18 16:15:43 -0800311}