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