From 2820564b7a5364156f8f9b7379168f046c486ffb Mon Sep 17 00:00:00 2001 From: srowen Date: Sun, 3 May 2009 05:59:49 +0000 Subject: [PATCH] Added 'shareText' integration code from Isaac git-svn-id: http://zxing.googlecode.com/svn/trunk@928 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- AUTHORS | 1 + .../integration/android/IntentIntegrator.java | 90 ++++++++++++++++--- 2 files changed, 81 insertions(+), 10 deletions(-) diff --git a/AUTHORS b/AUTHORS index 3f5048d8..0eacff80 100644 --- a/AUTHORS +++ b/AUTHORS @@ -8,6 +8,7 @@ Christian Brunschen (Google) Daniel Switkin (Google) David Albert (Bug Labs) Fred Lin (Anobiit) +Isaac Potoczny-Jones John Connolly (Bug Labs) Joseph Wain (Google) Kevin O'Sullivan (SITA) diff --git a/android-integration/src/com/google/zxing/integration/android/IntentIntegrator.java b/android-integration/src/com/google/zxing/integration/android/IntentIntegrator.java index 943b5121..1a8d8310 100644 --- a/android-integration/src/com/google/zxing/integration/android/IntentIntegrator.java +++ b/android-integration/src/com/google/zxing/integration/android/IntentIntegrator.java @@ -28,6 +28,8 @@ import android.net.Uri; * way to invoke barcode scanning and receive the result, without any need to integrate, modify, or learn the * project's source code.

* + *

Initiating a barcode can

+ * *

Integration is essentially as easy as calling {@link #initiateScan(Activity)} and waiting * for the result in your app.

* @@ -57,31 +59,40 @@ import android.net.Uri; * {@link #initiateScan(Activity, int, int, int, int)} to customize the download prompt with * different text labels.

* + *

Sharing text via barcode

+ * + *

To share text, encoded as a QR Code on-screen, similarly, see {@link #shareText(Activity, String)}.

+ * *

Some code, particularly download integration, was contributed from the Anobiit application.

* * @author Sean Owen * @author Fred Lin + * @author Isaac Potoczny-Jones */ public final class IntentIntegrator { public static final int REQUEST_CODE = 0x0ba7c0de; // get it? + private static final String DEFAULT_TITLE = "Install Barcode Scanner?"; + private static final String DEFAULT_MESSAGE = + "This application requires Barcode Scanner. Would you like to install it?"; + private static final String DEFAULT_YES = "Yes"; + private static final String DEFAULT_NO = "No"; + private IntentIntegrator() { } /** - * See {@link #initiateScan(Activity, String, String, String, String)} -- same, but uses default English labels. + * See {@link #initiateScan(Activity, String, String, String, String)} -- + * same, but uses default English labels. */ public static void initiateScan(Activity activity) { - initiateScan(activity, - "Install Barcode Scanner?", - "This application requires Barcode Scanner. Would you like to install it?", - "Yes", - "No"); + initiateScan(activity, DEFAULT_TITLE, DEFAULT_MESSAGE, DEFAULT_YES, DEFAULT_NO); } /** - * See {@link #initiateScan(Activity, String, String, String, String)} -- same, but takes string IDs which refer + * See {@link #initiateScan(Activity, String, String, String, String)} -- + * same, but takes string IDs which refer * to the {@link Activity}'s resource bundle entries. */ public static void initiateScan(Activity activity, @@ -101,8 +112,10 @@ public final class IntentIntegrator { * * @param stringTitle title of dialog prompting user to download Barcode Scanner * @param stringMessage text of dialog prompting user to download Barcode Scanner - * @param stringButtonYes text of button user clicks when agreeing to download Barcode Scanner (e.g. "Yes") - * @param stringButtonNo text of button user clicks when declining to download Barcode Scanner (e.g. "No") + * @param stringButtonYes text of button user clicks when agreeing to download + * Barcode Scanner (e.g. "Yes") + * @param stringButtonNo text of button user clicks when declining to download + * Barcode Scanner (e.g. "No") * @return the contents of the barcode that was scanned, or null if none was found * @throws InterruptedException if timeout expires before a scan completes */ @@ -143,7 +156,8 @@ public final class IntentIntegrator { /** - *

Call this from your {@link Activity}'s {@link Activity#onActivityResult(int, int, Intent)} method.

+ *

Call this from your {@link Activity}'s + * {@link Activity#onActivityResult(int, int, Intent)} method.

* * @return null if the event handled here was not related to {@link IntentIntegrator}, or * else an {@link IntentResult} containing the result of the scan. If the user cancelled scanning, @@ -162,4 +176,60 @@ public final class IntentIntegrator { return null; } + /** + * See {@link #shareText(Activity, String, String, String, String, String)} -- + * same, but uses default English labels. + */ + public static void shareText(Activity activity, String text) { + shareText(activity, text, DEFAULT_TITLE, DEFAULT_MESSAGE, DEFAULT_YES, DEFAULT_NO); + } + + /** + * See {@link #shareText(Activity, String, String, String, String, String)} -- + * same, but takes string IDs which refer to the {@link Activity}'s resource bundle entries. + */ + public static void shareText(Activity activity, + String text, + int stringTitle, + int stringMessage, + int stringButtonYes, + int stringButtonNo) { + shareText(activity, + text, + activity.getString(stringTitle), + activity.getString(stringMessage), + activity.getString(stringButtonYes), + activity.getString(stringButtonNo)); + } + + /** + * Shares the given text by encoding it as a barcode, such that another user can + * scan the text off the screen of the device. + * + * @param text the text string to encode as a barcode + * @param stringTitle title of dialog prompting user to download Barcode Scanner + * @param stringMessage text of dialog prompting user to download Barcode Scanner + * @param stringButtonYes text of button user clicks when agreeing to download + * Barcode Scanner (e.g. "Yes") + * @param stringButtonNo text of button user clicks when declining to download + * Barcode Scanner (e.g. "No") + */ + public static void shareText(Activity activity, + String text, + String stringTitle, + String stringMessage, + String stringButtonYes, + String stringButtonNo) { + + Intent intent = new Intent(); + intent.setAction("com.google.zxing.client.android.ENCODE"); + intent.putExtra("ENCODE_TYPE", "TEXT_TYPE"); + intent.putExtra("ENCODE_DATA", text); + try { + activity.startActivity(intent); + } catch (ActivityNotFoundException e) { + showDownloadDialog(activity, stringTitle, stringMessage, stringButtonYes, stringButtonNo); + } + } + } -- 2.20.1