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;
22 import android.app.Activity;
23 import android.content.Intent;
24 import android.graphics.Bitmap;
25 import android.net.Uri;
26 import android.os.Bundle;
27 import android.os.Handler;
28 import android.os.Message;
30 import java.util.Vector;
33 * This class handles all the messaging which comprises the state machine for capture.
35 * @author dswitkin@google.com (Daniel Switkin)
37 public final class CaptureActivityHandler extends Handler {
38 private final CaptureActivity activity;
39 private final DecodeThread decodeThread;
48 CaptureActivityHandler(CaptureActivity activity,
49 Vector<BarcodeFormat> decodeFormats,
51 boolean beginScanning) {
52 this.activity = activity;
53 decodeThread = new DecodeThread(activity, decodeFormats, characterSet,
54 new ViewfinderResultPointCallback(activity.getViewfinderView()));
56 state = State.SUCCESS;
58 // Start ourselves capturing previews and decoding.
59 CameraManager.get().startPreview();
61 restartPreviewAndDecode();
66 public void handleMessage(Message message) {
67 switch (message.what) {
69 // When one auto focus pass finishes, start another. This is the closest thing to
70 // continuous AF. It does seem to hunt a bit, but I'm not sure what else to do.
71 if (state == State.PREVIEW) {
72 CameraManager.get().requestAutoFocus(this, R.id.auto_focus);
75 case R.id.restart_preview:
76 restartPreviewAndDecode();
78 case R.id.decode_succeeded:
79 state = State.SUCCESS;
80 Bundle bundle = message.getData();
81 Bitmap barcode = bundle == null ? null : (Bitmap) bundle.getParcelable(DecodeThread.BARCODE_BITMAP);
82 activity.handleDecode((Result) message.obj, barcode);
84 case R.id.decode_failed:
85 // We're decoding as fast as possible, so when one decode fails, start another.
86 state = State.PREVIEW;
87 CameraManager.get().requestPreviewFrame(decodeThread.getHandler(), R.id.decode);
89 case R.id.return_scan_result:
90 activity.setResult(Activity.RESULT_OK, (Intent) message.obj);
93 case R.id.launch_product_query:
94 String url = (String) message.obj;
95 activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
100 public void quitSynchronously() {
102 CameraManager.get().stopPreview();
103 Message quit = Message.obtain(decodeThread.getHandler(), R.id.quit);
107 } catch (InterruptedException e) {
110 // Be absolutely sure we don't send any queued up messages
111 removeMessages(R.id.decode_succeeded);
112 removeMessages(R.id.decode_failed);
115 private void restartPreviewAndDecode() {
116 if (state == State.SUCCESS) {
117 state = State.PREVIEW;
118 CameraManager.get().requestPreviewFrame(decodeThread.getHandler(), R.id.decode);
119 CameraManager.get().requestAutoFocus(this, R.id.auto_focus);
120 activity.drawViewfinder();