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