@Override
  public void handleMessage(Message message) {
    switch (message.what) {
      case DecodeStatus.RESTART_PREVIEW:
        restartPreviewAndDecode();
        break;
      case DecodeStatus.DECODE_SUCCEEDED:
        state = State.SUCCESS;
        Bundle bundle = message.getData();

        activity.handleDecode((Result) message.obj, bundle);
        break;
      case DecodeStatus.DECODE_FAILED:
        // We're decoding as fast as possible, so when one decode fails,
        // start another.
        state = State.PREVIEW;
        cameraManager.requestPreviewFrame(decodeThread.getHandler(), DecodeStatus.DECODE);
        break;
      case DecodeStatus.RETURN_SCAN_RESULT:
        activity.setResult(Activity.RESULT_OK, (Intent) message.obj);
        activity.finish();
        break;
    }
  }
 private void restartPreviewAndDecode() {
   if (state == State.SUCCESS) {
     state = State.PREVIEW;
     cameraManager.requestPreviewFrame(decodeThread.getHandler(), DecodeStatus.DECODE);
   }
 }