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