Issue 521, avoid an NPE
[zxing.git] / android / src / com / google / zxing / client / android / encode / QRCodeEncoder.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.EncodeHintType;
21 import com.google.zxing.MultiFormatWriter;
22 import com.google.zxing.Result;
23 import com.google.zxing.WriterException;
24 import com.google.zxing.client.android.Contents;
25 import com.google.zxing.client.android.Intents;
26 import com.google.zxing.client.android.R;
27 import com.google.zxing.client.result.AddressBookParsedResult;
28 import com.google.zxing.client.result.ParsedResult;
29 import com.google.zxing.client.result.ResultParser;
30 import com.google.zxing.common.BitMatrix;
31
32 import android.app.Activity;
33 import android.content.Intent;
34 import android.graphics.Bitmap;
35 import android.net.Uri;
36 import android.os.Bundle;
37 import android.os.Handler;
38 import android.provider.Contacts;
39 import android.telephony.PhoneNumberUtils;
40 import android.util.Log;
41
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.util.Hashtable;
45
46 /**
47  * This class does the work of decoding the user's request and extracting all the data
48  * to be encoded in a barcode.
49  *
50  * @author dswitkin@google.com (Daniel Switkin)
51  */
52 final class QRCodeEncoder {
53
54   private static final String TAG = QRCodeEncoder.class.getSimpleName();
55
56   private static final int WHITE = 0xFFFFFFFF;
57   private static final int BLACK = 0xFF000000;
58
59   private final Activity activity;
60   private String contents;
61   private String displayContents;
62   private String title;
63   private BarcodeFormat format;
64
65   QRCodeEncoder(Activity activity, Intent intent) {
66     this.activity = activity;
67     if (intent == null) {
68       throw new IllegalArgumentException("No valid data to encode.");
69     }
70
71     String action = intent.getAction();
72     if (action.equals(Intents.Encode.ACTION)) {
73       if (!encodeContentsFromZXingIntent(intent)) {
74         throw new IllegalArgumentException("No valid data to encode.");
75       }
76     } else if (action.equals(Intent.ACTION_SEND)) {
77       if (!encodeContentsFromShareIntent(intent)) {
78         throw new IllegalArgumentException("No valid data to encode.");
79       }
80     }
81   }
82
83   public void requestBarcode(Handler handler, int pixelResolution) {
84     Thread encodeThread = new EncodeThread(contents, handler, pixelResolution,
85         format);
86     encodeThread.start();
87   }
88
89   public String getContents() {
90     return contents;
91   }
92
93   public String getDisplayContents() {
94     return displayContents;
95   }
96
97   public String getTitle() {
98     return title;
99   }
100
101   public String getFormat() {
102     return format.toString();
103   }
104
105   // It would be nice if the string encoding lived in the core ZXing library,
106   // but we use platform specific code like PhoneNumberUtils, so it can't.
107   private boolean encodeContentsFromZXingIntent(Intent intent) {
108      // Default to QR_CODE if no format given.
109     String formatString = intent.getStringExtra(Intents.Encode.FORMAT);
110     try {
111       format = BarcodeFormat.valueOf(formatString);
112     } catch (IllegalArgumentException iae) {
113       // Ignore it then
114       format = null;
115       formatString = null;
116     }
117     if (format == null || BarcodeFormat.QR_CODE.equals(format)) {
118       String type = intent.getStringExtra(Intents.Encode.TYPE);
119       if (type == null || type.length() == 0) {
120         return false;
121       }
122       this.format = BarcodeFormat.QR_CODE;
123       encodeQRCodeContents(intent, type);
124     } else {
125       String data = intent.getStringExtra(Intents.Encode.DATA);
126       if (data != null && data.length() > 0) {
127         contents = data;
128         displayContents = data;
129         title = activity.getString(R.string.contents_text);
130       }
131     }
132     return contents != null && contents.length() > 0;
133   }
134
135   // Handles send intents from the Contacts app, retrieving a contact as a VCARD.
136   private boolean encodeContentsFromShareIntent(Intent intent) {
137     format = BarcodeFormat.QR_CODE;
138     try {
139       Uri uri = (Uri)intent.getExtras().getParcelable(Intent.EXTRA_STREAM);
140       InputStream stream = activity.getContentResolver().openInputStream(uri);
141       int length = stream.available();
142       if (length <= 0) {
143         Log.w(TAG, "Content stream is empty");
144         return false;
145       }
146       byte[] vcard = new byte[length];
147       int bytesRead = stream.read(vcard, 0, length);
148       if (bytesRead < length) {
149         Log.w(TAG, "Unable to fully read available bytes from content stream");
150         return false;
151       }
152       String vcardString = new String(vcard, 0, bytesRead, "UTF-8");
153       Log.d(TAG, "Encoding share intent content:");
154       Log.d(TAG, vcardString);
155       Result result = new Result(vcardString, vcard, null, BarcodeFormat.QR_CODE);
156       ParsedResult parsedResult = ResultParser.parseResult(result);
157       if (!(parsedResult instanceof AddressBookParsedResult)) {
158         Log.d(TAG, "Result was not an address");
159         return false;
160       }
161       if (!encodeQRCodeContents((AddressBookParsedResult) parsedResult)) {
162         Log.d(TAG, "Unable to encode contents");
163         return false;
164       }
165     } catch (IOException e) {
166       Log.w(TAG, e);
167       return false;
168     } catch (NullPointerException e) {
169       Log.w(TAG, e);
170       // In case the uri was not found in the Intent.
171       return false;
172     }
173     return contents != null && contents.length() > 0;
174   }
175
176   private void encodeQRCodeContents(Intent intent, String type) {
177     if (type.equals(Contents.Type.TEXT)) {
178       String data = intent.getStringExtra(Intents.Encode.DATA);
179       if (data != null && data.length() > 0) {
180         contents = data;
181         displayContents = data;
182         title = activity.getString(R.string.contents_text);
183       }
184     } else if (type.equals(Contents.Type.EMAIL)) {
185       String data = trim(intent.getStringExtra(Intents.Encode.DATA));
186       if (data != null) {
187         contents = "mailto:" + data;
188         displayContents = data;
189         title = activity.getString(R.string.contents_email);
190       }
191     } else if (type.equals(Contents.Type.PHONE)) {
192       String data = trim(intent.getStringExtra(Intents.Encode.DATA));
193       if (data != null) {
194         contents = "tel:" + data;
195         displayContents = PhoneNumberUtils.formatNumber(data);
196         title = activity.getString(R.string.contents_phone);
197       }
198     } else if (type.equals(Contents.Type.SMS)) {
199       String data = trim(intent.getStringExtra(Intents.Encode.DATA));
200       if (data != null) {
201         contents = "sms:" + data;
202         displayContents = PhoneNumberUtils.formatNumber(data);
203         title = activity.getString(R.string.contents_sms);
204       }
205     } else if (type.equals(Contents.Type.CONTACT)) {
206       Bundle bundle = intent.getBundleExtra(Intents.Encode.DATA);
207       if (bundle != null) {
208         StringBuilder newContents = new StringBuilder();
209         StringBuilder newDisplayContents = new StringBuilder();
210         newContents.append("MECARD:");
211         String name = trim(bundle.getString(Contacts.Intents.Insert.NAME));
212         if (name != null) {
213           newContents.append("N:").append(name).append(';');
214           newDisplayContents.append(name);
215         }
216         String address = trim(bundle.getString(Contacts.Intents.Insert.POSTAL));
217         if (address != null) {
218           newContents.append("ADR:").append(address).append(';');
219           newDisplayContents.append('\n').append(address);
220         }
221         for (int x = 0; x < Contents.PHONE_KEYS.length; x++) {
222           String phone = trim(bundle.getString(Contents.PHONE_KEYS[x]));
223           if (phone != null) {
224             newContents.append("TEL:").append(phone).append(';');
225             newDisplayContents.append('\n').append(PhoneNumberUtils.formatNumber(phone));
226           }
227         }
228         for (int x = 0; x < Contents.EMAIL_KEYS.length; x++) {
229           String email = trim(bundle.getString(Contents.EMAIL_KEYS[x]));
230           if (email != null) {
231             newContents.append("EMAIL:").append(email).append(';');
232             newDisplayContents.append('\n').append(email);
233           }
234         }
235         // Make sure we've encoded at least one field.
236         if (newDisplayContents.length() > 0) {
237           newContents.append(';');
238           contents = newContents.toString();
239           displayContents = newDisplayContents.toString();
240           title = activity.getString(R.string.contents_contact);
241         } else {
242           contents = null;
243           displayContents = null;
244         }
245       }
246     } else if (type.equals(Contents.Type.LOCATION)) {
247       Bundle bundle = intent.getBundleExtra(Intents.Encode.DATA);
248       if (bundle != null) {
249         // These must use Bundle.getFloat(), not getDouble(), it's part of the API.
250         float latitude = bundle.getFloat("LAT", Float.MAX_VALUE);
251         float longitude = bundle.getFloat("LONG", Float.MAX_VALUE);
252         if (latitude != Float.MAX_VALUE && longitude != Float.MAX_VALUE) {
253           contents = "geo:" + latitude + ',' + longitude;
254           displayContents = latitude + "," + longitude;
255           title = activity.getString(R.string.contents_location);
256         }
257       }
258     }
259   }
260
261   private boolean encodeQRCodeContents(AddressBookParsedResult contact) {
262     StringBuilder newContents = new StringBuilder();
263     StringBuilder newDisplayContents = new StringBuilder();
264     newContents.append("MECARD:");
265     String[] names = contact.getNames();
266     if (names != null && names.length > 0) {
267       String name = trim(names[0]);
268       if (name != null) {
269         newContents.append("N:").append(name).append(';');
270         newDisplayContents.append(name);
271       }
272     }
273     String[] addresses = contact.getAddresses();
274     if (addresses != null) {
275       for (String address : addresses) {
276         address = trim(address);
277         if (address != null) {
278           newContents.append("ADR:").append(address).append(';');
279           newDisplayContents.append('\n').append(address);
280         }
281       }
282     }
283     String[] phoneNumbers = contact.getPhoneNumbers();
284     if (phoneNumbers != null) {
285       for (String phone : phoneNumbers) {
286         phone = trim(phone);
287         if (phone != null) {
288           newContents.append("TEL:").append(phone).append(';');
289           newDisplayContents.append('\n').append(PhoneNumberUtils.formatNumber(phone));
290         }
291       }
292     }
293     String[] emails = contact.getEmails();
294     if (emails != null) {
295       for (String email : emails) {
296         email = trim(email);
297         if (email != null) {
298           newContents.append("EMAIL:").append(email).append(';');
299           newDisplayContents.append('\n').append(email);
300         }
301       }
302     }
303     String url = trim(contact.getURL());
304     if (url != null) {
305       newContents.append("URL:").append(url).append(';');
306       newDisplayContents.append('\n').append(url);
307     }
308     // Make sure we've encoded at least one field.
309     if (newDisplayContents.length() > 0) {
310       newContents.append(';');
311       contents = newContents.toString();
312       displayContents = newDisplayContents.toString();
313       title = activity.getString(R.string.contents_contact);
314       return true;
315     } else {
316       contents = null;
317       displayContents = null;
318       return false;
319     }
320   }
321
322   static Bitmap encodeAsBitmap(String contents,
323                                BarcodeFormat format,
324                                int desiredWidth,
325                                int desiredHeight) throws WriterException {
326     Hashtable hints = null;
327     String encoding = guessAppropriateEncoding(contents);
328     if (encoding != null) {
329       hints = new Hashtable(2);
330       hints.put(EncodeHintType.CHARACTER_SET, encoding);
331     }
332     MultiFormatWriter writer = new MultiFormatWriter();    
333     BitMatrix result = writer.encode(contents, format, desiredWidth, desiredHeight, hints);
334     int width = result.getWidth();
335     int height = result.getHeight();
336     int[] pixels = new int[width * height];
337     // All are 0, or black, by default
338     for (int y = 0; y < height; y++) {
339       int offset = y * width;
340       for (int x = 0; x < width; x++) {
341         pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
342       }
343     }
344
345     Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
346     bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
347     return bitmap;
348   }
349
350   private static String guessAppropriateEncoding(CharSequence contents) {
351     // Very crude at the moment
352     for (int i = 0; i < contents.length(); i++) {
353       if (contents.charAt(i) > 0xFF) {
354         return "UTF-8";
355       }
356     }
357     return null;
358   }
359
360   private static String trim(String s) {
361     if (s == null) {
362       return null;
363     }
364     s = s.trim();
365     return s.length() == 0 ? null : s;
366   }
367
368 }