private void dismissKeyguardOnNextActivity() {
   try {
     WindowManagerGlobal.getWindowManagerService().dismissKeyguard();
   } catch (RemoteException e) {
     Log.w(TAG, "Error dismissing keyguard", e);
   }
 }
    void attach(IWallpaperEngineWrapper wrapper) {
      if (DEBUG) Log.v(TAG, "attach: " + this + " wrapper=" + wrapper);
      if (mDestroyed) {
        return;
      }

      mIWallpaperEngine = wrapper;
      mCaller = wrapper.mCaller;
      mConnection = wrapper.mConnection;
      mWindowToken = wrapper.mWindowToken;
      mSurfaceHolder.setSizeFromLayout();
      mInitializing = true;
      mSession = WindowManagerGlobal.getWindowSession(getMainLooper());

      mWindow.setSession(mSession);

      mScreenOn = ((PowerManager) getSystemService(Context.POWER_SERVICE)).isScreenOn();

      IntentFilter filter = new IntentFilter();
      filter.addAction(Intent.ACTION_SCREEN_ON);
      filter.addAction(Intent.ACTION_SCREEN_OFF);
      registerReceiver(mReceiver, filter);

      if (DEBUG) Log.v(TAG, "onCreate(): " + this);
      onCreate(mSurfaceHolder);

      mInitializing = false;
      mReportedVisible = false;
      updateSurface(false, false, false);
    }
  @Override
  protected void onAttachedToWindow() {
    super.onAttachedToWindow();

    mSettingsObserver.observe();
    mDoubleTapGesture =
        new GestureDetector(
            mContext,
            new GestureDetector.SimpleOnGestureListener() {
              @Override
              public boolean onDoubleTap(MotionEvent e) {
                PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
                Log.d(TAG, "Gesture!!");
                if (pm != null) pm.goToSleep(e.getEventTime());
                else Log.d(TAG, "getSystemService returned null PowerManager");

                return true;
              }
            });

    // We really need to be able to animate while window animations are going on
    // so that activities may be started asynchronously from panel animations
    final ViewRootImpl root = getViewRootImpl();
    if (root != null) {
      root.setDrawDuringWindowsAnimating(true);
    }

    // We need to ensure that our window doesn't suffer from overdraw which would normally
    // occur if our window is translucent. Since we are drawing the whole window anyway with
    // the scrim, we don't need the window to be cleared in the beginning.
    if (mService.isScrimSrcModeEnabled()) {
      IBinder windowToken = getWindowToken();
      WindowManager.LayoutParams lp = (WindowManager.LayoutParams) getLayoutParams();
      lp.token = windowToken;
      setLayoutParams(lp);
      WindowManagerGlobal.getInstance().changeCanvasOpacity(windowToken, true);
      setWillNotDraw(false);
    } else {
      setWillNotDraw(!DEBUG);
    }
  }
 private void showBugreportDialog() {
   final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
   builder.setPositiveButton(
       com.android.internal.R.string.report,
       new OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog, int which) {
           if (which == DialogInterface.BUTTON_POSITIVE) {
             // Add a little delay before executing, to give the
             // dialog a chance to go away before it takes a
             // screenshot.
             mHandler.postDelayed(
                 new Runnable() {
                   @Override
                   public void run() {
                     try {
                       ActivityManagerNative.getDefault().requestBugReport();
                     } catch (RemoteException e) {
                     }
                   }
                 },
                 500);
           }
         }
       });
   builder.setMessage(com.android.internal.R.string.bugreport_message);
   builder.setTitle(com.android.internal.R.string.bugreport_title);
   builder.setCancelable(true);
   final Dialog dialog = builder.create();
   dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
   try {
     WindowManagerGlobal.getWindowManagerService().dismissKeyguard();
   } catch (RemoteException e) {
   }
   dialog.show();
 }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.prefs_powermenu);
    /*
    mShowPowerSaver = (CheckBoxPreference) findPreference(PREF_POWER_SAVER);
    int powerSaverVal = 0;
    try {
        powerSaverVal = Settings.Secure.getInt(mContentRes,
         Settings.Secure.POWER_SAVER_MODE);
    } catch (SettingNotFoundException e) {
        mShowPowerSaver.setEnabled(false);
        mShowPowerSaver
                .setSummary("You need to enable power saver before you can see it in the power menu.");
    }
    mShowPowerSaver.setChecked(powerSaverVal != 0); */

    mShowTorchToggle = (CheckBoxPreference) findPreference(PREF_TORCH_TOGGLE);
    mShowTorchToggle.setChecked(
        Settings.System.getBoolean(
            mContentRes, Settings.System.POWER_DIALOG_SHOW_TORCH_TOGGLE, false));

    mShowScreenShot = (CheckBoxPreference) findPreference(PREF_SCREENSHOT);
    mShowScreenShot.setChecked(
        Settings.System.getBoolean(
            mContentRes, Settings.System.POWER_DIALOG_SHOW_SCREENSHOT, true));

    mShowAirplaneToggle = (CheckBoxPreference) findPreference(PREF_AIRPLANE_TOGGLE);
    mShowAirplaneToggle.setChecked(
        Settings.System.getBoolean(
            mContentRes, Settings.System.POWER_DIALOG_SHOW_AIRPLANE_TOGGLE, false));

    mShowNavBarHide = (CheckBoxPreference) findPreference(PREF_NAVBAR_HIDE);
    mShowNavBarHide.setChecked(
        Settings.System.getBoolean(
            mContentRes, Settings.System.POWER_DIALOG_SHOW_NAVBAR_HIDE, false));

    mShowVolumeStateToggle = (CheckBoxPreference) findPreference(PREF_VOLUME_STATE_TOGGLE);
    mShowVolumeStateToggle.setChecked(
        Settings.System.getBoolean(
            mContentRes, Settings.System.POWER_DIALOG_SHOW_VOLUME_STATE_TOGGLE, true));

    mShowRebootKeyguard = (CheckBoxPreference) findPreference(PREF_REBOOT_KEYGUARD);
    mShowRebootKeyguard.setChecked(
        Settings.System.getBoolean(
            mContentRes, Settings.System.POWER_DIALOG_SHOW_REBOOT_KEYGUARD, true));

    PreferenceScreen prefSet = getPreferenceScreen();
    mExpandedDesktopPref = (ListPreference) prefSet.findPreference(KEY_EXPANDED_DESKTOP);
    mExpandedDesktopNoNavbarPref =
        (CheckBoxPreference) findPreference(KEY_EXPANDED_DESKTOP_NO_NAVBAR);

    int expandedDesktopValue =
        Settings.System.getInt(getContentResolver(), Settings.System.EXPANDED_DESKTOP_STYLE, 0);

    // Hide no-op "Status bar visible" mode on devices without navbar
    try {
      if (WindowManagerGlobal.getWindowManagerService().hasNavigationBar()) {
        mExpandedDesktopPref.setOnPreferenceChangeListener(this);
        mExpandedDesktopPref.setValue(String.valueOf(expandedDesktopValue));
        updateExpandedDesktop(expandedDesktopValue);

        prefSet.removePreference(mExpandedDesktopNoNavbarPref);
      } else {
        mExpandedDesktopNoNavbarPref.setChecked(expandedDesktopValue > 0);

        prefSet.removePreference(mExpandedDesktopPref);
      }
    } catch (RemoteException e) {
      Log.e(TAG, "Error getting navigation bar status");
    }
  }