Reordered the address book fields to something a little more standard/reasonable.
[zxing.git] / android / src / com / android / barcodes / BarcodesCaptureActivityHandler.java
1 /*
2  * Copyright (C) 2008 Google Inc.
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.android.barcodes;
17
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;
23
24 /**
25  * This class handles all the messaging which comprises the state machine for capture.
26  */
27 public class BarcodesCaptureActivityHandler extends Handler {
28
29     private final BarcodesCaptureActivity mActivity;
30     private final DecodeThread mDecodeThread;
31     private State mState;
32
33     private enum State {
34         PREVIEW,
35         SUCCESS,
36         DONE
37     }
38
39     BarcodesCaptureActivityHandler(BarcodesCaptureActivity activity, String decodeMode) {
40         mActivity = activity;
41         mDecodeThread = new DecodeThread(activity, decodeMode);
42         mDecodeThread.start();
43         mState = State.SUCCESS;
44
45         // Start ourselves capturing previews and decoding.
46         restartPreviewAndDecode();
47     }
48
49     public void handleMessage(Message message) {
50         switch (message.what) {
51             case R.id.auto_focus:
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);
55                 break;
56             case R.id.restart_preview:
57                 restartPreviewAndDecode();
58                 break;
59             case R.id.decode_succeeded:
60                 mState = State.SUCCESS;
61                 int duration = message.arg1;
62                 mActivity.handleDecode((Result) message.obj, duration);
63                 break;
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);
68                 break;
69             case R.id.return_scan_result:
70                 mActivity.setResult(Activity.RESULT_OK, (Intent) message.obj);
71                 mActivity.finish();
72                 break;
73         }
74     }
75
76     public void quitSynchronously() {
77         mState = State.DONE;
78         CameraManager.get().stopPreview();
79         Message quit = Message.obtain(mDecodeThread.mHandler, R.id.quit);
80         quit.sendToTarget();
81         try {
82             mDecodeThread.join();
83         } catch (InterruptedException e) {
84         }
85         
86         // Be absolutely sure we don't send any queued up messages
87         removeMessages(R.id.decode_succeeded);
88         removeMessages(R.id.decode_failed);
89     }
90
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();
99         }
100     }
101
102 }