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