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