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