KeyguardServiceDelegate.java revision 8d78693157cb3fa7ab1c9072ef913af4d67c6314
1package com.android.server.policy.keyguard; 2 3import android.app.ActivityManager; 4import android.app.ActivityManagerInternal; 5import android.app.ActivityManagerNative; 6import android.content.ComponentName; 7import android.content.Context; 8import android.content.Intent; 9import android.content.ServiceConnection; 10import android.content.res.Resources; 11import android.os.Bundle; 12import android.os.Handler; 13import android.os.IBinder; 14import android.os.RemoteException; 15import android.os.UserHandle; 16import android.util.Log; 17import android.util.Slog; 18import android.view.View; 19import android.view.WindowManagerPolicy.OnKeyguardExitResult; 20 21import com.android.internal.policy.IKeyguardDrawnCallback; 22import com.android.internal.policy.IKeyguardExitCallback; 23import com.android.internal.policy.IKeyguardService; 24import com.android.server.LocalServices; 25import com.android.server.UiThread; 26 27import java.io.PrintWriter; 28 29/** 30 * A local class that keeps a cache of keyguard state that can be restored in the event 31 * keyguard crashes. It currently also allows runtime-selectable 32 * local or remote instances of keyguard. 33 */ 34public class KeyguardServiceDelegate { 35 private static final String TAG = "KeyguardServiceDelegate"; 36 private static final boolean DEBUG = true; 37 38 private static final int SCREEN_STATE_OFF = 0; 39 private static final int SCREEN_STATE_TURNING_ON = 1; 40 private static final int SCREEN_STATE_ON = 2; 41 42 private static final int INTERACTIVE_STATE_SLEEP = 0; 43 private static final int INTERACTIVE_STATE_AWAKE = 1; 44 private static final int INTERACTIVE_STATE_GOING_TO_SLEEP = 2; 45 46 protected KeyguardServiceWrapper mKeyguardService; 47 private final Context mContext; 48 private final Handler mHandler; 49 private final KeyguardState mKeyguardState = new KeyguardState(); 50 private DrawnListener mDrawnListenerWhenConnect; 51 52 private static final class KeyguardState { 53 KeyguardState() { 54 reset(); 55 } 56 boolean showing; 57 boolean showingAndNotOccluded; 58 boolean inputRestricted; 59 boolean occluded; 60 boolean secure; 61 boolean dreaming; 62 boolean systemIsReady; 63 boolean deviceHasKeyguard; 64 public boolean enabled; 65 public int offReason; 66 public int currentUser; 67 public boolean bootCompleted; 68 public int screenState; 69 public int interactiveState; 70 71 private void reset() { 72 // Assume keyguard is showing and secure until we know for sure. This is here in 73 // the event something checks before the service is actually started. 74 // KeyguardService itself should default to this state until the real state is known. 75 showing = true; 76 showingAndNotOccluded = true; 77 secure = true; 78 deviceHasKeyguard = true; 79 enabled = true; 80 currentUser = UserHandle.USER_NULL; 81 } 82 }; 83 84 public interface DrawnListener { 85 void onDrawn(); 86 } 87 88 // A delegate class to map a particular invocation with a ShowListener object. 89 private final class KeyguardShowDelegate extends IKeyguardDrawnCallback.Stub { 90 private DrawnListener mDrawnListener; 91 92 KeyguardShowDelegate(DrawnListener drawnListener) { 93 mDrawnListener = drawnListener; 94 } 95 96 @Override 97 public void onDrawn() throws RemoteException { 98 if (DEBUG) Log.v(TAG, "**** SHOWN CALLED ****"); 99 if (mDrawnListener != null) { 100 mDrawnListener.onDrawn(); 101 } 102 } 103 }; 104 105 // A delegate class to map a particular invocation with an OnKeyguardExitResult object. 106 private final class KeyguardExitDelegate extends IKeyguardExitCallback.Stub { 107 private OnKeyguardExitResult mOnKeyguardExitResult; 108 109 KeyguardExitDelegate(OnKeyguardExitResult onKeyguardExitResult) { 110 mOnKeyguardExitResult = onKeyguardExitResult; 111 } 112 113 @Override 114 public void onKeyguardExitResult(boolean success) throws RemoteException { 115 if (DEBUG) Log.v(TAG, "**** onKeyguardExitResult(" + success +") CALLED ****"); 116 if (mOnKeyguardExitResult != null) { 117 mOnKeyguardExitResult.onKeyguardExitResult(success); 118 } 119 } 120 }; 121 122 public KeyguardServiceDelegate(Context context) { 123 mContext = context; 124 mHandler = UiThread.getHandler(); 125 } 126 127 public void bindService(Context context) { 128 Intent intent = new Intent(); 129 final Resources resources = context.getApplicationContext().getResources(); 130 131 final ComponentName keyguardComponent = ComponentName.unflattenFromString( 132 resources.getString(com.android.internal.R.string.config_keyguardComponent)); 133 intent.addFlags(Intent.FLAG_DEBUG_TRIAGED_MISSING); 134 intent.setComponent(keyguardComponent); 135 136 if (!context.bindServiceAsUser(intent, mKeyguardConnection, 137 Context.BIND_AUTO_CREATE, mHandler, UserHandle.SYSTEM)) { 138 Log.v(TAG, "*** Keyguard: can't bind to " + keyguardComponent); 139 mKeyguardState.showing = false; 140 mKeyguardState.showingAndNotOccluded = false; 141 mKeyguardState.secure = false; 142 synchronized (mKeyguardState) { 143 // TODO: Fix synchronisation model in this class. The other state in this class 144 // is at least self-healing but a race condition here can lead to the scrim being 145 // stuck on keyguard-less devices. 146 mKeyguardState.deviceHasKeyguard = false; 147 } 148 } else { 149 if (DEBUG) Log.v(TAG, "*** Keyguard started"); 150 } 151 } 152 153 private final ServiceConnection mKeyguardConnection = new ServiceConnection() { 154 @Override 155 public void onServiceConnected(ComponentName name, IBinder service) { 156 if (DEBUG) Log.v(TAG, "*** Keyguard connected (yay!)"); 157 mKeyguardService = new KeyguardServiceWrapper(mContext, 158 IKeyguardService.Stub.asInterface(service)); 159 if (mKeyguardState.systemIsReady) { 160 // If the system is ready, it means keyguard crashed and restarted. 161 mKeyguardService.onSystemReady(); 162 if (mKeyguardState.currentUser != UserHandle.USER_NULL) { 163 // There has been a user switch earlier 164 mKeyguardService.setCurrentUser(mKeyguardState.currentUser); 165 } 166 // This is used to hide the scrim once keyguard displays. 167 if (mKeyguardState.interactiveState == INTERACTIVE_STATE_AWAKE) { 168 mKeyguardService.onStartedWakingUp(); 169 } 170 if (mKeyguardState.screenState == SCREEN_STATE_ON 171 || mKeyguardState.screenState == SCREEN_STATE_TURNING_ON) { 172 mKeyguardService.onScreenTurningOn( 173 new KeyguardShowDelegate(mDrawnListenerWhenConnect)); 174 } 175 if (mKeyguardState.screenState == SCREEN_STATE_ON) { 176 mKeyguardService.onScreenTurnedOn(); 177 } 178 mDrawnListenerWhenConnect = null; 179 } 180 if (mKeyguardState.bootCompleted) { 181 mKeyguardService.onBootCompleted(); 182 } 183 if (mKeyguardState.occluded) { 184 mKeyguardService.setOccluded(mKeyguardState.occluded, false /* animate */); 185 } 186 if (!mKeyguardState.enabled) { 187 mKeyguardService.setKeyguardEnabled(mKeyguardState.enabled); 188 } 189 } 190 191 @Override 192 public void onServiceDisconnected(ComponentName name) { 193 if (DEBUG) Log.v(TAG, "*** Keyguard disconnected (boo!)"); 194 mKeyguardService = null; 195 mKeyguardState.reset(); 196 mHandler.post(() -> { 197 try { 198 ActivityManagerNative.getDefault().setLockScreenShown(true); 199 } catch (RemoteException e) { 200 // Local call. 201 } 202 }); 203 } 204 }; 205 206 public boolean isShowing() { 207 if (mKeyguardService != null) { 208 mKeyguardState.showing = mKeyguardService.isShowing(); 209 } 210 return mKeyguardState.showing; 211 } 212 213 public boolean isTrusted() { 214 if (mKeyguardService != null) { 215 return mKeyguardService.isTrusted(); 216 } 217 return false; 218 } 219 220 public boolean hasLockscreenWallpaper() { 221 if (mKeyguardService != null) { 222 return mKeyguardService.hasLockscreenWallpaper(); 223 } 224 return false; 225 } 226 227 public boolean isInputRestricted() { 228 if (mKeyguardService != null) { 229 mKeyguardState.inputRestricted = mKeyguardService.isInputRestricted(); 230 } 231 return mKeyguardState.inputRestricted; 232 } 233 234 public void verifyUnlock(final OnKeyguardExitResult onKeyguardExitResult) { 235 if (mKeyguardService != null) { 236 mKeyguardService.verifyUnlock(new KeyguardExitDelegate(onKeyguardExitResult)); 237 } 238 } 239 240 public void setOccluded(boolean isOccluded, boolean animate) { 241 if (mKeyguardService != null) { 242 if (DEBUG) Log.v(TAG, "setOccluded(" + isOccluded + ") animate=" + animate); 243 mKeyguardService.setOccluded(isOccluded, animate); 244 } 245 mKeyguardState.occluded = isOccluded; 246 } 247 248 public void dismiss(boolean allowWhileOccluded) { 249 if (mKeyguardService != null) { 250 mKeyguardService.dismiss(allowWhileOccluded); 251 } 252 } 253 254 public boolean isSecure(int userId) { 255 if (mKeyguardService != null) { 256 mKeyguardState.secure = mKeyguardService.isSecure(userId); 257 } 258 return mKeyguardState.secure; 259 } 260 261 public void onDreamingStarted() { 262 if (mKeyguardService != null) { 263 mKeyguardService.onDreamingStarted(); 264 } 265 mKeyguardState.dreaming = true; 266 } 267 268 public void onDreamingStopped() { 269 if (mKeyguardService != null) { 270 mKeyguardService.onDreamingStopped(); 271 } 272 mKeyguardState.dreaming = false; 273 } 274 275 public void onStartedWakingUp() { 276 if (mKeyguardService != null) { 277 if (DEBUG) Log.v(TAG, "onStartedWakingUp()"); 278 mKeyguardService.onStartedWakingUp(); 279 } 280 mKeyguardState.interactiveState = INTERACTIVE_STATE_AWAKE; 281 } 282 283 public void onScreenTurnedOff() { 284 if (mKeyguardService != null) { 285 if (DEBUG) Log.v(TAG, "onScreenTurnedOff()"); 286 mKeyguardService.onScreenTurnedOff(); 287 } 288 mKeyguardState.screenState = SCREEN_STATE_OFF; 289 } 290 291 public void onScreenTurningOn(final DrawnListener drawnListener) { 292 if (mKeyguardService != null) { 293 if (DEBUG) Log.v(TAG, "onScreenTurnedOn(showListener = " + drawnListener + ")"); 294 mKeyguardService.onScreenTurningOn(new KeyguardShowDelegate(drawnListener)); 295 } else { 296 // try again when we establish a connection 297 Slog.w(TAG, "onScreenTurningOn(): no keyguard service!"); 298 // This shouldn't happen, but if it does, show the scrim immediately and 299 // invoke the listener's callback after the service actually connects. 300 mDrawnListenerWhenConnect = drawnListener; 301 } 302 mKeyguardState.screenState = SCREEN_STATE_TURNING_ON; 303 } 304 305 public void onScreenTurnedOn() { 306 if (mKeyguardService != null) { 307 if (DEBUG) Log.v(TAG, "onScreenTurnedOn()"); 308 mKeyguardService.onScreenTurnedOn(); 309 } 310 mKeyguardState.screenState = SCREEN_STATE_ON; 311 } 312 313 public void onStartedGoingToSleep(int why) { 314 if (mKeyguardService != null) { 315 mKeyguardService.onStartedGoingToSleep(why); 316 } 317 mKeyguardState.offReason = why; 318 mKeyguardState.interactiveState = INTERACTIVE_STATE_GOING_TO_SLEEP; 319 } 320 321 public void onFinishedGoingToSleep(int why, boolean cameraGestureTriggered) { 322 if (mKeyguardService != null) { 323 mKeyguardService.onFinishedGoingToSleep(why, cameraGestureTriggered); 324 } 325 mKeyguardState.interactiveState = INTERACTIVE_STATE_SLEEP; 326 } 327 328 public void setKeyguardEnabled(boolean enabled) { 329 if (mKeyguardService != null) { 330 mKeyguardService.setKeyguardEnabled(enabled); 331 } 332 mKeyguardState.enabled = enabled; 333 } 334 335 public void onSystemReady() { 336 if (mKeyguardService != null) { 337 mKeyguardService.onSystemReady(); 338 } else { 339 mKeyguardState.systemIsReady = true; 340 } 341 } 342 343 public void doKeyguardTimeout(Bundle options) { 344 if (mKeyguardService != null) { 345 mKeyguardService.doKeyguardTimeout(options); 346 } 347 } 348 349 public void setCurrentUser(int newUserId) { 350 if (mKeyguardService != null) { 351 mKeyguardService.setCurrentUser(newUserId); 352 } 353 mKeyguardState.currentUser = newUserId; 354 } 355 356 public void setSwitchingUser(boolean switching) { 357 if (mKeyguardService != null) { 358 mKeyguardService.setSwitchingUser(switching); 359 } 360 } 361 362 public void startKeyguardExitAnimation(long startTime, long fadeoutDuration) { 363 if (mKeyguardService != null) { 364 mKeyguardService.startKeyguardExitAnimation(startTime, fadeoutDuration); 365 } 366 } 367 368 public void onBootCompleted() { 369 if (mKeyguardService != null) { 370 mKeyguardService.onBootCompleted(); 371 } 372 mKeyguardState.bootCompleted = true; 373 } 374 375 public void dump(String prefix, PrintWriter pw) { 376 pw.println(prefix + TAG); 377 prefix += " "; 378 pw.println(prefix + "showing=" + mKeyguardState.showing); 379 pw.println(prefix + "showingAndNotOccluded=" + mKeyguardState.showingAndNotOccluded); 380 pw.println(prefix + "inputRestricted=" + mKeyguardState.inputRestricted); 381 pw.println(prefix + "occluded=" + mKeyguardState.occluded); 382 pw.println(prefix + "secure=" + mKeyguardState.secure); 383 pw.println(prefix + "dreaming=" + mKeyguardState.dreaming); 384 pw.println(prefix + "systemIsReady=" + mKeyguardState.systemIsReady); 385 pw.println(prefix + "deviceHasKeyguard=" + mKeyguardState.deviceHasKeyguard); 386 pw.println(prefix + "enabled=" + mKeyguardState.enabled); 387 pw.println(prefix + "offReason=" + mKeyguardState.offReason); 388 pw.println(prefix + "currentUser=" + mKeyguardState.currentUser); 389 pw.println(prefix + "bootCompleted=" + mKeyguardState.bootCompleted); 390 pw.println(prefix + "screenState=" + mKeyguardState.screenState); 391 pw.println(prefix + "interactiveState=" + mKeyguardState.interactiveState); 392 if (mKeyguardService != null) { 393 mKeyguardService.dump(prefix, pw); 394 } 395 } 396} 397