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