Issue 294, add support for several address lines in parsed results
[zxing.git] / android / src / com / google / zxing / client / android / encode / EncodeActivity.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.android.encode;
18
19 import com.google.zxing.client.android.Intents;
20 import com.google.zxing.client.android.R;
21
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;
37
38 /**
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.
41  *
42  * @author dswitkin@google.com (Daniel Switkin)
43  */
44 public final class EncodeActivity extends Activity {
45   private QRCodeEncoder qrCodeEncoder;
46   private ProgressDialog progressDialog;
47   private boolean firstLayout;
48
49   /**
50    * This needs to be delayed until after the first layout so that the view dimensions will be
51    * available.
52    */
53   private final OnGlobalLayoutListener layoutListener = new OnGlobalLayoutListener() {
54     public void onGlobalLayout() {
55       if (firstLayout) {
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;
61
62         Intent intent = getIntent();
63         try {
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);
71         }
72         firstLayout = false;
73       }
74     }
75   };
76
77   private final Handler handler = new Handler() {
78     @Override
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());
89           qrCodeEncoder = null;
90           break;
91         case R.id.encode_failed:
92           showErrorMessage(R.string.msg_encode_barcode_failed);
93           qrCodeEncoder = null;
94           break;
95       }
96     }
97   };
98
99   private final OnClickListener clickListener = new OnClickListener() {
100     public void onClick(DialogInterface dialog, int which) {
101       finish();
102     }
103   };
104
105   private final OnCancelListener cancelListener = new OnCancelListener() {
106     public void onCancel(DialogInterface dialog) {
107       finish();
108     }
109   };
110
111   @Override
112   public void onCreate(Bundle icicle) {
113     super.onCreate(icicle);
114
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);
120         return;
121       }
122     }
123     finish();
124   }
125
126   @Override
127   protected void onResume() {
128     super.onResume();
129
130     View layout = findViewById(R.id.encode_view);
131     layout.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
132     firstLayout = true;
133   }
134
135   private void showErrorMessage(int message) {
136     if (progressDialog != null) {
137       progressDialog.dismiss();
138       progressDialog = null;
139     }
140     AlertDialog.Builder builder = new AlertDialog.Builder(this);
141     builder.setMessage(message);
142     builder.setPositiveButton(R.string.button_ok, clickListener);
143     builder.show();
144   }
145 }