private void cancelAutoFocus() {
    Log.e(TAG, "cancelAutofocus");
    // Note: CameraController.getFocusMode(); will return
    // 'FOCUS_MODE_AUTO' if actual
    // mode is in fact FOCUS_MODE_CONTINUOUS_PICTURE or
    // FOCUS_MODE_CONTINUOUS_VIDEO
    int fm = CameraController.getFocusMode();
    if (fm != CameraParameters.AF_MODE_UNSUPPORTED) {
      if (fm != preferenceFocusMode && preferenceFocusMode != CameraParameters.MF_MODE) {
        CameraController.cancelAutoFocus();
        CameraController.setCameraFocusMode(preferenceFocusMode);
      }
    }

    // Reset the tap area before calling mListener.cancelAutofocus.
    // Otherwise, focus mode stays at auto and the tap area passed to the
    // driver is not reset.
    CameraController.setCameraFocusAreas(null);
    ApplicationScreen.instance.setCameraMeteringMode(ApplicationScreen.getMeteringMode());
    resetTouchFocus();

    mState = STATE_IDLE;
    CameraController.setFocusState(CameraController.FOCUS_STATE_IDLE);

    updateFocusUI();
    mHandler.removeMessages(RESET_TOUCH_FOCUS);
  }
  // This can only be called after mParameters is initialized.
  public int getFocusMode() {
    if (mOverrideFocusMode != CameraParameters.AF_MODE_UNSUPPORTED) return mOverrideFocusMode;

    if (mFocusAreaSupported && mFocusArea != null) mFocusMode = CameraParameters.AF_MODE_AUTO;
    else mFocusMode = ApplicationScreen.instance.getFocusModePref(mDefaultFocusMode);

    if (!isSupported(mFocusMode, CameraController.getSupportedFocusModes())) {
      // For some reasons, the driver does not support the current
      // focus mode. Fall back to auto.
      if (isSupported(CameraParameters.AF_MODE_AUTO, CameraController.getSupportedFocusModes()))
        mFocusMode = CameraParameters.AF_MODE_AUTO;
      else mFocusMode = CameraController.getFocusMode();
    }
    return mFocusMode;
  }
  public boolean onBroadcast(int arg1, int arg2) {
    if (arg1 == ApplicationInterface.MSG_CONTROL_LOCKED) {
      mFocusDisabled = true;
    } else if (arg1 == ApplicationInterface.MSG_CONTROL_UNLOCKED) {
      mFocusDisabled = false;
    } else if (arg1 == ApplicationInterface.MSG_FOCUS_LOCKED) {
      mFocusLocked = true;
    } else if (arg1 == ApplicationInterface.MSG_FOCUS_UNLOCKED) {
      mFocusLocked = false;
    } else if (arg1 == ApplicationInterface.MSG_CAPTURE_FINISHED) {
      mFocusDisabled = false;
      cancelAutoFocus();
    } else if (arg1 == ApplicationInterface.MSG_FOCUS_CHANGED) {
      int fm = CameraController.getFocusMode();
      if (fm != -1) preferenceFocusMode = fm;
    } else if (arg1 == ApplicationInterface.MSG_PREVIEW_CHANGED) {
      initialize(CameraController.isFrontCamera(), 90);
    }

    return false;
  }
  public void onTouchFocusArea(MotionEvent e) {
    if (!mFocusAreaSupported) return;

    int xRaw = (int) e.getRawX();
    int yRaw = (int) e.getRawY();

    if (e.getPointerCount() > 1) {
      final int location[] = {0, 0};
      focusLayout.getLocationOnScreen(location);
      xRaw = (int) e.getX(0) + location[0];
      yRaw = (int) e.getY(0) + location[1];
    }

    // Initialize variables.
    int focusWidth = mFocusIndicatorRotateLayout.getWidth();
    int focusHeight = mFocusIndicatorRotateLayout.getHeight();
    int previewWidth = mPreviewWidth;
    int previewHeight = mPreviewHeight;
    int displayWidth = ApplicationScreen.getAppResources().getDisplayMetrics().widthPixels;
    int displayHeight = ApplicationScreen.getAppResources().getDisplayMetrics().heightPixels;
    int diffWidth = displayWidth - previewWidth;
    int diffHeight = displayHeight - previewHeight;

    // Initialize variables.
    int paramsLayoutHeight = 0;

    int xOffset = (focusLayout.getWidth() - previewWidth) / 2;
    int yOffset = (focusLayout.getHeight() - previewHeight) / 2;

    if (mFocusArea == null) {
      mFocusArea = new ArrayList<Area>();
      mFocusArea.add(new Area(new Rect(), 1000));
    }

    // Use margin to set the metering indicator to the touched area.
    RelativeLayout.LayoutParams p =
        (RelativeLayout.LayoutParams) mFocusIndicatorRotateLayout.getLayoutParams();
    int left =
        Util.clamp(
            xRaw - focusWidth / 2 + xOffset,
            diffWidth / 2,
            (previewWidth - focusWidth + xOffset * 2) - diffWidth / 2);
    int top =
        Util.clamp(
            yRaw - focusHeight / 2 + yOffset - diffHeight / 2,
            0,
            previewHeight - focusHeight + yOffset * 2);

    p.leftMargin = left;
    p.topMargin = top;

    int[] rules = p.getRules();
    rules[RelativeLayout.CENTER_IN_PARENT] = 0;

    mFocusIndicatorRotateLayout.setLayoutParams(p);

    calculateTapAreaByTopLeft(
        focusWidth,
        focusHeight,
        1f,
        top,
        left,
        ApplicationScreen.getPreviewSurfaceView().getWidth(),
        ApplicationScreen.getPreviewSurfaceView().getHeight(),
        mFocusArea.get(0).rect);

    // Set the focus area and metering area.
    if (mFocusAreaSupported
        && (e.getActionMasked() == MotionEvent.ACTION_UP
            || e.getActionMasked() == MotionEvent.ACTION_POINTER_UP)) {
      setFocusParameters();

      int focusMode = CameraController.getFocusMode();
      if (focusMode == CameraParameters.AF_MODE_AUTO
          || focusMode == CameraParameters.AF_MODE_MACRO) {
        CameraController.cancelAutoFocus();
        autoFocus();
      } else {
        mState = STATE_FOCUSING;
        updateFocusUI();
      }
    }
    mFocusIndicatorRotateLayout.requestLayout();
  }
  @Override
  public boolean onTouch(View view, MotionEvent e) {
    if (CameraController.isRemoteCamera()) {
      if (e.getAction() == MotionEvent.ACTION_UP) {
        onTouchAreas(e);
      }
      return true;
    }

    updateCurrentTouch(e);

    if (e.getPointerCount() > 1) {
      splitMode = true;
      mHandler.removeMessages(START_TOUCH_FOCUS);

      onTouchMeteringArea(e);
      onTouchFocusArea(e);

      return true;
    }

    if (splitMode && e.getAction() == MotionEvent.ACTION_DOWN) {
      mMeteringIndicatorRotateLayout.setVisibility(View.GONE);
      ApplicationScreen.instance.setCameraMeteringMode(ApplicationScreen.getMeteringMode());
      splitMode = false;
    }

    if (splitMode) {
      return true;
    }

    // Check if it's double click
    if (e.getAction() == MotionEvent.ACTION_UP) {
      lastTouchTime1 = lastTouchTime2;
      lastTouchTime2 = System.currentTimeMillis();

      if (lastTouchTime2 - lastTouchTime1 < 1000) {
        isDoubleClick = true;

        // If shot on double click
        if (ApplicationScreen.instance.isShotOnTap() == 2) {
          // Cancel delayed focus, which was created by second click
          mHandler.removeMessages(START_TOUCH_FOCUS);

          // If state is Focused start capture
          if (mState == STATE_SUCCESS) {
            String modeID = PluginManager.getInstance().getActiveMode().modeID;
            if (!modeID.equals("video") && !currentTouch) {
              ApplicationScreen.getGUIManager().onHardwareShutterButtonPressed();
              isDoubleClick = false;
            }
          }
          return true;
        }
      } else {
        isDoubleClick = false;
      }
    }

    // Not handle touch event if no need of autoFocus and refuse 'shot on
    // tap' in video mode.
    if (!mInitialized
        || mState == STATE_FOCUSING_SNAP_ON_FINISH
        || mState == STATE_INACTIVE
        || mFocusDisabled
        || !CameraController.isFocusModeSupported()
        || (!(needAutoFocusCall() || isContinuousFocusMode())
            && !(ApplicationScreen.instance.isShotOnTap() > 0
                && !PluginManager.getInstance().getActiveMode().modeID.equals("video"))))
      return false;

    // Let users be able to cancel previous touch focus.
    if ((mState == STATE_FOCUSING)
        && !delayedFocus
        && ApplicationScreen.instance.isShotOnTap() != 2) {
      focusCanceled = true;
      cancelAutoFocus();
      int fm = CameraController.getFocusMode();
      if ((preferenceFocusMode == CameraParameters.AF_MODE_CONTINUOUS_PICTURE
              || preferenceFocusMode == CameraParameters.AF_MODE_CONTINUOUS_VIDEO)
          && fm != CameraParameters.AF_MODE_UNSUPPORTED
          && preferenceFocusMode != CameraController.getFocusMode()
          && preferenceFocusMode != CameraParameters.MF_MODE) {
        CameraController.setCameraFocusMode(preferenceFocusMode);
      } else if (CameraController.isGalaxyNote3 && preferenceFocusMode != CameraParameters.MF_MODE)
      // Kind of hack to
      // prevent Note 3 of
      // permanent 'auto focus
      // failed' state
      {
        CameraController.setCameraFocusMode(CameraParameters.AF_MODE_CONTINUOUS_PICTURE);
        CameraController.setCameraFocusMode(preferenceFocusMode);
      }
      return true;
    }

    switch (e.getAction()) {
      case MotionEvent.ACTION_DOWN:
        focusCanceled = false;
        delayedFocus = false;
        X = e.getX();
        Y = e.getY();

        lastEvent = MotionEvent.obtain(e);
        mHandler.sendEmptyMessageDelayed(START_TOUCH_FOCUS, START_TOUCH_FOCUS_DELAY);

        return true;
      case MotionEvent.ACTION_MOVE:
        {
          float difX = e.getX();
          float difY = e.getY();

          if ((Math.abs(difX - X) > 50 || Math.abs(difY - Y) > 50) && !focusCanceled) {
            focusCanceled = true;
            cancelAutoFocus();
            mHandler.removeMessages(START_TOUCH_FOCUS);
            return true;
          } else return true;
        }
      case MotionEvent.ACTION_UP:
        mHandler.removeMessages(START_TOUCH_FOCUS);
        if (focusCanceled || delayedFocus) return true;
        break;
      default:
        break;
    }

    onTouchAreas(e);

    return true;
  }