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.FileOutputStream;
47 import java.io.IOException;
50 * This class encodes data from an Intent into a QR code, and then displays it full screen so that
51 * another person can scan it with their device.
53 * @author dswitkin@google.com (Daniel Switkin)
55 public final class EncodeActivity extends Activity {
57 private static final String TAG = EncodeActivity.class.getSimpleName();
59 private static final int SHARE_BARCODE_DIMENSION = 300;
61 private QRCodeEncoder qrCodeEncoder;
62 private ProgressDialog progressDialog;
63 private boolean firstLayout;
66 * This needs to be delayed until after the first layout so that the view dimensions will be
69 private final OnGlobalLayoutListener layoutListener = new OnGlobalLayoutListener() {
70 public void onGlobalLayout() {
72 View layout = findViewById(R.id.encode_view);
73 int width = layout.getWidth();
74 int height = layout.getHeight();
75 int smallerDimension = width < height ? width : height;
76 smallerDimension = smallerDimension * 7 / 8;
78 Intent intent = getIntent();
80 qrCodeEncoder = new QRCodeEncoder(EncodeActivity.this, intent);
81 setTitle(getString(R.string.app_name) + " - " + qrCodeEncoder.getTitle());
82 qrCodeEncoder.requestBarcode(handler, smallerDimension);
83 progressDialog = ProgressDialog.show(EncodeActivity.this, null,
84 getString(R.string.msg_encode_in_progress), true, true, cancelListener);
85 } catch (IllegalArgumentException e) {
86 showErrorMessage(R.string.msg_encode_contents_failed);
93 private final Handler handler = new Handler() {
95 public void handleMessage(Message message) {
96 switch (message.what) {
97 case R.id.encode_succeeded:
98 progressDialog.dismiss();
99 progressDialog = null;
100 Bitmap image = (Bitmap) message.obj;
101 ImageView view = (ImageView) findViewById(R.id.image_view);
102 view.setImageBitmap(image);
103 TextView contents = (TextView) findViewById(R.id.contents_text_view);
104 contents.setText(qrCodeEncoder.getDisplayContents());
105 //qrCodeEncoder = null;
107 case R.id.encode_failed:
108 showErrorMessage(R.string.msg_encode_barcode_failed);
109 qrCodeEncoder = null;
115 private final OnClickListener clickListener = new OnClickListener() {
116 public void onClick(DialogInterface dialog, int which) {
121 private final OnCancelListener cancelListener = new OnCancelListener() {
122 public void onCancel(DialogInterface dialog) {
128 public void onCreate(Bundle icicle) {
129 super.onCreate(icicle);
131 Intent intent = getIntent();
132 if (intent != null) {
133 String action = intent.getAction();
134 if (action.equals(Intents.Encode.ACTION) || action.equals(Intent.ACTION_SEND)) {
135 setContentView(R.layout.encode);
143 public boolean onCreateOptionsMenu(Menu menu) {
144 super.onCreateOptionsMenu(menu);
145 menu.add(0, Menu.FIRST, 0, R.string.menu_share).setIcon(android.R.drawable.ic_menu_share);
150 public boolean onOptionsItemSelected(MenuItem item) {
151 if (qrCodeEncoder == null) { // Odd
152 Log.w(TAG, "No existing barcode to send?");
159 bitmap = QRCodeEncoder.encodeAsBitmap(qrCodeEncoder.getContents(),
160 BarcodeFormat.QR_CODE,
161 SHARE_BARCODE_DIMENSION,
162 SHARE_BARCODE_DIMENSION);
163 } catch (WriterException we) {
164 Log.w(TAG, we.toString());
170 File bsRoot = new File(Environment.getExternalStorageDirectory(), "BarcodeScanner");
172 File barcodesRoot = new File(bsRoot, "barcodes");
173 barcodesRoot.mkdir();
174 barcodeFile = new File(barcodesRoot, "barcode-" + System.currentTimeMillis() + ".png");
175 barcodeFile.delete();
176 FileOutputStream fos = new FileOutputStream(barcodeFile);
177 bitmap.compress(Bitmap.CompressFormat.PNG, 0, fos);
179 } catch (IOException ioe) {
180 Log.w(TAG, ioe.toString());
184 Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
185 intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.app_name) + " - " + qrCodeEncoder.getTitle());
186 intent.putExtra(Intent.EXTRA_TEXT, qrCodeEncoder.getContents());
187 intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + barcodeFile.getAbsolutePath()));
188 intent.setType("image/png");
189 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
190 startActivity(Intent.createChooser(intent, null));
195 protected void onResume() {
198 View layout = findViewById(R.id.encode_view);
199 layout.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
203 private void showErrorMessage(int message) {
204 if (progressDialog != null) {
205 progressDialog.dismiss();
206 progressDialog = null;
208 AlertDialog.Builder builder = new AlertDialog.Builder(this);
209 builder.setMessage(message);
210 builder.setPositiveButton(R.string.button_ok, clickListener);