private void applyKeyguardFlags(State state) {
   if (state.keyguardShowing) {
     mLpChanged.flags |= WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
     mLpChanged.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
   } else {
     mLpChanged.flags &= ~WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
     mLpChanged.privateFlags &= ~WindowManager.LayoutParams.PRIVATE_FLAG_KEYGUARD;
   }
 }
 private void applyForceStatusBarVisibleFlag(State state) {
   if (state.forceStatusBarVisible) {
     mLpChanged.privateFlags |=
         WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_STATUS_BAR_VISIBLE_TRANSPARENT;
   } else {
     mLpChanged.privateFlags &=
         ~WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_STATUS_BAR_VISIBLE_TRANSPARENT;
   }
 }
  private static final View createScrim(Context context) {
    View view = new View(context);

    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;

    final int stretch = ViewGroup.LayoutParams.MATCH_PARENT;
    final int type = WindowManager.LayoutParams.TYPE_KEYGUARD_SCRIM;
    WindowManager.LayoutParams lp =
        new WindowManager.LayoutParams(stretch, stretch, type, flags, PixelFormat.TRANSLUCENT);
    lp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
    lp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
    lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_FAKE_HARDWARE_ACCELERATED;
    lp.setTitle("KeyguardScrim");
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    wm.addView(view, lp);
    // Disable pretty much everything in statusbar until keyguard comes back and we know
    // the state of the world.
    view.setSystemUiVisibility(
        View.STATUS_BAR_DISABLE_HOME
            | View.STATUS_BAR_DISABLE_BACK
            | View.STATUS_BAR_DISABLE_RECENT
            | View.STATUS_BAR_DISABLE_EXPAND
            | View.STATUS_BAR_DISABLE_SEARCH);
    return view;
  }
 private WindowManager.LayoutParams generateLayoutParam() {
   WindowManager.LayoutParams lp =
       new WindowManager.LayoutParams(
           ViewGroup.LayoutParams.MATCH_PARENT,
           ViewGroup.LayoutParams.MATCH_PARENT,
           WindowManager.LayoutParams.TYPE_STATUS_BAR_PANEL,
           WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
               | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
               | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
               | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
           PixelFormat.TRANSLUCENT);
   // Turn on hardware acceleration for high end gfx devices.
   if (ActivityManager.isHighEndGfx()) {
     lp.flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
     lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_FORCE_HARDWARE_ACCELERATED;
   }
   // This title is for debugging only. See: dumpsys window
   lp.setTitle("PieControlPanel");
   lp.windowAnimations = android.R.style.Animation;
   lp.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_BEHIND;
   return lp;
 }
  /** Show the keyguard. Will handle creating and attaching to the view manager lazily. */
  public synchronized void show() {
    if (DEBUG) Log.d(TAG, "show(); mKeyguardView==" + mKeyguardView);

    Resources res = mContext.getResources();
    boolean enableScreenRotation =
        SystemProperties.getBoolean("lockscreen.rot_override", false)
            || res.getBoolean(R.bool.config_enableLockScreenRotation);
    enableScreenRotation =
        Settings.System.getInt(
                mContext.getContentResolver(),
                Settings.System.LOCKSCREEN_LANDSCAPE,
                enableScreenRotation ? 1 : 0)
            == 1;
    if (mKeyguardHost == null) {
      if (DEBUG) Log.d(TAG, "keyguard host is null, creating it...");

      mKeyguardHost = new KeyguardViewHost(mContext, mCallback);

      final int stretch = ViewGroup.LayoutParams.MATCH_PARENT;
      int flags =
          WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN
              | WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER
              | WindowManager.LayoutParams.FLAG_KEEP_SURFACE_WHILE_ANIMATING
          /*| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
          | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR*/ ;
      if (!mNeedsInput) {
        flags |= WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
      }
      if (ActivityManager.isHighEndGfx(
          ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE))
              .getDefaultDisplay())) {
        flags |= WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
      }
      WindowManager.LayoutParams lp =
          new WindowManager.LayoutParams(
              stretch,
              stretch,
              WindowManager.LayoutParams.TYPE_KEYGUARD,
              flags,
              PixelFormat.TRANSLUCENT);
      lp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
      lp.windowAnimations = com.android.internal.R.style.Animation_LockScreen;
      if (ActivityManager.isHighEndGfx(
          ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE))
              .getDefaultDisplay())) {
        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;
      lp.setTitle("Keyguard");
      mWindowLayoutParams = lp;

      mViewManager.addView(mKeyguardHost, lp);
    }

    if (enableScreenRotation
        && Settings.System.getInt(
                mContext.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1)
            == 1) {
      if (DEBUG) Log.d(TAG, "Rotation sensor for lock screen On!");
      mWindowLayoutParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR;
    } else {
      if (DEBUG) Log.d(TAG, "Rotation sensor for lock screen Off!");
      mWindowLayoutParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_NOSENSOR;
    }

    mViewManager.updateViewLayout(mKeyguardHost, mWindowLayoutParams);

    if (mKeyguardView == null) {
      if (DEBUG) Log.d(TAG, "keyguard view is null, creating it...");
      mKeyguardView = mKeyguardViewProperties.createKeyguardView(mContext, mUpdateMonitor, this);
      mKeyguardView.setId(R.id.lock_screen);
      mKeyguardView.setCallback(mCallback);

      final ViewGroup.LayoutParams lp =
          new FrameLayout.LayoutParams(
              ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

      mKeyguardHost.addView(mKeyguardView, lp);

      if (mScreenOn) {
        mKeyguardView.show();
      }
    }

    // Disable aspects of the system/status/navigation bars that are not appropriate or
    // useful for the lockscreen 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_BACK | View.STATUS_BAR_DISABLE_HOME);
    mKeyguardHost.setSystemUiVisibility(visFlags);

    mViewManager.updateViewLayout(mKeyguardHost, mWindowLayoutParams);
    mKeyguardHost.setVisibility(View.VISIBLE);
    mKeyguardView.requestFocus();
  }
    void updateSurface(boolean forceRelayout, boolean forceReport, boolean redrawNeeded) {
      if (mDestroyed) {
        Log.w(TAG, "Ignoring updateSurface: destroyed");
      }

      int myWidth = mSurfaceHolder.getRequestedWidth();
      if (myWidth <= 0) myWidth = ViewGroup.LayoutParams.MATCH_PARENT;
      int myHeight = mSurfaceHolder.getRequestedHeight();
      if (myHeight <= 0) myHeight = ViewGroup.LayoutParams.MATCH_PARENT;

      final boolean creating = !mCreated;
      final boolean surfaceCreating = !mSurfaceCreated;
      final boolean formatChanged = mFormat != mSurfaceHolder.getRequestedFormat();
      boolean sizeChanged = mWidth != myWidth || mHeight != myHeight;
      final boolean typeChanged = mType != mSurfaceHolder.getRequestedType();
      final boolean flagsChanged =
          mCurWindowFlags != mWindowFlags || mCurWindowPrivateFlags != mWindowPrivateFlags;
      if (forceRelayout
          || creating
          || surfaceCreating
          || formatChanged
          || sizeChanged
          || typeChanged
          || flagsChanged
          || redrawNeeded
          || !mIWallpaperEngine.mShownReported) {

        if (DEBUG)
          Log.v(
              TAG,
              "Changes: creating="
                  + creating
                  + " format="
                  + formatChanged
                  + " size="
                  + sizeChanged);

        try {
          mWidth = myWidth;
          mHeight = myHeight;
          mFormat = mSurfaceHolder.getRequestedFormat();
          mType = mSurfaceHolder.getRequestedType();

          mLayout.x = 0;
          mLayout.y = 0;
          mLayout.width = myWidth;
          mLayout.height = myHeight;

          mLayout.format = mFormat;

          mCurWindowFlags = mWindowFlags;
          mLayout.flags =
              mWindowFlags
                  | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
                  | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                  | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
          mCurWindowPrivateFlags = mWindowPrivateFlags;
          mLayout.privateFlags = mWindowPrivateFlags;

          mLayout.memoryType = mType;
          mLayout.token = mWindowToken;

          if (!mCreated) {
            mLayout.type = mIWallpaperEngine.mWindowType;
            mLayout.gravity = Gravity.START | Gravity.TOP;
            mLayout.setTitle(WallpaperService.this.getClass().getName());
            mLayout.windowAnimations = com.android.internal.R.style.Animation_Wallpaper;
            mInputChannel = new InputChannel();
            if (mSession.addToDisplay(
                    mWindow,
                    mWindow.mSeq,
                    mLayout,
                    View.VISIBLE,
                    Display.DEFAULT_DISPLAY,
                    mContentInsets,
                    mInputChannel)
                < 0) {
              Log.w(TAG, "Failed to add window while updating wallpaper surface.");
              return;
            }
            mCreated = true;

            mInputEventReceiver = new WallpaperInputEventReceiver(mInputChannel, Looper.myLooper());
          }

          mSurfaceHolder.mSurfaceLock.lock();
          mDrawingAllowed = true;

          final int relayoutResult =
              mSession.relayout(
                  mWindow,
                  mWindow.mSeq,
                  mLayout,
                  mWidth,
                  mHeight,
                  View.VISIBLE,
                  0,
                  mWinFrame,
                  mContentInsets,
                  mVisibleInsets,
                  mConfiguration,
                  mSurfaceHolder.mSurface);

          if (DEBUG) Log.v(TAG, "New surface: " + mSurfaceHolder.mSurface + ", frame=" + mWinFrame);

          int w = mWinFrame.width();
          if (mCurWidth != w) {
            sizeChanged = true;
            mCurWidth = w;
          }
          int h = mWinFrame.height();
          if (mCurHeight != h) {
            sizeChanged = true;
            mCurHeight = h;
          }

          mSurfaceHolder.setSurfaceFrameSize(w, h);
          mSurfaceHolder.mSurfaceLock.unlock();

          if (!mSurfaceHolder.mSurface.isValid()) {
            reportSurfaceDestroyed();
            if (DEBUG) Log.v(TAG, "Layout: Surface destroyed");
            return;
          }

          boolean didSurface = false;

          try {
            mSurfaceHolder.ungetCallbacks();

            if (surfaceCreating) {
              mIsCreating = true;
              didSurface = true;
              if (DEBUG) Log.v(TAG, "onSurfaceCreated(" + mSurfaceHolder + "): " + this);
              onSurfaceCreated(mSurfaceHolder);
              SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
              if (callbacks != null) {
                for (SurfaceHolder.Callback c : callbacks) {
                  c.surfaceCreated(mSurfaceHolder);
                }
              }
            }

            redrawNeeded |=
                creating || (relayoutResult & WindowManagerGlobal.RELAYOUT_RES_FIRST_TIME) != 0;

            if (forceReport || creating || surfaceCreating || formatChanged || sizeChanged) {
              if (DEBUG) {
                RuntimeException e = new RuntimeException();
                e.fillInStackTrace();
                Log.w(
                    TAG,
                    "forceReport="
                        + forceReport
                        + " creating="
                        + creating
                        + " formatChanged="
                        + formatChanged
                        + " sizeChanged="
                        + sizeChanged,
                    e);
              }
              if (DEBUG)
                Log.v(
                    TAG,
                    "onSurfaceChanged("
                        + mSurfaceHolder
                        + ", "
                        + mFormat
                        + ", "
                        + mCurWidth
                        + ", "
                        + mCurHeight
                        + "): "
                        + this);
              didSurface = true;
              onSurfaceChanged(mSurfaceHolder, mFormat, mCurWidth, mCurHeight);
              SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
              if (callbacks != null) {
                for (SurfaceHolder.Callback c : callbacks) {
                  c.surfaceChanged(mSurfaceHolder, mFormat, mCurWidth, mCurHeight);
                }
              }
            }

            if (redrawNeeded) {
              onSurfaceRedrawNeeded(mSurfaceHolder);
              SurfaceHolder.Callback callbacks[] = mSurfaceHolder.getCallbacks();
              if (callbacks != null) {
                for (SurfaceHolder.Callback c : callbacks) {
                  if (c instanceof SurfaceHolder.Callback2) {
                    ((SurfaceHolder.Callback2) c).surfaceRedrawNeeded(mSurfaceHolder);
                  }
                }
              }
            }

            if (didSurface && !mReportedVisible) {
              // This wallpaper is currently invisible, but its
              // surface has changed.  At this point let's tell it
              // again that it is invisible in case the report about
              // the surface caused it to start running.  We really
              // don't want wallpapers running when not visible.
              if (mIsCreating) {
                // Some wallpapers will ignore this call if they
                // had previously been told they were invisble,
                // so if we are creating a new surface then toggle
                // the state to get them to notice.
                if (DEBUG) Log.v(TAG, "onVisibilityChanged(true) at surface: " + this);
                onVisibilityChanged(true);
              }
              if (DEBUG) Log.v(TAG, "onVisibilityChanged(false) at surface: " + this);
              onVisibilityChanged(false);
            }

          } finally {
            mIsCreating = false;
            mSurfaceCreated = true;
            if (redrawNeeded) {
              mSession.finishDrawing(mWindow);
            }
            mIWallpaperEngine.reportShown();
          }
        } catch (RemoteException ex) {
        }
        if (DEBUG)
          Log.v(
              TAG,
              "Layout: x="
                  + mLayout.x
                  + " y="
                  + mLayout.y
                  + " w="
                  + mLayout.width
                  + " h="
                  + mLayout.height);
      }
    }
  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);
  }