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;
33 import java.io.FileNotFoundException;
34 import java.io.FileOutputStream;
35 import java.io.IOException;
37 public final class CameraTestActivity extends Activity implements SurfaceHolder.Callback {
39 public static final String GET_CAMERA_PARAMETERS = "GET_CAMERA_PARAMETERS";
40 private static final String[] EMAIL_ADDRESS = {"zxing-external@google.com"};
42 private SaveThread mSaveThread = null;
43 private boolean mGetCameraParameters;
46 public void onCreate(Bundle icicle) {
47 super.onCreate(icicle);
48 requestWindowFeature(Window.FEATURE_NO_TITLE);
50 Window window = getWindow();
51 window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
52 window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
54 mGetCameraParameters = getIntent().getBooleanExtra(GET_CAMERA_PARAMETERS, false);
55 if (mGetCameraParameters) {
56 setContentView(R.layout.camera_parameters);
58 setContentView(R.layout.camera_test);
60 CameraManager.init(getApplication());
62 SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
63 SurfaceHolder surfaceHolder = surfaceView.getHolder();
64 surfaceHolder.addCallback(this);
65 surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
69 protected void onResume() {
71 if (mSaveThread == null && !mGetCameraParameters) {
72 mSaveThread = new SaveThread(this);
78 protected void onPause() {
81 CameraManager.get().stopPreview();
82 if (mSaveThread != null) {
83 Message quit = Message.obtain(mSaveThread.mHandler, R.id.quit);
87 } catch (InterruptedException e) {
91 CameraManager.get().closeDriver();
94 public final Handler mHandler = new Handler() {
96 public void handleMessage(Message message) {
97 switch (message.what) {
99 // Do not continuously auto focus
101 case R.id.save_succeeded:
102 Toast.makeText(CameraTestActivity.this, R.string.save_succeeded, 500).show();
104 case R.id.save_failed:
105 Toast.makeText(CameraTestActivity.this, R.string.save_failed, 2000).show();
112 public boolean onKeyDown(int keyCode, KeyEvent event) {
113 if (!mGetCameraParameters) {
114 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
115 if (event.getRepeatCount() == 0) {
116 CameraManager.get().requestAutoFocus(mHandler, R.id.auto_focus);
119 } else if (keyCode == KeyEvent.KEYCODE_CAMERA || keyCode == KeyEvent.KEYCODE_SEARCH) {
120 if (event.getRepeatCount() == 0) {
121 CameraManager.get().requestPreviewFrame(mSaveThread.mHandler, R.id.save);
126 return super.onKeyDown(keyCode, event);
129 public void surfaceCreated(SurfaceHolder holder) {
131 String parameters = CameraManager.get().openDriver(holder, mGetCameraParameters);
132 CameraManager.get().startPreview();
133 if (mGetCameraParameters) {
134 collectStatsAndSendEmail(parameters);
136 } catch (IOException e) {
137 // IOException clause added for Android 1.5
138 throw new RuntimeException(e);
142 public void surfaceDestroyed(SurfaceHolder holder) {
146 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
150 private void collectStatsAndSendEmail(String parameters) {
151 StringBuffer result = new StringBuffer();
152 result.append("Device info:");
153 result.append("\n Board: ");
154 result.append(Build.BOARD);
155 result.append("\n Brand: ");
156 result.append(Build.BRAND);
157 result.append("\n Device: ");
158 result.append(Build.DEVICE);
159 result.append("\n Display: ");
160 result.append(Build.DISPLAY);
161 result.append("\n Fingerprint: ");
162 result.append(Build.FINGERPRINT);
163 result.append("\n Host: ");
164 result.append(Build.HOST);
165 result.append("\n ID: ");
166 result.append(Build.ID);
167 result.append("\n Model: ");
168 result.append(Build.MODEL);
169 result.append("\n Product: ");
170 result.append(Build.PRODUCT);
171 result.append("\n Tags: ");
172 result.append(Build.TAGS);
173 result.append("\n Type: ");
174 result.append(Build.TYPE);
175 result.append("\n User: ");
176 result.append(Build.USER);
177 result.append("\n Version Incremental: ");
178 result.append(Build.VERSION.INCREMENTAL);
179 result.append("\n Version Release: ");
180 result.append(Build.VERSION.RELEASE);
181 result.append("\n Version SDK: ");
182 result.append(Build.VERSION.SDK);
184 result.append("\n\n");
185 result.append(parameters);
187 File file = new File("/sdcard/CameraParameters.txt");
189 FileOutputStream stream = new FileOutputStream(file);
190 stream.write(result.toString().getBytes());
192 } catch (FileNotFoundException e) {
194 } catch (IOException e) {
198 Intent intent = new Intent(Intent.ACTION_SEND);
199 intent.putExtra(Intent.EXTRA_EMAIL, EMAIL_ADDRESS);
200 intent.putExtra(Intent.EXTRA_SUBJECT, "Camera parameters report");
201 intent.putExtra(Intent.EXTRA_TEXT, result.toString());
202 intent.setType("text/plain");
203 startActivity(intent);