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