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