2 * Copyright (C) 2008 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.android;
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.graphics.Bitmap;
22 import android.graphics.BitmapFactory;
23 import android.os.Bundle;
24 import android.os.Handler;
25 import android.os.Message;
26 import android.provider.Contacts;
27 import android.util.Log;
28 import org.apache.http.HttpEntity;
29 import org.apache.http.HttpResponse;
30 import org.apache.http.client.methods.HttpGet;
33 import java.net.URLEncoder;
35 public final class QRCodeEncoder {
37 private static final String TAG = "QRCodeEncoder";
38 private static final String CHART_SERVER_URL =
39 "//chart.apis.google." + LocaleManager.getCountryTLD() + "/chart?cht=qr&chs=";
41 private final Activity mActivity;
42 private String mContents;
43 private String mDisplayContents;
44 private String mTitle;
45 private String mUserAgent;
47 public QRCodeEncoder(Activity activity, Intent intent) {
49 if (!encodeContents(intent)) {
50 throw new IllegalArgumentException("No valid data to encode.");
52 mUserAgent = mActivity.getString(R.string.zxing_user_agent);
55 // Once the core ZXing library supports encoding, we'll be able to generate the bitmap
56 // synchronously. For now, it's a network request, so it's handled on a thread.
57 public void requestBarcode(Handler handler, int pixelResolution) {
58 Thread mNetworkThread = new NetworkThread(mContents, handler, pixelResolution);
59 mNetworkThread.start();
62 public String getContents() {
66 public String getDisplayContents() {
67 return mDisplayContents;
70 public String getTitle() {
74 // Perhaps the string encoding should live in the core ZXing library too.
75 private boolean encodeContents(Intent intent) {
76 if (intent == null) return false;
77 String type = intent.getStringExtra(Intents.Encode.TYPE);
78 if (type == null || type.length() == 0) return false;
80 if (type.equals(Contents.Type.TEXT)) {
81 String string = intent.getStringExtra(Intents.Encode.DATA);
82 if (string != null && string.length() > 0) {
84 mDisplayContents = string;
85 mTitle = mActivity.getString(R.string.contents_text);
87 } else if (type.equals(Contents.Type.EMAIL)) {
88 String string = intent.getStringExtra(Intents.Encode.DATA);
89 if (string != null && string.length() > 0) {
90 mContents = "mailto:" + string;
91 mDisplayContents = string;
92 mTitle = mActivity.getString(R.string.contents_email);
94 } else if (type.equals(Contents.Type.PHONE)) {
95 String string = intent.getStringExtra(Intents.Encode.DATA);
96 if (string != null && string.length() > 0) {
97 mContents = "tel:" + string;
98 mDisplayContents = string;
99 mTitle = mActivity.getString(R.string.contents_phone);
101 } else if (type.equals(Contents.Type.SMS)) {
102 String string = intent.getStringExtra(Intents.Encode.DATA);
103 if (string != null && string.length() > 0) {
104 mContents = "sms:" + string;
105 mDisplayContents = string;
106 mTitle = mActivity.getString(R.string.contents_sms);
108 } else if (type.equals(Contents.Type.CONTACT)) {
109 Bundle bundle = intent.getBundleExtra(Intents.Encode.DATA);
110 if (bundle != null) {
111 String name = bundle.getString(Contacts.Intents.Insert.NAME);
112 if (name != null && name.length() > 0) {
113 mContents = "MECARD:N:" + name + ";";
114 mDisplayContents = name;
115 String address = bundle.getString(Contacts.Intents.Insert.POSTAL);
116 if (address != null && address.length() > 0) {
117 mContents += "ADR:" + address + ";";
118 mDisplayContents += "\n" + address;
120 String phone = bundle.getString(Contacts.Intents.Insert.PHONE);
121 if (phone != null && phone.length() > 0) {
122 mContents += "TEL:" + phone + ";";
123 mDisplayContents += "\n" + phone;
125 String email = bundle.getString(Contacts.Intents.Insert.EMAIL);
126 if (email != null && email.length() > 0) {
127 mContents += "EMAIL:" + email + ";";
128 mDisplayContents += "\n" + email;
131 mTitle = mActivity.getString(R.string.contents_contact);
134 } else if (type.equals(Contents.Type.LOCATION)) {
135 Bundle bundle = intent.getBundleExtra(Intents.Encode.DATA);
136 if (bundle != null) {
137 float latitude = bundle.getFloat("LAT", Float.MAX_VALUE);
138 float longitude = bundle.getFloat("LONG", Float.MAX_VALUE);
139 if (latitude != Float.MAX_VALUE && longitude != Float.MAX_VALUE) {
140 mContents = "geo:" + latitude + "," + longitude;
141 mDisplayContents = latitude + "," + longitude;
142 mTitle = mActivity.getString(R.string.contents_location);
146 return mContents != null && mContents.length() > 0;
149 private class NetworkThread extends Thread {
151 private final String mContents;
152 private final Handler mHandler;
153 private final int mPixelResolution;
155 public NetworkThread(String contents, Handler handler, int pixelResolution) {
156 mContents = contents;
158 mPixelResolution = pixelResolution;
162 AndroidHttpClient client = null;
164 String url = CHART_SERVER_URL + mPixelResolution + "x" + mPixelResolution + "&chl=" +
165 URLEncoder.encode(mContents, "UTF8");
166 URI uri = new URI("http", url, null);
167 HttpGet get = new HttpGet(uri);
168 client = AndroidHttpClient.newInstance(mUserAgent);
169 HttpResponse response = client.execute(get);
170 HttpEntity entity = response.getEntity();
171 Bitmap image = BitmapFactory.decodeStream(entity.getContent());
173 Message message = Message.obtain(mHandler, R.id.encode_succeeded);
175 message.sendToTarget();
177 Log.e(TAG, "Could not decode png from the network");
178 Message message = Message.obtain(mHandler, R.id.encode_failed);
179 message.sendToTarget();
181 } catch (Exception e) {
182 Log.e(TAG, e.toString());
183 Message message = Message.obtain(mHandler, R.id.encode_failed);
184 message.sendToTarget();
186 if (client != null) {