PageRenderTime 28ms CodeModel.GetById 14ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/ItemID/src/com/ideal/itemid/IntentIntegrator.java

http://eyes-free.googlecode.com/
Java | 292 lines | 103 code | 27 blank | 162 comment | 7 complexity | 026e58768c3b247f503e2488898546f7 MD5 | raw file
  1/*
  2 * Copyright 2009 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
 17package com.ideal.itemid;
 18
 19import android.app.AlertDialog;
 20import android.app.Activity;
 21import android.content.ActivityNotFoundException;
 22import android.content.DialogInterface;
 23import android.content.Intent;
 24import android.net.Uri;
 25
 26/**
 27 * <p>
 28 * A utility class which helps ease integration with Barcode Scanner via
 29 * {@link Intent}s. This is a simple way to invoke barcode scanning and receive
 30 * the result, without any need to integrate, modify, or learn the project's
 31 * source code.
 32 * </p>
 33 * <h2>Initiating a barcode scan</h2>
 34 * <p>
 35 * Integration is essentially as easy as calling {@link #initiateScan(Activity)}
 36 * and waiting for the result in your app.
 37 * </p>
 38 * <p>
 39 * It does require that the Barcode Scanner application is installed. The
 40 * {@link #initiateScan(Activity)} method will prompt the user to download the
 41 * application, if needed.
 42 * </p>
 43 * <p>
 44 * There are a few steps to using this integration. First, your {@link Activity}
 45 * must implement the method {@link Activity#onActivityResult(int, int, Intent)}
 46 * and include a line of code like this:
 47 * </p>
 48 * <p>
 49 * {@code public void onActivityResult(int requestCode, int resultCode, Intent
 50 * intent) IntentResult scanResult =
 51 * IntentIntegrator.parseActivityResult(requestCode, resultCode, intent); if
 52 * (scanResult != null) { // handle scan result } // else continue with any
 53 * other code you need in the method ... } }
 54 * </p>
 55 * <p>
 56 * This is where you will handle a scan result. Second, just call this in
 57 * response to a user action somewhere to begin the scan process:
 58 * </p>
 59 * <p>
 60 * {@code IntentIntegrator.initiateScan(yourActivity);}
 61 * </p>
 62 * <p>
 63 * You can use
 64 * {@link #initiateScan(Activity, CharSequence, CharSequence, CharSequence, CharSequence)}
 65 * or {@link #initiateScan(Activity, int, int, int, int)} to customize the
 66 * download prompt with different text labels.
 67 * </p>
 68 * <p>
 69 * Note that {@link #initiateScan(Activity)} returns an {@link AlertDialog}
 70 * which is non-null if the user was prompted to download the application. This
 71 * lets the calling app potentially manage the dialog. In particular, ideally,
 72 * the app dismisses the dialog if it's still active in its
 73 * {@link Activity#onPause()} method.
 74 * </p>
 75 * <h2>Sharing text via barcode</h2>
 76 * <p>
 77 * To share text, encoded as a QR Code on-screen, similarly, see
 78 * {@link #shareText(Activity, CharSequence)}.
 79 * </p>
 80 * <p>
 81 * Some code, particularly download integration, was contributed from the
 82 * Anobiit application.
 83 * </p>
 84 * 
 85 * @author Sean Owen
 86 * @author Fred Lin
 87 * @author Isaac Potoczny-Jones
 88 * @author Brad Drehmer
 89 */
 90public final class IntentIntegrator {
 91
 92    public static final int REQUEST_CODE = 0x0ba7c0de; // get it?
 93
 94    public static final String DEFAULT_TITLE = "Install Barcode Scanner?";
 95
 96    public static final String DEFAULT_MESSAGE = "This application requires Barcode Scanner. Would you like to install it?";
 97
 98    public static final String DEFAULT_YES = "Yes";
 99
100    public static final String DEFAULT_NO = "No";
101
102    // supported barcode formats
103    public static final String PRODUCT_CODE_TYPES = "UPC_A,UPC_E,EAN_8,EAN_13";
104
105    public static final String ONE_D_CODE_TYPES = PRODUCT_CODE_TYPES + ",CODE_39,CODE_93,CODE_128";
106
107    public static final String QR_CODE_TYPES = "QR_CODE";
108
109    public static final String ALL_CODE_TYPES = null;
110
111    private IntentIntegrator() {
112    }
113
114    /**
115     * See
116     * {@link #initiateScan(Activity, CharSequence, CharSequence, CharSequence, CharSequence)}
117     * -- same, but uses default English labels.
118     */
119    public static AlertDialog initiateScan(Activity activity) {
120        return initiateScan(activity, DEFAULT_TITLE, DEFAULT_MESSAGE, DEFAULT_YES, DEFAULT_NO);
121    }
122
123    /**
124     * See
125     * {@link #initiateScan(Activity, CharSequence, CharSequence, CharSequence, CharSequence)}
126     * -- same, but takes string IDs which refer to the {@link Activity}'s
127     * resource bundle entries.
128     */
129    public static AlertDialog initiateScan(Activity activity, int stringTitle, int stringMessage,
130            int stringButtonYes, int stringButtonNo) {
131        return initiateScan(activity, activity.getString(stringTitle), activity
132                .getString(stringMessage), activity.getString(stringButtonYes), activity
133                .getString(stringButtonNo));
134    }
135
136    /**
137     * See
138     * {@link #initiateScan(Activity, CharSequence, CharSequence, CharSequence, CharSequence, CharSequence)}
139     * -- same, but scans for all supported barcode types.
140     * 
141     * @param stringTitle title of dialog prompting user to download Barcode
142     *            Scanner
143     * @param stringMessage text of dialog prompting user to download Barcode
144     *            Scanner
145     * @param stringButtonYes text of button user clicks when agreeing to
146     *            download Barcode Scanner (e.g. "Yes")
147     * @param stringButtonNo text of button user clicks when declining to
148     *            download Barcode Scanner (e.g. "No")
149     * @return an {@link AlertDialog} if the user was prompted to download the
150     *         app, null otherwise
151     */
152    public static AlertDialog initiateScan(Activity activity, CharSequence stringTitle,
153            CharSequence stringMessage, CharSequence stringButtonYes, CharSequence stringButtonNo) {
154
155        return initiateScan(activity, stringTitle, stringMessage, stringButtonYes, stringButtonNo,
156                ALL_CODE_TYPES);
157    }
158
159    /**
160     * Invokes scanning.
161     * 
162     * @param stringTitle title of dialog prompting user to download Barcode
163     *            Scanner
164     * @param stringMessage text of dialog prompting user to download Barcode
165     *            Scanner
166     * @param stringButtonYes text of button user clicks when agreeing to
167     *            download Barcode Scanner (e.g. "Yes")
168     * @param stringButtonNo text of button user clicks when declining to
169     *            download Barcode Scanner (e.g. "No")
170     * @param stringDesiredBarcodeFormats a comma separated list of codes you
171     *            would like to scan for.
172     * @return an {@link AlertDialog} if the user was prompted to download the
173     *         app, null otherwise
174     * @throws InterruptedException if timeout expires before a scan completes
175     */
176    public static AlertDialog initiateScan(Activity activity, CharSequence stringTitle,
177            CharSequence stringMessage, CharSequence stringButtonYes, CharSequence stringButtonNo,
178            CharSequence stringDesiredBarcodeFormats) {
179        Intent intentScan = new Intent("com.google.zxing.client.android.SCAN");
180        intentScan.addCategory(Intent.CATEGORY_DEFAULT);
181
182        // check which types of codes to scan for
183        if (stringDesiredBarcodeFormats != null) {
184            // set the desired barcode types
185            intentScan.putExtra("SCAN_FORMATS", stringDesiredBarcodeFormats);
186        }
187
188        try {
189            activity.startActivityForResult(intentScan, REQUEST_CODE);
190            return null;
191        } catch (ActivityNotFoundException e) {
192            return showDownloadDialog(activity, stringTitle, stringMessage, stringButtonYes,
193                    stringButtonNo);
194        }
195    }
196
197    private static AlertDialog showDownloadDialog(final Activity activity,
198            CharSequence stringTitle, CharSequence stringMessage, CharSequence stringButtonYes,
199            CharSequence stringButtonNo) {
200        AlertDialog.Builder downloadDialog = new AlertDialog.Builder(activity);
201        downloadDialog.setTitle(stringTitle);
202        downloadDialog.setMessage(stringMessage);
203        downloadDialog.setPositiveButton(stringButtonYes, new DialogInterface.OnClickListener() {
204            public void onClick(DialogInterface dialogInterface, int i) {
205                Uri uri = Uri.parse("market://search?q=pname:com.google.zxing.client.android");
206                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
207                activity.startActivity(intent);
208            }
209        });
210        downloadDialog.setNegativeButton(stringButtonNo, new DialogInterface.OnClickListener() {
211            public void onClick(DialogInterface dialogInterface, int i) {
212            }
213        });
214        return downloadDialog.show();
215    }
216
217    /**
218     * <p>
219     * Call this from your {@link Activity}'s
220     * {@link Activity#onActivityResult(int, int, Intent)} method.
221     * </p>
222     * 
223     * @return null if the event handled here was not related to
224     *         {@link IntentIntegrator}, or else an {@link IntentResult}
225     *         containing the result of the scan. If the user cancelled
226     *         scanning, the fields will be null.
227     */
228    public static IntentResult parseActivityResult(int requestCode, int resultCode, Intent intent) {
229        if (requestCode == REQUEST_CODE) {
230            if (resultCode == Activity.RESULT_OK) {
231                String contents = intent.getStringExtra("SCAN_RESULT");
232                String formatName = intent.getStringExtra("SCAN_RESULT_FORMAT");
233                return new IntentResult(contents, formatName);
234            } else {
235                return new IntentResult(null, null);
236            }
237        }
238        return null;
239    }
240
241    /**
242     * See
243     * {@link #shareText(Activity, CharSequence, CharSequence, CharSequence, CharSequence, CharSequence)}
244     * -- same, but uses default English labels.
245     */
246    public static void shareText(Activity activity, CharSequence text) {
247        shareText(activity, text, DEFAULT_TITLE, DEFAULT_MESSAGE, DEFAULT_YES, DEFAULT_NO);
248    }
249
250    /**
251     * See
252     * {@link #shareText(Activity, CharSequence, CharSequence, CharSequence, CharSequence, CharSequence)}
253     * -- same, but takes string IDs which refer to the {@link Activity}'s
254     * resource bundle entries.
255     */
256    public static void shareText(Activity activity, CharSequence text, int stringTitle,
257            int stringMessage, int stringButtonYes, int stringButtonNo) {
258        shareText(activity, text, activity.getString(stringTitle), activity
259                .getString(stringMessage), activity.getString(stringButtonYes), activity
260                .getString(stringButtonNo));
261    }
262
263    /**
264     * Shares the given text by encoding it as a barcode, such that another user
265     * can scan the text off the screen of the device.
266     * 
267     * @param text the text string to encode as a barcode
268     * @param stringTitle title of dialog prompting user to download Barcode
269     *            Scanner
270     * @param stringMessage text of dialog prompting user to download Barcode
271     *            Scanner
272     * @param stringButtonYes text of button user clicks when agreeing to
273     *            download Barcode Scanner (e.g. "Yes")
274     * @param stringButtonNo text of button user clicks when declining to
275     *            download Barcode Scanner (e.g. "No")
276     */
277    public static void shareText(Activity activity, CharSequence text, CharSequence stringTitle,
278            CharSequence stringMessage, CharSequence stringButtonYes, CharSequence stringButtonNo) {
279
280        Intent intent = new Intent();
281        intent.setAction("com.google.zxing.client.android.ENCODE");
282        intent.putExtra("ENCODE_TYPE", "TEXT_TYPE");
283        intent.putExtra("ENCODE_DATA", text);
284        try {
285            activity.startActivity(intent);
286        } catch (ActivityNotFoundException e) {
287            showDownloadDialog(activity, stringTitle, stringMessage, stringButtonYes,
288                    stringButtonNo);
289        }
290    }
291
292}