2 * Copyright (C) 2008 Google Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.android;
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.graphics.PixelFormat;
23 import android.os.Bundle;
24 import android.os.Handler;
25 import android.os.Message;
26 import android.view.KeyEvent;
27 import android.view.Menu;
28 import android.view.Window;
29 import android.view.WindowManager.LayoutParams;
30 import com.google.zxing.Result;
31 import com.google.zxing.ResultPoint;
32 import com.google.zxing.client.result.ParsedReaderResult;
33 import com.google.zxing.client.result.ParsedReaderResultType;
36 * The barcode reader activity itself. This is loosely based on the CameraPreview
37 * example included in the Android SDK.
39 * @author dswitkin@google.com (Daniel Switkin)
40 * @author Android Team (for CameraPreview example)
42 public final class BarcodeReaderCaptureActivity extends Activity {
44 private CameraManager cameraManager;
45 private CameraSurfaceView surfaceView;
46 private WorkerThread workerThread;
48 private static final int ABOUT_ID = Menu.FIRST;
51 public void onCreate(Bundle icicle) {
52 super.onCreate(icicle);
53 requestWindowFeature(Window.FEATURE_NO_TITLE);
55 // Make sure to create a TRANSLUCENT window. This is required for SurfaceView to work.
56 // Eventually this'll be done by the system automatically.
57 getWindow().setAttributes(new LayoutParams(LayoutParams.APPLICATION_TYPE,
58 LayoutParams.NO_STATUS_BAR_FLAG));
59 getWindow().setFormat(PixelFormat.TRANSLUCENT);
61 cameraManager = new CameraManager(getApplication());
62 surfaceView = new CameraSurfaceView(getApplication(), cameraManager);
63 setContentView(surfaceView);
64 workerThread = new WorkerThread(surfaceView, cameraManager, messageHandler);
65 workerThread.requestPreviewLoop();
68 // TODO re-enable this when issues with Matrix.setPolyToPoly() are resolved
69 //GridSampler.setGridSampler(new AndroidGraphicsGridSampler());
73 protected boolean isFullscreenOpaque() {
74 // Our main window is set to translucent, but we know that we will
75 // fill it with opaque data. Tell the system that so it can perform
76 // some important optimizations.
81 protected void onResume() {
83 cameraManager.openDriver();
84 if (workerThread == null) {
85 workerThread = new WorkerThread(surfaceView, cameraManager, messageHandler);
86 workerThread.requestPreviewLoop();
92 protected void onPause() {
94 if (workerThread != null) {
95 workerThread.requestExitAndWait();
98 cameraManager.closeDriver();
102 public boolean onKeyDown(int keyCode, KeyEvent event) {
103 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
104 workerThread.requestStillAndDecode();
107 return super.onKeyDown(keyCode, event);
112 public boolean onCreateOptionsMenu(Menu menu) {
113 super.onCreateOptionsMenu(menu);
114 menu.add(0, ABOUT_ID, R.string.menu_about);
119 public boolean onOptionsItemSelected(Menu.Item item) {
120 switch (item.getId()) {
122 Context context = getApplication();
123 showAlert(context.getString(R.string.title_about),
124 context.getString(R.string.msg_about),
125 context.getString(R.string.button_ok), null, true, null);
128 return super.onOptionsItemSelected(item);
131 private final Handler messageHandler = new Handler() {
133 public void handleMessage(Message message) {
134 switch (message.what) {
135 case R.id.decoding_succeeded_message:
136 handleDecode((Result) message.obj);
138 case R.id.decoding_failed_message:
139 Context context = getApplication();
140 showAlert(context.getString(R.string.title_no_barcode_detected),
141 context.getString(R.string.msg_no_barcode_detected),
142 context.getString(R.string.button_ok), null, true, null);
148 public void restartPreview() {
149 workerThread.requestPreviewLoop();
152 // TODO(dswitkin): These deprecated showAlert calls need to be updated.
153 private void handleDecode(Result rawResult) {
154 ResultPoint[] points = rawResult.getResultPoints();
155 if (points != null && points.length > 0) {
156 surfaceView.drawResultPoints(points);
159 Context context = getApplication();
160 ParsedReaderResult readerResult = parseReaderResult(rawResult);
161 ResultHandler handler = new ResultHandler(this, readerResult);
162 if (handler.getIntent() != null) {
163 // Can be handled by some external app; ask if the user wants to
164 // proceed first though
165 Message yesMessage = handler.obtainMessage(R.string.button_yes);
166 Message noMessage = handler.obtainMessage(R.string.button_no);
167 showAlert(context.getString(getDialogTitleID(readerResult.getType())),
168 readerResult.getDisplayResult(), context.getString(R.string.button_yes),
169 yesMessage, context.getString(R.string.button_no), noMessage, true, noMessage);
171 // Just show information to user
172 Message okMessage = handler.obtainMessage(R.string.button_ok);
173 showAlert(context.getString(R.string.title_barcode_detected),
174 readerResult.getDisplayResult(), context.getString(R.string.button_ok), okMessage, null,
175 null, true, okMessage);
179 private static ParsedReaderResult parseReaderResult(Result rawResult) {
180 ParsedReaderResult readerResult = ParsedReaderResult.parseReaderResult(rawResult);
181 if (readerResult.getType().equals(ParsedReaderResultType.TEXT)) {
182 String rawText = rawResult.getText();
183 AndroidIntentParsedResult androidResult = AndroidIntentParsedResult.parse(rawText);
184 if (androidResult != null) {
185 Intent intent = androidResult.getIntent();
186 if (!Intent.VIEW_ACTION.equals(intent.getAction())) {
187 // For now, don't take anything that just parses as a View action. A lot
188 // of things are accepted as a View action by default.
189 readerResult = androidResult;
196 private static int getDialogTitleID(ParsedReaderResultType type) {
197 if (type.equals(ParsedReaderResultType.ADDRESSBOOK)) {
198 return R.string.title_add_contact;
199 } else if (type.equals(ParsedReaderResultType.URI) ||
200 type.equals(ParsedReaderResultType.BOOKMARK) ||
201 type.equals(ParsedReaderResultType.URLTO)) {
202 return R.string.title_open_url;
203 } else if (type.equals(ParsedReaderResultType.EMAIL) ||
204 type.equals(ParsedReaderResultType.EMAIL_ADDRESS)) {
205 return R.string.title_compose_email;
206 } else if (type.equals(ParsedReaderResultType.UPC)) {
207 return R.string.title_lookup_barcode;
208 } else if (type.equals(ParsedReaderResultType.TEL)) {
209 return R.string.title_dial;
211 return R.string.title_barcode_detected;