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 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.content.res.Configuration; |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 21 | import android.os.RemoteException; |
| 22 | import android.os.ServiceManager; |
| 23 | import android.util.Log; |
| 24 | |
| 25 | /** |
| 26 | * This class provides access to the system uimode services. These services |
| 27 | * allow applications to control UI modes of the device. |
| 28 | * It provides functionality to disable the car mode and it gives access to the |
| 29 | * night mode settings. |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 30 | * |
| 31 | * <p>These facilities are built on top of the underlying |
| 32 | * {@link android.content.Intent#ACTION_DOCK_EVENT} broadcasts that are sent when the user |
| 33 | * physical places the device into and out of a dock. When that happens, |
| 34 | * the UiModeManager switches the system {@link android.content.res.Configuration} |
| 35 | * to the appropriate UI mode, sends broadcasts about the mode switch, and |
| 36 | * starts the corresponding mode activity if appropriate. See the |
| 37 | * broadcasts {@link #ACTION_ENTER_CAR_MODE} and |
| 38 | * {@link #ACTION_ENTER_DESK_MODE} for more information. |
| 39 | * |
| 40 | * <p>In addition, the user may manually switch the system to car mode without |
| 41 | * physically being in a dock. While in car mode -- whether by manual action |
| 42 | * from the user or being physically placed in a dock -- a notification is |
| 43 | * displayed allowing the user to exit dock mode. Thus the dock mode |
| 44 | * represented here may be different than the current state of the underlying |
| 45 | * dock event broadcast. |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 46 | * |
| 47 | * <p>You do not instantiate this class directly; instead, retrieve it through |
| 48 | * {@link android.content.Context#getSystemService |
Tobias Haamel | 29274dc | 2010-02-22 22:25:33 +0100 | [diff] [blame] | 49 | * Context.getSystemService(Context.UI_MODE_SERVICE)}. |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 50 | */ |
| 51 | public class UiModeManager { |
| 52 | private static final String TAG = "UiModeManager"; |
| 53 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 54 | /** |
| 55 | * Broadcast sent when the device's UI has switched to car mode, either |
| 56 | * by being placed in a car dock or explicit action of the user. After |
| 57 | * sending the broadcast, the system will start the intent |
| 58 | * {@link android.content.Intent#ACTION_MAIN} with category |
| 59 | * {@link android.content.Intent#CATEGORY_CAR_DOCK} |
| 60 | * to display the car UI, which typically what an application would |
| 61 | * implement to provide their own interface. However, applications can |
| 62 | * also monitor this Intent in order to be informed of mode changes or |
| 63 | * prevent the normal car UI from being displayed by setting the result |
| 64 | * of the broadcast to {@link Activity#RESULT_CANCELED}. |
| 65 | */ |
| 66 | public static String ACTION_ENTER_CAR_MODE = "android.app.action.ENTER_CAR_MODE"; |
| 67 | |
| 68 | /** |
| 69 | * Broadcast sent when the device's UI has switch away from car mode back |
| 70 | * to normal mode. Typically used by a car mode app, to dismiss itself |
| 71 | * when the user exits car mode. |
| 72 | */ |
| 73 | public static String ACTION_EXIT_CAR_MODE = "android.app.action.EXIT_CAR_MODE"; |
| 74 | |
| 75 | /** |
| 76 | * Broadcast sent when the device's UI has switched to desk mode, |
| 77 | * by being placed in a desk dock. After |
| 78 | * sending the broadcast, the system will start the intent |
| 79 | * {@link android.content.Intent#ACTION_MAIN} with category |
| 80 | * {@link android.content.Intent#CATEGORY_DESK_DOCK} |
| 81 | * to display the desk UI, which typically what an application would |
| 82 | * implement to provide their own interface. However, applications can |
| 83 | * also monitor this Intent in order to be informed of mode changes or |
| 84 | * prevent the normal desk UI from being displayed by setting the result |
| 85 | * of the broadcast to {@link Activity#RESULT_CANCELED}. |
| 86 | */ |
| 87 | public static String ACTION_ENTER_DESK_MODE = "android.app.action.ENTER_DESK_MODE"; |
| 88 | |
| 89 | /** |
Daniel Sandler | b999abc | 2010-03-11 15:19:53 -0500 | [diff] [blame] | 90 | * Broadcast sent when the device's UI has switched away from desk mode back |
| 91 | * to normal mode. Typically used by a desk mode app, to dismiss itself |
| 92 | * when the user exits desk mode. |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 93 | */ |
| 94 | public static String ACTION_EXIT_DESK_MODE = "android.app.action.EXIT_DESK_MODE"; |
| 95 | |
| 96 | /** Constant for {@link #setNightMode(int)} and {@link #getNightMode()}: |
| 97 | * automatically switch night mode on and off based on the time. |
| 98 | */ |
| 99 | public static final int MODE_NIGHT_AUTO = Configuration.UI_MODE_NIGHT_UNDEFINED >> 4; |
| 100 | |
| 101 | /** Constant for {@link #setNightMode(int)} and {@link #getNightMode()}: |
| 102 | * never run in night mode. |
| 103 | */ |
| 104 | public static final int MODE_NIGHT_NO = Configuration.UI_MODE_NIGHT_NO >> 4; |
| 105 | |
| 106 | /** Constant for {@link #setNightMode(int)} and {@link #getNightMode()}: |
| 107 | * always run in night mode. |
| 108 | */ |
| 109 | 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] | 110 | |
| 111 | private IUiModeManager mService; |
| 112 | |
| 113 | /*package*/ UiModeManager() { |
| 114 | mService = IUiModeManager.Stub.asInterface( |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 115 | ServiceManager.getService(Context.UI_MODE_SERVICE)); |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | /** |
Dianne Hackborn | f5c5d22 | 2010-04-09 13:14:48 -0700 | [diff] [blame] | 119 | * Flag for use with {@link #enableCarMode(int)}: go to the car |
| 120 | * home activity as part of the enable. Enabling this way ensures |
| 121 | * a clean transition between the current activity (in non-car-mode) and |
| 122 | * the car home activity that will serve as home while in car mode. This |
| 123 | * 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] | 124 | */ |
Dianne Hackborn | f5c5d22 | 2010-04-09 13:14:48 -0700 | [diff] [blame] | 125 | public static final int ENABLE_CAR_MODE_GO_CAR_HOME = 0x0001; |
Dianne Hackborn | d49258f | 2010-03-26 00:44:29 -0700 | [diff] [blame] | 126 | |
| 127 | /** |
Dianne Hackborn | 9c9c532 | 2010-03-30 23:12:22 -0700 | [diff] [blame] | 128 | * Force device into car mode, like it had been placed in the car dock. |
| 129 | * This will cause the device to switch to the car home UI as part of |
| 130 | * the mode switch. |
| 131 | * @param flags Must be 0. |
| 132 | */ |
| 133 | public void enableCarMode(int flags) { |
| 134 | if (mService != null) { |
| 135 | try { |
Dianne Hackborn | f5c5d22 | 2010-04-09 13:14:48 -0700 | [diff] [blame] | 136 | mService.enableCarMode(flags); |
Dianne Hackborn | 9c9c532 | 2010-03-30 23:12:22 -0700 | [diff] [blame] | 137 | } catch (RemoteException e) { |
| 138 | Log.e(TAG, "disableCarMode: RemoteException", e); |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
Dianne Hackborn | f5c5d22 | 2010-04-09 13:14:48 -0700 | [diff] [blame] | 144 | * Flag for use with {@link #disableCarMode(int)}: go to the normal |
| 145 | * home activity as part of the disable. Disabling this way ensures |
| 146 | * a clean transition between the current activity (in car mode) and |
| 147 | * the original home activity (which was typically last running without |
| 148 | * being in car mode). |
| 149 | */ |
| 150 | public static final int DISABLE_CAR_MODE_GO_HOME = 0x0001; |
| 151 | |
| 152 | /** |
Dianne Hackborn | d49258f | 2010-03-26 00:44:29 -0700 | [diff] [blame] | 153 | * Turn off special mode if currently in car mode. |
| 154 | * @param flags May be 0 or {@link #DISABLE_CAR_MODE_GO_HOME}. |
| 155 | */ |
| 156 | public void disableCarMode(int flags) { |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 157 | if (mService != null) { |
| 158 | try { |
Dianne Hackborn | d49258f | 2010-03-26 00:44:29 -0700 | [diff] [blame] | 159 | mService.disableCarMode(flags); |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 160 | } catch (RemoteException e) { |
| 161 | Log.e(TAG, "disableCarMode: RemoteException", e); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 167 | * Return the current running mode type. May be one of |
| 168 | * {@link Configuration#UI_MODE_TYPE_NORMAL Configuration.UI_MODE_TYPE_NORMAL}, |
| 169 | * {@link Configuration#UI_MODE_TYPE_DESK Configuration.UI_MODE_TYPE_DESK}, or |
| 170 | * {@link Configuration#UI_MODE_TYPE_CAR Configuration.UI_MODE_TYPE_CAR}, |
| 171 | */ |
| 172 | public int getCurrentModeType() { |
| 173 | if (mService != null) { |
| 174 | try { |
| 175 | return mService.getCurrentModeType(); |
| 176 | } catch (RemoteException e) { |
| 177 | Log.e(TAG, "getCurrentModeType: RemoteException", e); |
| 178 | } |
| 179 | } |
| 180 | return Configuration.UI_MODE_TYPE_NORMAL; |
| 181 | } |
| 182 | |
| 183 | /** |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 184 | * Sets the night mode. Changes to the night mode are only effective when |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 185 | * the car or desk mode is enabled on a device. |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 186 | * |
| 187 | * <p>The mode can be one of: |
| 188 | * <ul> |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 189 | * <li><em>{@link #MODE_NIGHT_NO}<em> - sets the device into notnight |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 190 | * mode.</li> |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 191 | * <li><em>{@link #MODE_NIGHT_YES}</em> - sets the device into night mode. |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 192 | * </li> |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 193 | * <li><em>{@link #MODE_NIGHT_AUTO}</em> - automatic night/notnight switching |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 194 | * depending on the location and certain other sensors.</li> |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 195 | * </ul> |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 196 | */ |
| 197 | public void setNightMode(int mode) { |
| 198 | if (mService != null) { |
| 199 | try { |
| 200 | mService.setNightMode(mode); |
| 201 | } catch (RemoteException e) { |
| 202 | Log.e(TAG, "setNightMode: RemoteException", e); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Returns the currently configured night mode. |
| 209 | * |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 210 | * @return {@link #MODE_NIGHT_NO}, {@link #MODE_NIGHT_YES}, or |
| 211 | * {@link #MODE_NIGHT_AUTO}. When an error occurred -1 is returned. |
Tobias Haamel | 5333288 | 2010-02-18 16:15:43 -0800 | [diff] [blame] | 212 | */ |
| 213 | public int getNightMode() { |
| 214 | if (mService != null) { |
| 215 | try { |
| 216 | return mService.getNightMode(); |
| 217 | } catch (RemoteException e) { |
| 218 | Log.e(TAG, "getNightMode: RemoteException", e); |
| 219 | } |
| 220 | } |
| 221 | return -1; |
| 222 | } |
| 223 | } |