2 * Copyright (C) 2008 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.android;
19 import com.google.zxing.BarcodeFormat;
20 import com.google.zxing.Result;
21 import com.google.zxing.client.android.camera.CameraManager;
23 import android.app.Activity;
24 import android.content.Intent;
25 import android.graphics.Bitmap;
26 import android.net.Uri;
27 import android.os.Bundle;
28 import android.os.Handler;
29 import android.os.Message;
30 import android.util.Log;
32 import java.util.Vector;
35 * This class handles all the messaging which comprises the state machine for capture.
37 * @author dswitkin@google.com (Daniel Switkin)
39 public final class CaptureActivityHandler extends Handler {
41 private static final String TAG = CaptureActivityHandler.class.getSimpleName();
43 private final CaptureActivity activity;
44 private final DecodeThread decodeThread;
53 CaptureActivityHandler(CaptureActivity activity, Vector<BarcodeFormat> decodeFormats,
54 String characterSet) {
55 this.activity = activity;
56 decodeThread = new DecodeThread(activity, decodeFormats, characterSet,
57 new ViewfinderResultPointCallback(activity.getViewfinderView()));
59 state = State.SUCCESS;
61 // Start ourselves capturing previews and decoding.
62 CameraManager.get().startPreview();
63 restartPreviewAndDecode();
67 public void handleMessage(Message message) {
68 switch (message.what) {
70 //Log.d(TAG, "Got auto-focus message");
71 // When one auto focus pass finishes, start another. This is the closest thing to
72 // continuous AF. It does seem to hunt a bit, but I'm not sure what else to do.
73 if (state == State.PREVIEW) {
74 CameraManager.get().requestAutoFocus(this, R.id.auto_focus);
77 case R.id.restart_preview:
78 Log.d(TAG, "Got restart preview message");
79 restartPreviewAndDecode();
81 case R.id.decode_succeeded:
82 Log.d(TAG, "Got decode succeeded message");
83 state = State.SUCCESS;
84 Bundle bundle = message.getData();
85 Bitmap barcode = bundle == null ? null :
86 (Bitmap) bundle.getParcelable(DecodeThread.BARCODE_BITMAP);
87 activity.handleDecode((Result) message.obj, barcode);
89 case R.id.decode_failed:
90 // We're decoding as fast as possible, so when one decode fails, start another.
91 state = State.PREVIEW;
92 CameraManager.get().requestPreviewFrame(decodeThread.getHandler(), R.id.decode);
94 case R.id.return_scan_result:
95 Log.d(TAG, "Got return scan result message");
96 activity.setResult(Activity.RESULT_OK, (Intent) message.obj);
99 case R.id.launch_product_query:
100 Log.d(TAG, "Got product query message");
101 String url = (String) message.obj;
102 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
103 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
104 activity.startActivity(intent);
109 public void quitSynchronously() {
111 CameraManager.get().stopPreview();
112 Message quit = Message.obtain(decodeThread.getHandler(), R.id.quit);
116 } catch (InterruptedException e) {
120 // Be absolutely sure we don't send any queued up messages
121 removeMessages(R.id.decode_succeeded);
122 removeMessages(R.id.decode_failed);
125 private void restartPreviewAndDecode() {
126 if (state == State.SUCCESS) {
127 state = State.PREVIEW;
128 CameraManager.get().requestPreviewFrame(decodeThread.getHandler(), R.id.decode);
129 CameraManager.get().requestAutoFocus(this, R.id.auto_focus);
130 activity.drawViewfinder();