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.android.encode;
19 import com.google.zxing.BarcodeFormat;
20 import com.google.zxing.WriterException;
21 import com.google.zxing.client.android.Intents;
22 import com.google.zxing.client.android.R;
24 import android.app.Activity;
25 import android.app.AlertDialog;
26 import android.app.ProgressDialog;
27 import android.content.DialogInterface;
28 import android.content.DialogInterface.OnCancelListener;
29 import android.content.DialogInterface.OnClickListener;
30 import android.content.Intent;
31 import android.graphics.Bitmap;
32 import android.net.Uri;
33 import android.os.Bundle;
34 import android.os.Handler;
35 import android.os.Environment;
36 import android.os.Message;
37 import android.util.Log;
38 import android.view.Menu;
39 import android.view.MenuItem;
40 import android.view.View;
41 import android.view.ViewTreeObserver.OnGlobalLayoutListener;
42 import android.widget.ImageView;
43 import android.widget.TextView;
46 import java.io.FileNotFoundException;
47 import java.io.FileOutputStream;
48 import java.io.IOException;
51 * This class encodes data from an Intent into a QR code, and then displays it full screen so that
52 * another person can scan it with their device.
54 * @author dswitkin@google.com (Daniel Switkin)
56 public final class EncodeActivity extends Activity {
58 private static final String TAG = EncodeActivity.class.getSimpleName();
60 private static final int SHARE_BARCODE_DIMENSION = 300;
61 private static final int MAX_BARCODE_FILENAME_LENGTH = 24;
63 private QRCodeEncoder qrCodeEncoder;
64 private ProgressDialog progressDialog;
65 private boolean firstLayout;
68 * This needs to be delayed until after the first layout so that the view dimensions will be
71 private final OnGlobalLayoutListener layoutListener = new OnGlobalLayoutListener() {
72 public void onGlobalLayout() {
74 View layout = findViewById(R.id.encode_view);
75 int width = layout.getWidth();
76 int height = layout.getHeight();
77 int smallerDimension = width < height ? width : height;
78 smallerDimension = smallerDimension * 7 / 8;
80 Intent intent = getIntent();
82 qrCodeEncoder = new QRCodeEncoder(EncodeActivity.this, intent);
83 setTitle(getString(R.string.app_name) + " - " + qrCodeEncoder.getTitle());
84 qrCodeEncoder.requestBarcode(handler, smallerDimension);
85 progressDialog = ProgressDialog.show(EncodeActivity.this, null,
86 getString(R.string.msg_encode_in_progress), true, true, cancelListener);
87 } catch (IllegalArgumentException e) {
88 showErrorMessage(R.string.msg_encode_contents_failed);
95 private final Handler handler = new Handler() {
97 public void handleMessage(Message message) {
98 switch (message.what) {
99 case R.id.encode_succeeded:
100 progressDialog.dismiss();
101 progressDialog = null;
102 Bitmap image = (Bitmap) message.obj;
103 ImageView view = (ImageView) findViewById(R.id.image_view);
104 view.setImageBitmap(image);
105 TextView contents = (TextView) findViewById(R.id.contents_text_view);
106 contents.setText(qrCodeEncoder.getDisplayContents());
107 //qrCodeEncoder = null;
109 case R.id.encode_failed:
110 showErrorMessage(R.string.msg_encode_barcode_failed);
111 qrCodeEncoder = null;
117 private final OnClickListener clickListener = new OnClickListener() {
118 public void onClick(DialogInterface dialog, int which) {
123 private final OnCancelListener cancelListener = new OnCancelListener() {
124 public void onCancel(DialogInterface dialog) {
130 public void onCreate(Bundle icicle) {
131 super.onCreate(icicle);
133 Intent intent = getIntent();
134 if (intent != null) {
135 String action = intent.getAction();
136 if (action.equals(Intents.Encode.ACTION) || action.equals(Intent.ACTION_SEND)) {
137 setContentView(R.layout.encode);
145 public boolean onCreateOptionsMenu(Menu menu) {
146 super.onCreateOptionsMenu(menu);
147 menu.add(0, Menu.FIRST, 0, R.string.menu_share).setIcon(android.R.drawable.ic_menu_share);
152 public boolean onOptionsItemSelected(MenuItem item) {
153 if (qrCodeEncoder == null) { // Odd
154 Log.w(TAG, "No existing barcode to send?");
158 String contents = qrCodeEncoder.getContents();
161 bitmap = QRCodeEncoder.encodeAsBitmap(contents, BarcodeFormat.QR_CODE,
162 SHARE_BARCODE_DIMENSION, SHARE_BARCODE_DIMENSION);
163 } catch (WriterException we) {
164 Log.w(TAG, we.toString());
168 File bsRoot = new File(Environment.getExternalStorageDirectory(), "BarcodeScanner");
169 File barcodesRoot = new File(bsRoot, "Barcodes");
170 if (!barcodesRoot.exists() && !barcodesRoot.mkdirs()) {
171 Log.v(TAG, "Couldn't make dir " + barcodesRoot);
172 showErrorMessage(R.string.msg_unmount_usb);
175 File barcodeFile = new File(barcodesRoot, makeBarcodeFileName(contents) + ".png");
176 barcodeFile.delete();
177 FileOutputStream fos = null;
179 fos = new FileOutputStream(barcodeFile);
180 bitmap.compress(Bitmap.CompressFormat.PNG, 0, fos);
181 } catch (FileNotFoundException fnfe) {
182 Log.v(TAG, "Couldn't access file " + barcodeFile + " due to " + fnfe);
183 showErrorMessage(R.string.msg_unmount_usb);
189 } catch (IOException ioe) {
195 Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
196 intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.app_name) + " - " +
197 qrCodeEncoder.getTitle());
198 intent.putExtra(Intent.EXTRA_TEXT, qrCodeEncoder.getContents());
199 intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + barcodeFile.getAbsolutePath()));
200 intent.setType("image/png");
201 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
202 startActivity(Intent.createChooser(intent, null));
206 private static CharSequence makeBarcodeFileName(CharSequence contents) {
207 int fileNameLength = Math.min(MAX_BARCODE_FILENAME_LENGTH, contents.length());
208 StringBuilder fileName = new StringBuilder(fileNameLength);
209 for (int i = 0; i < fileNameLength; i++) {
210 char c = contents.charAt(i);
211 if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9')) {
214 fileName.append('_');
221 protected void onResume() {
224 View layout = findViewById(R.id.encode_view);
225 layout.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
229 private void showErrorMessage(int message) {
230 if (progressDialog != null) {
231 progressDialog.dismiss();
232 progressDialog = null;
234 AlertDialog.Builder builder = new AlertDialog.Builder(this);
235 builder.setMessage(message);
236 builder.setPositiveButton(R.string.button_ok, clickListener);