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