Try adding current javadoc to SVN
[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.net.Uri;
23 import android.os.Bundle;
24 import android.os.Handler;
25 import android.os.Message;
26 import com.google.zxing.Result;
27
28 /**
29  * This class handles all the messaging which comprises the state machine for capture.
30  */
31 public final class CaptureActivityHandler extends Handler {
32
33   private final CaptureActivity mActivity;
34   private final DecodeThread mDecodeThread;
35   private State mState;
36
37   private enum State {
38     PREVIEW,
39     SUCCESS,
40     DONE
41   }
42
43   CaptureActivityHandler(CaptureActivity activity, String decodeMode,
44                                  boolean beginScanning) {
45     mActivity = activity;
46     mDecodeThread = new DecodeThread(activity, decodeMode);
47     mDecodeThread.start();
48     mState = State.SUCCESS;
49
50     // Start ourselves capturing previews and decoding.
51     CameraManager.get().startPreview();
52     if (beginScanning) {
53       restartPreviewAndDecode();
54     }
55   }
56
57   @Override
58   public void handleMessage(Message message) {
59     switch (message.what) {
60       case R.id.auto_focus:
61         // When one auto focus pass finishes, start another. This is the closest thing to
62         // continuous AF. It does seem to hunt a bit, but I'm not sure what else to do.
63         if (mState == State.PREVIEW) {
64           CameraManager.get().requestAutoFocus(this, R.id.auto_focus);
65         }
66         break;
67       case R.id.restart_preview:
68         restartPreviewAndDecode();
69         break;
70       case R.id.decode_succeeded:
71         mState = State.SUCCESS;
72         Bundle bundle = message.getData();
73         Bitmap barcode = bundle.getParcelable(DecodeThread.BARCODE_BITMAP);
74         mActivity.handleDecode((Result) message.obj, barcode);
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       case R.id.launch_product_query:
86         String url = (String) message.obj;
87         mActivity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
88         break;
89     }
90   }
91
92   public void quitSynchronously() {
93     mState = State.DONE;
94     CameraManager.get().stopPreview();
95     Message quit = Message.obtain(mDecodeThread.mHandler, R.id.quit);
96     quit.sendToTarget();
97     try {
98       mDecodeThread.join();
99     } catch (InterruptedException e) {
100     }
101
102     // Be absolutely sure we don't send any queued up messages
103     removeMessages(R.id.decode_succeeded);
104     removeMessages(R.id.decode_failed);
105   }
106
107   private void restartPreviewAndDecode() {
108     if (mState == State.SUCCESS) {
109       mState = State.PREVIEW;
110       CameraManager.get().requestPreviewFrame(mDecodeThread.mHandler, R.id.decode);
111       CameraManager.get().requestAutoFocus(this, R.id.auto_focus);
112       mActivity.drawViewfinder();
113     }
114   }
115
116 }