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