aae1b98c48b1af966260a08ef60979674c0b0924
[zxing.git] / android / src / com / android / barcodes / BarcodesEncodeActivity.java
1 /*
2  * Copyright (C) 2008 Google Inc.
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.android.barcodes;
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.ViewTreeObserver.OnGlobalLayoutListener;
31 import android.widget.ImageView;
32 import android.widget.LinearLayout;
33 import android.widget.TextView;
34
35 /**
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.
38  */
39 public class BarcodesEncodeActivity extends Activity {
40
41     private QRCodeEncoder mQRCodeEncoder;
42     private ProgressDialog mProgressDialog;
43     private boolean mFirstLayout;
44
45     @Override
46     public void onCreate(Bundle icicle) {
47         super.onCreate(icicle);
48
49         Intent intent = getIntent();
50         if (intent == null || !intent.getAction().equals(Intents.Encode.ACTION)) {
51             finish();
52         } else {
53             setContentView(R.layout.encode);
54         }
55     }
56
57     @Override
58     protected void onResume() {
59         super.onResume();
60
61         LinearLayout layout = (LinearLayout) findViewById(R.id.encode_view);
62         layout.getViewTreeObserver().addOnGlobalLayoutListener(mLayoutListener);
63         mFirstLayout = true;
64     }
65
66     /**
67      * This needs to be delayed until after the first layout so that the view dimensions will be
68      * available.
69      */
70     public OnGlobalLayoutListener mLayoutListener = new OnGlobalLayoutListener() {
71         public void onGlobalLayout() {
72             if (mFirstLayout) {
73                 LinearLayout layout = (LinearLayout) findViewById(R.id.encode_view);
74                 int width = layout.getWidth();
75                 int height = layout.getHeight();
76                 int smallerDimension = (width < height) ? width : height;
77
78                 Intent intent = getIntent();
79                 try {
80                     mQRCodeEncoder = new QRCodeEncoder(BarcodesEncodeActivity.this, intent);
81                     setTitle(getString(R.string.app_name) + " - " + mQRCodeEncoder.getTitle());
82                     mQRCodeEncoder.requestBarcode(mHandler, smallerDimension);
83                     mProgressDialog = ProgressDialog.show(BarcodesEncodeActivity.this, null,
84                             getString(R.string.msg_encode_in_progress), true, true, mCancelListener);
85                 } catch (IllegalArgumentException e) {
86                     showErrorMessage(R.string.msg_encode_contents_failed);
87                 }
88                 mFirstLayout = false;
89             }
90         }
91     };
92
93     public Handler mHandler = new Handler() {
94         public void handleMessage(Message message) {
95             switch (message.what) {
96                 case R.id.encode_succeeded:
97                     mProgressDialog.dismiss();
98                     mProgressDialog = null;
99                     Bitmap image = (Bitmap)message.obj;
100                     ImageView view = (ImageView) findViewById(R.id.image_view);
101                     view.setImageBitmap(image);
102                     TextView contents = (TextView) findViewById(R.id.contents_text_view);
103                     contents.setText(mQRCodeEncoder.getDisplayContents());
104                     mQRCodeEncoder = null;
105                     break;
106                 case R.id.encode_failed:
107                     showErrorMessage(R.string.msg_encode_barcode_failed);
108                     mQRCodeEncoder = null;
109                     break;
110             }
111         }
112     };
113
114     private void showErrorMessage(int message) {
115         if (mProgressDialog != null) {
116             mProgressDialog.dismiss();
117             mProgressDialog = null;
118         }
119         AlertDialog.Builder builder = new AlertDialog.Builder(this);
120         builder.setMessage(message);
121         builder.setPositiveButton(R.string.button_ok, mClickListener);
122         builder.show();
123     }
124
125     private OnClickListener mClickListener = new OnClickListener() {
126         public void onClick(DialogInterface dialog, int which) {
127             finish();
128         }
129     };
130
131     private OnCancelListener mCancelListener = new OnCancelListener() {
132         public void onCancel(DialogInterface dialog) {
133             finish();
134         }
135     };
136
137 }