Add history feature; group some functionality into subpackages
[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 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.View;
31 import android.view.ViewTreeObserver.OnGlobalLayoutListener;
32 import android.widget.ImageView;
33 import android.widget.TextView;
34 import com.google.zxing.client.android.R;
35 import com.google.zxing.client.android.Intents;
36
37 /**
38  * This class encodes data from an Intent into a QR code, and then displays it full screen so that
39  * another person can scan it with their device.
40  *
41  * @author dswitkin@google.com (Daniel Switkin)
42  */
43 final class EncodeActivity extends Activity {
44   private QRCodeEncoder qrCodeEncoder;
45   private ProgressDialog progressDialog;
46   private boolean firstLayout;
47
48   /**
49    * This needs to be delayed until after the first layout so that the view dimensions will be
50    * available.
51    */
52   private final OnGlobalLayoutListener layoutListener = new OnGlobalLayoutListener() {
53     public void onGlobalLayout() {
54       if (firstLayout) {
55         View layout = findViewById(R.id.encode_view);
56         int width = layout.getWidth();
57         int height = layout.getHeight();
58         int smallerDimension = width < height ? width : height;
59         smallerDimension = smallerDimension * 7 / 8;
60
61         Intent intent = getIntent();
62         try {
63           qrCodeEncoder = new QRCodeEncoder(EncodeActivity.this, intent);
64           setTitle(getString(R.string.app_name) + " - " + qrCodeEncoder.getTitle());
65           qrCodeEncoder.requestBarcode(handler, smallerDimension);
66           progressDialog = ProgressDialog.show(EncodeActivity.this, null,
67               getString(R.string.msg_encode_in_progress), true, true, cancelListener);
68         } catch (IllegalArgumentException e) {
69           showErrorMessage(R.string.msg_encode_contents_failed);
70         }
71         firstLayout = false;
72       }
73     }
74   };
75
76   private final Handler handler = new Handler() {
77     @Override
78     public void handleMessage(Message message) {
79       switch (message.what) {
80         case R.id.encode_succeeded:
81           progressDialog.dismiss();
82           progressDialog = null;
83           Bitmap image = (Bitmap) message.obj;
84           ImageView view = (ImageView) findViewById(R.id.image_view);
85           view.setImageBitmap(image);
86           TextView contents = (TextView) findViewById(R.id.contents_text_view);
87           contents.setText(qrCodeEncoder.getDisplayContents());
88           qrCodeEncoder = null;
89           break;
90         case R.id.encode_failed:
91           showErrorMessage(R.string.msg_encode_barcode_failed);
92           qrCodeEncoder = null;
93           break;
94       }
95     }
96   };
97
98   private final OnClickListener clickListener = new OnClickListener() {
99     public void onClick(DialogInterface dialog, int which) {
100       finish();
101     }
102   };
103
104   private final OnCancelListener cancelListener = new OnCancelListener() {
105     public void onCancel(DialogInterface dialog) {
106       finish();
107     }
108   };
109
110   @Override
111   public void onCreate(Bundle icicle) {
112     super.onCreate(icicle);
113
114     Intent intent = getIntent();
115     if (intent != null && (intent.getAction().equals(Intents.Encode.ACTION))) {
116       setContentView(R.layout.encode);
117     } else {
118       finish();
119     }
120   }
121
122   @Override
123   protected void onResume() {
124     super.onResume();
125
126     View layout = findViewById(R.id.encode_view);
127     layout.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
128     firstLayout = true;
129   }
130
131   private void showErrorMessage(int message) {
132     if (progressDialog != null) {
133       progressDialog.dismiss();
134       progressDialog = null;
135     }
136     AlertDialog.Builder builder = new AlertDialog.Builder(this);
137     builder.setMessage(message);
138     builder.setPositiveButton(R.string.button_ok, clickListener);
139     builder.show();
140   }
141 }