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