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