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.client.android.Intents;
20 import com.google.zxing.client.android.R;
22 import android.app.Activity;
23 import android.app.AlertDialog;
24 import android.app.ProgressDialog;
25 import android.content.DialogInterface;
26 import android.content.DialogInterface.OnCancelListener;
27 import android.content.DialogInterface.OnClickListener;
28 import android.content.Intent;
29 import android.graphics.Bitmap;
30 import android.os.Bundle;
31 import android.os.Handler;
32 import android.os.Message;
33 import android.view.View;
34 import android.view.ViewTreeObserver.OnGlobalLayoutListener;
35 import android.widget.ImageView;
36 import android.widget.TextView;
39 * This class encodes data from an Intent into a QR code, and then displays it full screen so that
40 * another person can scan it with their device.
42 * @author dswitkin@google.com (Daniel Switkin)
44 public final class EncodeActivity extends Activity {
45 private QRCodeEncoder qrCodeEncoder;
46 private ProgressDialog progressDialog;
47 private boolean firstLayout;
50 * This needs to be delayed until after the first layout so that the view dimensions will be
53 private final OnGlobalLayoutListener layoutListener = new OnGlobalLayoutListener() {
54 public void onGlobalLayout() {
56 View layout = findViewById(R.id.encode_view);
57 int width = layout.getWidth();
58 int height = layout.getHeight();
59 int smallerDimension = width < height ? width : height;
60 smallerDimension = smallerDimension * 7 / 8;
62 Intent intent = getIntent();
64 qrCodeEncoder = new QRCodeEncoder(EncodeActivity.this, intent);
65 setTitle(getString(R.string.app_name) + " - " + qrCodeEncoder.getTitle());
66 qrCodeEncoder.requestBarcode(handler, smallerDimension);
67 progressDialog = ProgressDialog.show(EncodeActivity.this, null,
68 getString(R.string.msg_encode_in_progress), true, true, cancelListener);
69 } catch (IllegalArgumentException e) {
70 showErrorMessage(R.string.msg_encode_contents_failed);
77 private final Handler handler = new Handler() {
79 public void handleMessage(Message message) {
80 switch (message.what) {
81 case R.id.encode_succeeded:
82 progressDialog.dismiss();
83 progressDialog = null;
84 Bitmap image = (Bitmap) message.obj;
85 ImageView view = (ImageView) findViewById(R.id.image_view);
86 view.setImageBitmap(image);
87 TextView contents = (TextView) findViewById(R.id.contents_text_view);
88 contents.setText(qrCodeEncoder.getDisplayContents());
91 case R.id.encode_failed:
92 showErrorMessage(R.string.msg_encode_barcode_failed);
99 private final OnClickListener clickListener = new OnClickListener() {
100 public void onClick(DialogInterface dialog, int which) {
105 private final OnCancelListener cancelListener = new OnCancelListener() {
106 public void onCancel(DialogInterface dialog) {
112 public void onCreate(Bundle icicle) {
113 super.onCreate(icicle);
115 Intent intent = getIntent();
116 if (intent != null) {
117 String action = intent.getAction();
118 if (action.equals(Intents.Encode.ACTION) || action.equals(Intent.ACTION_SEND)) {
119 setContentView(R.layout.encode);
127 protected void onResume() {
130 View layout = findViewById(R.id.encode_view);
131 layout.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
135 private void showErrorMessage(int message) {
136 if (progressDialog != null) {
137 progressDialog.dismiss();
138 progressDialog = null;
140 AlertDialog.Builder builder = new AlertDialog.Builder(this);
141 builder.setMessage(message);
142 builder.setPositiveButton(R.string.button_ok, clickListener);