More reckless refactoring and code style tweaks -- mostly adding braces around condit...
[zxing.git] / android / src / com / google / zxing / client / android / CaptureActivityHandler.java
1 /*
2  * Copyright (C) 2008 ZXing authors
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
17 package com.google.zxing.client.android;
18
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.graphics.Bitmap;
22 import android.os.Bundle;
23 import android.os.Handler;
24 import android.os.Message;
25 import com.google.zxing.Result;
26
27 /**
28  * This class handles all the messaging which comprises the state machine for capture.
29  */
30 public final class CaptureActivityHandler extends Handler {
31
32   private final CaptureActivity mActivity;
33   private final DecodeThread mDecodeThread;
34   private State mState;
35
36   private enum State {
37     PREVIEW,
38     SUCCESS,
39     DONE
40   }
41
42   CaptureActivityHandler(CaptureActivity activity, String decodeMode,
43                                  boolean beginScanning) {
44     mActivity = activity;
45     mDecodeThread = new DecodeThread(activity, decodeMode);
46     mDecodeThread.start();
47     mState = State.SUCCESS;
48
49     // Start ourselves capturing previews and decoding.
50     CameraManager.get().startPreview();
51     if (beginScanning) {
52       restartPreviewAndDecode();
53     }
54   }
55
56   @Override
57   public void handleMessage(Message message) {
58     switch (message.what) {
59       case R.id.auto_focus:
60         // When one auto focus pass finishes, start another. This is the closest thing to
61         // continuous AF. It does seem to hunt a bit, but I'm not sure what else to do.
62         if (mState == State.PREVIEW) {
63           CameraManager.get().requestAutoFocus(this, R.id.auto_focus);
64         }
65         break;
66       case R.id.restart_preview:
67         restartPreviewAndDecode();
68         break;
69       case R.id.decode_succeeded:
70         mState = State.SUCCESS;
71         Bundle bundle = message.getData();
72         Bitmap barcode = bundle.getParcelable(DecodeThread.BARCODE_BITMAP);
73         int duration = message.arg1;
74         mActivity.handleDecode((Result) message.obj, barcode, duration);
75         break;
76       case R.id.decode_failed:
77         // We're decoding as fast as possible, so when one decode fails, start another.
78         mState = State.PREVIEW;
79         CameraManager.get().requestPreviewFrame(mDecodeThread.mHandler, R.id.decode);
80         break;
81       case R.id.return_scan_result:
82         mActivity.setResult(Activity.RESULT_OK, (Intent) message.obj);
83         mActivity.finish();
84         break;
85     }
86   }
87
88   public void quitSynchronously() {
89     mState = State.DONE;
90     CameraManager.get().stopPreview();
91     Message quit = Message.obtain(mDecodeThread.mHandler, R.id.quit);
92     quit.sendToTarget();
93     try {
94       mDecodeThread.join();
95     } catch (InterruptedException e) {
96     }
97
98     // Be absolutely sure we don't send any queued up messages
99     removeMessages(R.id.decode_succeeded);
100     removeMessages(R.id.decode_failed);
101   }
102
103   private void restartPreviewAndDecode() {
104     if (mState == State.SUCCESS) {
105       mState = State.PREVIEW;
106       CameraManager.get().requestPreviewFrame(mDecodeThread.mHandler, R.id.decode);
107       CameraManager.get().requestAutoFocus(this, R.id.auto_focus);
108       mActivity.drawViewfinder();
109     }
110   }
111
112 }