6560ec5188a080e6ef3b4c584e0af5599672b0a6
[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.os.Bundle;
23 import android.provider.Contacts;
24 import android.view.View;
25 import android.widget.Button;
26
27 public final class ZXingTestActivity extends Activity {
28
29   @Override
30   public void onCreate(Bundle icicle) {
31     super.onCreate(icicle);
32
33     setContentView(R.layout.test);
34
35     View test_camera = findViewById(R.id.test_camera);
36     test_camera.setOnClickListener(mTestCamera);
37
38     View run_benchmark = findViewById(R.id.run_benchmark);
39     run_benchmark.setOnClickListener(mRunBenchmark);
40
41     View scan_product = findViewById(R.id.scan_product);
42     scan_product.setOnClickListener(mScanProduct);
43
44     View scan_qr_code = findViewById(R.id.scan_qr_code);
45     scan_qr_code.setOnClickListener(mScanQRCode);
46
47     View scan_anything = findViewById(R.id.scan_anything);
48     scan_anything.setOnClickListener(mScanAnything);
49
50     View search_book_contents = findViewById(R.id.search_book_contents);
51     search_book_contents.setOnClickListener(mSearchBookContents);
52
53     View encode_url = findViewById(R.id.encode_url);
54     encode_url.setOnClickListener(mEncodeURL);
55
56     View encode_email = findViewById(R.id.encode_email);
57     encode_email.setOnClickListener(mEncodeEmail);
58
59     View encode_phone = findViewById(R.id.encode_phone);
60     encode_phone.setOnClickListener(mEncodePhone);
61
62     View encode_sms = findViewById(R.id.encode_sms);
63     encode_sms.setOnClickListener(mEncodeSMS);
64
65     View encode_contact = findViewById(R.id.encode_contact);
66     encode_contact.setOnClickListener(mEncodeContact);
67
68     View encode_location = findViewById(R.id.encode_location);
69     encode_location.setOnClickListener(mEncodeLocation);
70
71     View encode_bad_data = findViewById(R.id.encode_bad_data);
72     encode_bad_data.setOnClickListener(mEncodeBadData);
73
74     View share_via_barcode = findViewById(R.id.share_via_barcode);
75     share_via_barcode.setOnClickListener(mShareViaBarcode);
76   }
77
78   public final Button.OnClickListener mTestCamera = new Button.OnClickListener() {
79     public void onClick(View v) {
80       Intent intent = new Intent(Intent.ACTION_VIEW);
81       intent.setClassName(ZXingTestActivity.this, CameraTestActivity.class.getName());
82       startActivity(intent);
83     }
84   };
85
86   public final Button.OnClickListener mRunBenchmark = new Button.OnClickListener() {
87     public void onClick(View v) {
88       Intent intent = new Intent(Intent.ACTION_VIEW);
89       intent.setClassName(ZXingTestActivity.this, BenchmarkActivity.class.getName());
90       startActivity(intent);
91     }
92   };
93
94   public final Button.OnClickListener mScanProduct = new Button.OnClickListener() {
95     public void onClick(View v) {
96       Intent intent = new Intent("com.google.zxing.client.android.SCAN");
97       intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
98       startActivityForResult(intent, 0);
99     }
100   };
101
102   public final Button.OnClickListener mScanQRCode = new Button.OnClickListener() {
103     public void onClick(View v) {
104       Intent intent = new Intent("com.google.zxing.client.android.SCAN");
105       intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
106       startActivityForResult(intent, 0);
107     }
108   };
109
110   public final Button.OnClickListener mScanAnything = new Button.OnClickListener() {
111     public void onClick(View v) {
112       Intent intent = new Intent("com.google.zxing.client.android.SCAN");
113       startActivityForResult(intent, 0);
114     }
115   };
116
117   public final Button.OnClickListener mSearchBookContents = new Button.OnClickListener() {
118     public void onClick(View v) {
119       Intent intent = new Intent("com.google.zxing.client.android.SEARCH_BOOK_CONTENTS");
120       intent.putExtra("ISBN", "9780441014989");
121       intent.putExtra("QUERY", "future");
122       startActivity(intent);
123     }
124   };
125
126   @Override
127   public void onActivityResult(int requestCode, int resultCode, Intent intent) {
128     if (requestCode == 0) {
129       if (resultCode == RESULT_OK) {
130         String contents = intent.getStringExtra("SCAN_RESULT");
131         String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
132         showDialog(R.string.result_succeeded, "Format: " + format + "\nContents: " + contents);
133       } else if (resultCode == RESULT_CANCELED) {
134         showDialog(R.string.result_failed, getString(R.string.result_failed_why));
135       }
136     }
137   }
138
139   public final Button.OnClickListener mEncodeURL = new Button.OnClickListener() {
140     public void onClick(View v) {
141       encodeBarcode("TEXT_TYPE", "http://www.nytimes.com");
142     }
143   };
144
145   public final Button.OnClickListener mEncodeEmail = new Button.OnClickListener() {
146     public void onClick(View v) {
147       encodeBarcode("EMAIL_TYPE", "foo@example.com");
148     }
149   };
150
151   public final Button.OnClickListener mEncodePhone = new Button.OnClickListener() {
152     public void onClick(View v) {
153       encodeBarcode("PHONE_TYPE", "2125551212");
154     }
155   };
156
157   public final Button.OnClickListener mEncodeSMS = new Button.OnClickListener() {
158     public void onClick(View v) {
159       encodeBarcode("SMS_TYPE", "2125551212");
160     }
161   };
162
163   public final Button.OnClickListener mEncodeContact = new Button.OnClickListener() {
164     public void onClick(View v) {
165       Bundle bundle = new Bundle();
166       bundle.putString(Contacts.Intents.Insert.NAME, "Jenny");
167       bundle.putString(Contacts.Intents.Insert.PHONE, "8675309");
168       bundle.putString(Contacts.Intents.Insert.EMAIL, "jenny@the80s.com");
169       bundle.putString(Contacts.Intents.Insert.POSTAL, "123 Fake St. San Francisco, CA 94102");
170       encodeBarcode("CONTACT_TYPE", bundle);
171     }
172   };
173
174   public final Button.OnClickListener mEncodeLocation = new Button.OnClickListener() {
175     public void onClick(View v) {
176       Bundle bundle = new Bundle();
177       bundle.putFloat("LAT", 40.829208f);
178       bundle.putFloat("LONG", -74.191279f);
179       encodeBarcode("LOCATION_TYPE", bundle);
180     }
181   };
182
183   public final Button.OnClickListener mEncodeBadData = new Button.OnClickListener() {
184     public void onClick(View v) {
185       encodeBarcode(null, "bar");
186     }
187   };
188
189   public final Button.OnClickListener mShareViaBarcode = new Button.OnClickListener() {
190     public void onClick(View v) {
191       startActivity(new Intent("com.google.zxing.client.android.SHARE"));
192     }
193   };
194
195   private void showDialog(int title, String message) {
196     AlertDialog.Builder builder = new AlertDialog.Builder(this);
197     builder.setTitle(title);
198     builder.setMessage(message);
199     builder.setPositiveButton("OK", null);
200     builder.show();
201   }
202
203   private void encodeBarcode(String type, String data) {
204     Intent intent = new Intent("com.google.zxing.client.android.ENCODE");
205     intent.putExtra("ENCODE_TYPE", type);
206     intent.putExtra("ENCODE_DATA", data);
207     startActivity(intent);
208   }
209
210   private void encodeBarcode(String type, Bundle data) {
211     Intent intent = new Intent("com.google.zxing.client.android.ENCODE");
212     intent.putExtra("ENCODE_TYPE", type);
213     intent.putExtra("ENCODE_DATA", data);
214     startActivity(intent);
215   }
216
217 }