Kenny Root | 15a4d2f | 2010-03-11 18:20:12 -0800 | [diff] [blame] | 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 | |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 17 | package android.app; |
| 18 | |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Tyler Gunn | f05f2c2 | 2019-09-30 15:12:24 -0700 | [diff] [blame] | 20 | import android.annotation.IntRange; |
| 21 | import android.annotation.Nullable; |
| 22 | import android.annotation.RequiresPermission; |
| 23 | import android.annotation.SystemApi; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 24 | import android.annotation.SystemService; |
Keun-young Park | 078f9b9 | 2016-03-02 02:18:19 +0000 | [diff] [blame] | 25 | import android.annotation.TestApi; |
Artur Satayev | d9b11b0 | 2019-12-10 17:47:51 +0000 | [diff] [blame] | 26 | import android.compat.annotation.UnsupportedAppUsage; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 27 | import android.content.Context; |
| 28 | import android.content.res.Configuration; |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 29 | import android.os.RemoteException; |
| 30 | import android.os.ServiceManager; |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 31 | import android.os.ServiceManager.ServiceNotFoundException; |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 32 | |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 33 | import java.lang.annotation.Retention; |
| 34 | import java.lang.annotation.RetentionPolicy; |
| 35 | |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 36 | /** |
| 37 | * This class provides access to the system uimode services. These services |
| 38 | * allow applications to control UI modes of the device. |
| 39 | * It provides functionality to disable the car mode and it gives access to the |
| 40 | * night mode settings. |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 41 | * |
| 42 | * <p>These facilities are built on top of the underlying |
| 43 | * {@link android.content.Intent#ACTION_DOCK_EVENT} broadcasts that are sent when the user |
| 44 | * physical places the device into and out of a dock. When that happens, |
| 45 | * the UiModeManager switches the system {@link android.content.res.Configuration} |
| 46 | * to the appropriate UI mode, sends broadcasts about the mode switch, and |
| 47 | * starts the corresponding mode activity if appropriate. See the |
| 48 | * broadcasts {@link #ACTION_ENTER_CAR_MODE} and |
| 49 | * {@link #ACTION_ENTER_DESK_MODE} for more information. |
| 50 | * |
| 51 | * <p>In addition, the user may manually switch the system to car mode without |
| 52 | * physically being in a dock. While in car mode -- whether by manual action |
| 53 | * from the user or being physically placed in a dock -- a notification is |
| 54 | * displayed allowing the user to exit dock mode. Thus the dock mode |
| 55 | * represented here may be different than the current state of the underlying |
| 56 | * dock event broadcast. |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 57 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 58 | @SystemService(Context.UI_MODE_SERVICE) |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 59 | public class UiModeManager { |
| 60 | private static final String TAG = "UiModeManager"; |
| 61 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 62 | /** |
| 63 | * Broadcast sent when the device's UI has switched to car mode, either |
| 64 | * by being placed in a car dock or explicit action of the user. After |
| 65 | * sending the broadcast, the system will start the intent |
| 66 | * {@link android.content.Intent#ACTION_MAIN} with category |
| 67 | * {@link android.content.Intent#CATEGORY_CAR_DOCK} |
| 68 | * to display the car UI, which typically what an application would |
| 69 | * implement to provide their own interface. However, applications can |
| 70 | * also monitor this Intent in order to be informed of mode changes or |
| 71 | * prevent the normal car UI from being displayed by setting the result |
| 72 | * of the broadcast to {@link Activity#RESULT_CANCELED}. |
| 73 | */ |
| 74 | public static String ACTION_ENTER_CAR_MODE = "android.app.action.ENTER_CAR_MODE"; |
Tyler Gunn | f05f2c2 | 2019-09-30 15:12:24 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Broadcast sent when the device's UI has switched to car mode, either by being placed in a car |
| 78 | * dock or explicit action of the user. |
| 79 | * <p> |
| 80 | * In addition to the behavior for {@link #ACTION_ENTER_CAR_MODE}, this broadcast includes the |
| 81 | * package name of the app which requested to enter car mode in the |
| 82 | * {@link #EXTRA_CALLING_PACKAGE}. If an app requested to enter car mode using |
| 83 | * {@link #enableCarMode(int, int)} and specified a priority this will be specified in the |
| 84 | * {@link #EXTRA_PRIORITY}. |
| 85 | * |
| 86 | * This is primarily intended to be received by other components of the Android OS. |
| 87 | * <p> |
| 88 | * Receiver requires permission: {@link android.Manifest.permission.HANDLE_CAR_MODE_CHANGES} |
| 89 | * @hide |
| 90 | */ |
| 91 | @SystemApi |
| 92 | public static final String ACTION_ENTER_CAR_MODE_PRIORITIZED = |
| 93 | "android.app.action.ENTER_CAR_MODE_PRIORITIZED"; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 94 | |
| 95 | /** |
| 96 | * Broadcast sent when the device's UI has switch away from car mode back |
| 97 | * to normal mode. Typically used by a car mode app, to dismiss itself |
| 98 | * when the user exits car mode. |
| 99 | */ |
| 100 | public static String ACTION_EXIT_CAR_MODE = "android.app.action.EXIT_CAR_MODE"; |
Tyler Gunn | f05f2c2 | 2019-09-30 15:12:24 -0700 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * Broadcast sent when the device's UI has switched away from car mode back to normal mode. |
| 104 | * Typically used by a car mode app, to dismiss itself when the user exits car mode. |
| 105 | * <p> |
| 106 | * In addition to the behavior for {@link #ACTION_EXIT_CAR_MODE}, this broadcast includes the |
| 107 | * package name of the app which requested to exit car mode in {@link #EXTRA_CALLING_PACKAGE}. |
| 108 | * If an app requested to enter car mode using {@link #enableCarMode(int, int)} and specified a |
| 109 | * priority this will be specified in the {@link #EXTRA_PRIORITY} when exiting car mode. |
| 110 | * <p> |
| 111 | * If {@link #DISABLE_CAR_MODE_ALL_PRIORITIES} is used when disabling car mode (i.e. this is |
| 112 | * initiated by the user via the persistent car mode notification), this broadcast is sent once |
| 113 | * for each priority level for which car mode is being disabled. |
| 114 | * <p> |
| 115 | * This is primarily intended to be received by other components of the Android OS. |
| 116 | * <p> |
| 117 | * Receiver requires permission: {@link android.Manifest.permission.HANDLE_CAR_MODE_CHANGES} |
| 118 | * @hide |
| 119 | */ |
| 120 | @SystemApi |
| 121 | public static final String ACTION_EXIT_CAR_MODE_PRIORITIZED = |
| 122 | "android.app.action.EXIT_CAR_MODE_PRIORITIZED"; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * Broadcast sent when the device's UI has switched to desk mode, |
| 126 | * by being placed in a desk dock. After |
| 127 | * sending the broadcast, the system will start the intent |
| 128 | * {@link android.content.Intent#ACTION_MAIN} with category |
| 129 | * {@link android.content.Intent#CATEGORY_DESK_DOCK} |
| 130 | * to display the desk UI, which typically what an application would |
| 131 | * implement to provide their own interface. However, applications can |
| 132 | * also monitor this Intent in order to be informed of mode changes or |
| 133 | * prevent the normal desk UI from being displayed by setting the result |
| 134 | * of the broadcast to {@link Activity#RESULT_CANCELED}. |
| 135 | */ |
| 136 | public static String ACTION_ENTER_DESK_MODE = "android.app.action.ENTER_DESK_MODE"; |
| 137 | |
| 138 | /** |
Daniel Sandler | b999abc | 2010-03-11 15:19:53 -0500 | [diff] [blame] | 139 | * Broadcast sent when the device's UI has switched away from desk mode back |
| 140 | * to normal mode. Typically used by a desk mode app, to dismiss itself |
| 141 | * when the user exits desk mode. |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 142 | */ |
| 143 | public static String ACTION_EXIT_DESK_MODE = "android.app.action.EXIT_DESK_MODE"; |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 144 | |
Tyler Gunn | f05f2c2 | 2019-09-30 15:12:24 -0700 | [diff] [blame] | 145 | /** |
| 146 | * String extra used with {@link #ACTION_ENTER_CAR_MODE_PRIORITIZED} and |
| 147 | * {@link #ACTION_EXIT_CAR_MODE_PRIORITIZED} to indicate the package name of the app which |
| 148 | * requested to enter or exit car mode. |
| 149 | * @hide |
| 150 | */ |
| 151 | @SystemApi |
| 152 | public static final String EXTRA_CALLING_PACKAGE = "android.app.extra.CALLING_PACKAGE"; |
| 153 | |
| 154 | /** |
| 155 | * Integer extra used with {@link #ACTION_ENTER_CAR_MODE_PRIORITIZED} and |
| 156 | * {@link #ACTION_EXIT_CAR_MODE_PRIORITIZED} to indicate the priority level at which car mode |
| 157 | * is being disabled. |
| 158 | * @hide |
| 159 | */ |
| 160 | @SystemApi |
| 161 | public static final String EXTRA_PRIORITY = "android.app.extra.PRIORITY"; |
| 162 | |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 163 | /** @hide */ |
Jeff Sharkey | ce8db99 | 2017-12-13 20:05:05 -0700 | [diff] [blame] | 164 | @IntDef(prefix = { "MODE_" }, value = { |
| 165 | MODE_NIGHT_AUTO, |
| 166 | MODE_NIGHT_NO, |
| 167 | MODE_NIGHT_YES |
| 168 | }) |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 169 | @Retention(RetentionPolicy.SOURCE) |
| 170 | public @interface NightMode {} |
| 171 | |
| 172 | /** |
| 173 | * Constant for {@link #setNightMode(int)} and {@link #getNightMode()}: |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 174 | * automatically switch night mode on and off based on the time. |
| 175 | */ |
| 176 | public static final int MODE_NIGHT_AUTO = Configuration.UI_MODE_NIGHT_UNDEFINED >> 4; |
| 177 | |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 178 | /** |
| 179 | * Constant for {@link #setNightMode(int)} and {@link #getNightMode()}: |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 180 | * never run in night mode. |
| 181 | */ |
| 182 | public static final int MODE_NIGHT_NO = Configuration.UI_MODE_NIGHT_NO >> 4; |
| 183 | |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 184 | /** |
| 185 | * Constant for {@link #setNightMode(int)} and {@link #getNightMode()}: |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 186 | * always run in night mode. |
| 187 | */ |
| 188 | public static final int MODE_NIGHT_YES = Configuration.UI_MODE_NIGHT_YES >> 4; |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 189 | |
| 190 | private IUiModeManager mService; |
| 191 | |
Tyler Gunn | f05f2c2 | 2019-09-30 15:12:24 -0700 | [diff] [blame] | 192 | /** |
| 193 | * Context required for getting the opPackageName of API caller; maybe be {@code null} if the |
| 194 | * old constructor marked with UnSupportedAppUsage is used. |
| 195 | */ |
| 196 | private @Nullable Context mContext; |
| 197 | |
Mathew Inwood | 4fb17d1 | 2018-08-14 14:25:44 +0100 | [diff] [blame] | 198 | @UnsupportedAppUsage |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 199 | /*package*/ UiModeManager() throws ServiceNotFoundException { |
Tyler Gunn | f05f2c2 | 2019-09-30 15:12:24 -0700 | [diff] [blame] | 200 | this(null /* context */); |
| 201 | } |
| 202 | |
| 203 | /*package*/ UiModeManager(Context context) throws ServiceNotFoundException { |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 204 | mService = IUiModeManager.Stub.asInterface( |
Jeff Sharkey | 49ca529 | 2016-05-10 12:54:45 -0600 | [diff] [blame] | 205 | ServiceManager.getServiceOrThrow(Context.UI_MODE_SERVICE)); |
Tyler Gunn | f05f2c2 | 2019-09-30 15:12:24 -0700 | [diff] [blame] | 206 | mContext = context; |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /** |
Dianne Hackborn | f5c5d22 | 2010-04-09 13:14:48 -0700 | [diff] [blame] | 210 | * Flag for use with {@link #enableCarMode(int)}: go to the car |
| 211 | * home activity as part of the enable. Enabling this way ensures |
| 212 | * a clean transition between the current activity (in non-car-mode) and |
| 213 | * the car home activity that will serve as home while in car mode. This |
| 214 | * will switch to the car home activity even if we are already in car mode. |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 215 | */ |
Dianne Hackborn | f5c5d22 | 2010-04-09 13:14:48 -0700 | [diff] [blame] | 216 | public static final int ENABLE_CAR_MODE_GO_CAR_HOME = 0x0001; |
keunyoung | 1d0a7cc | 2014-07-28 13:12:50 -0700 | [diff] [blame] | 217 | |
| 218 | /** |
keunyoung | c093bf2 | 2014-08-11 18:51:15 -0700 | [diff] [blame] | 219 | * Flag for use with {@link #enableCarMode(int)}: allow sleep mode while in car mode. |
| 220 | * By default, when this flag is not set, the system may hold a full wake lock to keep the |
| 221 | * screen turned on and prevent the system from entering sleep mode while in car mode. |
| 222 | * Setting this flag disables such behavior and the system may enter sleep mode |
| 223 | * if there is no other user activity and no other wake lock held. |
keunyoung | 1d0a7cc | 2014-07-28 13:12:50 -0700 | [diff] [blame] | 224 | * Setting this flag can be relevant for a car dock application that does not require the |
| 225 | * screen kept on. |
| 226 | */ |
keunyoung | c093bf2 | 2014-08-11 18:51:15 -0700 | [diff] [blame] | 227 | public static final int ENABLE_CAR_MODE_ALLOW_SLEEP = 0x0002; |
keunyoung | 1d0a7cc | 2014-07-28 13:12:50 -0700 | [diff] [blame] | 228 | |
Tyler Gunn | f05f2c2 | 2019-09-30 15:12:24 -0700 | [diff] [blame] | 229 | /** @hide */ |
| 230 | @IntDef(prefix = { "ENABLE_CAR_MODE_" }, value = { |
| 231 | ENABLE_CAR_MODE_GO_CAR_HOME, |
| 232 | ENABLE_CAR_MODE_ALLOW_SLEEP |
| 233 | }) |
| 234 | @Retention(RetentionPolicy.SOURCE) |
| 235 | public @interface EnableCarMode {} |
| 236 | |
Dianne Hackborn | d49258f | 2010-03-26 00:44:29 -0700 | [diff] [blame] | 237 | /** |
Dianne Hackborn | 9c9c532 | 2010-03-30 23:12:22 -0700 | [diff] [blame] | 238 | * Force device into car mode, like it had been placed in the car dock. |
| 239 | * This will cause the device to switch to the car home UI as part of |
| 240 | * the mode switch. |
| 241 | * @param flags Must be 0. |
| 242 | */ |
| 243 | public void enableCarMode(int flags) { |
Tyler Gunn | f05f2c2 | 2019-09-30 15:12:24 -0700 | [diff] [blame] | 244 | enableCarMode(DEFAULT_PRIORITY, flags); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Force device into car mode, like it had been placed in the car dock. This will cause the |
| 249 | * device to switch to the car home UI as part of the mode switch. |
| 250 | * <p> |
| 251 | * An app may request to enter car mode when the system is already in car mode. The app may |
| 252 | * specify a "priority" when entering car mode. The device will remain in car mode |
| 253 | * (i.e. {@link #getCurrentModeType()} is {@link Configuration#UI_MODE_TYPE_CAR}) as long as |
| 254 | * there is a priority level at which car mode have been enabled. For example assume app A |
| 255 | * enters car mode at priority level 100, and then app B enters car mode at the default priority |
| 256 | * (0). If app A exits car mode, the device will remain in car mode until app B exits car mode. |
| 257 | * <p> |
| 258 | * Specifying a priority level when entering car mode is important in cases where multiple apps |
| 259 | * on a device implement a car-mode {@link android.telecom.InCallService} (see |
| 260 | * {@link android.telecom.TelecomManager#METADATA_IN_CALL_SERVICE_CAR_MODE_UI}). The |
| 261 | * {@link android.telecom.InCallService} associated with the highest priority app which entered |
| 262 | * car mode will be bound to by Telecom and provided with information about ongoing calls on |
| 263 | * the device. |
| 264 | * <p> |
| 265 | * System apps holding the required permission can enable car mode when the app determines the |
| 266 | * correct conditions exist for that app to be in car mode. The device maker should ensure that |
| 267 | * where multiple apps exist on the device which can potentially enter car mode, appropriate |
| 268 | * priorities are used to ensure that calls delivered by the |
| 269 | * {@link android.telecom.InCallService} API are delivered to the highest priority app. |
| 270 | * If app A and app B can both potentially enable car mode, and it is desired that app B is the |
| 271 | * one which should receive call information, the priority for app B should be higher than the |
| 272 | * one for app A. |
| 273 | * <p> |
| 274 | * When an app uses a priority to enable car mode, they can disable car mode at the specified |
| 275 | * priority level using {@link #disableCarMode(int)}. An app may only enable car mode at a |
| 276 | * single priority. |
| 277 | * <p> |
| 278 | * Public apps are assumed to enter/exit car mode at {@link #DEFAULT_PRIORITY}. |
| 279 | * |
| 280 | * @param priority The declared priority for the caller. |
| 281 | * @param flags Car mode flags. |
| 282 | * @hide |
| 283 | */ |
| 284 | @SystemApi |
| 285 | @TestApi |
| 286 | @RequiresPermission(android.Manifest.permission.ENTER_CAR_MODE_PRIORITIZED) |
| 287 | public void enableCarMode(@IntRange(from = 0) int priority, @EnableCarMode int flags) { |
Dianne Hackborn | 9c9c532 | 2010-03-30 23:12:22 -0700 | [diff] [blame] | 288 | if (mService != null) { |
| 289 | try { |
Tyler Gunn | f05f2c2 | 2019-09-30 15:12:24 -0700 | [diff] [blame] | 290 | mService.enableCarMode(flags, priority, |
| 291 | mContext == null ? null : mContext.getOpPackageName()); |
Dianne Hackborn | 9c9c532 | 2010-03-30 23:12:22 -0700 | [diff] [blame] | 292 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 293 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 9c9c532 | 2010-03-30 23:12:22 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | /** |
Dianne Hackborn | f5c5d22 | 2010-04-09 13:14:48 -0700 | [diff] [blame] | 299 | * Flag for use with {@link #disableCarMode(int)}: go to the normal |
| 300 | * home activity as part of the disable. Disabling this way ensures |
| 301 | * a clean transition between the current activity (in car mode) and |
| 302 | * the original home activity (which was typically last running without |
| 303 | * being in car mode). |
| 304 | */ |
| 305 | public static final int DISABLE_CAR_MODE_GO_HOME = 0x0001; |
Tyler Gunn | f05f2c2 | 2019-09-30 15:12:24 -0700 | [diff] [blame] | 306 | |
| 307 | /** |
| 308 | * Flag for use with {@link #disableCarMode(int)}: Disables car mode at ALL priority levels. |
| 309 | * Primarily intended for use from {@link com.android.internal.app.DisableCarModeActivity} to |
| 310 | * provide the user with a means to exit car mode at all priority levels. |
| 311 | * @hide |
| 312 | */ |
| 313 | public static final int DISABLE_CAR_MODE_ALL_PRIORITIES = 0x0002; |
| 314 | |
| 315 | /** @hide */ |
| 316 | @IntDef(prefix = { "DISABLE_CAR_MODE_" }, value = { |
| 317 | DISABLE_CAR_MODE_GO_HOME |
| 318 | }) |
| 319 | @Retention(RetentionPolicy.SOURCE) |
| 320 | public @interface DisableCarMode {} |
| 321 | |
| 322 | /** |
| 323 | * The default priority used for entering car mode. |
| 324 | * <p> |
| 325 | * Callers of the {@link UiModeManager#enableCarMode(int)} priority will be assigned the |
| 326 | * default priority. |
| 327 | * <p> |
| 328 | * System apps can specify a priority other than the default priority when using |
| 329 | * {@link UiModeManager#enableCarMode(int, int)} to enable car mode. |
| 330 | * @hide |
| 331 | */ |
| 332 | @SystemApi |
| 333 | public static final int DEFAULT_PRIORITY = 0; |
Dianne Hackborn | f5c5d22 | 2010-04-09 13:14:48 -0700 | [diff] [blame] | 334 | |
| 335 | /** |
Dianne Hackborn | d49258f | 2010-03-26 00:44:29 -0700 | [diff] [blame] | 336 | * Turn off special mode if currently in car mode. |
Tyler Gunn | f05f2c2 | 2019-09-30 15:12:24 -0700 | [diff] [blame] | 337 | * @param flags One of the disable car mode flags. |
Dianne Hackborn | d49258f | 2010-03-26 00:44:29 -0700 | [diff] [blame] | 338 | */ |
Tyler Gunn | f05f2c2 | 2019-09-30 15:12:24 -0700 | [diff] [blame] | 339 | public void disableCarMode(@DisableCarMode int flags) { |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 340 | if (mService != null) { |
| 341 | try { |
Tyler Gunn | f05f2c2 | 2019-09-30 15:12:24 -0700 | [diff] [blame] | 342 | mService.disableCarModeByCallingPackage(flags, |
| 343 | mContext == null ? null : mContext.getOpPackageName()); |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 344 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 345 | throw e.rethrowFromSystemServer(); |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /** |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 351 | * Return the current running mode type. May be one of |
| 352 | * {@link Configuration#UI_MODE_TYPE_NORMAL Configuration.UI_MODE_TYPE_NORMAL}, |
John Spurlock | 6c19129 | 2014-04-03 16:37:27 -0400 | [diff] [blame] | 353 | * {@link Configuration#UI_MODE_TYPE_DESK Configuration.UI_MODE_TYPE_DESK}, |
| 354 | * {@link Configuration#UI_MODE_TYPE_CAR Configuration.UI_MODE_TYPE_CAR}, |
| 355 | * {@link Configuration#UI_MODE_TYPE_TELEVISION Configuration.UI_MODE_TYPE_TELEVISION}, |
Zak Cohen | 1a6acdb | 2016-12-12 15:21:21 -0800 | [diff] [blame] | 356 | * {@link Configuration#UI_MODE_TYPE_APPLIANCE Configuration.UI_MODE_TYPE_APPLIANCE}, |
| 357 | * {@link Configuration#UI_MODE_TYPE_WATCH Configuration.UI_MODE_TYPE_WATCH}, or |
| 358 | * {@link Configuration#UI_MODE_TYPE_VR_HEADSET Configuration.UI_MODE_TYPE_VR_HEADSET}. |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 359 | */ |
| 360 | public int getCurrentModeType() { |
| 361 | if (mService != null) { |
| 362 | try { |
| 363 | return mService.getCurrentModeType(); |
| 364 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 365 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | return Configuration.UI_MODE_TYPE_NORMAL; |
| 369 | } |
| 370 | |
| 371 | /** |
Lucas Dupin | b01c390 | 2018-09-19 09:51:02 -0700 | [diff] [blame] | 372 | * Sets the system-wide night mode. |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 373 | * <p> |
| 374 | * The mode can be one of: |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 375 | * <ul> |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 376 | * <li><em>{@link #MODE_NIGHT_NO}<em> sets the device into |
| 377 | * {@code notnight} mode</li> |
| 378 | * <li><em>{@link #MODE_NIGHT_YES}</em> sets the device into |
| 379 | * {@code night} mode</li> |
| 380 | * <li><em>{@link #MODE_NIGHT_AUTO}</em> automatically switches between |
| 381 | * {@code night} and {@code notnight} based on the device's current |
| 382 | * location and certain other sensors</li> |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 383 | * </ul> |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 384 | * <p> |
| 385 | * <strong>Note:</strong> On API 22 and below, changes to the night mode |
| 386 | * are only effective when the {@link Configuration#UI_MODE_TYPE_CAR car} |
| 387 | * or {@link Configuration#UI_MODE_TYPE_DESK desk} mode is enabled on a |
Winson | adc6967 | 2019-01-28 14:56:12 -0800 | [diff] [blame] | 388 | * device. On API 23 through API 28, changes to night mode are always effective. |
| 389 | * <p> |
| 390 | * Starting in API 29, when the device is in car mode and this method is called, night mode |
| 391 | * will change, but the new setting is not persisted and the previously persisted setting |
| 392 | * will be restored when the device exits car mode. |
Lucas Dupin | b01c390 | 2018-09-19 09:51:02 -0700 | [diff] [blame] | 393 | * <p> |
| 394 | * Changes to night mode take effect globally and will result in a configuration change |
| 395 | * (and potentially an Activity lifecycle event) being applied to all running apps. |
| 396 | * Developers interested in an app-local implementation of night mode should consider using |
Nan Zhang | 99adf3c | 2018-09-21 14:51:38 -0700 | [diff] [blame] | 397 | * {@link android.support.v7.app.AppCompatDelegate#setDefaultNightMode(int)} to manage the |
Lucas Dupin | b01c390 | 2018-09-19 09:51:02 -0700 | [diff] [blame] | 398 | * -night qualifier locally. |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 399 | * |
| 400 | * @param mode the night mode to set |
| 401 | * @see #getNightMode() |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 402 | */ |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 403 | public void setNightMode(@NightMode int mode) { |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 404 | if (mService != null) { |
| 405 | try { |
| 406 | mService.setNightMode(mode); |
| 407 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 408 | throw e.rethrowFromSystemServer(); |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | /** |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 414 | * Returns the currently configured night mode. |
| 415 | * <p> |
| 416 | * May be one of: |
| 417 | * <ul> |
| 418 | * <li>{@link #MODE_NIGHT_NO}</li> |
| 419 | * <li>{@link #MODE_NIGHT_YES}</li> |
| 420 | * <li>{@link #MODE_NIGHT_AUTO}</li> |
| 421 | * <li>{@code -1} on error</li> |
| 422 | * </ul> |
| 423 | * |
| 424 | * @return the current night mode, or {@code -1} on error |
| 425 | * @see #setNightMode(int) |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 426 | */ |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 427 | public @NightMode int getNightMode() { |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 428 | if (mService != null) { |
| 429 | try { |
| 430 | return mService.getNightMode(); |
| 431 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 432 | throw e.rethrowFromSystemServer(); |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | return -1; |
| 436 | } |
keunyoung | a771049 | 2015-09-23 11:33:58 -0700 | [diff] [blame] | 437 | |
| 438 | /** |
| 439 | * @return If UI mode is locked or not. When UI mode is locked, calls to change UI mode |
| 440 | * like {@link #enableCarMode(int)} will silently fail. |
Keun-young Park | 078f9b9 | 2016-03-02 02:18:19 +0000 | [diff] [blame] | 441 | * @hide |
keunyoung | a771049 | 2015-09-23 11:33:58 -0700 | [diff] [blame] | 442 | */ |
Keun-young Park | 078f9b9 | 2016-03-02 02:18:19 +0000 | [diff] [blame] | 443 | @TestApi |
keunyoung | a771049 | 2015-09-23 11:33:58 -0700 | [diff] [blame] | 444 | public boolean isUiModeLocked() { |
| 445 | if (mService != null) { |
| 446 | try { |
| 447 | return mService.isUiModeLocked(); |
| 448 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 449 | throw e.rethrowFromSystemServer(); |
keunyoung | a771049 | 2015-09-23 11:33:58 -0700 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | return true; |
| 453 | } |
| 454 | |
| 455 | /** |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 456 | * Returns whether night mode is locked or not. |
| 457 | * <p> |
| 458 | * When night mode is locked, only privileged system components may change |
| 459 | * night mode and calls from non-privileged applications to change night |
| 460 | * mode will fail silently. |
| 461 | * |
| 462 | * @return {@code true} if night mode is locked or {@code false} otherwise |
Keun-young Park | 078f9b9 | 2016-03-02 02:18:19 +0000 | [diff] [blame] | 463 | * @hide |
keunyoung | a771049 | 2015-09-23 11:33:58 -0700 | [diff] [blame] | 464 | */ |
Keun-young Park | 078f9b9 | 2016-03-02 02:18:19 +0000 | [diff] [blame] | 465 | @TestApi |
keunyoung | a771049 | 2015-09-23 11:33:58 -0700 | [diff] [blame] | 466 | public boolean isNightModeLocked() { |
| 467 | if (mService != null) { |
| 468 | try { |
| 469 | return mService.isNightModeLocked(); |
| 470 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 471 | throw e.rethrowFromSystemServer(); |
keunyoung | a771049 | 2015-09-23 11:33:58 -0700 | [diff] [blame] | 472 | } |
| 473 | } |
| 474 | return true; |
| 475 | } |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 476 | } |