Android4.4之Keyguard--KeyguardViewManager

KeyguardViewManager

private ViewManagerHost mKeyguardHost;//Keyguard's root view
private KeyguardHostView mKeyguardView;

public ViewManagerHost(Context context) {
    super(context);
    setBackground(mBackgroundDrawable);
    //save Orientation、ScreenWidth and ScreenHeight
    mCreateOrientation = context.getResources().getConfiguration().orientation;
    mCreateScreenWidthDp = context.getResources().getConfiguration().screenWidthDp;
    mCreateScreenHeightDp = context.getResources().getConfiguration().screenHeightDp;
}

//only when Orientation、ScreenWidth or ScreenHeight changed and ViewManagerHost is VISIBLE, force to recreate view
protected void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (mCreateOrientation != newConfig.orientation
        || mCreateScreenWidthDp != newConfig.screenWidthDp
        || mCreateScreenHeightDp != newConfig.screenHeightDp) {
        if (mKeyguardHost.getVisibility() == View.VISIBLE) {
            maybeCreateKeyguardLocked(KeyguardUtils.shouldEnableScreenRotation(mContext), true, null);
        } 
    } 
}

//KeyguardHostView will handle back and menu event and then dispatchKeyEvent
public boolean dispatchKeyEvent(KeyEvent event) {
    if (mKeyguardView != null) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            int keyCode = event.getKeyCode();
            if (keyCode == KeyEvent.KEYCODE_BACK && mKeyguardView.handleBackKey()) {
                return true;
            } else if (keyCode == KeyEvent.KEYCODE_MENU && mKeyguardView.handleMenuKey()) {
                return true;
            }
        }
        return mKeyguardView.dispatchKeyEvent(event);
    }
    return super.dispatchKeyEvent(event);
}


private void maybeCreateKeyguardLocked(boolean enableScreenRotation, boolean force, Bundle options) {
    if (mKeyguardHost == null) {
        //create ViewManagerHost and add it
        mKeyguardHost = new ViewManagerHost(mContext);
        //WindowManager.LayoutParams lp   mViewManager is WindowManager (acturally is WindowManagerGlobal)
        mViewManager.addView(mKeyguardHost, lp);

        KeyguardUpdateMonitor.getInstance(mContext).registerCallback(mBackgroundChanger);
    }

    if (force && mKeyguardView != null) {
        mKeyguardView.cleanUp();
    }

    if (force || mKeyguardView == null) {
        mKeyguardHost.setCustomBackground(null);
        mKeyguardHost.removeAllViews();
        //create KeyguardHostView
        inflateKeyguardView(options);
        mKeyguardView.requestFocus();
    }
}

private void inflateKeyguardView(Bundle options) {
    //KeyguardHostView's view id and layout id
    int resId = R.id.keyguard_host_view;
    int layoutId = R.layout.keyguard_host_view;
    View v = mKeyguardHost.findViewById(resId);
    //if there is KeyguardHostView, remove it 
    if (v != null) {
        mKeyguardHost.removeView(v);
    }

    //save Orientation,ScreenWidth and ScreenHeight
    mCreateOrientation = mContext.getResources().getConfiguration().orientation;
    mCreateScreenWidthDp = mContext.getResources().getConfiguration().screenWidthDp;
    mCreateScreenHeightDp = mContext.getResources().getConfiguration().screenHeightDp;

    //inflate KeyguardHostView and ViewGroup is ViewManagerHost, and attachToRoot is true
    final LayoutInflater inflater = LayoutInflater.from(mContext);
    View view = inflater.inflate(layoutId, mKeyguardHost, true);
    mKeyguardView = (KeyguardHostView) view.findViewById(resId);

    //set LockPatternUtils and ViewMediatorCallback
    mKeyguardView.setLockPatternUtils(mLockPatternUtils);
    mKeyguardView.setViewMediatorCallback(mViewMediatorCallback);
}


//Only ViewManagerHost is VISIBLE means is showing
public synchronized boolean isShowing() {
    return (mKeyguardHost != null && mKeyguardHost.getVisibility() == View.VISIBLE);
}


//KeyguardViewManager.java
//when screen is on, just call KeyguardHostView's dismiss
public synchronized void dismiss() {
    if (mScreenOn) {
        mKeyguardView.dismiss();
    }
}

//KeyguardHostView.java
public void dismiss() { showNextSecurityScreenOrFinish(false, true);}


public synchronized void hide() {
    // 1st: set ViewManagerHost GONE
    if (mKeyguardHost != null) {
        mKeyguardHost.setVisibility(View.GONE);

        // Don't do this right away, so we can let the view continue to animate as it goes away.
        if (mKeyguardView != null) {
            final KeyguardViewBase lastView = mKeyguardView;
            mKeyguardView = null;
            mKeyguardHost.postDelayed(new Runnable() {
                @Override
                public void run() {
                    synchronized (KeyguardViewManager.this) {
                        //2nd: KeyguardHostView cleanUp
                        lastView.cleanUp();
                        mKeyguardHost.setCustomBackground(null);
                        updateShowWallpaper(true);
                        //3nd: remove KeyguardHostView from ViewManagerHost
                        mKeyguardHost.removeView(lastView);
                        mViewMediatorCallback.keyguardGone();
                    }
                }
            }, HIDE_KEYGUARD_DELAY);
        }
    }
}

//
public synchronized void show(Bundle options) {
    maybeCreateKeyguardLocked(enableScreenRotation, false, options);

    mKeyguardHost.setVisibility(View.VISIBLE);
    mKeyguardView.show();
    mKeyguardView.requestFocus();
}
//KeyguardHostView.java
public void show() {
    showPrimarySecurityScreen(false);
}

public synchronized void verifyUnlock() {
    show(null);
    mKeyguardView.verifyUnlock();
}

public synchronized void onScreenTurnedOff() {
    mScreenOn = false;
    if (mKeyguardView != null) {
        mKeyguardView.onScreenTurnedOff();
    }
}

public synchronized void onScreenTurnedOn(final IKeyguardShowCallback callback) {
    mScreenOn = true;
    if (mKeyguardView != null) {
        mKeyguardView.onScreenTurnedOn();
    } else if (callback != null) {
	//this callback seems has empty implement
	callback.onShown(token);
    }
}

//KeyguardHostView.java
public void onScreenTurnedOn() {
    mScreenOn = true;
    showPrimarySecurityScreen(false);
    getSecurityView(mCurrentSecuritySelection).onResume(KeyguardSecurityView.SCREEN_ON);
}

public void onScreenTurnedOff() {
    mScreenOn = false;
    showPrimarySecurityScreen(true);
    getSecurityView(mCurrentSecuritySelection).onPause();
}




你可能感兴趣的:(Android4.4之Keyguard--KeyguardViewManager)