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