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.app.AlertDialog;
21 import android.content.Intent;
22 import android.content.pm.PackageInfo;
23 import android.content.pm.PackageManager;
24 import android.os.Bundle;
25 import android.provider.Contacts;
26 import android.view.Menu;
27 import android.view.MenuItem;
28 import android.view.View;
29 import android.widget.Button;
31 public final class ZXingTestActivity extends Activity {
33 private static final int ABOUT_ID = Menu.FIRST;
34 private static final String PACKAGE_NAME = "com.google.zxing.client.androidtest";
37 public void onCreate(Bundle icicle) {
38 super.onCreate(icicle);
39 setContentView(R.layout.test);
41 findViewById(R.id.take_test_photos).setOnClickListener(mTakeTestPhotos);
42 findViewById(R.id.get_camera_parameters).setOnClickListener(mGetCameraParameters);
43 findViewById(R.id.run_benchmark).setOnClickListener(mRunBenchmark);
44 findViewById(R.id.scan_product).setOnClickListener(mScanProduct);
45 findViewById(R.id.scan_qr_code).setOnClickListener(mScanQRCode);
46 findViewById(R.id.scan_anything).setOnClickListener(mScanAnything);
47 findViewById(R.id.search_book_contents).setOnClickListener(mSearchBookContents);
48 findViewById(R.id.encode_url).setOnClickListener(mEncodeURL);
49 findViewById(R.id.encode_email).setOnClickListener(mEncodeEmail);
50 findViewById(R.id.encode_phone).setOnClickListener(mEncodePhone);
51 findViewById(R.id.encode_sms).setOnClickListener(mEncodeSMS);
52 findViewById(R.id.encode_contact).setOnClickListener(mEncodeContact);
53 findViewById(R.id.encode_location).setOnClickListener(mEncodeLocation);
54 findViewById(R.id.encode_bad_data).setOnClickListener(mEncodeBadData);
55 findViewById(R.id.share_via_barcode).setOnClickListener(mShareViaBarcode);
59 public boolean onCreateOptionsMenu(Menu menu) {
60 super.onCreateOptionsMenu(menu);
61 menu.add(0, ABOUT_ID, 0, R.string.about_menu)
62 .setIcon(android.R.drawable.ic_menu_info_details);
67 public boolean onOptionsItemSelected(MenuItem item) {
68 switch (item.getItemId()) {
71 String versionName = "unknown";
73 PackageInfo info = getPackageManager().getPackageInfo(PACKAGE_NAME, 0);
74 versionCode = info.versionCode;
75 versionName = info.versionName;
76 } catch (PackageManager.NameNotFoundException e) {
78 AlertDialog.Builder builder = new AlertDialog.Builder(this);
79 builder.setTitle(getString(R.string.app_name) + " " + versionName + " (" + versionCode +
81 builder.setMessage(getString(R.string.about_message));
82 builder.setPositiveButton(R.string.ok_button, null);
86 return super.onOptionsItemSelected(item);
89 public final Button.OnClickListener mTakeTestPhotos = new Button.OnClickListener() {
90 public void onClick(View v) {
91 Intent intent = new Intent(Intent.ACTION_VIEW);
92 intent.setClassName(ZXingTestActivity.this, CameraTestActivity.class.getName());
93 intent.putExtra(CameraTestActivity.GET_CAMERA_PARAMETERS, false);
94 startActivity(intent);
98 public final Button.OnClickListener mGetCameraParameters = new Button.OnClickListener() {
99 public void onClick(View v) {
100 Intent intent = new Intent(Intent.ACTION_VIEW);
101 intent.setClassName(ZXingTestActivity.this, CameraTestActivity.class.getName());
102 intent.putExtra(CameraTestActivity.GET_CAMERA_PARAMETERS, true);
103 startActivity(intent);
107 public final Button.OnClickListener mRunBenchmark = new Button.OnClickListener() {
108 public void onClick(View v) {
109 Intent intent = new Intent(Intent.ACTION_VIEW);
110 intent.setClassName(ZXingTestActivity.this, BenchmarkActivity.class.getName());
111 startActivity(intent);
115 public final Button.OnClickListener mScanProduct = new Button.OnClickListener() {
116 public void onClick(View v) {
117 Intent intent = new Intent("com.google.zxing.client.android.SCAN");
118 intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
119 startActivityForResult(intent, 0);
123 public final Button.OnClickListener mScanQRCode = new Button.OnClickListener() {
124 public void onClick(View v) {
125 Intent intent = new Intent("com.google.zxing.client.android.SCAN");
126 intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
127 startActivityForResult(intent, 0);
131 public final Button.OnClickListener mScanAnything = new Button.OnClickListener() {
132 public void onClick(View v) {
133 Intent intent = new Intent("com.google.zxing.client.android.SCAN");
134 startActivityForResult(intent, 0);
138 public final Button.OnClickListener mSearchBookContents = new Button.OnClickListener() {
139 public void onClick(View v) {
140 Intent intent = new Intent("com.google.zxing.client.android.SEARCH_BOOK_CONTENTS");
141 intent.putExtra("ISBN", "9780441014989");
142 intent.putExtra("QUERY", "future");
143 startActivity(intent);
148 public void onActivityResult(int requestCode, int resultCode, Intent intent) {
149 if (requestCode == 0) {
150 if (resultCode == RESULT_OK) {
151 String contents = intent.getStringExtra("SCAN_RESULT");
152 String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
153 showDialog(R.string.result_succeeded, "Format: " + format + "\nContents: " + contents);
154 } else if (resultCode == RESULT_CANCELED) {
155 showDialog(R.string.result_failed, getString(R.string.result_failed_why));
160 public final Button.OnClickListener mEncodeURL = new Button.OnClickListener() {
161 public void onClick(View v) {
162 encodeBarcode("TEXT_TYPE", "http://www.nytimes.com");
166 public final Button.OnClickListener mEncodeEmail = new Button.OnClickListener() {
167 public void onClick(View v) {
168 encodeBarcode("EMAIL_TYPE", "foo@example.com");
172 public final Button.OnClickListener mEncodePhone = new Button.OnClickListener() {
173 public void onClick(View v) {
174 encodeBarcode("PHONE_TYPE", "2125551212");
178 public final Button.OnClickListener mEncodeSMS = new Button.OnClickListener() {
179 public void onClick(View v) {
180 encodeBarcode("SMS_TYPE", "2125551212");
184 public final Button.OnClickListener mEncodeContact = new Button.OnClickListener() {
185 public void onClick(View v) {
186 Bundle bundle = new Bundle();
187 bundle.putString(Contacts.Intents.Insert.NAME, "Jenny");
188 bundle.putString(Contacts.Intents.Insert.PHONE, "8675309");
189 bundle.putString(Contacts.Intents.Insert.EMAIL, "jenny@the80s.com");
190 bundle.putString(Contacts.Intents.Insert.POSTAL, "123 Fake St. San Francisco, CA 94102");
191 encodeBarcode("CONTACT_TYPE", bundle);
195 public final Button.OnClickListener mEncodeLocation = new Button.OnClickListener() {
196 public void onClick(View v) {
197 Bundle bundle = new Bundle();
198 bundle.putFloat("LAT", 40.829208f);
199 bundle.putFloat("LONG", -74.191279f);
200 encodeBarcode("LOCATION_TYPE", bundle);
204 public final Button.OnClickListener mEncodeBadData = new Button.OnClickListener() {
205 public void onClick(View v) {
206 encodeBarcode(null, "bar");
210 public final Button.OnClickListener mShareViaBarcode = new Button.OnClickListener() {
211 public void onClick(View v) {
212 startActivity(new Intent("com.google.zxing.client.android.SHARE"));
216 private void showDialog(int title, String message) {
217 AlertDialog.Builder builder = new AlertDialog.Builder(this);
218 builder.setTitle(title);
219 builder.setMessage(message);
220 builder.setPositiveButton("OK", null);
224 private void encodeBarcode(String type, String data) {
225 Intent intent = new Intent("com.google.zxing.client.android.ENCODE");
226 intent.putExtra("ENCODE_TYPE", type);
227 intent.putExtra("ENCODE_DATA", data);
228 startActivity(intent);
231 private void encodeBarcode(String type, Bundle data) {
232 Intent intent = new Intent("com.google.zxing.client.android.ENCODE");
233 intent.putExtra("ENCODE_TYPE", type);
234 intent.putExtra("ENCODE_DATA", data);
235 startActivity(intent);