2 * Copyright (C) 2008 ZXing authors
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.androidtest;
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.os.Build;
22 import android.os.Bundle;
23 import android.os.Handler;
24 import android.os.Message;
25 import android.view.KeyEvent;
26 import android.view.SurfaceHolder;
27 import android.view.SurfaceView;
28 import android.view.Window;
29 import android.view.WindowManager;
30 import android.widget.Toast;
32 import java.io.IOException;
34 public final class CameraTestActivity extends Activity implements SurfaceHolder.Callback {
36 public static final String GET_CAMERA_PARAMETERS = "GET_CAMERA_PARAMETERS";
37 private static final String[] EMAIL_ADDRESS = {"zxing@googlegroups.com"};
39 private SaveThread mSaveThread = null;
40 private boolean mGetCameraParameters;
43 public void onCreate(Bundle icicle) {
44 super.onCreate(icicle);
45 requestWindowFeature(Window.FEATURE_NO_TITLE);
47 Window window = getWindow();
48 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
49 window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
51 mGetCameraParameters = getIntent().getBooleanExtra(GET_CAMERA_PARAMETERS, false);
52 if (mGetCameraParameters) {
53 setContentView(R.layout.camera_parameters);
55 setContentView(R.layout.camera_test);
57 CameraManager.init(getApplication());
59 SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
60 SurfaceHolder surfaceHolder = surfaceView.getHolder();
61 surfaceHolder.addCallback(this);
62 surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
66 protected void onResume() {
68 if (mSaveThread == null && !mGetCameraParameters) {
69 mSaveThread = new SaveThread(this, CameraManager.get().getFramingRect());
75 protected void onPause() {
78 CameraManager.get().stopPreview();
79 if (mSaveThread != null) {
80 Message quit = Message.obtain(mSaveThread.mHandler, R.id.quit);
84 } catch (InterruptedException e) {
88 CameraManager.get().closeDriver();
91 public final Handler mHandler = new Handler() {
93 public void handleMessage(Message message) {
94 switch (message.what) {
96 // Do not continuously auto focus
98 case R.id.save_succeeded:
99 Toast.makeText(CameraTestActivity.this, R.string.save_succeeded, 500).show();
101 case R.id.save_failed:
102 Toast.makeText(CameraTestActivity.this, R.string.save_failed, 2000).show();
109 public boolean onKeyDown(int keyCode, KeyEvent event) {
110 if (!mGetCameraParameters) {
111 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
112 if (event.getRepeatCount() == 0) {
113 CameraManager.get().requestAutoFocus(mHandler, R.id.auto_focus);
116 } else if (keyCode == KeyEvent.KEYCODE_CAMERA || keyCode == KeyEvent.KEYCODE_SEARCH) {
117 if (event.getRepeatCount() == 0) {
118 CameraManager.get().requestPreviewFrame(mSaveThread.mHandler, R.id.save);
123 return super.onKeyDown(keyCode, event);
126 public void surfaceCreated(SurfaceHolder holder) {
128 String parameters = CameraManager.get().openDriver(holder, mGetCameraParameters);
129 CameraManager.get().startPreview();
130 if (mGetCameraParameters) {
131 collectStatsAndSendEmail(parameters);
133 } catch (IOException e) {
134 // IOException clause added for Android 1.5
135 throw new RuntimeException(e);
139 public void surfaceDestroyed(SurfaceHolder holder) {
143 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
147 private void collectStatsAndSendEmail(String parameters) {
148 StringBuffer result = new StringBuffer();
149 result.append("Device info:");
150 result.append("\n Board: ");
151 result.append(Build.BOARD);
152 result.append("\n Brand: ");
153 result.append(Build.BRAND);
154 result.append("\n Device: ");
155 result.append(Build.DEVICE);
156 result.append("\n Display: ");
157 result.append(Build.DISPLAY);
158 result.append("\n Fingerprint: ");
159 result.append(Build.FINGERPRINT);
160 result.append("\n Host: ");
161 result.append(Build.HOST);
162 result.append("\n ID: ");
163 result.append(Build.ID);
164 result.append("\n Model: ");
165 result.append(Build.MODEL);
166 result.append("\n Product: ");
167 result.append(Build.PRODUCT);
168 result.append("\n Tags: ");
169 result.append(Build.TAGS);
170 result.append("\n Type: ");
171 result.append(Build.TYPE);
172 result.append("\n User: ");
173 result.append(Build.USER);
174 result.append("\n Version Incremental: ");
175 result.append(Build.VERSION.INCREMENTAL);
176 result.append("\n Version Release: ");
177 result.append(Build.VERSION.RELEASE);
178 result.append("\n Version SDK: ");
179 result.append(Build.VERSION.SDK);
181 result.append("\n\n");
182 result.append(parameters);
184 Intent intent = new Intent(Intent.ACTION_SEND);
185 intent.putExtra(Intent.EXTRA_EMAIL, EMAIL_ADDRESS);
186 intent.putExtra(Intent.EXTRA_SUBJECT, "Camera parameters report");
187 intent.putExtra(Intent.EXTRA_TEXT, result.toString());
188 intent.setType("text/plain");
189 startActivity(intent);