Issue 112
[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 com.google.zxing.BarcodeFormat;
20 import com.google.zxing.Result;
21
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;
29
30 import java.util.Vector;
31
32 /**
33  * This class handles all the messaging which comprises the state machine for capture.
34  *
35  * @author dswitkin@google.com (Daniel Switkin)
36  */
37 public final class CaptureActivityHandler extends Handler {
38   private final CaptureActivity activity;
39   private final DecodeThread decodeThread;
40   private State state;
41
42   private enum State {
43     PREVIEW,
44     SUCCESS,
45     DONE
46   }
47
48   CaptureActivityHandler(CaptureActivity activity,
49                          Vector<BarcodeFormat> decodeFormats,
50                          boolean beginScanning) {
51     this.activity = activity;
52     decodeThread = new DecodeThread(activity, decodeFormats,
53         new ViewfinderResultPointCallback(activity.getViewfinderView()));
54     decodeThread.start();
55     state = State.SUCCESS;
56
57     // Start ourselves capturing previews and decoding.
58     CameraManager.get().startPreview();
59     if (beginScanning) {
60       restartPreviewAndDecode();
61     }
62   }
63
64   @Override
65   public void handleMessage(Message message) {
66     switch (message.what) {
67       case R.id.auto_focus:
68         // When one auto focus pass finishes, start another. This is the closest thing to
69         // continuous AF. It does seem to hunt a bit, but I'm not sure what else to do.
70         if (state == State.PREVIEW) {
71           CameraManager.get().requestAutoFocus(this, R.id.auto_focus);
72         }
73         break;
74       case R.id.restart_preview:
75         restartPreviewAndDecode();
76         break;
77       case R.id.decode_succeeded:
78         state = State.SUCCESS;
79         Bundle bundle = message.getData();
80         Bitmap barcode = bundle == null ? null : (Bitmap) bundle.getParcelable(DecodeThread.BARCODE_BITMAP);
81         activity.handleDecode((Result) message.obj, barcode);
82         break;
83       case R.id.decode_failed:
84         // We're decoding as fast as possible, so when one decode fails, start another.
85         state = State.PREVIEW;
86         CameraManager.get().requestPreviewFrame(decodeThread.getHandler(), R.id.decode);
87         break;
88       case R.id.return_scan_result:
89         activity.setResult(Activity.RESULT_OK, (Intent) message.obj);
90         activity.finish();
91         break;
92       case R.id.launch_product_query:
93         String url = (String) message.obj;
94         activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
95         break;
96     }
97   }
98
99   public void quitSynchronously() {
100     state = State.DONE;
101     CameraManager.get().stopPreview();
102     Message quit = Message.obtain(decodeThread.getHandler(), R.id.quit);
103     quit.sendToTarget();
104     try {
105       decodeThread.join();
106     } catch (InterruptedException e) {
107     }
108
109     // Be absolutely sure we don't send any queued up messages
110     removeMessages(R.id.decode_succeeded);
111     removeMessages(R.id.decode_failed);
112   }
113
114   private void restartPreviewAndDecode() {
115     if (state == State.SUCCESS) {
116       state = State.PREVIEW;
117       CameraManager.get().requestPreviewFrame(decodeThread.getHandler(), R.id.decode);
118       CameraManager.get().requestAutoFocus(this, R.id.auto_focus);
119       activity.drawViewfinder();
120     }
121   }
122 }