private void sendReplyMessage(int id, Object arg, long delayMS) {
   Message message = Message.obtain(handler, id, arg);
   if (delayMS > 0L) {
     handler.sendMessageDelayed(message, delayMS);
   } else {
     handler.sendMessage(message);
   }
 }
  // Briefly show the contents of the barcode, then handle the result outside Barcode Scanner.
  private void handleDecodeExternally(Result rawResult, Bitmap barcode) {
    viewfinderView.drawResultBitmap(barcode);

    // Since this message will only be shown for a second, just tell the user what kind of
    // barcode was found (e.g. contact info) rather than the full contents, which they won't
    // have time to read.
    ResultHandler resultHandler = ResultHandlerFactory.makeResultHandler(this, rawResult);
    statusView.setText(getString(resultHandler.getDisplayTitle()));

    if (copyToClipboard) {
      ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
      clipboard.setText(resultHandler.getDisplayContents());
    }

    if (source == Source.NATIVE_APP_INTENT) {
      // Hand back whatever action they requested - this can be changed to Intents.Scan.ACTION when
      // the deprecated intent is retired.
      Intent intent = new Intent(getIntent().getAction());
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
      intent.putExtra(Intents.Scan.RESULT, rawResult.toString());
      intent.putExtra(Intents.Scan.RESULT_FORMAT, rawResult.getBarcodeFormat().toString());

      // crop barcode bitmap by 5pixels on each side
      barcode =
          Bitmap.createBitmap(barcode, 5, 5, barcode.getWidth() - 10, barcode.getHeight() - 10);

      ByteArrayOutputStream bitmapOutStream = new ByteArrayOutputStream();
      barcode.compress(Bitmap.CompressFormat.JPEG, 35, bitmapOutStream);
      intent.putExtra(Intents.Scan.RESULT_BITMAP_BYTES, bitmapOutStream.toByteArray());
      Message message = Message.obtain(handler, R.id.return_scan_result);
      message.obj = intent;
      handler.sendMessageDelayed(message, INTENT_RESULT_DURATION);
    } else if (source == Source.PRODUCT_SEARCH_LINK) {
      // Reformulate the URL which triggered us into a query, so that the request goes to the same
      // TLD as the scan URL.
      Message message = Message.obtain(handler, R.id.launch_product_query);
      int end = sourceUrl.lastIndexOf("/scan");
      message.obj =
          sourceUrl.substring(0, end)
              + "?q="
              + resultHandler.getDisplayContents().toString()
              + "&source=zxing";
      handler.sendMessageDelayed(message, INTENT_RESULT_DURATION);
    } else if (source == Source.ZXING_LINK) {
      // Replace each occurrence of RETURN_CODE_PLACEHOLDER in the returnUrlTemplate
      // with the scanned code. This allows both queries and REST-style URLs to work.
      Message message = Message.obtain(handler, R.id.launch_product_query);
      message.obj =
          returnUrlTemplate.replace(
              RETURN_CODE_PLACEHOLDER, resultHandler.getDisplayContents().toString());
      handler.sendMessageDelayed(message, INTENT_RESULT_DURATION);
    }
  }
Ejemplo n.º 3
0
 private void sendReplyMessage(int id, Object arg) {
   Message message = Message.obtain(handler, id, arg);
   long resultDurationMS =
       getIntent()
           .getLongExtra(
               Intents.Scan.RESULT_DISPLAY_DURATION_MS, DEFAULT_INTENT_RESULT_DURATION_MS);
   if (resultDurationMS > 0L) {
     handler.sendMessageDelayed(message, resultDurationMS);
   } else {
     handler.sendMessage(message);
   }
 }
Ejemplo n.º 4
0
  // Briefly show the contents of the barcode, then handle the result outside Barcode Scanner.
  private void handleDecodeExternally(
      Result rawResult, ResultHandler resultHandler, Bitmap barcode) {
    viewfinderView.drawResultBitmap(barcode);

    // Since this message will only be shown for a second, just tell the user what kind of
    // barcode was found (e.g. contact info) rather than the full contents, which they won't
    // have time to read.
    // TODO
    // TODO
    statusView.setText(getString(resultHandler.getDisplayTitle()));

    if (copyToClipboard && !resultHandler.areContentsSecure()) {
      ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
      clipboard.setText(resultHandler.getDisplayContents());
    }

    if (source == Source.NATIVE_APP_INTENT) {
      // Hand back whatever action they requested - this can be changed to Intents.Scan.ACTION when
      // the deprecated intent is retired.
      Intent intent = new Intent(getIntent().getAction());
      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
      intent.putExtra(Intents.Scan.RESULT, rawResult.toString());
      intent.putExtra("RTN_CONTENT", resultHandler.getDisplayContents());
      intent.putExtra(Intents.Scan.RESULT_FORMAT, rawResult.getBarcodeFormat().toString());
      byte[] rawBytes = rawResult.getRawBytes();
      if (rawBytes != null && rawBytes.length > 0) {
        intent.putExtra(Intents.Scan.RESULT_BYTES, rawBytes);
      }
      Message message = Message.obtain(handler, R.id.return_scan_result);
      message.obj = intent;
      handler.sendMessageDelayed(message, INTENT_RESULT_DURATION);
    } else if (source == Source.PRODUCT_SEARCH_LINK) {
      // Reformulate the URL which triggered us into a query, so that the request goes to the same
      // TLD as the scan URL.
      Message message = Message.obtain(handler, R.id.launch_product_query);
      int end = sourceUrl.lastIndexOf("/scan");
      message.obj =
          sourceUrl.substring(0, end)
              + "?q="
              + resultHandler.getDisplayContents().toString()
              + "&source=zxing";
      handler.sendMessageDelayed(message, INTENT_RESULT_DURATION);
    } else if (source == Source.ZXING_LINK) {
      // Replace each occurrence of RETURN_CODE_PLACEHOLDER in the returnUrlTemplate
      // with the scanned code. This allows both queries and REST-style URLs to work.
      Message message = Message.obtain(handler, R.id.launch_product_query);
      message.obj =
          returnUrlTemplate.replace(
              RETURN_CODE_PLACEHOLDER, resultHandler.getDisplayContents().toString());
      handler.sendMessageDelayed(message, INTENT_RESULT_DURATION);
    }
  }
 @Override
 protected void onPause() {
   super.onPause();
   if (handler != null) {
     handler.quitSynchronously();
     handler = null;
   }
   CameraManager.get().closeDriver();
 }
 @Override
 protected void onPause() {
   if (handler != null) {
     handler.quitSynchronously();
     handler = null;
   }
   inactivityTimer.onPause();
   cameraManager.closeDriver();
   if (!hasSurface) {
     SurfaceView surfaceView = (SurfaceView) findViewById(fakeR.getId("id", "preview_view"));
     SurfaceHolder surfaceHolder = surfaceView.getHolder();
     surfaceHolder.removeCallback(this);
   }
   super.onPause();
 }
Ejemplo n.º 7
0
 private void decodeOrStoreSavedBitmap(Bitmap bitmap, Result result) {
   // Bitmap isn't used yet -- will be used soon
   if (handler == null) {
     savedResultToShow = result;
   } else {
     if (result != null) {
       savedResultToShow = result;
     }
     if (savedResultToShow != null) {
       Message message = Message.obtain(handler, R.id.decode_succeeded, savedResultToShow);
       handler.sendMessage(message);
     }
     savedResultToShow = null;
   }
 }
Ejemplo n.º 8
0
 @Override
 protected void onPause() {
   if (handler != null) {
     handler.quitSynchronously();
     handler = null;
   }
   inactivityTimer.onPause();
   ambientLightManager.stop();
   cameraManager.closeDriver();
   if (!hasSurface) {
     Logger.i("remove callback");
     SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
     SurfaceHolder surfaceHolder = surfaceView.getHolder();
     surfaceHolder.removeCallback(this);
   }
   super.onPause();
 }
 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
   if (keyCode == KeyEvent.KEYCODE_BACK) {
     if (source == Source.NATIVE_APP_INTENT) {
       setResult(RESULT_CANCELED);
       finish();
       return true;
     } else if ((source == Source.NONE || source == Source.ZXING_LINK) && lastResult != null) {
       resetStatusView();
       if (handler != null) {
         handler.sendEmptyMessage(R.id.restart_preview);
       }
       return true;
     }
   } else if (keyCode == KeyEvent.KEYCODE_FOCUS || keyCode == KeyEvent.KEYCODE_CAMERA) {
     // Handle these events so they don't launch the Camera app
     return true;
   }
   return super.onKeyDown(keyCode, event);
 }
Ejemplo n.º 10
0
  /**
   * A valid barcode has been found, so give an indication of success and show the results.
   *
   * @param rawResult The contents of the barcode.
   * @param barcode A greyscale bitmap of the camera data which was decoded.
   */
  public void handleDecode(Result rawResult, Bitmap barcode) {
    inactivityTimer.onActivity();
    lastResult = rawResult;
    ResultHandler resultHandler = ResultHandlerFactory.makeResultHandler(this, rawResult);

    if (barcode == null) {
      // This is from history -- no saved barcode
      handleDecodeInternally(rawResult, resultHandler, null);
    } else {
      drawResultPoints(barcode, rawResult);
      switch (source) {
        case NATIVE_APP_INTENT:
        case PRODUCT_SEARCH_LINK:
          handleDecodeExternally(rawResult, resultHandler, barcode);
          break;
        case ZXING_LINK:
          if (returnUrlTemplate == null) {
            handleDecodeInternally(rawResult, resultHandler, barcode);
          } else {
            handleDecodeExternally(rawResult, resultHandler, barcode);
          }
          break;
        case NONE:
          SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
          if (prefs.getBoolean(PreferencesActivity.KEY_BULK_MODE, false)) {
            Toast.makeText(this, R.string.msg_bulk_mode_scanned, Toast.LENGTH_SHORT).show();
            // Wait a moment or else it will scan the same barcode continuously about 3 times
            if (handler != null) {
              handler.sendEmptyMessageDelayed(R.id.restart_preview, BULK_MODE_SCAN_DELAY_MS);
            }
            resetStatusView();
          } else {
            handleDecodeInternally(rawResult, resultHandler, barcode);
          }
          break;
      }
    }
  }
 public void restartPreviewAfterDelay(long delayMS) {
   if (handler != null) {
     handler.sendEmptyMessageDelayed(fakeR.getId("id", "restart_preview"), delayMS);
   }
   resetStatusView();
 }
 public void restartPreviewAfterDelay(long delayMS) {
   if (handler != null) {
     handler.sendEmptyMessageDelayed(R.id.restart_preview, delayMS);
   }
   resetStatusView();
 }