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