/** Show the keyguard. Will handle creating and attaching to the view manager lazily. */
  public synchronized void show(Bundle options) {
    if (DEBUG) Log.d(TAG, "show(); mKeyguardView=" + mKeyguardView);

    boolean enableScreenRotation = KeyguardUtils.shouldEnableScreenRotation(mContext);
    if (DEBUG) Log.d(TAG, "show() query screen rotation after");

    /// M: Incoming Indicator for Keyguard Rotation @{
    KeyguardUpdateMonitor.getInstance(mContext).setQueryBaseTime();
    /// @}
    maybeCreateKeyguardLocked(enableScreenRotation, false, options);

    if (DEBUG) Log.d(TAG, "show() maybeCreateKeyguardLocked finish");

    maybeEnableScreenRotation(enableScreenRotation);

    // Disable common aspects of the system/status/navigation bars that are not appropriate or
    // useful on any keyguard screen but can be re-shown by dialogs or SHOW_WHEN_LOCKED
    // activities. Other disabled bits are handled by the KeyguardViewMediator talking
    // directly to the status bar service.
    int visFlags = View.STATUS_BAR_DISABLE_HOME;
    if (shouldEnableTranslucentDecor()) {
      mWindowLayoutParams.flags |=
          WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
              | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
    }
    if (DEBUG) Log.d(TAG, "show:setSystemUiVisibility(" + Integer.toHexString(visFlags) + ")");
    mKeyguardHost.setSystemUiVisibility(visFlags);

    mViewManager.updateViewLayout(mKeyguardHost, mWindowLayoutParams);
    mKeyguardHost.setVisibility(View.VISIBLE);
    mKeyguardView.show();
    mKeyguardView.requestFocus();
    if (DEBUG) Log.d(TAG, "show() exit; mKeyguardView=" + mKeyguardView);
  }
  private void inflateKeyguardView(Bundle options) {
    /// M: add for power-off alarm @{
    int resId = R.id.keyguard_host_view;
    int layoutId = R.layout.keyguard_host_view;
    if (PowerOffAlarmManager.isAlarmBoot()) {
      resId = R.id.power_off_alarm_host_view;
      layoutId = R.layout.mtk_power_off_alarm_host_view;
    }
    /// @}
    View v = mKeyguardHost.findViewById(resId);
    if (v != null) {
      mKeyguardHost.removeView(v);
    }
    /// M: Save new orientation
    mCreateOrientation = mContext.getResources().getConfiguration().orientation;
    mCreateScreenWidthDp = mContext.getResources().getConfiguration().screenWidthDp;
    mCreateScreenHeightDp = mContext.getResources().getConfiguration().screenHeightDp;

    final LayoutInflater inflater = LayoutInflater.from(mContext);
    View view = inflater.inflate(layoutId, mKeyguardHost, true);
    mKeyguardView = (KeyguardHostView) view.findViewById(resId);
    mKeyguardView.setLockPatternUtils(mLockPatternUtils);
    mKeyguardView.setViewMediatorCallback(mViewMediatorCallback);
    mKeyguardView.initializeSwitchingUserState(
        options != null && options.getBoolean(IS_SWITCHING_USER));

    // HACK
    // The keyguard view will have set up window flags in onFinishInflate before we set
    // the view mediator callback. Make sure it knows the correct IME state.
    if (mViewMediatorCallback != null) {
      KeyguardPasswordView kpv =
          (KeyguardPasswordView) mKeyguardView.findViewById(R.id.keyguard_password_view);

      if (kpv != null) {
        mViewMediatorCallback.setNeedsInput(kpv.needsInput());
      }
    }

    if (options != null) {
      int widgetToShow =
          options.getInt(
              LockPatternUtils.KEYGUARD_SHOW_APPWIDGET, AppWidgetManager.INVALID_APPWIDGET_ID);
      if (widgetToShow != AppWidgetManager.INVALID_APPWIDGET_ID) {
        mKeyguardView.goToWidget(widgetToShow);
      }
    }
  }
  /** Hides the keyguard view */
  public synchronized void hide() {
    if (DEBUG) Log.d(TAG, "hide() mKeyguardView=" + mKeyguardView);

    if (mKeyguardHost != null) {
      mKeyguardHost.setVisibility(View.GONE);

      /// M: reset the simpin show next view flag
      mShowNextViewAfterSimLock = false;

      // We really only want to preserve keyguard state for configuration changes. Hence
      // we should clear state of widgets (e.g. Music) when we hide keyguard so it can
      // start with a fresh state when we return.
      mStateContainer.clear();

      // 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) {
                  if (DEBUG) Log.d(TAG, "hide() runnable lastView=" + lastView);
                  lastView.cleanUp();
                  // Let go of any large bitmaps.
                  mKeyguardHost.setCustomBackground(null);
                  updateShowWallpaper(true);
                  mKeyguardHost.removeView(lastView);
                  mViewMediatorCallback.keyguardGone();
                }
              }
            },
            HIDE_KEYGUARD_DELAY);
      }
    }
  }
 @Override
 public void onSetBackground(Bitmap bmp) {
   mKeyguardHost.setCustomBackground(
       bmp != null ? new BitmapDrawable(mContext.getResources(), bmp) : null);
   updateShowWallpaper(bmp == null);
 }
 /** @return Whether the keyguard is showing */
 public synchronized boolean isShowing() {
   return (mKeyguardHost != null && mKeyguardHost.getVisibility() == View.VISIBLE);
 }
  public synchronized void onScreenTurnedOn(final IKeyguardShowCallback callback) {
    if (DEBUG) Log.d(TAG, "onScreenTurnedOn()");
    mScreenOn = true;

    // If keyguard is not showing, we need to inform PhoneWindowManager with a null
    // token so it doesn't wait for us to draw...
    final IBinder token = isShowing() ? mKeyguardHost.getWindowToken() : null;

    if (DEBUG && token == null)
      Slog.v(
          TAG, "send wm null token: " + (mKeyguardHost == null ? "host was null" : "not showing"));

    if (mKeyguardView != null) {
      mKeyguardView.onScreenTurnedOn();

      if (mCreateOrientation != mContext.getResources().getConfiguration().orientation) {
        if (DEBUG)
          Log.d(
              TAG,
              "onScreenTurnedOn orientation is different, recreate it. mCreateOrientation="
                  + mCreateOrientation
                  + ", newConfig="
                  + mContext.getResources().getConfiguration().orientation);
        maybeCreateKeyguardLocked(KeyguardUtils.shouldEnableScreenRotation(mContext), true, null);
      }
      // Caller should wait for this window to be shown before turning
      // on the screen.
      if (callback != null) {
        if (mKeyguardHost.getVisibility() == View.VISIBLE) {
          // Keyguard may be in the process of being shown, but not yet
          // updated with the window manager...  give it a chance to do so.
          if (DEBUG) Log.d(TAG, "onScreenTurnedOn mKeyguardView visible, post runnable");
          mKeyguardHost.post(
              new Runnable() {
                @Override
                public void run() {
                  try {
                    if (DEBUG)
                      Log.d(
                          TAG,
                          "onScreenTurnedOn before callback.onShown() mKeyguardHost is Visible");
                    callback.onShown(token);
                  } catch (RemoteException e) {
                    Slog.w(TAG, "Exception calling onShown():", e);
                  }
                }
              });
        } else {
          try {
            if (DEBUG)
              Log.d(TAG, "onScreenTurnedOn before callback.onShown() mKeyguardHost is NOT Visible");
            callback.onShown(token);
          } catch (RemoteException e) {
            Slog.w(TAG, "Exception calling onShown():", e);
          }
        }
      }
    } else if (callback != null) {
      try {
        if (DEBUG) Log.d(TAG, "onScreenTurnedOn before callback.onShown() mKeyguardView is null");
        callback.onShown(token);
      } catch (RemoteException e) {
        Slog.w(TAG, "Exception calling onShown():", e);
      }
    }
  }
  private void maybeCreateKeyguardLocked(
      boolean enableScreenRotation, boolean force, Bundle options) {
    if (mKeyguardHost != null) {
      mKeyguardHost.saveHierarchyState(mStateContainer);
    }

    if (mKeyguardHost == null) {
      if (DEBUG) Log.d(TAG, "keyguard host is null, creating it...");

      mKeyguardHost = new ViewManagerHost(mContext);

      int flags =
          WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
              | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
              | WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN
              | WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;

      if (!mNeedsInput) {
        flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
      }

      final int stretch = ViewGroup.LayoutParams.MATCH_PARENT;
      final int type = WindowManager.LayoutParams.TYPE_KEYGUARD;
      WindowManager.LayoutParams lp =
          new WindowManager.LayoutParams(stretch, stretch, type, flags, PixelFormat.TRANSLUCENT);
      lp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
      lp.windowAnimations = R.style.Animation_LockScreen;
      lp.screenOrientation =
          enableScreenRotation
              ? ActivityInfo.SCREEN_ORIENTATION_USER
              : ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;

      if (ActivityManager.isHighEndGfx()) {
        lp.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
        lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_HARDWARE_ACCELERATED;
      }
      lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SET_NEEDS_MENU_KEY;
      /// M: Poke user activity when operating Keyguard
      // lp.inputFeatures |= WindowManager.LayoutParams.INPUT_FEATURE_DISABLE_USER_ACTIVITY;
      lp.setTitle("Keyguard");
      mWindowLayoutParams = lp;
      /// M: skip add KeyguardHost into viewManager in AT case
      if (!KeyguardViewMediator.isKeyguardInActivity) {
        mViewManager.addView(mKeyguardHost, lp);
      } else {
        if (DEBUG) Log.d(TAG, "skip add mKeyguardHost into mViewManager for testing");
      }
      KeyguardUpdateMonitor.getInstance(mContext).registerCallback(mBackgroundChanger);
    }

    /// M: If force and keyguardView is not null, we should relase memory hold by old keyguardview
    if (force && mKeyguardView != null) {
      mKeyguardView.cleanUp();
    }

    if (force || mKeyguardView == null) {
      mKeyguardHost.setCustomBackground(null);
      mKeyguardHost.removeAllViews();
      inflateKeyguardView(options);
      mKeyguardView.requestFocus();
    }
    updateUserActivityTimeoutInWindowLayoutParams();
    mViewManager.updateViewLayout(mKeyguardHost, mWindowLayoutParams);

    mKeyguardHost.restoreHierarchyState(mStateContainer);
  }