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