summaryrefslogtreecommitdiff
path: root/src/com/android/barcodes/ResultHandler.java
blob: ddaa003356da9a338f90ceec14f8fd1732d1baa5 (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
/*
 * 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.content.Intent;
import android.net.Uri;
import android.provider.Contacts;
import android.view.View;
import android.widget.Button;
import com.google.zxing.Result;
import com.google.zxing.client.result.AddressBookParsedResult;
import com.google.zxing.client.result.EmailAddressParsedResult;
import com.google.zxing.client.result.GeoParsedResult;
import com.google.zxing.client.result.ISBNParsedResult;
import com.google.zxing.client.result.ParsedResult;
import com.google.zxing.client.result.ParsedResultType;
import com.google.zxing.client.result.ResultParser;
import com.google.zxing.client.result.SMSParsedResult;
import com.google.zxing.client.result.TelParsedResult;
import com.google.zxing.client.result.UPCParsedResult;
import com.google.zxing.client.result.URIParsedResult;

/**
 * Handles the result of barcode decoding in the context of the Android platform, by dispatching the
 * proper intents to open other activities like GMail, Maps, etc.
 */
final class ResultHandler implements Button.OnClickListener {

    private static final String TAG = "ResultHandler";

    private final Intent mIntent;
    private final BarcodesCaptureActivity mCaptureActivity;

    ResultHandler(BarcodesCaptureActivity captureActivity, ParsedResult result) {
        mCaptureActivity = captureActivity;
        mIntent = resultToIntent(result);
    }

    public void onClick(View view) {
        if (mIntent != null) {
            mCaptureActivity.startActivity(mIntent);
        }
    }

    public Intent getIntent() {
        return mIntent;
    }

    public static ParsedResult parseResult(Result rawResult) {
        ParsedResult result = ResultParser.parseResult(rawResult);
        if (result.getType().equals(ParsedResultType.TEXT)) {
            String rawText = rawResult.getText();
            AndroidIntentParsedResult androidResult = AndroidIntentParsedResult.parse(rawText);
            if (androidResult != null) {
                Intent intent = androidResult.getIntent();
                if (!Intent.ACTION_VIEW.equals(intent.getAction())) {
                    // For now, don't take anything that just parses as a View action. A lot
                    // of things are accepted as a View action by default.
                    result = androidResult;
                }
            }
        }
        return result;
    }

    public static int getActionButtonText(ParsedResultType type) {
        int buttonText;
        if (type.equals(ParsedResultType.ADDRESSBOOK)) {
            buttonText = R.string.button_add_contact;
        } else if (type.equals(ParsedResultType.URI)) {
            buttonText = R.string.button_open_browser;
        } else if (type.equals(ParsedResultType.EMAIL_ADDRESS)) {
            buttonText = R.string.button_email;
        } else if (type.equals(ParsedResultType.UPC)) {
            buttonText = R.string.button_lookup_product;
        } else if (type.equals(ParsedResultType.TEL)) {
            buttonText = R.string.button_dial;
        } else if (type.equals(ParsedResultType.GEO)) {
            buttonText = R.string.button_show_map;
        } else if (type.equals(ParsedResultType.ISBN)) {
            buttonText = R.string.button_lookup_book;
        } else {
            buttonText = 0;
        }
        return buttonText;
    }

    private static Intent resultToIntent(ParsedResult result) {
        Intent intent = null;
        ParsedResultType type = result.getType();
        if (type.equals(ParsedResultType.ADDRESSBOOK)) {
            AddressBookParsedResult addressResult = (AddressBookParsedResult) result;
            intent = new Intent(Contacts.Intents.Insert.ACTION, Contacts.People.CONTENT_URI);
            putExtra(intent, Contacts.Intents.Insert.NAME, addressResult.getNames());
            putExtra(intent, Contacts.Intents.Insert.PHONE, addressResult.getPhoneNumbers());
            putExtra(intent, Contacts.Intents.Insert.EMAIL, addressResult.getEmails());
            putExtra(intent, Contacts.Intents.Insert.NOTES, addressResult.getNote());
            putExtra(intent, Contacts.Intents.Insert.POSTAL, addressResult.getAddress());
            putExtra(intent, Contacts.Intents.Insert.COMPANY, addressResult.getOrg());
            putExtra(intent, Contacts.Intents.Insert.JOB_TITLE, addressResult.getTitle());
        } else if (type.equals(ParsedResultType.EMAIL_ADDRESS)) {
            EmailAddressParsedResult emailResult = (EmailAddressParsedResult) result;
            intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(emailResult.getMailtoURI()));
            putExtra(intent, "subject", emailResult.getSubject());
            putExtra(intent, "body", emailResult.getBody());
        } else if (type.equals(ParsedResultType.SMS)) {
            SMSParsedResult smsResult = (SMSParsedResult) result;
            intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(smsResult.getSMSURI()));
            putExtra(intent, "subject", smsResult.getSubject());
            putExtra(intent, "body", smsResult.getBody());
        } else if (type.equals(ParsedResultType.TEL)) {
            TelParsedResult telResult = (TelParsedResult) result;
            intent = new Intent(Intent.ACTION_DIAL, Uri.parse(telResult.getTelURI()));
        } else if (type.equals(ParsedResultType.GEO)) {
            GeoParsedResult geoResult = (GeoParsedResult) result;
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse(geoResult.getGeoURI()));
        } else if (type.equals(ParsedResultType.UPC)) {
            UPCParsedResult upcResult = (UPCParsedResult) result;
            Uri uri = Uri.parse("http://www.google.com/products?q=" + upcResult.getUPC());
            intent = new Intent(Intent.ACTION_VIEW, uri);
        } else if (type.equals(ParsedResultType.ISBN)) {
            ISBNParsedResult isbnResult = (ISBNParsedResult) result;
            Uri uri = Uri.parse("http://www.google.com/products?q=" + isbnResult.getISBN());
            intent = new Intent(Intent.ACTION_VIEW, uri);
        } else if (type.equals(ParsedResultType.URI)) {
            URIParsedResult uriResult = (URIParsedResult) result;
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uriResult.getURI()));
        } else if (type.equals(ParsedResultType.ANDROID_INTENT)) {
            intent = ((AndroidIntentParsedResult) result).getIntent();
        }
        return intent;
    }

    private static void putExtra(Intent intent, String key, String value) {
        if (value != null && value.length() > 0) {
            intent.putExtra(key, value);
        }
    }

    private static void putExtra(Intent intent, String key, String[] value) {
        if (value != null && value.length > 0) {
            putExtra(intent, key, value[0]);
        }
    }

}