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