CaptureActivityHandler.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (C) 2008 ZXing authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.usai.redant.photo;
  17. import android.content.ActivityNotFoundException;
  18. import android.content.pm.PackageManager;
  19. import android.content.pm.ResolveInfo;
  20. import android.graphics.BitmapFactory;
  21. import android.provider.Browser;
  22. import com.google.zxing.BarcodeFormat;
  23. import com.google.zxing.DecodeHintType;
  24. import com.google.zxing.Result;
  25. import com.usai.redant.camera.CameraManager;
  26. import android.app.Activity;
  27. import android.content.Intent;
  28. import android.graphics.Bitmap;
  29. import android.net.Uri;
  30. import android.os.Bundle;
  31. import android.os.Handler;
  32. import android.os.Message;
  33. import android.util.Log;
  34. import java.util.Collection;
  35. import java.util.Map;
  36. /**
  37. * This class handles all the messaging which comprises the state machine for capture.
  38. *
  39. * @author dswitkin@google.com (Daniel Switkin)
  40. */
  41. public final class CaptureActivityHandler extends Handler {
  42. private static final String TAG = CaptureActivityHandler.class.getSimpleName();
  43. private final CaptureActivity activity;
  44. private final DecodeThread decodeThread;
  45. private State state;
  46. private final CameraManager cameraManager;
  47. private enum State {
  48. PREVIEW,
  49. SUCCESS,
  50. DONE
  51. }
  52. CaptureActivityHandler(CaptureActivity activity,
  53. Collection<BarcodeFormat> decodeFormats,
  54. Map<DecodeHintType,?> baseHints,
  55. String characterSet,
  56. CameraManager cameraManager) {
  57. this.activity = activity;
  58. decodeThread = new DecodeThread(activity, decodeFormats, baseHints, characterSet,
  59. new ViewfinderResultPointCallback(activity.getViewfinderView()));
  60. decodeThread.start();
  61. state = State.SUCCESS;
  62. // Start ourselves capturing previews and decoding.
  63. this.cameraManager = cameraManager;
  64. cameraManager.startPreview();
  65. restartPreviewAndDecode();
  66. }
  67. @Override
  68. public void handleMessage(Message message) {
  69. switch (message.what) {
  70. case R.id.restart_preview:
  71. Log.d(TAG, "Got restart preview message");
  72. restartPreviewAndDecode();
  73. break;
  74. case R.id.decode_succeeded:
  75. Log.d(TAG, "Got decode succeeded message");
  76. state = State.SUCCESS;
  77. Bundle bundle = message.getData();
  78. Bitmap barcode = null;
  79. float scaleFactor = 1.0f;
  80. if (bundle != null) {
  81. byte[] compressedBitmap = bundle.getByteArray(DecodeThread.BARCODE_BITMAP);
  82. if (compressedBitmap != null) {
  83. barcode = BitmapFactory.decodeByteArray(compressedBitmap, 0, compressedBitmap.length, null);
  84. // Mutable copy:
  85. barcode = barcode.copy(Bitmap.Config.ARGB_8888, true);
  86. }
  87. scaleFactor = bundle.getFloat(DecodeThread.BARCODE_SCALED_FACTOR);
  88. }
  89. activity.handleDecode((Result) message.obj, barcode, scaleFactor);
  90. break;
  91. case R.id.decode_failed:
  92. // We're decoding as fast as possible, so when one decode fails, start another.
  93. state = State.PREVIEW;
  94. cameraManager.requestPreviewFrame(decodeThread.getHandler(), R.id.decode);
  95. break;
  96. case R.id.return_scan_result:
  97. Log.d(TAG, "Got return scan result message");
  98. activity.setResult(Activity.RESULT_OK, (Intent) message.obj);
  99. activity.finish();
  100. break;
  101. case R.id.launch_product_query:
  102. Log.d(TAG, "Got product query message");
  103. String url = (String) message.obj;
  104. Intent intent = new Intent(Intent.ACTION_VIEW);
  105. intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
  106. intent.setData(Uri.parse(url));
  107. ResolveInfo resolveInfo =
  108. activity.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
  109. String browserPackageName = null;
  110. if (resolveInfo != null && resolveInfo.activityInfo != null) {
  111. browserPackageName = resolveInfo.activityInfo.packageName;
  112. Log.d(TAG, "Using browser in package " + browserPackageName);
  113. }
  114. // Needed for default Android browser / Chrome only apparently
  115. if ("com.android.browser".equals(browserPackageName) || "com.android.chrome".equals(browserPackageName)) {
  116. intent.setPackage(browserPackageName);
  117. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  118. intent.putExtra(Browser.EXTRA_APPLICATION_ID, browserPackageName);
  119. }
  120. try {
  121. activity.startActivity(intent);
  122. } catch (ActivityNotFoundException ignored) {
  123. Log.w(TAG, "Can't find anything to handle VIEW of URI " + url);
  124. }
  125. break;
  126. }
  127. }
  128. public void quitSynchronously() {
  129. state = State.DONE;
  130. cameraManager.stopPreview();
  131. Message quit = Message.obtain(decodeThread.getHandler(), R.id.quit);
  132. quit.sendToTarget();
  133. try {
  134. // Wait at most half a second; should be enough time, and onPause() will timeout quickly
  135. decodeThread.join(500L);
  136. } catch (InterruptedException e) {
  137. // continue
  138. }
  139. // Be absolutely sure we don't send any queued up messages
  140. removeMessages(R.id.decode_succeeded);
  141. removeMessages(R.id.decode_failed);
  142. }
  143. private void restartPreviewAndDecode() {
  144. if (state == State.SUCCESS) {
  145. state = State.PREVIEW;
  146. cameraManager.requestPreviewFrame(decodeThread.getHandler(), R.id.decode);
  147. activity.drawViewfinder();
  148. }
  149. }
  150. }