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