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