2 * Copyright (C) 2008 Google Inc.
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.
16 package com.android.barcodes;
18 import android.app.Activity;
19 import android.content.Intent;
20 import android.os.Handler;
21 import android.os.Message;
22 import com.google.zxing.Result;
25 * This class handles all the messaging which comprises the state machine for capture.
27 public class BarcodesCaptureActivityHandler extends Handler {
29 private final BarcodesCaptureActivity mActivity;
30 private final DecodeThread mDecodeThread;
39 BarcodesCaptureActivityHandler(BarcodesCaptureActivity activity, String decodeMode) {
41 mDecodeThread = new DecodeThread(activity, decodeMode);
42 mDecodeThread.start();
43 mState = State.SUCCESS;
45 // Start ourselves capturing previews and decoding.
46 restartPreviewAndDecode();
49 public void handleMessage(Message message) {
50 switch (message.what) {
52 // When one auto focus pass finishes, start another. This is the closest thing to
53 // continuous AF. It does seem to hunt a bit, but I'm not sure what else to do.
54 CameraManager.get().requestAutoFocus(this, R.id.auto_focus);
56 case R.id.restart_preview:
57 restartPreviewAndDecode();
59 case R.id.decode_succeeded:
60 mState = State.SUCCESS;
61 int duration = message.arg1;
62 mActivity.handleDecode((Result) message.obj, duration);
64 case R.id.decode_failed:
65 // We're decoding as fast as possible, so when one decode fails, start another.
66 mState = State.PREVIEW;
67 CameraManager.get().requestPreviewFrame(mDecodeThread.mHandler, R.id.decode);
69 case R.id.return_scan_result:
70 mActivity.setResult(Activity.RESULT_OK, (Intent) message.obj);
76 public void quitSynchronously() {
78 CameraManager.get().stopPreview();
79 Message quit = Message.obtain(mDecodeThread.mHandler, R.id.quit);
83 } catch (InterruptedException e) {
86 // Be absolutely sure we don't send any queued up messages
87 removeMessages(R.id.decode_succeeded);
88 removeMessages(R.id.decode_failed);
91 private void restartPreviewAndDecode() {
92 if (mState == State.SUCCESS) {
93 mState = State.PREVIEW;
94 CameraManager.get().startPreview();
95 CameraManager.get().requestPreviewFrame(mDecodeThread.mHandler, R.id.decode);
96 CameraManager.get().requestAutoFocus(this, R.id.auto_focus);
97 mActivity.resetStatusViewColor();
98 mActivity.drawViewfinder();