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