Added a new feature to the test app, which captures all the device info and default...
[zxing.git] / androidtest / src / com / google / zxing / client / androidtest / ZXingTestActivity.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.androidtest;
18
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;
30
31 public final class ZXingTestActivity extends Activity {
32
33   private static final int ABOUT_ID = Menu.FIRST;
34   private static final String PACKAGE_NAME = "com.google.zxing.client.androidtest";
35
36   @Override
37   public void onCreate(Bundle icicle) {
38     super.onCreate(icicle);
39     setContentView(R.layout.test);
40
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);
56   }
57
58   @Override
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);
63     return true;
64   }
65
66   @Override
67   public boolean onOptionsItemSelected(MenuItem item) {
68     switch (item.getItemId()) {
69       case ABOUT_ID:
70         int versionCode = 0;
71         String versionName = "unknown";
72         try {
73           PackageInfo info = getPackageManager().getPackageInfo(PACKAGE_NAME, 0);
74           versionCode = info.versionCode;
75           versionName = info.versionName;
76         } catch (PackageManager.NameNotFoundException e) {
77         }
78         AlertDialog.Builder builder = new AlertDialog.Builder(this);
79         builder.setTitle(getString(R.string.app_name) + " " + versionName + " (" + versionCode +
80             ")");
81         builder.setMessage(getString(R.string.about_message));
82         builder.setPositiveButton(R.string.ok_button, null);
83         builder.show();
84         break;
85     }
86     return super.onOptionsItemSelected(item);
87   }
88
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);
95     }
96   };
97
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);
104     }
105   };
106
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);
112     }
113   };
114
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);
120     }
121   };
122
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);
128     }
129   };
130
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);
135     }
136   };
137
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);
144     }
145   };
146
147   @Override
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));
156       }
157     }
158   }
159
160   public final Button.OnClickListener mEncodeURL = new Button.OnClickListener() {
161     public void onClick(View v) {
162       encodeBarcode("TEXT_TYPE", "http://www.nytimes.com");
163     }
164   };
165
166   public final Button.OnClickListener mEncodeEmail = new Button.OnClickListener() {
167     public void onClick(View v) {
168       encodeBarcode("EMAIL_TYPE", "foo@example.com");
169     }
170   };
171
172   public final Button.OnClickListener mEncodePhone = new Button.OnClickListener() {
173     public void onClick(View v) {
174       encodeBarcode("PHONE_TYPE", "2125551212");
175     }
176   };
177
178   public final Button.OnClickListener mEncodeSMS = new Button.OnClickListener() {
179     public void onClick(View v) {
180       encodeBarcode("SMS_TYPE", "2125551212");
181     }
182   };
183
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);
192     }
193   };
194
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);
201     }
202   };
203
204   public final Button.OnClickListener mEncodeBadData = new Button.OnClickListener() {
205     public void onClick(View v) {
206       encodeBarcode(null, "bar");
207     }
208   };
209
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"));
213     }
214   };
215
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);
221     builder.show();
222   }
223
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);
229   }
230
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);
236   }
237
238 }