Code snippets about android locks screen

记录了系统锁屏控制的代码片段, 可以从比较高的层次进行锁屏控制。

各种上层接口会调用到KeyguardViewMediator::doKeyguardLocked()进行锁屏。Mediator设计模式?

In doKeyguardLocked()@KeyguardViewMediator.java
It can be seen that if system reboots abnormally, and nothing disable the screen lock, and sim card sits there, the first lock screen will be ignored.

650    private void KeyguardViewMediator::doKeyguardLocked() {

651        // if another app is disabling us, don't show

652        if (!mExternallyEnabled) {

653            if (DEBUG) Log.d(TAG, "doKeyguard: not showing because externally disabled");

654

655            // note: we *should* set mNeedToReshowWhenReenabled=true here, but that makes

656            // for an occasional ugly flicker in this situation:

657            // 1) receive a call with the screen on (no keyguard) or make a call

658            // 2) screen times out

659            // 3) user hits key to turn screen back on

660            // instead, we reenable the keyguard when we know the screen is off and the call

661            // ends (see the broadcast receiver below)

662            // TODO: clean this up when we have better support at the window manager level

663            // for apps that wish to be on top of the keyguard

664            return;

665        }

666

667        // if the keyguard is already showing, don't bother

668        if (mKeyguardViewManager.isShowing()) {

669            if (DEBUG) Log.d(TAG, "doKeyguard: not showing because it is already showing");

670            return;

671        }

672

673        // if the setup wizard hasn't run yet, don't show

674        final boolean provisioned = mUpdateMonitor.isDeviceProvisioned();

675        final IccCard.State[] state;

676        int numPhones = TelephonyManager.getDefault().getPhoneCount();

677        state = new IccCard.State[numPhones];

678        boolean lockedOrMissing = false;

679        boolean isPinLockedFlag = false;



680        for (int i = 0; i < numPhones; i++) {

681            state[i] = mUpdateMonitor.getSimState(i);

682            lockedOrMissing = lockedOrMissing || isLockedOrMissing(state[i]);

683            isPinLockedFlag = isPinLockedFlag || isPinLockedFun(state[i]);

684            if (lockedOrMissing) break;

685        }

686

687        if (!lockedOrMissing && !provisioned) {

688            if (DEBUG) Log.d(TAG, "doKeyguard: not showing because device isn't provisioned"

689                    + " and the sim is not locked or missing");

690            return;

691        }

692

693        if (mLockPatternUtils.isLockScreenDisabled() && !lockedOrMissing) {

694            if (DEBUG) Log.d(TAG, "doKeyguard: not showing because lockscreen is off");

695            return;

696        }

697

698

699        mySingleton obj = mySingleton.getInstance();

700        if (obj.isAbnormalBootup()) {

701            if(obj.getAbnormal()==0)// avoid 1st abnormal screenLock

702             {

703                  obj.addAbnormal();// set to true

704                  Log.d(TAG, "KeyGuardViewMediator.java doKeyguardLocked() obj.getAbnormal()="+obj.getAbnormal());

705                  return ;

706             }

707

708            // abnormal reboot and PIN_REQUIRED will not show Locked screen , if pin pass , isPinLockedFlag turn false , will unlock this "if()"

709             if(isPinLockedFlag && obj.getAbnormal()==1){

710                 obj.addAbnormal();// set to true

711                  Log.d(TAG, "KeyGuardViewMediator.java doKeyguardLocked() obj.getAbnormal()="+obj.getAbnormal());

712                 return;

713              }

714             Log.d(TAG, "Showing the lock screen");

715      }

716

717

718        if (DEBUG) Log.d(TAG, "doKeyguard: showing the lock screen");

719        showLocked();

720    }

calls ==>

798    /**

799     * Send message to keyguard telling it to show itself

800     * @see #handleShow()

801     */

802    private void showLocked() {

803        if (DEBUG) Log.d(TAG, "showLocked");

804        // ensure we stay awake until we are finished displaying the keyguard

805        mShowKeyguardWakeLock.acquire();

806        Message msg = mHandler.obtainMessage(SHOW);

807        mHandler.sendMessage(msg);

808    }

message handler |=>

1104    /**

1105     * This handler will be associated with the policy thread, which will also

1106     * be the UI thread of the keyguard.  Since the apis of the policy, and therefore

1107     * this class, can be called by other threads, any action that directly

1108     * interacts with the keyguard ui should be posted to this handler, rather

1109     * than called directly.

1110     */

1111    private Handler mHandler = new Handler() {

1112        @Override

1113        public void handleMessage(Message msg) {

1114            switch (msg.what) {

1115                case TIMEOUT:

1116                    handleTimeout(msg.arg1);

1117                    return ;

1118                case SHOW:

1119                    handleShow();

1120                    return ;

1121                case HIDE:

1122                    handleHide();

1123                    return ;

1124                case RESET:

1125                    handleReset();

1126                    return ;

1127                case VERIFY_UNLOCK:

1128                    handleVerifyUnlock();

1129                    return;

1130                case NOTIFY_SCREEN_OFF:

1131                    handleNotifyScreenOff();

1132                    return;

1133                case NOTIFY_SCREEN_ON:

1134                    handleNotifyScreenOn((KeyguardViewManager.ShowListener)msg.obj);

1135                    return;

1136                case WAKE_WHEN_READY:

1137                    handleWakeWhenReady(msg.arg1);

1138                    return;

1139                case KEYGUARD_DONE:

1140                

1141                    ScreenStatusNotification(KEYGUARD_DONE);

1142                    

1143                    handleKeyguardDone(msg.arg1 != 0);

1144                    return;

1145                case KEYGUARD_DONE_DRAWING:

1146                    handleKeyguardDoneDrawing();

1147                    return;

1148                case KEYGUARD_DONE_AUTHENTICATING:

1149                    keyguardDone(true);

1150                    return;

1151                case SET_HIDDEN:

1152                    handleSetHidden(msg.arg1 != 0);

1153                    break;

1154                case KEYGUARD_TIMEOUT:

1155                    synchronized (KeyguardViewMediator.this) {

1156                        doKeyguardLocked();

1157                    }

1158                    break;

1159            }

1160        }

1161    };

calls ==>

1248    /**

1249     * Handle message sent by {@link #showLocked}.

1250     * @see #SHOW

1251     */

1252    private void handleShow() {

1253        synchronized (KeyguardViewMediator.this) {

1254            if (DEBUG) Log.d(TAG, "handleShow");

1255            if (!mSystemReady) return;

1256

1257            mKeyguardViewManager.show();

1258            mShowing = true;

1259            updateActivityLockScreenState();

1260            adjustUserActivityLocked();

1261            adjustStatusBarLocked();

1262            try {

1263                ActivityManagerNative.getDefault().closeSystemDialogs("lock");

1264            } catch (RemoteException e) {

1265            }

1266

1267            // Do this at the end to not slow down display of the keyguard.

1268            playSounds(true);

1269

1270            mShowKeyguardWakeLock.release();

1271        }

1272    }

calls==>

KeyguardViewManager::show() to show lock screen.

 

END


 

 

 

你可能感兴趣的:(android)