Added send barcode feature
[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.BarcodeFormat;
20 import com.google.zxing.WriterException;
21 import com.google.zxing.client.android.Intents;
22 import com.google.zxing.client.android.R;
23
24 import android.app.Activity;
25 import android.app.AlertDialog;
26 import android.app.ProgressDialog;
27 import android.content.DialogInterface;
28 import android.content.DialogInterface.OnCancelListener;
29 import android.content.DialogInterface.OnClickListener;
30 import android.content.Intent;
31 import android.graphics.Bitmap;
32 import android.net.Uri;
33 import android.os.Bundle;
34 import android.os.Handler;
35 import android.os.Environment;
36 import android.os.Message;
37 import android.util.Log;
38 import android.view.Menu;
39 import android.view.MenuItem;
40 import android.view.View;
41 import android.view.ViewTreeObserver.OnGlobalLayoutListener;
42 import android.widget.ImageView;
43 import android.widget.TextView;
44
45 import java.io.File;
46 import java.io.FileOutputStream;
47 import java.io.IOException;
48
49 /**
50  * This class encodes data from an Intent into a QR code, and then displays it full screen so that
51  * another person can scan it with their device.
52  *
53  * @author dswitkin@google.com (Daniel Switkin)
54  */
55 public final class EncodeActivity extends Activity {
56
57   private static final String TAG = EncodeActivity.class.getSimpleName();
58
59   private static final int SHARE_BARCODE_DIMENSION = 300;
60
61   private QRCodeEncoder qrCodeEncoder;
62   private ProgressDialog progressDialog;
63   private boolean firstLayout;
64
65   /**
66    * This needs to be delayed until after the first layout so that the view dimensions will be
67    * available.
68    */
69   private final OnGlobalLayoutListener layoutListener = new OnGlobalLayoutListener() {
70     public void onGlobalLayout() {
71       if (firstLayout) {
72         View layout = findViewById(R.id.encode_view);
73         int width = layout.getWidth();
74         int height = layout.getHeight();
75         int smallerDimension = width < height ? width : height;
76         smallerDimension = smallerDimension * 7 / 8;
77
78         Intent intent = getIntent();
79         try {
80           qrCodeEncoder = new QRCodeEncoder(EncodeActivity.this, intent);
81           setTitle(getString(R.string.app_name) + " - " + qrCodeEncoder.getTitle());
82           qrCodeEncoder.requestBarcode(handler, smallerDimension);
83           progressDialog = ProgressDialog.show(EncodeActivity.this, null,
84               getString(R.string.msg_encode_in_progress), true, true, cancelListener);
85         } catch (IllegalArgumentException e) {
86           showErrorMessage(R.string.msg_encode_contents_failed);
87         }
88         firstLayout = false;
89       }
90     }
91   };
92
93   private final Handler handler = new Handler() {
94     @Override
95     public void handleMessage(Message message) {
96       switch (message.what) {
97         case R.id.encode_succeeded:
98           progressDialog.dismiss();
99           progressDialog = null;
100           Bitmap image = (Bitmap) message.obj;
101           ImageView view = (ImageView) findViewById(R.id.image_view);
102           view.setImageBitmap(image);
103           TextView contents = (TextView) findViewById(R.id.contents_text_view);
104           contents.setText(qrCodeEncoder.getDisplayContents());
105           //qrCodeEncoder = null;
106           break;
107         case R.id.encode_failed:
108           showErrorMessage(R.string.msg_encode_barcode_failed);
109           qrCodeEncoder = null;
110           break;
111       }
112     }
113   };
114
115   private final OnClickListener clickListener = new OnClickListener() {
116     public void onClick(DialogInterface dialog, int which) {
117       finish();
118     }
119   };
120
121   private final OnCancelListener cancelListener = new OnCancelListener() {
122     public void onCancel(DialogInterface dialog) {
123       finish();
124     }
125   };
126
127   @Override
128   public void onCreate(Bundle icicle) {
129     super.onCreate(icicle);
130
131     Intent intent = getIntent();
132     if (intent != null) {
133       String action = intent.getAction();
134       if (action.equals(Intents.Encode.ACTION) || action.equals(Intent.ACTION_SEND)) {
135         setContentView(R.layout.encode);
136         return;
137       }
138     }
139     finish();
140   }
141
142   @Override
143   public boolean onCreateOptionsMenu(Menu menu) {
144     super.onCreateOptionsMenu(menu);
145     menu.add(0, Menu.FIRST, 0, R.string.menu_share).setIcon(android.R.drawable.ic_menu_share);
146     return true;
147   }
148
149   @Override
150   public boolean onOptionsItemSelected(MenuItem item) {
151     if (qrCodeEncoder == null) { // Odd
152       Log.w(TAG, "No existing barcode to send?");
153       return true;
154     }
155
156
157     Bitmap bitmap;
158     try {
159       bitmap = QRCodeEncoder.encodeAsBitmap(qrCodeEncoder.getContents(),
160                                             BarcodeFormat.QR_CODE,
161                                             SHARE_BARCODE_DIMENSION,
162                                             SHARE_BARCODE_DIMENSION);
163     } catch (WriterException we) {
164       Log.w(TAG, we.toString());
165       return true;
166     }
167
168     File barcodeFile;
169     try {
170       File bsRoot = new File(Environment.getExternalStorageDirectory(), "BarcodeScanner");
171       bsRoot.mkdir();
172       File barcodesRoot = new File(bsRoot, "barcodes");
173       barcodesRoot.mkdir();
174       barcodeFile = new File(barcodesRoot, "barcode-" + System.currentTimeMillis() + ".png");
175       barcodeFile.delete();
176       FileOutputStream fos = new FileOutputStream(barcodeFile);
177       bitmap.compress(Bitmap.CompressFormat.PNG, 0, fos);
178       fos.close();
179     } catch (IOException ioe) {
180       Log.w(TAG, ioe.toString());
181       return true;
182     }
183
184     Intent intent = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
185     intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.app_name) + " - " + qrCodeEncoder.getTitle());
186     intent.putExtra(Intent.EXTRA_TEXT, qrCodeEncoder.getContents());
187     intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + barcodeFile.getAbsolutePath()));
188     intent.setType("image/png");
189     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
190     startActivity(Intent.createChooser(intent, null));
191     return true;
192   }
193
194   @Override
195   protected void onResume() {
196     super.onResume();
197
198     View layout = findViewById(R.id.encode_view);
199     layout.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
200     firstLayout = true;
201   }
202
203   private void showErrorMessage(int message) {
204     if (progressDialog != null) {
205       progressDialog.dismiss();
206       progressDialog = null;
207     }
208     AlertDialog.Builder builder = new AlertDialog.Builder(this);
209     builder.setMessage(message);
210     builder.setPositiveButton(R.string.button_ok, clickListener);
211     builder.show();
212   }
213 }