Example #1
0
  public static void gotoSleepIfDoubleTap(Context context, MotionEvent event) {
    if (sDoubleTapAreaRadius == -1) {
      sDoubleTapAreaRadius =
          context.getResources().getDimensionPixelSize(R.dimen.double_tap_area_radius);
      if (DebugLog.DEBUG)
        DebugLog.d(TAG, "gotoSleepIfDoubleTap sDoubleTapAreaRadius:" + sDoubleTapAreaRadius);
    }
    int action = event.getAction();
    if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_UP) {
      if (action == MotionEvent.ACTION_DOWN && sEventCount % 2 != 0) {
        sEventCount--;
      }
      sEventCount++;
    }
    if (sEventCount == SECOND_DOWN_ACTION) { // second down action
      long timeInterval = SystemClock.elapsedRealtime() - mLastClickTime;
      boolean isInDoubleTapArea = isDoubleTapDistanceShortEnough(event);
      if (timeInterval > DOUBLE_TAP_MAX_TIME || !isInDoubleTapArea) {
        sEventCount = 1;
      }
    }
    if (sEventCount == FIRST_DOWN_ACTION) { // first down action
      mLastClickTime = SystemClock.elapsedRealtime();
    }

    if (DebugLog.DEBUG)
      DebugLog.d(
          TAG,
          "gotoSleepIfDoubleTap  sEventCount: "
              + sEventCount
              + "  interval: "
              + (SystemClock.elapsedRealtime() - mLastClickTime)
              + "  preDownX: "
              + mPreTapX);
    if (sEventCount % 2 == 0) { // up action
      float downX = event.getX();
      float downY = event.getY();
      if (sEventCount % 4 == 0) { // second up action
        long clickTime = SystemClock.elapsedRealtime();
        long timeInterval = Math.abs(clickTime - mLastClickTime);
        boolean isDoubleTap =
            (timeInterval < DOUBLE_TAP_MAX_TIME && timeInterval > DOUBLE_TAP_MIN_TIME);
        boolean isDoubleTapDistanceShort = isDoubleTapDistanceShortEnough(event);
        if (DebugLog.DEBUG)
          DebugLog.d(
              TAG,
              "isDoubleTap: " + isDoubleTap + "  isInDoubleTapArea: " + isDoubleTapDistanceShort);
        if (isDoubleTap && isDoubleTapDistanceShort) {
          if (DebugLog.DEBUG)
            DebugLog.d(
                TAG,
                "gotoSleepIfDoubleTap()  mLastClickTime: "
                    + mLastClickTime
                    + "  clickTime:"
                    + clickTime);
          PowerManager pw = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
          pw.goToSleep(SystemClock.uptimeMillis());
        }
        mLastClickTime = clickTime;
        sEventCount = 0;
      }
      mPreTapX = downX;
      mPreTapY = downY;
    }
  }
Example #2
0
 @Override
 protected void toggleState() {
   PowerManager pm = (PowerManager) mView.getContext().getSystemService(Context.POWER_SERVICE);
   pm.goToSleep(SystemClock.uptimeMillis() + 1);
 }
 private void goToSleep() {
   enableKeyguard(true);
   mPowerManager.goToSleep(SystemClock.uptimeMillis());
 }
 private void screenOff() {
   PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
   pm.goToSleep(SystemClock.uptimeMillis() + 1);
 }