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 eu.siacs.conversations.utils.zxing;
18
19import java.util.Arrays;
20import java.util.Collection;
21import java.util.Collections;
22import java.util.HashMap;
23import java.util.List;
24import java.util.Map;
25
26import android.app.Activity;
27import android.support.v7.app.AlertDialog;
28import android.app.Fragment;
29import android.content.ActivityNotFoundException;
30import android.content.DialogInterface;
31import android.content.Intent;
32import android.content.pm.PackageManager;
33import android.content.pm.ResolveInfo;
34import android.net.Uri;
35import android.os.Bundle;
36import android.util.Log;
37
38import eu.siacs.conversations.ui.UriHandlerActivity;
39
40/**
41 * <p>A utility class which helps ease integration with Barcode Scanner via {@link Intent}s. This is a simple
42 * way to invoke barcode scanning and receive the result, without any need to integrate, modify, or learn the
43 * project's source code.</p>
44 *
45 * <h2>Initiating a barcode scan</h2>
46 *
47 * <p>To integrate, create an instance of {@code IntentIntegrator} and call {@link #initiateScan()} and wait
48 * for the result in your app.</p>
49 *
50 * <p>It does require that the Barcode Scanner (or work-alike) application is installed. The
51 * {@link #initiateScan()} method will prompt the user to download the application, if needed.</p>
52 *
53 * <p>There are a few steps to using this integration. First, your {@link Activity} must implement
54 * the method {@link Activity#onActivityResult(int, int, Intent)} and include a line of code like this:</p>
55 *
56 * <pre>{@code
57 * public void onActivityResult(int requestCode, int resultCode, Intent intent) {
58 * IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
59 * if (scanResult != null) {
60 * // handle scan result
61 * }
62 * // else continue with any other code you need in the method
63 * ...
64 * }
65 * }</pre>
66 *
67 * <p>This is where you will handle a scan result.</p>
68 *
69 * <p>Second, just call this in response to a user action somewhere to begin the scan process:</p>
70 *
71 * <pre>{@code
72 * IntentIntegrator integrator = new IntentIntegrator(yourActivity);
73 * integrator.initiateScan();
74 * }</pre>
75 *
76 * <p>Note that {@link #initiateScan()} returns an {@link AlertDialog} which is non-null if the
77 * user was prompted to download the application. This lets the calling app potentially manage the dialog.
78 * In particular, ideally, the app dismisses the dialog if it's still active in its {@link Activity#onPause()}
79 * method.</p>
80 *
81 * <p>You can use {@link #setTitle(String)} to customize the title of this download prompt dialog (or, use
82 * {@link #setTitleByID(int)} to set the title by string resource ID.) Likewise, the prompt message, and
83 * yes/no button labels can be changed.</p>
84 *
85 * <p>Finally, you can use {@link #addExtra(String, Object)} to add more parameters to the Intent used
86 * to invoke the scanner. This can be used to set additional options not directly exposed by this
87 * simplified API.</p>
88 *
89 * <p>By default, this will only allow applications that are known to respond to this intent correctly
90 * do so. The apps that are allowed to response can be set with {@link #setTargetApplications(List)}.
91 * For example, set to {@link #TARGET_BARCODE_SCANNER_ONLY} to only target the Barcode Scanner app itself.</p>
92 *
93 * <h2>Sharing text via barcode</h2>
94 *
95 * <p>To share text, encoded as a QR Code on-screen, similarly, see {@link #shareText(CharSequence)}.</p>
96 *
97 * <p>Some code, particularly download integration, was contributed from the Anobiit application.</p>
98 *
99 * <h2>Enabling experimental barcode formats</h2>
100 *
101 * <p>Some formats are not enabled by default even when scanning with {@link #ALL_CODE_TYPES}, such as
102 * PDF417. Use {@link #initiateScan(Collection)} with
103 * a collection containing the names of formats to scan for explicitly, like "PDF_417", to use such
104 * formats.</p>
105 *
106 * @author Sean Owen
107 * @author Fred Lin
108 * @author Isaac Potoczny-Jones
109 * @author Brad Drehmer
110 * @author gcstang
111 */
112public class IntentIntegrator {
113
114 public static final int REQUEST_CODE = 0x0000c0de; // Only use bottom 16 bits
115 private static final String TAG = IntentIntegrator.class.getSimpleName();
116
117 public static final String DEFAULT_TITLE = "Install Barcode Scanner?";
118 public static final String DEFAULT_MESSAGE =
119 "This application requires Barcode Scanner. Would you like to install it?";
120 public static final String DEFAULT_YES = "Yes";
121 public static final String DEFAULT_NO = "No";
122
123 private static final String BS_PACKAGE = "com.google.zxing.client.android";
124 private static final String BSPLUS_PACKAGE = "com.srowen.bs.android";
125
126 // supported barcode formats
127 public static final Collection<String> PRODUCT_CODE_TYPES = list("UPC_A", "UPC_E", "EAN_8", "EAN_13", "RSS_14");
128 public static final Collection<String> ONE_D_CODE_TYPES =
129 list("UPC_A", "UPC_E", "EAN_8", "EAN_13", "CODE_39", "CODE_93", "CODE_128",
130 "ITF", "RSS_14", "RSS_EXPANDED");
131 public static final Collection<String> QR_CODE_TYPES = Collections.singleton("QR_CODE");
132 public static final Collection<String> DATA_MATRIX_TYPES = Collections.singleton("DATA_MATRIX");
133
134 public static final Collection<String> ALL_CODE_TYPES = null;
135
136 public static final List<String> TARGET_BARCODE_SCANNER_ONLY = Collections.singletonList(BS_PACKAGE);
137 public static final List<String> TARGET_ALL_KNOWN = list(
138 BSPLUS_PACKAGE, // Barcode Scanner+
139 BSPLUS_PACKAGE + ".simple", // Barcode Scanner+ Simple
140 BS_PACKAGE // Barcode Scanner
141 // What else supports this intent?
142 );
143
144 // Should be FLAG_ACTIVITY_NEW_DOCUMENT in API 21+.
145 // Defined once here because the current value is deprecated, so generates just one warning
146 private static final int FLAG_NEW_DOC = Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET;
147
148 private final Activity activity;
149 private final Fragment fragment;
150
151 private String title;
152 private String message;
153 private String buttonYes;
154 private String buttonNo;
155 private List<String> targetApplications;
156 private final Map<String,Object> moreExtras = new HashMap<String,Object>(3);
157
158 /**
159 * @param activity {@link Activity} invoking the integration
160 */
161 public IntentIntegrator(Activity activity) {
162 this.activity = activity;
163 this.fragment = null;
164 initializeConfiguration();
165 }
166
167 /**
168 * @param fragment {@link Fragment} invoking the integration.
169 * {@link #startActivityForResult(Intent, int)} will be called on the {@link Fragment} instead
170 * of an {@link Activity}
171 */
172 public IntentIntegrator(Fragment fragment) {
173 this.activity = fragment.getActivity();
174 this.fragment = fragment;
175 initializeConfiguration();
176 }
177
178 private void initializeConfiguration() {
179 title = DEFAULT_TITLE;
180 message = DEFAULT_MESSAGE;
181 buttonYes = DEFAULT_YES;
182 buttonNo = DEFAULT_NO;
183 targetApplications = TARGET_ALL_KNOWN;
184 }
185
186 public String getTitle() {
187 return title;
188 }
189
190 public void setTitle(String title) {
191 this.title = title;
192 }
193
194 public void setTitleByID(int titleID) {
195 title = activity.getString(titleID);
196 }
197
198 public String getMessage() {
199 return message;
200 }
201
202 public void setMessage(String message) {
203 this.message = message;
204 }
205
206 public void setMessageByID(int messageID) {
207 message = activity.getString(messageID);
208 }
209
210 public String getButtonYes() {
211 return buttonYes;
212 }
213
214 public void setButtonYes(String buttonYes) {
215 this.buttonYes = buttonYes;
216 }
217
218 public void setButtonYesByID(int buttonYesID) {
219 buttonYes = activity.getString(buttonYesID);
220 }
221
222 public String getButtonNo() {
223 return buttonNo;
224 }
225
226 public void setButtonNo(String buttonNo) {
227 this.buttonNo = buttonNo;
228 }
229
230 public void setButtonNoByID(int buttonNoID) {
231 buttonNo = activity.getString(buttonNoID);
232 }
233
234 public Collection<String> getTargetApplications() {
235 return targetApplications;
236 }
237
238 public final void setTargetApplications(List<String> targetApplications) {
239 if (targetApplications.isEmpty()) {
240 throw new IllegalArgumentException("No target applications");
241 }
242 this.targetApplications = targetApplications;
243 }
244
245 public void setSingleTargetApplication(String targetApplication) {
246 this.targetApplications = Collections.singletonList(targetApplication);
247 }
248
249 public Map<String,?> getMoreExtras() {
250 return moreExtras;
251 }
252
253 public final void addExtra(String key, Object value) {
254 moreExtras.put(key, value);
255 }
256
257 /**
258 * Initiates a scan for all known barcode types with the default camera.
259 *
260 * @return the {@link AlertDialog} that was shown to the user prompting them to download the app
261 * if a prompt was needed, or null otherwise.
262 */
263 public final AlertDialog initiateScan() {
264 return initiateScan(ALL_CODE_TYPES, -1);
265 }
266
267 /**
268 * Initiates a scan for all known barcode types with the specified camera.
269 *
270 * @param cameraId camera ID of the camera to use. A negative value means "no preference".
271 * @return the {@link AlertDialog} that was shown to the user prompting them to download the app
272 * if a prompt was needed, or null otherwise.
273 */
274 public final AlertDialog initiateScan(int cameraId) {
275 return initiateScan(ALL_CODE_TYPES, cameraId);
276 }
277
278 /**
279 * Initiates a scan, using the default camera, only for a certain set of barcode types, given as strings corresponding
280 * to their names in ZXing's {@code BarcodeFormat} class like "UPC_A". You can supply constants
281 * like {@link #PRODUCT_CODE_TYPES} for example.
282 *
283 * @param desiredBarcodeFormats names of {@code BarcodeFormat}s to scan for
284 * @return the {@link AlertDialog} that was shown to the user prompting them to download the app
285 * if a prompt was needed, or null otherwise.
286 */
287 public final AlertDialog initiateScan(Collection<String> desiredBarcodeFormats) {
288 return initiateScan(desiredBarcodeFormats, -1);
289 }
290
291 /**
292 * Initiates a scan, using the specified camera, only for a certain set of barcode types, given as strings corresponding
293 * to their names in ZXing's {@code BarcodeFormat} class like "UPC_A". You can supply constants
294 * like {@link #PRODUCT_CODE_TYPES} for example.
295 *
296 * @param desiredBarcodeFormats names of {@code BarcodeFormat}s to scan for
297 * @param cameraId camera ID of the camera to use. A negative value means "no preference".
298 * @return the {@link AlertDialog} that was shown to the user prompting them to download the app
299 * if a prompt was needed, or null otherwise
300 */
301 public final AlertDialog initiateScan(Collection<String> desiredBarcodeFormats, int cameraId) {
302 Intent intentScan = new Intent(BS_PACKAGE + ".SCAN");
303 intentScan.addCategory(Intent.CATEGORY_DEFAULT);
304
305 // check which types of codes to scan for
306 if (desiredBarcodeFormats != null) {
307 // set the desired barcode types
308 StringBuilder joinedByComma = new StringBuilder();
309 for (String format : desiredBarcodeFormats) {
310 if (joinedByComma.length() > 0) {
311 joinedByComma.append(',');
312 }
313 joinedByComma.append(format);
314 }
315 intentScan.putExtra("SCAN_FORMATS", joinedByComma.toString());
316 }
317
318 // check requested camera ID
319 if (cameraId >= 0) {
320 intentScan.putExtra("SCAN_CAMERA_ID", cameraId);
321 }
322
323 String targetAppPackage = findTargetAppPackage(intentScan);
324 if (targetAppPackage == null) {
325 return showDownloadDialog();
326 }
327 intentScan.setPackage(targetAppPackage);
328 intentScan.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
329 intentScan.addFlags(FLAG_NEW_DOC);
330 attachMoreExtras(intentScan);
331 startActivityForResult(intentScan, REQUEST_CODE);
332 return null;
333 }
334
335 /**
336 * Start an activity. This method is defined to allow different methods of activity starting for
337 * newer versions of Android and for compatibility library.
338 *
339 * @param intent Intent to start.
340 * @param code Request code for the activity
341 * @see Activity#startActivityForResult(Intent, int)
342 * @see Fragment#startActivityForResult(Intent, int)
343 */
344 protected void startActivityForResult(Intent intent, int code) {
345 if (fragment == null) {
346 activity.startActivityForResult(intent, code);
347 } else {
348 fragment.startActivityForResult(intent, code);
349 }
350 }
351
352 private String findTargetAppPackage(Intent intent) {
353 PackageManager pm = activity.getPackageManager();
354 List<ResolveInfo> availableApps = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
355 if (availableApps != null) {
356 for (String targetApp : targetApplications) {
357 if (contains(availableApps, targetApp)) {
358 return targetApp;
359 }
360 }
361 }
362 return null;
363 }
364
365 private static boolean contains(Iterable<ResolveInfo> availableApps, String targetApp) {
366 for (ResolveInfo availableApp : availableApps) {
367 String packageName = availableApp.activityInfo.packageName;
368 if (targetApp.equals(packageName)) {
369 return true;
370 }
371 }
372 return false;
373 }
374
375 private AlertDialog showDownloadDialog() {
376 AlertDialog.Builder downloadDialog = new AlertDialog.Builder(activity);
377 downloadDialog.setTitle(title);
378 downloadDialog.setMessage(message);
379 downloadDialog.setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {
380 @Override
381 public void onClick(DialogInterface dialogInterface, int i) {
382 String packageName;
383 if (targetApplications.contains(BS_PACKAGE)) {
384 // Prefer to suggest download of BS if it's anywhere in the list
385 packageName = BS_PACKAGE;
386 } else {
387 // Otherwise, first option:
388 packageName = targetApplications.get(0);
389 }
390 Uri uri = Uri.parse("market://details?id=" + packageName);
391 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
392 try {
393 if (fragment == null) {
394 activity.startActivity(intent);
395 finishIfNeeded();
396 } else {
397 fragment.startActivity(intent);
398 }
399 } catch (ActivityNotFoundException anfe) {
400 // Hmm, market is not installed
401 Log.w(TAG, "Google Play is not installed; cannot install " + packageName);
402 }
403 }
404 });
405 downloadDialog.setNegativeButton(buttonNo, new DialogInterface.OnClickListener() {
406 @Override
407 public void onClick(DialogInterface dialogInterface, int i) {
408 finishIfNeeded();
409 }
410 });
411 downloadDialog.setCancelable(true);
412 downloadDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
413 @Override
414 public void onCancel(DialogInterface dialogInterface) {
415 finishIfNeeded();
416 }
417 });
418 return downloadDialog.show();
419 }
420
421 private void finishIfNeeded() {
422 if (fragment != null) {
423 return;
424 }
425 if (activity != null && activity instanceof UriHandlerActivity) {
426 activity.finish();
427 }
428 }
429
430
431 /**
432 * <p>Call this from your {@link Activity}'s
433 * {@link Activity#onActivityResult(int, int, Intent)} method.</p>
434 *
435 * @param requestCode request code from {@code onActivityResult()}
436 * @param resultCode result code from {@code onActivityResult()}
437 * @param intent {@link Intent} from {@code onActivityResult()}
438 * @return null if the event handled here was not related to this class, or
439 * else an {@link IntentResult} containing the result of the scan. If the user cancelled scanning,
440 * the fields will be null.
441 */
442 public static IntentResult parseActivityResult(int requestCode, int resultCode, Intent intent) {
443 if (requestCode == REQUEST_CODE) {
444 if (resultCode == Activity.RESULT_OK) {
445 String contents = intent.getStringExtra("SCAN_RESULT");
446 String formatName = intent.getStringExtra("SCAN_RESULT_FORMAT");
447 byte[] rawBytes = intent.getByteArrayExtra("SCAN_RESULT_BYTES");
448 int intentOrientation = intent.getIntExtra("SCAN_RESULT_ORIENTATION", Integer.MIN_VALUE);
449 Integer orientation = intentOrientation == Integer.MIN_VALUE ? null : intentOrientation;
450 String errorCorrectionLevel = intent.getStringExtra("SCAN_RESULT_ERROR_CORRECTION_LEVEL");
451 return new IntentResult(contents,
452 formatName,
453 rawBytes,
454 orientation,
455 errorCorrectionLevel);
456 }
457 return new IntentResult();
458 }
459 return null;
460 }
461
462
463 /**
464 * Defaults to type "TEXT_TYPE".
465 *
466 * @param text the text string to encode as a barcode
467 * @return the {@link AlertDialog} that was shown to the user prompting them to download the app
468 * if a prompt was needed, or null otherwise
469 * @see #shareText(CharSequence, CharSequence)
470 */
471 public final AlertDialog shareText(CharSequence text) {
472 return shareText(text, "TEXT_TYPE");
473 }
474
475 /**
476 * Shares the given text by encoding it as a barcode, such that another user can
477 * scan the text off the screen of the device.
478 *
479 * @param text the text string to encode as a barcode
480 * @param type type of data to encode. See {@code com.google.zxing.client.android.Contents.Type} constants.
481 * @return the {@link AlertDialog} that was shown to the user prompting them to download the app
482 * if a prompt was needed, or null otherwise
483 */
484 public final AlertDialog shareText(CharSequence text, CharSequence type) {
485 Intent intent = new Intent();
486 intent.addCategory(Intent.CATEGORY_DEFAULT);
487 intent.setAction(BS_PACKAGE + ".ENCODE");
488 intent.putExtra("ENCODE_TYPE", type);
489 intent.putExtra("ENCODE_DATA", text);
490 String targetAppPackage = findTargetAppPackage(intent);
491 if (targetAppPackage == null) {
492 return showDownloadDialog();
493 }
494 intent.setPackage(targetAppPackage);
495 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
496 intent.addFlags(FLAG_NEW_DOC);
497 attachMoreExtras(intent);
498 if (fragment == null) {
499 activity.startActivity(intent);
500 } else {
501 fragment.startActivity(intent);
502 }
503 return null;
504 }
505
506 private static List<String> list(String... values) {
507 return Collections.unmodifiableList(Arrays.asList(values));
508 }
509
510 private void attachMoreExtras(Intent intent) {
511 for (Map.Entry<String,Object> entry : moreExtras.entrySet()) {
512 String key = entry.getKey();
513 Object value = entry.getValue();
514 // Kind of hacky
515 if (value instanceof Integer) {
516 intent.putExtra(key, (Integer) value);
517 } else if (value instanceof Long) {
518 intent.putExtra(key, (Long) value);
519 } else if (value instanceof Boolean) {
520 intent.putExtra(key, (Boolean) value);
521 } else if (value instanceof Double) {
522 intent.putExtra(key, (Double) value);
523 } else if (value instanceof Float) {
524 intent.putExtra(key, (Float) value);
525 } else if (value instanceof Bundle) {
526 intent.putExtra(key, (Bundle) value);
527 } else {
528 intent.putExtra(key, value.toString());
529 }
530 }
531 }
532
533}