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