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;
19 import android.app.Activity;
20 import android.app.AlertDialog;
21 import android.app.ProgressDialog;
22 import android.content.DialogInterface;
23 import android.content.DialogInterface.OnCancelListener;
24 import android.content.DialogInterface.OnClickListener;
25 import android.content.Intent;
26 import android.graphics.Bitmap;
27 import android.os.Bundle;
28 import android.os.Handler;
29 import android.os.Message;
30 import android.view.ViewTreeObserver.OnGlobalLayoutListener;
31 import android.widget.ImageView;
32 import android.widget.LinearLayout;
33 import android.widget.TextView;
36 * This class encodes data from an Intent into a QR code, and then displays it full screen so that
37 * another person can scan it with their device.
39 public class EncodeActivity extends Activity {
41 private QRCodeEncoder mQRCodeEncoder;
42 private ProgressDialog mProgressDialog;
43 private boolean mFirstLayout;
46 public void onCreate(Bundle icicle) {
47 super.onCreate(icicle);
49 Intent intent = getIntent();
50 if (intent != null && (intent.getAction().equals(Intents.Encode.ACTION) ||
51 intent.getAction().equals(Intents.Encode.DEPRECATED_ACTION))) {
52 setContentView(R.layout.encode);
59 protected void onResume() {
62 LinearLayout layout = (LinearLayout) findViewById(R.id.encode_view);
63 layout.getViewTreeObserver().addOnGlobalLayoutListener(mLayoutListener);
68 * This needs to be delayed until after the first layout so that the view dimensions will be
71 public OnGlobalLayoutListener mLayoutListener = new OnGlobalLayoutListener() {
72 public void onGlobalLayout() {
74 LinearLayout layout = (LinearLayout) 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 mQRCodeEncoder = new QRCodeEncoder(EncodeActivity.this, intent);
83 setTitle(getString(R.string.app_name) + " - " + mQRCodeEncoder.getTitle());
84 mQRCodeEncoder.requestBarcode(mHandler, smallerDimension);
85 mProgressDialog = ProgressDialog.show(EncodeActivity.this, null,
86 getString(R.string.msg_encode_in_progress), true, true, mCancelListener);
87 } catch (IllegalArgumentException e) {
88 showErrorMessage(R.string.msg_encode_contents_failed);
95 public Handler mHandler = new Handler() {
96 public void handleMessage(Message message) {
97 switch (message.what) {
98 case R.id.encode_succeeded:
99 mProgressDialog.dismiss();
100 mProgressDialog = null;
101 Bitmap image = (Bitmap) message.obj;
102 ImageView view = (ImageView) findViewById(R.id.image_view);
103 view.setImageBitmap(image);
104 TextView contents = (TextView) findViewById(R.id.contents_text_view);
105 contents.setText(mQRCodeEncoder.getDisplayContents());
106 mQRCodeEncoder = null;
108 case R.id.encode_failed:
109 showErrorMessage(R.string.msg_encode_barcode_failed);
110 mQRCodeEncoder = null;
116 private void showErrorMessage(int message) {
117 if (mProgressDialog != null) {
118 mProgressDialog.dismiss();
119 mProgressDialog = null;
121 AlertDialog.Builder builder = new AlertDialog.Builder(this);
122 builder.setMessage(message);
123 builder.setPositiveButton(R.string.button_ok, mClickListener);
127 private OnClickListener mClickListener = new OnClickListener() {
128 public void onClick(DialogInterface dialog, int which) {
133 private OnCancelListener mCancelListener = new OnCancelListener() {
134 public void onCancel(DialogInterface dialog) {