KeyguardUpdateMonitor.java revision 4eb36cfdcabb51f67a887d867de9559966606d86
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 17package com.android.internal.policy.impl.keyguard; 18 19import android.app.ActivityManagerNative; 20import android.app.IUserSwitchObserver; 21import android.app.admin.DevicePolicyManager; 22import android.content.BroadcastReceiver; 23import android.content.Context; 24import android.content.Intent; 25import android.content.IntentFilter; 26import android.database.ContentObserver; 27import static android.os.BatteryManager.BATTERY_STATUS_FULL; 28import static android.os.BatteryManager.BATTERY_STATUS_UNKNOWN; 29import static android.os.BatteryManager.BATTERY_HEALTH_UNKNOWN; 30import static android.os.BatteryManager.EXTRA_STATUS; 31import static android.os.BatteryManager.EXTRA_PLUGGED; 32import static android.os.BatteryManager.EXTRA_LEVEL; 33import static android.os.BatteryManager.EXTRA_HEALTH; 34import android.media.AudioManager; 35import android.os.BatteryManager; 36import android.os.Handler; 37import android.os.IRemoteCallback; 38import android.os.Message; 39import android.os.RemoteException; 40import android.provider.Settings; 41 42import com.android.internal.telephony.IccCardConstants; 43import com.android.internal.telephony.TelephonyIntents; 44 45import android.telephony.TelephonyManager; 46import android.util.Log; 47import com.android.internal.R; 48import com.google.android.collect.Lists; 49 50import java.lang.ref.WeakReference; 51import java.util.ArrayList; 52 53/** 54 * Watches for updates that may be interesting to the keyguard, and provides 55 * the up to date information as well as a registration for callbacks that care 56 * to be updated. 57 * 58 * Note: under time crunch, this has been extended to include some stuff that 59 * doesn't really belong here. see {@link #handleBatteryUpdate} where it shutdowns 60 * the device, and {@link #getFailedUnlockAttempts()}, {@link #reportFailedAttempt()} 61 * and {@link #clearFailedUnlockAttempts()}. Maybe we should rename this 'KeyguardContext'... 62 */ 63public class KeyguardUpdateMonitor { 64 65 private static final String TAG = "KeyguardUpdateMonitor"; 66 private static final boolean DEBUG = false; 67 private static final boolean DEBUG_SIM_STATES = DEBUG || false; 68 private static final int FAILED_BIOMETRIC_UNLOCK_ATTEMPTS_BEFORE_BACKUP = 3; 69 private static final int LOW_BATTERY_THRESHOLD = 20; 70 71 // Callback messages 72 private static final int MSG_TIME_UPDATE = 301; 73 private static final int MSG_BATTERY_UPDATE = 302; 74 private static final int MSG_CARRIER_INFO_UPDATE = 303; 75 private static final int MSG_SIM_STATE_CHANGE = 304; 76 private static final int MSG_RINGER_MODE_CHANGED = 305; 77 private static final int MSG_PHONE_STATE_CHANGED = 306; 78 private static final int MSG_CLOCK_VISIBILITY_CHANGED = 307; 79 private static final int MSG_DEVICE_PROVISIONED = 308; 80 private static final int MSG_DPM_STATE_CHANGED = 309; 81 private static final int MSG_USER_SWITCHED = 310; 82 private static final int MSG_USER_REMOVED = 311; 83 private static final int MSG_KEYGUARD_VISIBILITY_CHANGED = 312; 84 protected static final int MSG_BOOT_COMPLETED = 313; 85 86 87 private static KeyguardUpdateMonitor sInstance; 88 89 private final Context mContext; 90 91 // Telephony state 92 private IccCardConstants.State mSimState = IccCardConstants.State.READY; 93 private CharSequence mTelephonyPlmn; 94 private CharSequence mTelephonySpn; 95 private int mRingMode; 96 private int mPhoneState; 97 private boolean mKeyguardIsVisible; 98 private boolean mBootCompleted; 99 100 // Device provisioning state 101 private boolean mDeviceProvisioned; 102 103 // Battery status 104 private BatteryStatus mBatteryStatus; 105 106 // Password attempts 107 private int mFailedAttempts = 0; 108 private int mFailedBiometricUnlockAttempts = 0; 109 110 private boolean mAlternateUnlockEnabled; 111 112 private boolean mClockVisible; 113 114 private final ArrayList<WeakReference<KeyguardUpdateMonitorCallback>> 115 mCallbacks = Lists.newArrayList(); 116 private ContentObserver mContentObserver; 117 118 private final Handler mHandler = new Handler() { 119 @Override 120 public void handleMessage(Message msg) { 121 switch (msg.what) { 122 case MSG_TIME_UPDATE: 123 handleTimeUpdate(); 124 break; 125 case MSG_BATTERY_UPDATE: 126 handleBatteryUpdate((BatteryStatus) msg.obj); 127 break; 128 case MSG_CARRIER_INFO_UPDATE: 129 handleCarrierInfoUpdate(); 130 break; 131 case MSG_SIM_STATE_CHANGE: 132 handleSimStateChange((SimArgs) msg.obj); 133 break; 134 case MSG_RINGER_MODE_CHANGED: 135 handleRingerModeChange(msg.arg1); 136 break; 137 case MSG_PHONE_STATE_CHANGED: 138 handlePhoneStateChanged((String)msg.obj); 139 break; 140 case MSG_CLOCK_VISIBILITY_CHANGED: 141 handleClockVisibilityChanged(); 142 break; 143 case MSG_DEVICE_PROVISIONED: 144 handleDeviceProvisioned(); 145 break; 146 case MSG_DPM_STATE_CHANGED: 147 handleDevicePolicyManagerStateChanged(); 148 break; 149 case MSG_USER_SWITCHED: 150 handleUserSwitched(msg.arg1, (IRemoteCallback)msg.obj); 151 break; 152 case MSG_USER_REMOVED: 153 handleUserRemoved(msg.arg1); 154 break; 155 case MSG_KEYGUARD_VISIBILITY_CHANGED: 156 handleKeyguardVisibilityChanged(msg.arg1); 157 break; 158 case MSG_BOOT_COMPLETED: 159 handleBootCompleted(); 160 break; 161 162 } 163 } 164 }; 165 166 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { 167 168 public void onReceive(Context context, Intent intent) { 169 final String action = intent.getAction(); 170 if (DEBUG) Log.d(TAG, "received broadcast " + action); 171 172 if (Intent.ACTION_TIME_TICK.equals(action) 173 || Intent.ACTION_TIME_CHANGED.equals(action) 174 || Intent.ACTION_TIMEZONE_CHANGED.equals(action)) { 175 mHandler.sendMessage(mHandler.obtainMessage(MSG_TIME_UPDATE)); 176 } else if (TelephonyIntents.SPN_STRINGS_UPDATED_ACTION.equals(action)) { 177 mTelephonyPlmn = getTelephonyPlmnFrom(intent); 178 mTelephonySpn = getTelephonySpnFrom(intent); 179 mHandler.sendMessage(mHandler.obtainMessage(MSG_CARRIER_INFO_UPDATE)); 180 } else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) { 181 final int status = intent.getIntExtra(EXTRA_STATUS, BATTERY_STATUS_UNKNOWN); 182 final int plugged = intent.getIntExtra(EXTRA_PLUGGED, 0); 183 final int level = intent.getIntExtra(EXTRA_LEVEL, 0); 184 final int health = intent.getIntExtra(EXTRA_HEALTH, BATTERY_HEALTH_UNKNOWN); 185 final Message msg = mHandler.obtainMessage( 186 MSG_BATTERY_UPDATE, new BatteryStatus(status, level, plugged, health)); 187 mHandler.sendMessage(msg); 188 } else if (TelephonyIntents.ACTION_SIM_STATE_CHANGED.equals(action)) { 189 if (DEBUG_SIM_STATES) { 190 Log.v(TAG, "action " + action + " state" + 191 intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE)); 192 } 193 mHandler.sendMessage(mHandler.obtainMessage( 194 MSG_SIM_STATE_CHANGE, SimArgs.fromIntent(intent))); 195 } else if (AudioManager.RINGER_MODE_CHANGED_ACTION.equals(action)) { 196 mHandler.sendMessage(mHandler.obtainMessage(MSG_RINGER_MODE_CHANGED, 197 intent.getIntExtra(AudioManager.EXTRA_RINGER_MODE, -1), 0)); 198 } else if (TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(action)) { 199 String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); 200 mHandler.sendMessage(mHandler.obtainMessage(MSG_PHONE_STATE_CHANGED, state)); 201 } else if (DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED 202 .equals(action)) { 203 mHandler.sendMessage(mHandler.obtainMessage(MSG_DPM_STATE_CHANGED)); 204 } else if (Intent.ACTION_USER_REMOVED.equals(action)) { 205 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_REMOVED, 206 intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0), 0)); 207 } else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) { 208 mHandler.sendMessage(mHandler.obtainMessage(MSG_BOOT_COMPLETED)); 209 } 210 } 211 }; 212 213 /** 214 * When we receive a 215 * {@link com.android.internal.telephony.TelephonyIntents#ACTION_SIM_STATE_CHANGED} broadcast, 216 * and then pass a result via our handler to {@link KeyguardUpdateMonitor#handleSimStateChange}, 217 * we need a single object to pass to the handler. This class helps decode 218 * the intent and provide a {@link SimCard.State} result. 219 */ 220 private static class SimArgs { 221 public final IccCardConstants.State simState; 222 223 SimArgs(IccCardConstants.State state) { 224 simState = state; 225 } 226 227 static SimArgs fromIntent(Intent intent) { 228 IccCardConstants.State state; 229 if (!TelephonyIntents.ACTION_SIM_STATE_CHANGED.equals(intent.getAction())) { 230 throw new IllegalArgumentException("only handles intent ACTION_SIM_STATE_CHANGED"); 231 } 232 String stateExtra = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE); 233 if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(stateExtra)) { 234 final String absentReason = intent 235 .getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON); 236 237 if (IccCardConstants.INTENT_VALUE_ABSENT_ON_PERM_DISABLED.equals( 238 absentReason)) { 239 state = IccCardConstants.State.PERM_DISABLED; 240 } else { 241 state = IccCardConstants.State.ABSENT; 242 } 243 } else if (IccCardConstants.INTENT_VALUE_ICC_READY.equals(stateExtra)) { 244 state = IccCardConstants.State.READY; 245 } else if (IccCardConstants.INTENT_VALUE_ICC_LOCKED.equals(stateExtra)) { 246 final String lockedReason = intent 247 .getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON); 248 if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PIN.equals(lockedReason)) { 249 state = IccCardConstants.State.PIN_REQUIRED; 250 } else if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PUK.equals(lockedReason)) { 251 state = IccCardConstants.State.PUK_REQUIRED; 252 } else { 253 state = IccCardConstants.State.UNKNOWN; 254 } 255 } else if (IccCardConstants.INTENT_VALUE_LOCKED_NETWORK.equals(stateExtra)) { 256 state = IccCardConstants.State.NETWORK_LOCKED; 257 } else if (IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(stateExtra) 258 || IccCardConstants.INTENT_VALUE_ICC_IMSI.equals(stateExtra)) { 259 // This is required because telephony doesn't return to "READY" after 260 // these state transitions. See bug 7197471. 261 state = IccCardConstants.State.READY; 262 } else { 263 state = IccCardConstants.State.UNKNOWN; 264 } 265 return new SimArgs(state); 266 } 267 268 public String toString() { 269 return simState.toString(); 270 } 271 } 272 273 /* package */ static class BatteryStatus { 274 public final int status; 275 public final int level; 276 public final int plugged; 277 public final int health; 278 public BatteryStatus(int status, int level, int plugged, int health) { 279 this.status = status; 280 this.level = level; 281 this.plugged = plugged; 282 this.health = health; 283 } 284 285 /** 286 * Determine whether the device is plugged in (USB, power, or wireless). 287 * @return true if the device is plugged in. 288 */ 289 boolean isPluggedIn() { 290 return plugged == BatteryManager.BATTERY_PLUGGED_AC 291 || plugged == BatteryManager.BATTERY_PLUGGED_USB 292 || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS; 293 } 294 295 /** 296 * Whether or not the device is charged. Note that some devices never return 100% for 297 * battery level, so this allows either battery level or status to determine if the 298 * battery is charged. 299 * @return true if the device is charged 300 */ 301 public boolean isCharged() { 302 return status == BATTERY_STATUS_FULL || level >= 100; 303 } 304 305 /** 306 * Whether battery is low and needs to be charged. 307 * @return true if battery is low 308 */ 309 public boolean isBatteryLow() { 310 return level < LOW_BATTERY_THRESHOLD; 311 } 312 313 } 314 315 public static KeyguardUpdateMonitor getInstance(Context context) { 316 if (sInstance == null) { 317 sInstance = new KeyguardUpdateMonitor(context); 318 } 319 return sInstance; 320 } 321 322 private KeyguardUpdateMonitor(Context context) { 323 mContext = context; 324 325 mDeviceProvisioned = Settings.Global.getInt( 326 mContext.getContentResolver(), Settings.Global.DEVICE_PROVISIONED, 0) != 0; 327 328 // Since device can't be un-provisioned, we only need to register a content observer 329 // to update mDeviceProvisioned when we are... 330 if (!mDeviceProvisioned) { 331 watchForDeviceProvisioning(); 332 } 333 334 // Take a guess at initial SIM state, battery status and PLMN until we get an update 335 mSimState = IccCardConstants.State.NOT_READY; 336 mBatteryStatus = new BatteryStatus(BATTERY_STATUS_UNKNOWN, 100, 0, 0); 337 mTelephonyPlmn = getDefaultPlmn(); 338 339 // Watch for interesting updates 340 final IntentFilter filter = new IntentFilter(); 341 filter.addAction(Intent.ACTION_TIME_TICK); 342 filter.addAction(Intent.ACTION_TIME_CHANGED); 343 filter.addAction(Intent.ACTION_BATTERY_CHANGED); 344 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); 345 filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED); 346 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); 347 filter.addAction(TelephonyIntents.SPN_STRINGS_UPDATED_ACTION); 348 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION); 349 filter.addAction(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED); 350 filter.addAction(Intent.ACTION_USER_REMOVED); 351 filter.addAction(Intent.ACTION_BOOT_COMPLETED); 352 context.registerReceiver(mBroadcastReceiver, filter); 353 354 try { 355 ActivityManagerNative.getDefault().registerUserSwitchObserver( 356 new IUserSwitchObserver.Stub() { 357 @Override 358 public void onUserSwitching(int newUserId, IRemoteCallback reply) { 359 mHandler.sendMessage(mHandler.obtainMessage(MSG_USER_SWITCHED, 360 newUserId, 0, reply)); 361 } 362 @Override 363 public void onUserSwitchComplete(int newUserId) throws RemoteException { 364 } 365 }); 366 } catch (RemoteException e) { 367 // TODO Auto-generated catch block 368 e.printStackTrace(); 369 } 370 } 371 372 private void watchForDeviceProvisioning() { 373 mContentObserver = new ContentObserver(mHandler) { 374 @Override 375 public void onChange(boolean selfChange) { 376 super.onChange(selfChange); 377 mDeviceProvisioned = Settings.Global.getInt(mContext.getContentResolver(), 378 Settings.Global.DEVICE_PROVISIONED, 0) != 0; 379 if (mDeviceProvisioned) { 380 mHandler.sendMessage(mHandler.obtainMessage(MSG_DEVICE_PROVISIONED)); 381 } 382 if (DEBUG) Log.d(TAG, "DEVICE_PROVISIONED state = " + mDeviceProvisioned); 383 } 384 }; 385 386 mContext.getContentResolver().registerContentObserver( 387 Settings.Global.getUriFor(Settings.Global.DEVICE_PROVISIONED), 388 false, mContentObserver); 389 390 // prevent a race condition between where we check the flag and where we register the 391 // observer by grabbing the value once again... 392 boolean provisioned = Settings.Global.getInt(mContext.getContentResolver(), 393 Settings.Global.DEVICE_PROVISIONED, 0) != 0; 394 if (provisioned != mDeviceProvisioned) { 395 mDeviceProvisioned = provisioned; 396 if (mDeviceProvisioned) { 397 mHandler.sendMessage(mHandler.obtainMessage(MSG_DEVICE_PROVISIONED)); 398 } 399 } 400 } 401 402 /** 403 * Handle {@link #MSG_DPM_STATE_CHANGED} 404 */ 405 protected void handleDevicePolicyManagerStateChanged() { 406 for (int i = mCallbacks.size() - 1; i >= 0; i--) { 407 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get(); 408 if (cb != null) { 409 cb.onDevicePolicyManagerStateChanged(); 410 } 411 } 412 } 413 414 /** 415 * Handle {@link #MSG_USER_SWITCHED} 416 */ 417 protected void handleUserSwitched(int userId, IRemoteCallback reply) { 418 for (int i = 0; i < mCallbacks.size(); i++) { 419 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get(); 420 if (cb != null) { 421 cb.onUserSwitched(userId); 422 } 423 } 424 setAlternateUnlockEnabled(false); 425 try { 426 reply.sendResult(null); 427 } catch (RemoteException e) { 428 } 429 } 430 431 /** 432 * Handle {@link #MSG_BOOT_COMPLETED} 433 */ 434 protected void handleBootCompleted() { 435 mBootCompleted = true; 436 for (int i = 0; i < mCallbacks.size(); i++) { 437 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get(); 438 if (cb != null) { 439 cb.onBootCompleted(); 440 } 441 } 442 } 443 444 /** 445 * We need to store this state in the KeyguardUpdateMonitor since this class will not be 446 * destroyed. 447 */ 448 public boolean hasBootCompleted() { 449 return mBootCompleted; 450 } 451 452 /** 453 * Handle {@link #MSG_USER_SWITCHED} 454 */ 455 protected void handleUserRemoved(int userId) { 456 for (int i = 0; i < mCallbacks.size(); i++) { 457 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get(); 458 if (cb != null) { 459 cb.onUserRemoved(userId); 460 } 461 } 462 } 463 464 /** 465 * Handle {@link #MSG_DEVICE_PROVISIONED} 466 */ 467 protected void handleDeviceProvisioned() { 468 for (int i = 0; i < mCallbacks.size(); i++) { 469 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get(); 470 if (cb != null) { 471 cb.onDeviceProvisioned(); 472 } 473 } 474 if (mContentObserver != null) { 475 // We don't need the observer anymore... 476 mContext.getContentResolver().unregisterContentObserver(mContentObserver); 477 mContentObserver = null; 478 } 479 } 480 481 /** 482 * Handle {@link #MSG_PHONE_STATE_CHANGED} 483 */ 484 protected void handlePhoneStateChanged(String newState) { 485 if (DEBUG) Log.d(TAG, "handlePhoneStateChanged(" + newState + ")"); 486 if (TelephonyManager.EXTRA_STATE_IDLE.equals(newState)) { 487 mPhoneState = TelephonyManager.CALL_STATE_IDLE; 488 } else if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(newState)) { 489 mPhoneState = TelephonyManager.CALL_STATE_OFFHOOK; 490 } else if (TelephonyManager.EXTRA_STATE_RINGING.equals(newState)) { 491 mPhoneState = TelephonyManager.CALL_STATE_RINGING; 492 } 493 for (int i = 0; i < mCallbacks.size(); i++) { 494 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get(); 495 if (cb != null) { 496 cb.onPhoneStateChanged(mPhoneState); 497 } 498 } 499 } 500 501 /** 502 * Handle {@link #MSG_RINGER_MODE_CHANGED} 503 */ 504 protected void handleRingerModeChange(int mode) { 505 if (DEBUG) Log.d(TAG, "handleRingerModeChange(" + mode + ")"); 506 mRingMode = mode; 507 for (int i = 0; i < mCallbacks.size(); i++) { 508 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get(); 509 if (cb != null) { 510 cb.onRingerModeChanged(mode); 511 } 512 } 513 } 514 515 /** 516 * Handle {@link #MSG_TIME_UPDATE} 517 */ 518 private void handleTimeUpdate() { 519 if (DEBUG) Log.d(TAG, "handleTimeUpdate"); 520 for (int i = 0; i < mCallbacks.size(); i++) { 521 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get(); 522 if (cb != null) { 523 cb.onTimeChanged(); 524 } 525 } 526 } 527 528 /** 529 * Handle {@link #MSG_BATTERY_UPDATE} 530 */ 531 private void handleBatteryUpdate(BatteryStatus status) { 532 if (DEBUG) Log.d(TAG, "handleBatteryUpdate"); 533 final boolean batteryUpdateInteresting = isBatteryUpdateInteresting(mBatteryStatus, status); 534 mBatteryStatus = status; 535 if (batteryUpdateInteresting) { 536 for (int i = 0; i < mCallbacks.size(); i++) { 537 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get(); 538 if (cb != null) { 539 cb.onRefreshBatteryInfo(status); 540 } 541 } 542 } 543 } 544 545 /** 546 * Handle {@link #MSG_CARRIER_INFO_UPDATE} 547 */ 548 private void handleCarrierInfoUpdate() { 549 if (DEBUG) Log.d(TAG, "handleCarrierInfoUpdate: plmn = " + mTelephonyPlmn 550 + ", spn = " + mTelephonySpn); 551 552 for (int i = 0; i < mCallbacks.size(); i++) { 553 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get(); 554 if (cb != null) { 555 cb.onRefreshCarrierInfo(mTelephonyPlmn, mTelephonySpn); 556 } 557 } 558 } 559 560 /** 561 * Handle {@link #MSG_SIM_STATE_CHANGE} 562 */ 563 private void handleSimStateChange(SimArgs simArgs) { 564 final IccCardConstants.State state = simArgs.simState; 565 566 if (DEBUG) { 567 Log.d(TAG, "handleSimStateChange: intentValue = " + simArgs + " " 568 + "state resolved to " + state.toString()); 569 } 570 571 if (state != IccCardConstants.State.UNKNOWN && state != mSimState) { 572 mSimState = state; 573 for (int i = 0; i < mCallbacks.size(); i++) { 574 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get(); 575 if (cb != null) { 576 cb.onSimStateChanged(state); 577 } 578 } 579 } 580 } 581 582 /** 583 * Handle {@link #MSG_CLOCK_VISIBILITY_CHANGED} 584 */ 585 private void handleClockVisibilityChanged() { 586 if (DEBUG) Log.d(TAG, "handleClockVisibilityChanged()"); 587 for (int i = 0; i < mCallbacks.size(); i++) { 588 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get(); 589 if (cb != null) { 590 cb.onClockVisibilityChanged(); 591 } 592 } 593 } 594 595 /** 596 * Handle {@link #MSG_KEYGUARD_VISIBILITY_CHANGED} 597 */ 598 private void handleKeyguardVisibilityChanged(int showing) { 599 if (DEBUG) Log.d(TAG, "handleKeyguardVisibilityChanged(" + showing + ")"); 600 boolean isShowing = (showing == 1); 601 mKeyguardIsVisible = isShowing; 602 for (int i = 0; i < mCallbacks.size(); i++) { 603 KeyguardUpdateMonitorCallback cb = mCallbacks.get(i).get(); 604 if (cb != null) { 605 cb.onKeyguardVisibilityChanged(isShowing); 606 } 607 } 608 } 609 610 public boolean isKeyguardVisible() { 611 return mKeyguardIsVisible; 612 } 613 614 private static boolean isBatteryUpdateInteresting(BatteryStatus old, BatteryStatus current) { 615 final boolean nowPluggedIn = current.isPluggedIn(); 616 final boolean wasPluggedIn = old.isPluggedIn(); 617 final boolean stateChangedWhilePluggedIn = 618 wasPluggedIn == true && nowPluggedIn == true 619 && (old.status != current.status); 620 621 // change in plug state is always interesting 622 if (wasPluggedIn != nowPluggedIn || stateChangedWhilePluggedIn) { 623 return true; 624 } 625 626 // change in battery level while plugged in 627 if (nowPluggedIn && old.level != current.level) { 628 return true; 629 } 630 631 // change where battery needs charging 632 if (!nowPluggedIn && current.isBatteryLow() && current.level != old.level) { 633 return true; 634 } 635 return false; 636 } 637 638 /** 639 * @param intent The intent with action {@link TelephonyIntents#SPN_STRINGS_UPDATED_ACTION} 640 * @return The string to use for the plmn, or null if it should not be shown. 641 */ 642 private CharSequence getTelephonyPlmnFrom(Intent intent) { 643 if (intent.getBooleanExtra(TelephonyIntents.EXTRA_SHOW_PLMN, false)) { 644 final String plmn = intent.getStringExtra(TelephonyIntents.EXTRA_PLMN); 645 return (plmn != null) ? plmn : getDefaultPlmn(); 646 } 647 return null; 648 } 649 650 /** 651 * @return The default plmn (no service) 652 */ 653 private CharSequence getDefaultPlmn() { 654 return mContext.getResources().getText(R.string.lockscreen_carrier_default); 655 } 656 657 /** 658 * @param intent The intent with action {@link Telephony.Intents#SPN_STRINGS_UPDATED_ACTION} 659 * @return The string to use for the plmn, or null if it should not be shown. 660 */ 661 private CharSequence getTelephonySpnFrom(Intent intent) { 662 if (intent.getBooleanExtra(TelephonyIntents.EXTRA_SHOW_SPN, false)) { 663 final String spn = intent.getStringExtra(TelephonyIntents.EXTRA_SPN); 664 if (spn != null) { 665 return spn; 666 } 667 } 668 return null; 669 } 670 671 /** 672 * Remove the given observer's callback. 673 * 674 * @param callback The callback to remove 675 */ 676 public void removeCallback(KeyguardUpdateMonitorCallback callback) { 677 if (DEBUG) Log.v(TAG, "*** unregister callback for " + callback); 678 for (int i = mCallbacks.size() - 1; i >= 0; i--) { 679 if (mCallbacks.get(i).get() == callback) { 680 mCallbacks.remove(i); 681 } 682 } 683 } 684 685 /** 686 * Register to receive notifications about general keyguard information 687 * (see {@link InfoCallback}. 688 * @param callback The callback to register 689 */ 690 public void registerCallback(KeyguardUpdateMonitorCallback callback) { 691 if (DEBUG) Log.v(TAG, "*** register callback for " + callback); 692 // Prevent adding duplicate callbacks 693 for (int i = 0; i < mCallbacks.size(); i++) { 694 if (mCallbacks.get(i).get() == callback) { 695 if (DEBUG) Log.e(TAG, "Object tried to add another callback", 696 new Exception("Called by")); 697 return; 698 } 699 } 700 mCallbacks.add(new WeakReference<KeyguardUpdateMonitorCallback>(callback)); 701 removeCallback(null); // remove unused references 702 sendUpdates(callback); 703 } 704 705 private void sendUpdates(KeyguardUpdateMonitorCallback callback) { 706 // Notify listener of the current state 707 callback.onRefreshBatteryInfo(mBatteryStatus); 708 callback.onTimeChanged(); 709 callback.onRingerModeChanged(mRingMode); 710 callback.onPhoneStateChanged(mPhoneState); 711 callback.onRefreshCarrierInfo(mTelephonyPlmn, mTelephonySpn); 712 callback.onClockVisibilityChanged(); 713 callback.onSimStateChanged(mSimState); 714 } 715 716 public void sendKeyguardVisibilityChanged(boolean showing) { 717 if (DEBUG) Log.d(TAG, "sendKeyguardVisibilityChanged(" + showing + ")"); 718 Message message = mHandler.obtainMessage(MSG_KEYGUARD_VISIBILITY_CHANGED); 719 message.arg1 = showing ? 1 : 0; 720 message.sendToTarget(); 721 } 722 723 public void reportClockVisible(boolean visible) { 724 mClockVisible = visible; 725 mHandler.obtainMessage(MSG_CLOCK_VISIBILITY_CHANGED).sendToTarget(); 726 } 727 728 public IccCardConstants.State getSimState() { 729 return mSimState; 730 } 731 732 /** 733 * Report that the user successfully entered the SIM PIN or PUK/SIM PIN so we 734 * have the information earlier than waiting for the intent 735 * broadcast from the telephony code. 736 * 737 * NOTE: Because handleSimStateChange() invokes callbacks immediately without going 738 * through mHandler, this *must* be called from the UI thread. 739 */ 740 public void reportSimUnlocked() { 741 handleSimStateChange(new SimArgs(IccCardConstants.State.READY)); 742 } 743 744 public CharSequence getTelephonyPlmn() { 745 return mTelephonyPlmn; 746 } 747 748 public CharSequence getTelephonySpn() { 749 return mTelephonySpn; 750 } 751 752 /** 753 * @return Whether the device is provisioned (whether they have gone through 754 * the setup wizard) 755 */ 756 public boolean isDeviceProvisioned() { 757 return mDeviceProvisioned; 758 } 759 760 public int getFailedUnlockAttempts() { 761 return mFailedAttempts; 762 } 763 764 public void clearFailedUnlockAttempts() { 765 mFailedAttempts = 0; 766 mFailedBiometricUnlockAttempts = 0; 767 } 768 769 public void reportFailedUnlockAttempt() { 770 mFailedAttempts++; 771 } 772 773 public boolean isClockVisible() { 774 return mClockVisible; 775 } 776 777 public int getPhoneState() { 778 return mPhoneState; 779 } 780 781 public void reportFailedBiometricUnlockAttempt() { 782 mFailedBiometricUnlockAttempts++; 783 } 784 785 public boolean getMaxBiometricUnlockAttemptsReached() { 786 return mFailedBiometricUnlockAttempts >= FAILED_BIOMETRIC_UNLOCK_ATTEMPTS_BEFORE_BACKUP; 787 } 788 789 public boolean isAlternateUnlockEnabled() { 790 return mAlternateUnlockEnabled; 791 } 792 793 public void setAlternateUnlockEnabled(boolean enabled) { 794 mAlternateUnlockEnabled = enabled; 795 } 796 797 public boolean isSimLocked() { 798 return isSimLocked(mSimState); 799 } 800 801 public static boolean isSimLocked(IccCardConstants.State state) { 802 return state == IccCardConstants.State.PIN_REQUIRED 803 || state == IccCardConstants.State.PUK_REQUIRED 804 || state == IccCardConstants.State.PERM_DISABLED; 805 } 806 807 public boolean isSimPinSecure() { 808 return isSimPinSecure(mSimState); 809 } 810 811 public static boolean isSimPinSecure(IccCardConstants.State state) { 812 final IccCardConstants.State simState = state; 813 return (simState == IccCardConstants.State.PIN_REQUIRED 814 || simState == IccCardConstants.State.PUK_REQUIRED 815 || simState == IccCardConstants.State.PERM_DISABLED); 816 } 817} 818