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,
54 Vector<BarcodeFormat> decodeFormats,
56 boolean beginScanning) {
57 this.activity = activity;
58 decodeThread = new DecodeThread(activity, decodeFormats, characterSet,
59 new ViewfinderResultPointCallback(activity.getViewfinderView()));
61 state = State.SUCCESS;
63 // Start ourselves capturing previews and decoding.
64 CameraManager.get().startPreview();
66 restartPreviewAndDecode();
71 public void handleMessage(Message message) {
72 switch (message.what) {
74 Log.v(TAG, "Got auto-focus message");
75 // When one auto focus pass finishes, start another. This is the closest thing to
76 // continuous AF. It does seem to hunt a bit, but I'm not sure what else to do.
77 if (state == State.PREVIEW) {
78 CameraManager.get().requestAutoFocus(this, R.id.auto_focus);
81 case R.id.restart_preview:
82 Log.v(TAG, "Got restart preview message");
83 restartPreviewAndDecode();
85 case R.id.decode_succeeded:
86 Log.v(TAG, "Got decode succeeded message");
87 state = State.SUCCESS;
88 Bundle bundle = message.getData();
89 Bitmap barcode = bundle == null ? null : (Bitmap) bundle.getParcelable(DecodeThread.BARCODE_BITMAP);
90 activity.handleDecode((Result) message.obj, barcode);
92 case R.id.decode_failed:
93 Log.v(TAG, "Got decode failed message");
94 // We're decoding as fast as possible, so when one decode fails, start another.
95 state = State.PREVIEW;
96 CameraManager.get().requestPreviewFrame(decodeThread.getHandler(), R.id.decode);
98 case R.id.return_scan_result:
99 Log.v(TAG, "Got return scan result message");
100 activity.setResult(Activity.RESULT_OK, (Intent) message.obj);
103 case R.id.launch_product_query:
104 Log.v(TAG, "Got product query message");
105 String url = (String) message.obj;
106 Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
107 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
108 activity.startActivity(intent);
113 public void quitSynchronously() {
115 CameraManager.get().stopPreview();
116 Message quit = Message.obtain(decodeThread.getHandler(), R.id.quit);
120 } catch (InterruptedException e) {
123 // Be absolutely sure we don't send any queued up messages
124 removeMessages(R.id.decode_succeeded);
125 removeMessages(R.id.decode_failed);
128 private void restartPreviewAndDecode() {
129 if (state == State.SUCCESS) {
130 state = State.PREVIEW;
131 CameraManager.get().requestPreviewFrame(decodeThread.getHandler(), R.id.decode);
132 CameraManager.get().requestAutoFocus(this, R.id.auto_focus);
133 activity.drawViewfinder();