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