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; |
Keun-young Park | 078f9b9 | 2016-03-02 02:18:19 +0000 | [diff] [blame] | 20 | import android.annotation.TestApi; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.res.Configuration; |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 23 | import android.os.RemoteException; |
| 24 | import android.os.ServiceManager; |
| 25 | import android.util.Log; |
| 26 | |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 27 | import java.lang.annotation.Retention; |
| 28 | import java.lang.annotation.RetentionPolicy; |
| 29 | |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 30 | /** |
| 31 | * This class provides access to the system uimode services. These services |
| 32 | * allow applications to control UI modes of the device. |
| 33 | * It provides functionality to disable the car mode and it gives access to the |
| 34 | * night mode settings. |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 35 | * |
| 36 | * <p>These facilities are built on top of the underlying |
| 37 | * {@link android.content.Intent#ACTION_DOCK_EVENT} broadcasts that are sent when the user |
| 38 | * physical places the device into and out of a dock. When that happens, |
| 39 | * the UiModeManager switches the system {@link android.content.res.Configuration} |
| 40 | * to the appropriate UI mode, sends broadcasts about the mode switch, and |
| 41 | * starts the corresponding mode activity if appropriate. See the |
| 42 | * broadcasts {@link #ACTION_ENTER_CAR_MODE} and |
| 43 | * {@link #ACTION_ENTER_DESK_MODE} for more information. |
| 44 | * |
| 45 | * <p>In addition, the user may manually switch the system to car mode without |
| 46 | * physically being in a dock. While in car mode -- whether by manual action |
| 47 | * from the user or being physically placed in a dock -- a notification is |
| 48 | * displayed allowing the user to exit dock mode. Thus the dock mode |
| 49 | * represented here may be different than the current state of the underlying |
| 50 | * dock event broadcast. |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 51 | * |
| 52 | * <p>You do not instantiate this class directly; instead, retrieve it through |
| 53 | * {@link android.content.Context#getSystemService |
Tobias Haamel | 29274dc | 2010-02-22 22:25:33 +0100 | [diff] [blame] | 54 | * Context.getSystemService(Context.UI_MODE_SERVICE)}. |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 55 | */ |
| 56 | public class UiModeManager { |
| 57 | private static final String TAG = "UiModeManager"; |
| 58 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 59 | /** |
| 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 Sandler | b999abc | 2010-03-11 15:19:53 -0500 | [diff] [blame] | 95 | * 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 Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 98 | */ |
| 99 | 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] | 100 | |
| 101 | /** @hide */ |
| 102 | @IntDef({MODE_NIGHT_AUTO, MODE_NIGHT_NO, MODE_NIGHT_YES}) |
| 103 | @Retention(RetentionPolicy.SOURCE) |
| 104 | public @interface NightMode {} |
| 105 | |
| 106 | /** |
| 107 | * Constant for {@link #setNightMode(int)} and {@link #getNightMode()}: |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 108 | * automatically switch night mode on and off based on the time. |
| 109 | */ |
| 110 | public static final int MODE_NIGHT_AUTO = Configuration.UI_MODE_NIGHT_UNDEFINED >> 4; |
| 111 | |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 112 | /** |
| 113 | * Constant for {@link #setNightMode(int)} and {@link #getNightMode()}: |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 114 | * never run in night mode. |
| 115 | */ |
| 116 | public static final int MODE_NIGHT_NO = Configuration.UI_MODE_NIGHT_NO >> 4; |
| 117 | |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 118 | /** |
| 119 | * Constant for {@link #setNightMode(int)} and {@link #getNightMode()}: |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 120 | * always run in night mode. |
| 121 | */ |
| 122 | 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] | 123 | |
| 124 | private IUiModeManager mService; |
| 125 | |
| 126 | /*package*/ UiModeManager() { |
| 127 | mService = IUiModeManager.Stub.asInterface( |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 128 | ServiceManager.getService(Context.UI_MODE_SERVICE)); |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** |
Dianne Hackborn | f5c5d22 | 2010-04-09 13:14:48 -0700 | [diff] [blame] | 132 | * Flag for use with {@link #enableCarMode(int)}: go to the car |
| 133 | * home activity as part of the enable. Enabling this way ensures |
| 134 | * a clean transition between the current activity (in non-car-mode) and |
| 135 | * the car home activity that will serve as home while in car mode. This |
| 136 | * 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] | 137 | */ |
Dianne Hackborn | f5c5d22 | 2010-04-09 13:14:48 -0700 | [diff] [blame] | 138 | public static final int ENABLE_CAR_MODE_GO_CAR_HOME = 0x0001; |
keunyoung | 1d0a7cc | 2014-07-28 13:12:50 -0700 | [diff] [blame] | 139 | |
| 140 | /** |
keunyoung | c093bf2 | 2014-08-11 18:51:15 -0700 | [diff] [blame] | 141 | * Flag for use with {@link #enableCarMode(int)}: allow sleep mode while in car mode. |
| 142 | * By default, when this flag is not set, the system may hold a full wake lock to keep the |
| 143 | * screen turned on and prevent the system from entering sleep mode while in car mode. |
| 144 | * Setting this flag disables such behavior and the system may enter sleep mode |
| 145 | * if there is no other user activity and no other wake lock held. |
keunyoung | 1d0a7cc | 2014-07-28 13:12:50 -0700 | [diff] [blame] | 146 | * Setting this flag can be relevant for a car dock application that does not require the |
| 147 | * screen kept on. |
| 148 | */ |
keunyoung | c093bf2 | 2014-08-11 18:51:15 -0700 | [diff] [blame] | 149 | public static final int ENABLE_CAR_MODE_ALLOW_SLEEP = 0x0002; |
keunyoung | 1d0a7cc | 2014-07-28 13:12:50 -0700 | [diff] [blame] | 150 | |
Dianne Hackborn | d49258f | 2010-03-26 00:44:29 -0700 | [diff] [blame] | 151 | /** |
Dianne Hackborn | 9c9c532 | 2010-03-30 23:12:22 -0700 | [diff] [blame] | 152 | * Force device into car mode, like it had been placed in the car dock. |
| 153 | * This will cause the device to switch to the car home UI as part of |
| 154 | * the mode switch. |
| 155 | * @param flags Must be 0. |
| 156 | */ |
| 157 | public void enableCarMode(int flags) { |
| 158 | if (mService != null) { |
| 159 | try { |
Dianne Hackborn | f5c5d22 | 2010-04-09 13:14:48 -0700 | [diff] [blame] | 160 | mService.enableCarMode(flags); |
Dianne Hackborn | 9c9c532 | 2010-03-30 23:12:22 -0700 | [diff] [blame] | 161 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 162 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 9c9c532 | 2010-03-30 23:12:22 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /** |
Dianne Hackborn | f5c5d22 | 2010-04-09 13:14:48 -0700 | [diff] [blame] | 168 | * Flag for use with {@link #disableCarMode(int)}: go to the normal |
| 169 | * home activity as part of the disable. Disabling this way ensures |
| 170 | * a clean transition between the current activity (in car mode) and |
| 171 | * the original home activity (which was typically last running without |
| 172 | * being in car mode). |
| 173 | */ |
| 174 | public static final int DISABLE_CAR_MODE_GO_HOME = 0x0001; |
| 175 | |
| 176 | /** |
Dianne Hackborn | d49258f | 2010-03-26 00:44:29 -0700 | [diff] [blame] | 177 | * Turn off special mode if currently in car mode. |
| 178 | * @param flags May be 0 or {@link #DISABLE_CAR_MODE_GO_HOME}. |
| 179 | */ |
| 180 | public void disableCarMode(int flags) { |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 181 | if (mService != null) { |
| 182 | try { |
Dianne Hackborn | d49258f | 2010-03-26 00:44:29 -0700 | [diff] [blame] | 183 | mService.disableCarMode(flags); |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 184 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 185 | throw e.rethrowFromSystemServer(); |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 191 | * Return the current running mode type. May be one of |
| 192 | * {@link Configuration#UI_MODE_TYPE_NORMAL Configuration.UI_MODE_TYPE_NORMAL}, |
John Spurlock | 6c19129 | 2014-04-03 16:37:27 -0400 | [diff] [blame] | 193 | * {@link Configuration#UI_MODE_TYPE_DESK Configuration.UI_MODE_TYPE_DESK}, |
| 194 | * {@link Configuration#UI_MODE_TYPE_CAR Configuration.UI_MODE_TYPE_CAR}, |
| 195 | * {@link Configuration#UI_MODE_TYPE_TELEVISION Configuration.UI_MODE_TYPE_TELEVISION}, |
| 196 | * {@link Configuration#UI_MODE_TYPE_APPLIANCE Configuration.UI_MODE_TYPE_APPLIANCE}, or |
| 197 | * {@link Configuration#UI_MODE_TYPE_WATCH Configuration.UI_MODE_TYPE_WATCH}. |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 198 | */ |
| 199 | public int getCurrentModeType() { |
| 200 | if (mService != null) { |
| 201 | try { |
| 202 | return mService.getCurrentModeType(); |
| 203 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 204 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | return Configuration.UI_MODE_TYPE_NORMAL; |
| 208 | } |
| 209 | |
| 210 | /** |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 211 | * Sets the night mode. |
| 212 | * <p> |
| 213 | * The mode can be one of: |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 214 | * <ul> |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 215 | * <li><em>{@link #MODE_NIGHT_NO}<em> sets the device into |
| 216 | * {@code notnight} mode</li> |
| 217 | * <li><em>{@link #MODE_NIGHT_YES}</em> sets the device into |
| 218 | * {@code night} mode</li> |
| 219 | * <li><em>{@link #MODE_NIGHT_AUTO}</em> automatically switches between |
| 220 | * {@code night} and {@code notnight} based on the device's current |
| 221 | * location and certain other sensors</li> |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 222 | * </ul> |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 223 | * <p> |
| 224 | * <strong>Note:</strong> On API 22 and below, changes to the night mode |
| 225 | * are only effective when the {@link Configuration#UI_MODE_TYPE_CAR car} |
| 226 | * or {@link Configuration#UI_MODE_TYPE_DESK desk} mode is enabled on a |
| 227 | * device. Starting in API 23, changes to night mode are always effective. |
| 228 | * |
| 229 | * @param mode the night mode to set |
| 230 | * @see #getNightMode() |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 231 | */ |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 232 | public void setNightMode(@NightMode int mode) { |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 233 | if (mService != null) { |
| 234 | try { |
| 235 | mService.setNightMode(mode); |
| 236 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 237 | throw e.rethrowFromSystemServer(); |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 243 | * Returns the currently configured night mode. |
| 244 | * <p> |
| 245 | * May be one of: |
| 246 | * <ul> |
| 247 | * <li>{@link #MODE_NIGHT_NO}</li> |
| 248 | * <li>{@link #MODE_NIGHT_YES}</li> |
| 249 | * <li>{@link #MODE_NIGHT_AUTO}</li> |
| 250 | * <li>{@code -1} on error</li> |
| 251 | * </ul> |
| 252 | * |
| 253 | * @return the current night mode, or {@code -1} on error |
| 254 | * @see #setNightMode(int) |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 255 | */ |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 256 | public @NightMode int getNightMode() { |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 257 | if (mService != null) { |
| 258 | try { |
| 259 | return mService.getNightMode(); |
| 260 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 261 | throw e.rethrowFromSystemServer(); |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | return -1; |
| 265 | } |
keunyoung | a771049 | 2015-09-23 11:33:58 -0700 | [diff] [blame] | 266 | |
| 267 | /** |
| 268 | * @return If UI mode is locked or not. When UI mode is locked, calls to change UI mode |
| 269 | * like {@link #enableCarMode(int)} will silently fail. |
Keun-young Park | 078f9b9 | 2016-03-02 02:18:19 +0000 | [diff] [blame] | 270 | * @hide |
keunyoung | a771049 | 2015-09-23 11:33:58 -0700 | [diff] [blame] | 271 | */ |
Keun-young Park | 078f9b9 | 2016-03-02 02:18:19 +0000 | [diff] [blame] | 272 | @TestApi |
keunyoung | a771049 | 2015-09-23 11:33:58 -0700 | [diff] [blame] | 273 | public boolean isUiModeLocked() { |
| 274 | if (mService != null) { |
| 275 | try { |
| 276 | return mService.isUiModeLocked(); |
| 277 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 278 | throw e.rethrowFromSystemServer(); |
keunyoung | a771049 | 2015-09-23 11:33:58 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | return true; |
| 282 | } |
| 283 | |
| 284 | /** |
Alan Viverette | 5794a5b | 2016-01-12 15:04:18 -0500 | [diff] [blame] | 285 | * Returns whether night mode is locked or not. |
| 286 | * <p> |
| 287 | * When night mode is locked, only privileged system components may change |
| 288 | * night mode and calls from non-privileged applications to change night |
| 289 | * mode will fail silently. |
| 290 | * |
| 291 | * @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] | 292 | * @hide |
keunyoung | a771049 | 2015-09-23 11:33:58 -0700 | [diff] [blame] | 293 | */ |
Keun-young Park | 078f9b9 | 2016-03-02 02:18:19 +0000 | [diff] [blame] | 294 | @TestApi |
keunyoung | a771049 | 2015-09-23 11:33:58 -0700 | [diff] [blame] | 295 | public boolean isNightModeLocked() { |
| 296 | if (mService != null) { |
| 297 | try { |
| 298 | return mService.isNightModeLocked(); |
| 299 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 300 | throw e.rethrowFromSystemServer(); |
keunyoung | a771049 | 2015-09-23 11:33:58 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | return true; |
| 304 | } |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 305 | } |