Advertisement
eijie

IntentIntegrator.java

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