/** Send a decode request for realtime OCR mode */
  private void restartOcrPreviewAndDecode() {
    // Continue capturing camera frames
    cameraManager.startPreview();

    // Continue requesting decode of images
    cameraManager.requestOcrDecode(decodeThread.getHandler(), R.id.ocr_continuous_decode);
    activity.drawViewfinder();
  }
  /** Start the preview, but don't try to OCR anything until the user presses the shutter button. */
  private void restartOcrPreview() {
    // Display the shutter and torch buttons
    activity.setButtonVisibility(true);

    if (getState() == State.SUCCESS) {
      setState(State.PREVIEW);

      // Draw the viewfinder.
      activity.drawViewfinder();

      // Start cycling the autofocus
      if (!isAutofocusLoopStarted) {
        // isAutofocusLoopStarted = true;
        requestAutofocus(R.id.auto_focus);
      }
    }
  }
  @Override
  public void handleMessage(Message message) {

    switch (message.what) {
      case R.id.user_requested_auto_focus:
        // Reset the state, but don't request more autofocusing.
        if (getState() == State.PREVIEW_FOCUSING) {
          setState(State.PREVIEW);
        } /* else if (state == State.CONTINUOUS_FOCUSING) {
            state = State.CONTINUOUS;
          } else if (state == State.CONTINUOUS_WAITING_FOR_AUTOFOCUS_TO_FINISH) {
            state = State.CONTINUOUS;
            restartOcrPreviewAndDecode();
          }*/
      case R.id.auto_focus:
        // If the last autofocus was successful, use a longer delay.
        if (message.getData().getBoolean("success")) {
          // Vibrate for 50 milliseconds
          v.vibrate(25);
          //        	focusStatus = true;
          ViewfinderView.cornerColor = Color.GREEN;
          ViewfinderView.frameColor = Color.GREEN;
          activity.drawViewfinder();
          if (CaptureActivity.clicked == true) {
            ocrDecode();
          }
          CaptureActivity.clicked = false;
          //        	try {
          //				Thread.sleep(1000);
          //			} catch (InterruptedException e) {
          //				// TODO Auto-generated catch block
          //				e.printStackTrace();
          //			}

          //        	ViewfinderView.cornerColor = Color.WHITE;
          //        	ViewfinderView.frameColor = Color.WHITE;
          //        	activity.drawViewfinder();

          // ViewfinderView.changeRectColor(Color.GREEN,activity);
          delay = CaptureActivity.AUTOFOCUS_SUCCESS_INTERVAL_MS;
        } else {
          Toast toast =
              Toast.makeText(
                  activity.getBaseContext(),
                  "Keep your phone at proper distance or turn ON the Torch.",
                  Toast.LENGTH_SHORT);
          toast.setGravity(Gravity.TOP, 0, 0);
          toast.show();
          ViewfinderView.cornerColor = Color.RED;
          ViewfinderView.frameColor = Color.RED;
          activity.drawViewfinder();
          //             focusStatus = false;
          // ViewfinderView.changeRectColor(Color.RED,activity);
          delay = CaptureActivity.AUTOFOCUS_FAILURE_INTERVAL_MS;
        }

        // Submit another delayed autofocus request.
        if (getState() == State.PREVIEW_FOCUSING || getState() == State.PREVIEW) {
          setState(State.PREVIEW);
          // requestDelayedAutofocus(delay, R.id.auto_focus);
        } /*else if (state == State.CONTINUOUS_FOCUSING || state == State.CONTINUOUS) {
            state = State.CONTINUOUS;
            requestDelayedAutofocus(delay, R.id.auto_focus);
          } else if (state == State.PREVIEW_FOCUSING) {
            requestDelayedAutofocus(delay, R.id.auto_focus);
          } else if (state == State.CONTINUOUS_FOCUSING) {
            requestDelayedAutofocus(delay, R.id.auto_focus);
          } else if (state == State.CONTINUOUS_WAITING_FOR_AUTOFOCUS_TO_FINISH) {
            state = State.CONTINUOUS;
            requestDelayedAutofocus(delay, R.id.auto_focus);
            restartOcrPreviewAndDecode();
          }*/ else {
          isAutofocusLoopStarted = false;
        }
        break;
      case R.id.restart_preview:
        restartOcrPreview();
        break;
        /* case R.id.ocr_continuous_decode_failed:
          DecodeHandler.resetDecodeState();
          try {
            activity.handleOcrContinuousDecode((OcrResultFailure) message.obj);
          } catch (NullPointerException e) {
            Log.w(TAG, "got bad OcrResultFailure", e);
          }
          if (state == State.CONTINUOUS) {
            restartOcrPreviewAndDecode();
          } else if (state == State.CONTINUOUS_FOCUSING) {
            state = State.CONTINUOUS_WAITING_FOR_AUTOFOCUS_TO_FINISH;
          }
          break;
        case R.id.ocr_continuous_decode_succeeded:
          DecodeHandler.resetDecodeState();
          try {
            activity.handleOcrContinuousDecode((OcrResult) message.obj);
          } catch (NullPointerException e) {
            // Continue
          }
          if (state == State.CONTINUOUS) {
            restartOcrPreviewAndDecode();
          } else if (state == State.CONTINUOUS_FOCUSING) {
            state = State.CONTINUOUS_WAITING_FOR_AUTOFOCUS_TO_FINISH;
          }
          break;*/
      case R.id.ocr_decode_succeeded:
        try {
          setState(State.SUCCESS);
          // activity.setShutterButtonClickable(true);
          activity.handleOcrDecode((OcrResult) message.obj);
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
        break;
      case R.id.ocr_decode_failed:
        setState(State.PREVIEW);
        // activity.setShutterButtonClickable(true);
        Toast toast =
            Toast.makeText(activity.getBaseContext(), "Please try again.", Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.TOP, 0, 0);
        toast.show();
        // requestAutofocus(R.id.auto_focus);
        break;
    }
  }