summaryrefslogtreecommitdiff
path: root/src/com/android/barcodes/QRCodeEncoder.java
blob: 27c12e6c0a2aa45c7dd638d6cf97ff4272512aa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Copyright (C) 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.barcodes;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.http.AndroidHttpClient;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.Contacts;
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;

import java.net.URI;

public class QRCodeEncoder {

    private static final String TAG = "QRCodeEncoder";
    private static final String CHART_SERVER_URL = "//chart.apis.google.com/chart?cht=qr&chs=";

    private Activity mActivity;
    private String mContents;
    private String mDisplayContents;
    private String mTitle;
    private NetworkThread mNetworkThread;

    public QRCodeEncoder(Activity activity, Intent intent) {
        mActivity = activity;
        if (!encodeContents(intent)) {
            throw new IllegalArgumentException("No valid data to encode.");
        }
    }

    // Once the core ZXing library supports encoding, we'll be able to generate the bitmap
    // synchronously. For now, it's a network request, so it's handled on a thread.
    public void requestBarcode(Handler handler, int pixelResolution) {
        mNetworkThread = new NetworkThread(mContents, handler, pixelResolution);
        mNetworkThread.start();
    }

    public String getContents() {
        return mContents;
    }

    public String getDisplayContents() {
        return mDisplayContents;
    }

    public String getTitle() {
        return mTitle;
    }

    // Perhaps the string encoding should live in the core ZXing library too.
    private boolean encodeContents(Intent intent) {
        if (intent == null) return false;
        String type = intent.getStringExtra(Intents.Encode.TYPE);
        if (type == null || type.length() == 0) return false;

        if (type.equals(Contents.Type.TEXT)) {
            String string = intent.getStringExtra(Intents.Encode.DATA);
            if (string != null && string.length() > 0) {
                mContents = string;
                mDisplayContents = string;
                mTitle = mActivity.getString(R.string.contents_text);
            }
        } else if (type.equals(Contents.Type.EMAIL)) {
            String string = intent.getStringExtra(Intents.Encode.DATA);
            if (string != null && string.length() > 0) {
                mContents = "mailto:" + string;
                mDisplayContents = string;
                mTitle = mActivity.getString(R.string.contents_email);
            }
        } else if (type.equals(Contents.Type.PHONE)) {
            String string = intent.getStringExtra(Intents.Encode.DATA);
            if (string != null && string.length() > 0) {
                mContents = "tel:" + string;
                mDisplayContents = string;
                mTitle = mActivity.getString(R.string.contents_phone);
            }
        } else if (type.equals(Contents.Type.SMS)) {
            String string = intent.getStringExtra(Intents.Encode.DATA);
            if (string != null && string.length() > 0) {
                mContents = "sms:" + string;
                mDisplayContents = string;
                mTitle = mActivity.getString(R.string.contents_sms);
            }
        } else if (type.equals(Contents.Type.CONTACT)) {
            Bundle bundle = intent.getBundleExtra(Intents.Encode.DATA);
            if (bundle != null) {
                String name = bundle.getString(Contacts.Intents.Insert.NAME);
                if (name != null && !name.equals("")) {
                    mContents = "MECARD:N:" + name + ";";
                    mDisplayContents = name;
                    String phone = bundle.getString(Contacts.Intents.Insert.PHONE);
                    if (phone != null && !phone.equals("")) {
                        mContents += "TEL:" + phone + ";";
                        mDisplayContents += "\n" + phone;
                    }
                    String email = bundle.getString(Contacts.Intents.Insert.EMAIL);
                    if (email != null && !email.equals("")) {
                        mContents += "EMAIL:" + email + ";";
                        mDisplayContents += "\n" + email;
                    }
                    mContents += ";";
                    mTitle = mActivity.getString(R.string.contents_contact);
                }
            }
        } else if (type.equals(Contents.Type.LOCATION)) {
            Bundle bundle = intent.getBundleExtra(Intents.Encode.DATA);
            if (bundle != null) {
                float latitude = bundle.getFloat("LAT", Float.MAX_VALUE);
                float longitude = bundle.getFloat("LONG", Float.MAX_VALUE);
                if (latitude != Float.MAX_VALUE && longitude != Float.MAX_VALUE) {
                    mContents = "geo:" + latitude + "," + longitude;
                    mDisplayContents = latitude + "," + longitude;
                    mTitle = mActivity.getString(R.string.contents_location);
                }
            }
        }
        return mContents != null && mContents.length() > 0;
    }

    private class NetworkThread extends Thread {

        private String mContents;
        private Handler mHandler;
        private int mPixelResolution;

        public NetworkThread(String contents, Handler handler, int pixelResolution) {
            mContents = contents;
            mHandler = handler;
            mPixelResolution = pixelResolution;
        }

        public void run() {
            String url = CHART_SERVER_URL + mPixelResolution + "x" + mPixelResolution + "&chl=" +
                    mContents;
            try {
                URI uri = new URI("http", url, null);
                HttpGet get = new HttpGet(uri);
                AndroidHttpClient client = AndroidHttpClient.newInstance("Android-Barcodes/0.1");
                HttpResponse response = client.execute(get);
                HttpEntity entity = response.getEntity();
                Bitmap image = BitmapFactory.decodeStream(entity.getContent());
                if (image != null) {
                    Message message = Message.obtain(mHandler, R.id.encode_succeeded);
                    message.obj = image;
                    message.sendToTarget();
                } else {
                    Log.e(TAG, "Could not decode png from the network");
                    Message message = Message.obtain(mHandler, R.id.encode_failed);
                    message.sendToTarget();
                }
            } catch (Exception e) {
                Log.e(TAG, e.toString());
                Message message = Message.obtain(mHandler, R.id.encode_failed);
                message.sendToTarget();
            }
        }
    }

}