Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import android.app.Activity; |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 20 | import android.app.ActivityManager; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 21 | import android.app.ActivityManagerNative; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 22 | import android.app.IUiModeManager; |
| 23 | import android.app.Notification; |
| 24 | import android.app.NotificationManager; |
| 25 | import android.app.PendingIntent; |
| 26 | import android.app.StatusBarManager; |
| 27 | import android.app.UiModeManager; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 28 | import android.content.BroadcastReceiver; |
| 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
| 31 | import android.content.IntentFilter; |
| 32 | import android.content.pm.PackageManager; |
| 33 | import android.content.res.Configuration; |
Mike Lockwood | e29db6a | 2010-03-05 13:45:51 -0500 | [diff] [blame] | 34 | import android.os.BatteryManager; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 35 | import android.os.Binder; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 36 | import android.os.Handler; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 37 | import android.os.IBinder; |
Mike Lockwood | e29db6a | 2010-03-05 13:45:51 -0500 | [diff] [blame] | 38 | import android.os.PowerManager; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 39 | import android.os.RemoteException; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 40 | import android.os.UserHandle; |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 41 | import android.provider.Settings; |
Jeff Brown | 11159e9 | 2012-10-11 15:58:37 -0700 | [diff] [blame] | 42 | import android.service.dreams.Sandman; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 43 | import android.util.Slog; |
| 44 | |
| 45 | import java.io.FileDescriptor; |
| 46 | import java.io.PrintWriter; |
| 47 | |
| 48 | import com.android.internal.R; |
| 49 | import com.android.internal.app.DisableCarModeActivity; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 50 | import com.android.server.twilight.TwilightListener; |
| 51 | import com.android.server.twilight.TwilightManager; |
| 52 | import com.android.server.twilight.TwilightState; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 53 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 54 | final class UiModeManagerService extends SystemService { |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 55 | private static final String TAG = UiModeManager.class.getSimpleName(); |
| 56 | private static final boolean LOG = false; |
| 57 | |
Daniel Sandler | 11ddf53 | 2011-11-16 11:10:03 -0800 | [diff] [blame] | 58 | // Enable launching of applications when entering the dock. |
| 59 | private static final boolean ENABLE_LAUNCH_CAR_DOCK_APP = true; |
John Spurlock | 960779d | 2012-05-29 14:37:05 -0400 | [diff] [blame] | 60 | private static final boolean ENABLE_LAUNCH_DESK_DOCK_APP = true; |
Daniel Sandler | 11ddf53 | 2011-11-16 11:10:03 -0800 | [diff] [blame] | 61 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 62 | final Object mLock = new Object(); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 63 | private int mDockState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 64 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 65 | private int mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 66 | int mNightMode = UiModeManager.MODE_NIGHT_NO; |
| 67 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 68 | private boolean mCarModeEnabled = false; |
Mike Lockwood | e29db6a | 2010-03-05 13:45:51 -0500 | [diff] [blame] | 69 | private boolean mCharging = false; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 70 | private int mDefaultUiModeType; |
| 71 | private boolean mCarModeKeepsScreenOn; |
| 72 | private boolean mDeskModeKeepsScreenOn; |
| 73 | private boolean mTelevision; |
John Spurlock | 6c19129 | 2014-04-03 16:37:27 -0400 | [diff] [blame] | 74 | private boolean mWatch; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 75 | private boolean mComputedNightMode; |
keunyoung | 1d0a7cc | 2014-07-28 13:12:50 -0700 | [diff] [blame] | 76 | private int mCarModeEnableFlags; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 77 | |
| 78 | int mCurUiMode = 0; |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 79 | private int mSetUiMode = 0; |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 80 | private boolean mHoldingConfiguration = false; |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 81 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 82 | private Configuration mConfiguration = new Configuration(); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 83 | boolean mSystemReady; |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 84 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 85 | private final Handler mHandler = new Handler(); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 86 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 87 | private TwilightManager mTwilightManager; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 88 | private NotificationManager mNotificationManager; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 89 | private StatusBarManager mStatusBarManager; |
Jeff Brown | 9fca9e9 | 2012-10-05 14:42:56 -0700 | [diff] [blame] | 90 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 91 | private PowerManager.WakeLock mWakeLock; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 92 | |
Jeff Brown | b880d88 | 2014-02-10 19:47:07 -0800 | [diff] [blame] | 93 | public UiModeManagerService(Context context) { |
| 94 | super(context); |
| 95 | } |
| 96 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 97 | private static Intent buildHomeIntent(String category) { |
Dianne Hackborn | f5c5d22 | 2010-04-09 13:14:48 -0700 | [diff] [blame] | 98 | Intent intent = new Intent(Intent.ACTION_MAIN); |
| 99 | intent.addCategory(category); |
| 100 | intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 101 | | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); |
| 102 | return intent; |
| 103 | } |
John Spurlock | 960779d | 2012-05-29 14:37:05 -0400 | [diff] [blame] | 104 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 105 | // The broadcast receiver which receives the result of the ordered broadcast sent when |
| 106 | // the dock state changes. The original ordered broadcast is sent with an initial result |
| 107 | // code of RESULT_OK. If any of the registered broadcast receivers changes this value, e.g., |
| 108 | // to RESULT_CANCELED, then the intent to start a dock app will not be sent. |
| 109 | private final BroadcastReceiver mResultReceiver = new BroadcastReceiver() { |
| 110 | @Override |
| 111 | public void onReceive(Context context, Intent intent) { |
| 112 | if (getResultCode() != Activity.RESULT_OK) { |
Daniel Sandler | 69a1da4 | 2011-11-04 15:08:30 -0400 | [diff] [blame] | 113 | if (LOG) { |
John Spurlock | 960779d | 2012-05-29 14:37:05 -0400 | [diff] [blame] | 114 | Slog.v(TAG, "Handling broadcast result for action " + intent.getAction() |
Daniel Sandler | 69a1da4 | 2011-11-04 15:08:30 -0400 | [diff] [blame] | 115 | + ": canceled: " + getResultCode()); |
| 116 | } |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 117 | return; |
| 118 | } |
| 119 | |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 120 | final int enableFlags = intent.getIntExtra("enableFlags", 0); |
| 121 | final int disableFlags = intent.getIntExtra("disableFlags", 0); |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 122 | synchronized (mLock) { |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 123 | updateAfterBroadcastLocked(intent.getAction(), enableFlags, disableFlags); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | }; |
| 127 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 128 | private final BroadcastReceiver mDockModeReceiver = new BroadcastReceiver() { |
| 129 | @Override |
| 130 | public void onReceive(Context context, Intent intent) { |
| 131 | int state = intent.getIntExtra(Intent.EXTRA_DOCK_STATE, |
| 132 | Intent.EXTRA_DOCK_STATE_UNDOCKED); |
| 133 | updateDockState(state); |
| 134 | } |
| 135 | }; |
| 136 | |
Mike Lockwood | e29db6a | 2010-03-05 13:45:51 -0500 | [diff] [blame] | 137 | private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() { |
| 138 | @Override |
| 139 | public void onReceive(Context context, Intent intent) { |
| 140 | mCharging = (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) != 0); |
| 141 | synchronized (mLock) { |
| 142 | if (mSystemReady) { |
Dianne Hackborn | f5c5d22 | 2010-04-09 13:14:48 -0700 | [diff] [blame] | 143 | updateLocked(0, 0); |
Mike Lockwood | e29db6a | 2010-03-05 13:45:51 -0500 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | } |
| 147 | }; |
| 148 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 149 | private final TwilightListener mTwilightListener = new TwilightListener() { |
Dianne Hackborn | 57f4503 | 2010-06-17 15:49:33 -0700 | [diff] [blame] | 150 | @Override |
Jeff Brown | 2416e09 | 2012-08-21 22:12:20 -0700 | [diff] [blame] | 151 | public void onTwilightStateChanged() { |
| 152 | updateTwilight(); |
Dianne Hackborn | 57f4503 | 2010-06-17 15:49:33 -0700 | [diff] [blame] | 153 | } |
| 154 | }; |
| 155 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 156 | @Override |
| 157 | public void onStart() { |
| 158 | final Context context = getContext(); |
| 159 | mTwilightManager = getLocalService(TwilightManager.class); |
| 160 | final PowerManager powerManager = |
| 161 | (PowerManager) context.getSystemService(Context.POWER_SERVICE); |
| 162 | mWakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, TAG); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 163 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 164 | context.registerReceiver(mDockModeReceiver, |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 165 | new IntentFilter(Intent.ACTION_DOCK_EVENT)); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 166 | context.registerReceiver(mBatteryReceiver, |
Mike Lockwood | e29db6a | 2010-03-05 13:45:51 -0500 | [diff] [blame] | 167 | new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); |
| 168 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 169 | mConfiguration.setToDefaults(); |
Mike Lockwood | e29db6a | 2010-03-05 13:45:51 -0500 | [diff] [blame] | 170 | |
Joe Onorato | 44fcb83 | 2011-12-14 20:59:30 -0800 | [diff] [blame] | 171 | mDefaultUiModeType = context.getResources().getInteger( |
| 172 | com.android.internal.R.integer.config_defaultUiModeType); |
Mike Lockwood | e29db6a | 2010-03-05 13:45:51 -0500 | [diff] [blame] | 173 | mCarModeKeepsScreenOn = (context.getResources().getInteger( |
| 174 | com.android.internal.R.integer.config_carDockKeepsScreenOn) == 1); |
| 175 | mDeskModeKeepsScreenOn = (context.getResources().getInteger( |
| 176 | com.android.internal.R.integer.config_deskDockKeepsScreenOn) == 1); |
Dianne Hackborn | 0cf2c8a | 2012-05-17 17:29:49 -0700 | [diff] [blame] | 177 | mTelevision = context.getPackageManager().hasSystemFeature( |
Tim Kilbourn | 0722968 | 2014-03-14 13:44:12 -0700 | [diff] [blame] | 178 | PackageManager.FEATURE_TELEVISION) || |
| 179 | context.getPackageManager().hasSystemFeature( |
| 180 | PackageManager.FEATURE_LEANBACK); |
John Spurlock | 6c19129 | 2014-04-03 16:37:27 -0400 | [diff] [blame] | 181 | mWatch = context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WATCH); |
Dianne Hackborn | 0cf2c8a | 2012-05-17 17:29:49 -0700 | [diff] [blame] | 182 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 183 | mNightMode = Settings.Secure.getInt(context.getContentResolver(), |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 184 | Settings.Secure.UI_NIGHT_MODE, UiModeManager.MODE_NIGHT_AUTO); |
Jeff Brown | 2416e09 | 2012-08-21 22:12:20 -0700 | [diff] [blame] | 185 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 186 | mTwilightManager.registerListener(mTwilightListener, mHandler); |
| 187 | |
| 188 | publishBinderService(Context.UI_MODE_SERVICE, mService); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 191 | private final IBinder mService = new IUiModeManager.Stub() { |
| 192 | @Override |
| 193 | public void enableCarMode(int flags) { |
| 194 | final long ident = Binder.clearCallingIdentity(); |
| 195 | try { |
| 196 | synchronized (mLock) { |
keunyoung | 1d0a7cc | 2014-07-28 13:12:50 -0700 | [diff] [blame] | 197 | setCarModeLocked(true, flags); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 198 | if (mSystemReady) { |
| 199 | updateLocked(flags, 0); |
| 200 | } |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 201 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 202 | } finally { |
| 203 | Binder.restoreCallingIdentity(ident); |
Mike Lockwood | 924e164 | 2010-03-05 11:56:53 -0500 | [diff] [blame] | 204 | } |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 205 | } |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 206 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 207 | @Override |
| 208 | public void disableCarMode(int flags) { |
| 209 | final long ident = Binder.clearCallingIdentity(); |
| 210 | try { |
| 211 | synchronized (mLock) { |
keunyoung | 1d0a7cc | 2014-07-28 13:12:50 -0700 | [diff] [blame] | 212 | setCarModeLocked(false, 0); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 213 | if (mSystemReady) { |
| 214 | updateLocked(0, flags); |
| 215 | } |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 216 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 217 | } finally { |
| 218 | Binder.restoreCallingIdentity(ident); |
Mike Lockwood | 924e164 | 2010-03-05 11:56:53 -0500 | [diff] [blame] | 219 | } |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 220 | } |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 221 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 222 | @Override |
| 223 | public int getCurrentModeType() { |
| 224 | final long ident = Binder.clearCallingIdentity(); |
| 225 | try { |
| 226 | synchronized (mLock) { |
| 227 | return mCurUiMode & Configuration.UI_MODE_TYPE_MASK; |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 228 | } |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 229 | } finally { |
| 230 | Binder.restoreCallingIdentity(ident); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 231 | } |
| 232 | } |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 233 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 234 | @Override |
| 235 | public void setNightMode(int mode) { |
| 236 | switch (mode) { |
| 237 | case UiModeManager.MODE_NIGHT_NO: |
| 238 | case UiModeManager.MODE_NIGHT_YES: |
| 239 | case UiModeManager.MODE_NIGHT_AUTO: |
| 240 | break; |
| 241 | default: |
| 242 | throw new IllegalArgumentException("Unknown mode: " + mode); |
| 243 | } |
| 244 | |
| 245 | final long ident = Binder.clearCallingIdentity(); |
| 246 | try { |
| 247 | synchronized (mLock) { |
| 248 | if (isDoingNightModeLocked() && mNightMode != mode) { |
| 249 | Settings.Secure.putInt(getContext().getContentResolver(), |
| 250 | Settings.Secure.UI_NIGHT_MODE, mode); |
| 251 | mNightMode = mode; |
| 252 | updateLocked(0, 0); |
| 253 | } |
| 254 | } |
| 255 | } finally { |
| 256 | Binder.restoreCallingIdentity(ident); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public int getNightMode() { |
| 262 | synchronized (mLock) { |
| 263 | return mNightMode; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | @Override |
| 268 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 269 | if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 270 | != PackageManager.PERMISSION_GRANTED) { |
| 271 | |
| 272 | pw.println("Permission Denial: can't dump uimode service from from pid=" |
| 273 | + Binder.getCallingPid() |
| 274 | + ", uid=" + Binder.getCallingUid()); |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | dumpImpl(pw); |
| 279 | } |
| 280 | }; |
| 281 | |
| 282 | void dumpImpl(PrintWriter pw) { |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 283 | synchronized (mLock) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 284 | pw.println("Current UI Mode Service state:"); |
| 285 | pw.print(" mDockState="); pw.print(mDockState); |
| 286 | pw.print(" mLastBroadcastState="); pw.println(mLastBroadcastState); |
| 287 | pw.print(" mNightMode="); pw.print(mNightMode); |
| 288 | pw.print(" mCarModeEnabled="); pw.print(mCarModeEnabled); |
keunyoung | 1d0a7cc | 2014-07-28 13:12:50 -0700 | [diff] [blame] | 289 | pw.print(" mComputedNightMode="); pw.print(mComputedNightMode); |
| 290 | pw.print(" mCarModeEnableFlags="); pw.println(mCarModeEnableFlags); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 291 | pw.print(" mCurUiMode=0x"); pw.print(Integer.toHexString(mCurUiMode)); |
| 292 | pw.print(" mSetUiMode=0x"); pw.println(Integer.toHexString(mSetUiMode)); |
| 293 | pw.print(" mHoldingConfiguration="); pw.print(mHoldingConfiguration); |
| 294 | pw.print(" mSystemReady="); pw.println(mSystemReady); |
| 295 | pw.print(" mTwilightService.getCurrentState()="); |
| 296 | pw.println(mTwilightManager.getCurrentState()); |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 297 | } |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 298 | } |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 299 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 300 | @Override |
| 301 | public void onBootPhase(int phase) { |
| 302 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
| 303 | synchronized (mLock) { |
| 304 | mSystemReady = true; |
| 305 | mCarModeEnabled = mDockState == Intent.EXTRA_DOCK_STATE_CAR; |
| 306 | updateComputedNightModeLocked(); |
| 307 | updateLocked(0, 0); |
| 308 | } |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 312 | boolean isDoingNightModeLocked() { |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 313 | return mCarModeEnabled || mDockState != Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 314 | } |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 315 | |
keunyoung | 1d0a7cc | 2014-07-28 13:12:50 -0700 | [diff] [blame] | 316 | void setCarModeLocked(boolean enabled, int flags) { |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 317 | if (mCarModeEnabled != enabled) { |
| 318 | mCarModeEnabled = enabled; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 319 | } |
keunyoung | 1d0a7cc | 2014-07-28 13:12:50 -0700 | [diff] [blame] | 320 | mCarModeEnableFlags = flags; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 321 | } |
| 322 | |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 323 | private void updateDockState(int newState) { |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 324 | synchronized (mLock) { |
| 325 | if (newState != mDockState) { |
| 326 | mDockState = newState; |
keunyoung | 1d0a7cc | 2014-07-28 13:12:50 -0700 | [diff] [blame] | 327 | setCarModeLocked(mDockState == Intent.EXTRA_DOCK_STATE_CAR, 0); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 328 | if (mSystemReady) { |
Dianne Hackborn | f5c5d22 | 2010-04-09 13:14:48 -0700 | [diff] [blame] | 329 | updateLocked(UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME, 0); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | } |
| 333 | } |
Mike Lockwood | e29db6a | 2010-03-05 13:45:51 -0500 | [diff] [blame] | 334 | |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 335 | private static boolean isDeskDockState(int state) { |
Daniel Sandler | 69a1da4 | 2011-11-04 15:08:30 -0400 | [diff] [blame] | 336 | switch (state) { |
| 337 | case Intent.EXTRA_DOCK_STATE_DESK: |
| 338 | case Intent.EXTRA_DOCK_STATE_LE_DESK: |
| 339 | case Intent.EXTRA_DOCK_STATE_HE_DESK: |
| 340 | return true; |
| 341 | default: |
| 342 | return false; |
| 343 | } |
| 344 | } |
| 345 | |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 346 | private void updateConfigurationLocked() { |
John Spurlock | 6c19129 | 2014-04-03 16:37:27 -0400 | [diff] [blame] | 347 | int uiMode = mDefaultUiModeType; |
| 348 | if (mTelevision) { |
| 349 | uiMode = Configuration.UI_MODE_TYPE_TELEVISION; |
| 350 | } else if (mWatch) { |
| 351 | uiMode = Configuration.UI_MODE_TYPE_WATCH; |
| 352 | } else if (mCarModeEnabled) { |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 353 | uiMode = Configuration.UI_MODE_TYPE_CAR; |
Daniel Sandler | 69a1da4 | 2011-11-04 15:08:30 -0400 | [diff] [blame] | 354 | } else if (isDeskDockState(mDockState)) { |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 355 | uiMode = Configuration.UI_MODE_TYPE_DESK; |
| 356 | } |
Dianne Hackborn | 9c9c532 | 2010-03-30 23:12:22 -0700 | [diff] [blame] | 357 | if (mCarModeEnabled) { |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 358 | if (mNightMode == UiModeManager.MODE_NIGHT_AUTO) { |
Jeff Brown | 2416e09 | 2012-08-21 22:12:20 -0700 | [diff] [blame] | 359 | updateComputedNightModeLocked(); |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 360 | uiMode |= mComputedNightMode ? Configuration.UI_MODE_NIGHT_YES |
| 361 | : Configuration.UI_MODE_NIGHT_NO; |
| 362 | } else { |
| 363 | uiMode |= mNightMode << 4; |
| 364 | } |
| 365 | } else { |
| 366 | // Disabling the car mode clears the night mode. |
Daniel Sandler | 8daf2a4 | 2010-04-02 10:15:09 -0400 | [diff] [blame] | 367 | uiMode = (uiMode & ~Configuration.UI_MODE_NIGHT_MASK) | Configuration.UI_MODE_NIGHT_NO; |
| 368 | } |
| 369 | |
| 370 | if (LOG) { |
John Spurlock | 960779d | 2012-05-29 14:37:05 -0400 | [diff] [blame] | 371 | Slog.d(TAG, |
| 372 | "updateConfigurationLocked: mDockState=" + mDockState |
Daniel Sandler | 8daf2a4 | 2010-04-02 10:15:09 -0400 | [diff] [blame] | 373 | + "; mCarMode=" + mCarModeEnabled |
| 374 | + "; mNightMode=" + mNightMode |
| 375 | + "; uiMode=" + uiMode); |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 376 | } |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 377 | |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 378 | mCurUiMode = uiMode; |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 379 | if (!mHoldingConfiguration) { |
Dianne Hackborn | 2ccda4d | 2010-03-22 21:49:15 -0700 | [diff] [blame] | 380 | mConfiguration.uiMode = uiMode; |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 381 | } |
| 382 | } |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 383 | |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 384 | private void sendConfigurationLocked() { |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 385 | if (mSetUiMode != mConfiguration.uiMode) { |
| 386 | mSetUiMode = mConfiguration.uiMode; |
| 387 | |
| 388 | try { |
| 389 | ActivityManagerNative.getDefault().updateConfiguration(mConfiguration); |
| 390 | } catch (RemoteException e) { |
| 391 | Slog.w(TAG, "Failure communicating with activity manager", e); |
Dianne Hackborn | b8b11a0 | 2010-03-10 15:53:11 -0800 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | } |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 395 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 396 | void updateLocked(int enableFlags, int disableFlags) { |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 397 | String action = null; |
| 398 | String oldAction = null; |
| 399 | if (mLastBroadcastState == Intent.EXTRA_DOCK_STATE_CAR) { |
| 400 | adjustStatusBarCarModeLocked(); |
| 401 | oldAction = UiModeManager.ACTION_EXIT_CAR_MODE; |
| 402 | } else if (isDeskDockState(mLastBroadcastState)) { |
| 403 | oldAction = UiModeManager.ACTION_EXIT_DESK_MODE; |
| 404 | } |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 405 | |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 406 | if (mCarModeEnabled) { |
| 407 | if (mLastBroadcastState != Intent.EXTRA_DOCK_STATE_CAR) { |
Tobias Haamel | 780b260 | 2010-03-15 12:54:45 +0100 | [diff] [blame] | 408 | adjustStatusBarCarModeLocked(); |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 409 | |
| 410 | if (oldAction != null) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 411 | getContext().sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL); |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 412 | } |
| 413 | mLastBroadcastState = Intent.EXTRA_DOCK_STATE_CAR; |
| 414 | action = UiModeManager.ACTION_ENTER_CAR_MODE; |
| 415 | } |
| 416 | } else if (isDeskDockState(mDockState)) { |
| 417 | if (!isDeskDockState(mLastBroadcastState)) { |
| 418 | if (oldAction != null) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 419 | getContext().sendBroadcastAsUser(new Intent(oldAction), UserHandle.ALL); |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 420 | } |
| 421 | mLastBroadcastState = mDockState; |
| 422 | action = UiModeManager.ACTION_ENTER_DESK_MODE; |
| 423 | } |
| 424 | } else { |
| 425 | mLastBroadcastState = Intent.EXTRA_DOCK_STATE_UNDOCKED; |
| 426 | action = oldAction; |
| 427 | } |
| 428 | |
| 429 | if (action != null) { |
| 430 | if (LOG) { |
| 431 | Slog.v(TAG, String.format( |
| 432 | "updateLocked: preparing broadcast: action=%s enable=0x%08x disable=0x%08x", |
| 433 | action, enableFlags, disableFlags)); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 434 | } |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 435 | |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 436 | // Send the ordered broadcast; the result receiver will receive after all |
| 437 | // broadcasts have been sent. If any broadcast receiver changes the result |
| 438 | // code from the initial value of RESULT_OK, then the result receiver will |
| 439 | // not launch the corresponding dock application. This gives apps a chance |
| 440 | // to override the behavior and stay in their app even when the device is |
| 441 | // placed into a dock. |
| 442 | Intent intent = new Intent(action); |
| 443 | intent.putExtra("enableFlags", enableFlags); |
| 444 | intent.putExtra("disableFlags", disableFlags); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 445 | getContext().sendOrderedBroadcastAsUser(intent, UserHandle.CURRENT, null, |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 446 | mResultReceiver, null, Activity.RESULT_OK, null, null); |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 447 | |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 448 | // Attempting to make this transition a little more clean, we are going |
| 449 | // to hold off on doing a configuration change until we have finished |
| 450 | // the broadcast and started the home activity. |
| 451 | mHoldingConfiguration = true; |
| 452 | updateConfigurationLocked(); |
| 453 | } else { |
| 454 | String category = null; |
| 455 | if (mCarModeEnabled) { |
| 456 | if (ENABLE_LAUNCH_CAR_DOCK_APP |
| 457 | && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) { |
| 458 | category = Intent.CATEGORY_CAR_DOCK; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 459 | } |
Daniel Sandler | 69a1da4 | 2011-11-04 15:08:30 -0400 | [diff] [blame] | 460 | } else if (isDeskDockState(mDockState)) { |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 461 | if (ENABLE_LAUNCH_DESK_DOCK_APP |
| 462 | && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) { |
| 463 | category = Intent.CATEGORY_DESK_DOCK; |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 464 | } |
| 465 | } else { |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 466 | if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) { |
| 467 | category = Intent.CATEGORY_HOME; |
| 468 | } |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 469 | } |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 470 | |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 471 | if (LOG) { |
| 472 | Slog.v(TAG, "updateLocked: null action, mDockState=" |
| 473 | + mDockState +", category=" + category); |
| 474 | } |
Daniel Sandler | 69a1da4 | 2011-11-04 15:08:30 -0400 | [diff] [blame] | 475 | |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 476 | sendConfigurationAndStartDreamOrDockAppLocked(category); |
| 477 | } |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 478 | |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 479 | // keep screen on when charging and in car mode |
| 480 | boolean keepScreenOn = mCharging && |
keunyoung | 1d0a7cc | 2014-07-28 13:12:50 -0700 | [diff] [blame] | 481 | ((mCarModeEnabled && mCarModeKeepsScreenOn && |
keunyoung | c093bf2 | 2014-08-11 18:51:15 -0700 | [diff] [blame] | 482 | (mCarModeEnableFlags & UiModeManager.ENABLE_CAR_MODE_ALLOW_SLEEP) == 0) || |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 483 | (mCurUiMode == Configuration.UI_MODE_TYPE_DESK && mDeskModeKeepsScreenOn)); |
| 484 | if (keepScreenOn != mWakeLock.isHeld()) { |
| 485 | if (keepScreenOn) { |
| 486 | mWakeLock.acquire(); |
Dianne Hackborn | f5c5d22 | 2010-04-09 13:14:48 -0700 | [diff] [blame] | 487 | } else { |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 488 | mWakeLock.release(); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 489 | } |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 493 | private void updateAfterBroadcastLocked(String action, int enableFlags, int disableFlags) { |
| 494 | // Launch a dock activity |
| 495 | String category = null; |
| 496 | if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) { |
| 497 | // Only launch car home when car mode is enabled and the caller |
| 498 | // has asked us to switch to it. |
| 499 | if (ENABLE_LAUNCH_CAR_DOCK_APP |
| 500 | && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) { |
| 501 | category = Intent.CATEGORY_CAR_DOCK; |
| 502 | } |
| 503 | } else if (UiModeManager.ACTION_ENTER_DESK_MODE.equals(action)) { |
| 504 | // Only launch car home when desk mode is enabled and the caller |
| 505 | // has asked us to switch to it. Currently re-using the car |
| 506 | // mode flag since we don't have a formal API for "desk mode". |
| 507 | if (ENABLE_LAUNCH_DESK_DOCK_APP |
| 508 | && (enableFlags & UiModeManager.ENABLE_CAR_MODE_GO_CAR_HOME) != 0) { |
| 509 | category = Intent.CATEGORY_DESK_DOCK; |
| 510 | } |
| 511 | } else { |
| 512 | // Launch the standard home app if requested. |
| 513 | if ((disableFlags & UiModeManager.DISABLE_CAR_MODE_GO_HOME) != 0) { |
| 514 | category = Intent.CATEGORY_HOME; |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | if (LOG) { |
| 519 | Slog.v(TAG, String.format( |
| 520 | "Handling broadcast result for action %s: enable=0x%08x, disable=0x%08x, " |
| 521 | + "category=%s", |
| 522 | action, enableFlags, disableFlags, category)); |
| 523 | } |
| 524 | |
| 525 | sendConfigurationAndStartDreamOrDockAppLocked(category); |
| 526 | } |
| 527 | |
| 528 | private void sendConfigurationAndStartDreamOrDockAppLocked(String category) { |
| 529 | // Update the configuration but don't send it yet. |
| 530 | mHoldingConfiguration = false; |
| 531 | updateConfigurationLocked(); |
| 532 | |
| 533 | // Start the dock app, if there is one. |
| 534 | boolean dockAppStarted = false; |
| 535 | if (category != null) { |
| 536 | // Now we are going to be careful about switching the |
| 537 | // configuration and starting the activity -- we need to |
| 538 | // do this in a specific order under control of the |
| 539 | // activity manager, to do it cleanly. So compute the |
| 540 | // new config, but don't set it yet, and let the |
| 541 | // activity manager take care of both the start and config |
| 542 | // change. |
| 543 | Intent homeIntent = buildHomeIntent(category); |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 544 | if (Sandman.shouldStartDockApp(getContext(), homeIntent)) { |
Jeff Brown | 11159e9 | 2012-10-11 15:58:37 -0700 | [diff] [blame] | 545 | try { |
| 546 | int result = ActivityManagerNative.getDefault().startActivityWithConfig( |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 547 | null, null, homeIntent, null, null, null, 0, 0, |
Jeff Brown | 11159e9 | 2012-10-11 15:58:37 -0700 | [diff] [blame] | 548 | mConfiguration, null, UserHandle.USER_CURRENT); |
| 549 | if (result >= ActivityManager.START_SUCCESS) { |
| 550 | dockAppStarted = true; |
| 551 | } else if (result != ActivityManager.START_INTENT_NOT_RESOLVED) { |
| 552 | Slog.e(TAG, "Could not start dock app: " + homeIntent |
| 553 | + ", startActivityWithConfig result " + result); |
| 554 | } |
| 555 | } catch (RemoteException ex) { |
| 556 | Slog.e(TAG, "Could not start dock app: " + homeIntent, ex); |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 557 | } |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | |
| 561 | // Send the new configuration. |
| 562 | sendConfigurationLocked(); |
| 563 | |
| 564 | // If we did not start a dock app, then start dreaming if supported. |
Jeff Brown | 11159e9 | 2012-10-11 15:58:37 -0700 | [diff] [blame] | 565 | if (category != null && !dockAppStarted) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 566 | Sandman.startDreamWhenDockedIfAppropriate(getContext()); |
Jeff Brown | 62c82e4 | 2012-09-26 01:30:41 -0700 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 570 | private void adjustStatusBarCarModeLocked() { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 571 | final Context context = getContext(); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 572 | if (mStatusBarManager == null) { |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 573 | mStatusBarManager = (StatusBarManager) |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 574 | context.getSystemService(Context.STATUS_BAR_SERVICE); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 575 | } |
| 576 | |
Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 577 | // Fear not: StatusBarManagerService manages a list of requests to disable |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 578 | // features of the status bar; these are ORed together to form the |
| 579 | // active disabled list. So if (for example) the device is locked and |
| 580 | // the status bar should be totally disabled, the calls below will |
| 581 | // have no effect until the device is unlocked. |
| 582 | if (mStatusBarManager != null) { |
| 583 | mStatusBarManager.disable(mCarModeEnabled |
| 584 | ? StatusBarManager.DISABLE_NOTIFICATION_TICKER |
| 585 | : StatusBarManager.DISABLE_NONE); |
| 586 | } |
| 587 | |
| 588 | if (mNotificationManager == null) { |
| 589 | mNotificationManager = (NotificationManager) |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 590 | context.getSystemService(Context.NOTIFICATION_SERVICE); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | if (mNotificationManager != null) { |
| 594 | if (mCarModeEnabled) { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 595 | Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 596 | |
| 597 | Notification n = new Notification(); |
| 598 | n.icon = R.drawable.stat_notify_car_mode; |
| 599 | n.defaults = Notification.DEFAULT_LIGHTS; |
| 600 | n.flags = Notification.FLAG_ONGOING_EVENT; |
| 601 | n.when = 0; |
| 602 | n.setLatestEventInfo( |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 603 | context, |
| 604 | context.getString(R.string.car_mode_disable_notification_title), |
| 605 | context.getString(R.string.car_mode_disable_notification_message), |
| 606 | PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0, |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 607 | null, UserHandle.CURRENT)); |
| 608 | mNotificationManager.notifyAsUser(null, |
| 609 | R.string.car_mode_disable_notification_title, n, UserHandle.ALL); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 610 | } else { |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 611 | mNotificationManager.cancelAsUser(null, |
| 612 | R.string.car_mode_disable_notification_title, UserHandle.ALL); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 613 | } |
| 614 | } |
| 615 | } |
| 616 | |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 617 | void updateTwilight() { |
Jeff Brown | 2416e09 | 2012-08-21 22:12:20 -0700 | [diff] [blame] | 618 | synchronized (mLock) { |
Jeff Brown | 487bb6e | 2012-10-11 13:35:42 -0700 | [diff] [blame] | 619 | if (isDoingNightModeLocked() && mNightMode == UiModeManager.MODE_NIGHT_AUTO) { |
Jeff Brown | 2416e09 | 2012-08-21 22:12:20 -0700 | [diff] [blame] | 620 | updateComputedNightModeLocked(); |
| 621 | updateLocked(0, 0); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 622 | } |
| 623 | } |
Jeff Brown | 2416e09 | 2012-08-21 22:12:20 -0700 | [diff] [blame] | 624 | } |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 625 | |
Jeff Brown | 2416e09 | 2012-08-21 22:12:20 -0700 | [diff] [blame] | 626 | private void updateComputedNightModeLocked() { |
Adam Lesinski | 182f73f | 2013-12-05 16:48:06 -0800 | [diff] [blame] | 627 | TwilightState state = mTwilightManager.getCurrentState(); |
Jeff Brown | 2416e09 | 2012-08-21 22:12:20 -0700 | [diff] [blame] | 628 | if (state != null) { |
| 629 | mComputedNightMode = state.isNight(); |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 630 | } |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 631 | } |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 632 | |
Bernd Holzhey | ba9ab18 | 2010-03-12 09:30:29 +0100 | [diff] [blame] | 633 | |
Dianne Hackborn | 7299c41 | 2010-03-04 18:41:49 -0800 | [diff] [blame] | 634 | } |