AI2 Component  (Version nb184)
BarcodeScanner.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2020 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime;
8 
9 import android.Manifest;
10 import android.app.Activity;
11 import android.content.ActivityNotFoundException;
12 import android.content.ComponentName;
13 import android.content.Intent;
14 
31 
37 @DesignerComponent(version = YaVersion.BARCODESCANNER_COMPONENT_VERSION,
38  description = "Component for using the Barcode Scanner to read a barcode",
39  category = ComponentCategory.SENSORS,
40  nonVisible = true,
41  iconName = "images/barcodeScanner.png")
42 @SimpleObject
43 @UsesActivities(activities = {
44  @ActivityElement(name = "com.google.zxing.client.android.AppInvCaptureActivity",
45  screenOrientation = "landscape",
46  stateNotNeeded = "true",
47  configChanges = "orientation|keyboardHidden",
48  theme = "@android:style/Theme.NoTitleBar.Fullscreen",
49  windowSoftInputMode = "stateAlwaysHidden")
50 })
51 @UsesPermissions(permissionNames = "android.permission.CAMERA")
52 @UsesLibraries(libraries = "Barcode.jar,QRGenerator.jar")
54  implements ActivityResultListener, Component {
55 
56  private static final String SCAN_INTENT = "com.google.zxing.client.android.SCAN";
57  private static final String LOCAL_SCAN = "com.google.zxing.client.android.AppInvCaptureActivity";
58  private static final String SCANNER_RESULT_NAME = "SCAN_RESULT";
59  private String result = "";
60  private boolean useExternalScanner = true;
61  private final ComponentContainer container;
62  private boolean havePermission = false; // Do we have CAMERA permission?
63 
64  /* Used to identify the call to startActivityForResult. Will be passed back into the
65  resultReturned() callback method. */
66  private int requestCode;
67 
73  public BarcodeScanner(ComponentContainer container) {
74  super(container.$form());
75  this.container = container;
76  }
77 
81  @SimpleProperty(description = "Text result of the previous scan.",
82  category = PropertyCategory.BEHAVIOR)
83  public String Result() {
84  return result;
85  }
86 
91  @SimpleFunction(description = "Begins a barcode scan, using the camera. When the scan " +
92  "is complete, the AfterScan event will be raised.")
93  public void DoScan() {
94  Intent intent = new Intent(SCAN_INTENT);
95  if (!useExternalScanner && (SdkLevel.getLevel() >= SdkLevel.LEVEL_ECLAIR)) { // Should we attempt to use an internal scanner?
96  // Make sure we have CAMERA permission
97  if (!havePermission) {
98  container.$form()
99  .askPermission(Manifest.permission.CAMERA,
101  @Override
102  public void HandlePermissionResponse(String permission, boolean granted) {
103  if (granted) {
104  BarcodeScanner.this.havePermission = true;
105  DoScan();
106  } else {
107  form.dispatchPermissionDeniedEvent(BarcodeScanner.this, "DoScan",
108  Manifest.permission.CAMERA);
109  }
110  }
111  });
112  return;
113  }
114  String packageName = container.$form().getPackageName();
115  intent.setComponent(new ComponentName(packageName, "com.google.zxing.client.android.AppInvCaptureActivity"));
116  }
117  if (requestCode == 0) {
118  requestCode = form.registerForActivityResult(this);
119  }
120  try {
121  container.$context().startActivityForResult(intent, requestCode);
122  } catch (ActivityNotFoundException e) {
123  e.printStackTrace();
124  container.$form().dispatchErrorOccurredEvent(this, "BarcodeScanner",
126  }
127  }
128 
129  @Override
130  public void resultReturned(int requestCode, int resultCode, Intent data) {
131  if (requestCode == this.requestCode && resultCode == Activity.RESULT_OK) {
132  if (data.hasExtra(SCANNER_RESULT_NAME)) {
133  result = data.getStringExtra(SCANNER_RESULT_NAME);
134  } else {
135  result = "";
136  }
137  AfterScan(result);
138  }
139  }
140 
144  @SimpleEvent
145  public void AfterScan(String result) {
146  EventDispatcher.dispatchEvent(this, "AfterScan", result);
147  }
148 
158  description = "If true App Inventor will look for and use an external scanning" +
159  " program such as \"Bar Code Scanner.\"")
160  public boolean UseExternalScanner() {
161  return useExternalScanner;
162  }
163 
173  @DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN, defaultValue = "True")
174  @SimpleProperty()
175  public void UseExternalScanner(boolean useExternalScanner) {
176  this.useExternalScanner = useExternalScanner;
177  }
178 
179 }
com.google.appinventor.components.runtime.EventDispatcher
Definition: EventDispatcher.java:22
com.google.appinventor.components.runtime.Form.askPermission
void askPermission(final String permission, final PermissionResultHandler responseRequestor)
Definition: Form.java:2633
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.annotations.UsesLibraries
Definition: UsesLibraries.java:21
com.google.appinventor.components.runtime.util.ErrorMessages
Definition: ErrorMessages.java:17
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_BOOLEAN
static final String PROPERTY_TYPE_BOOLEAN
Definition: PropertyTypeConstants.java:35
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.annotations.SimpleEvent
Definition: SimpleEvent.java:20
com.google.appinventor.components.annotations.PropertyCategory.BEHAVIOR
BEHAVIOR
Definition: PropertyCategory.java:15
com.google.appinventor.components.runtime.util.SdkLevel.LEVEL_ECLAIR
static final int LEVEL_ECLAIR
Definition: SdkLevel.java:22
com.google.appinventor.components.runtime.BarcodeScanner
Definition: BarcodeScanner.java:53
com.google.appinventor.components.annotations.UsesPermissions
Definition: UsesPermissions.java:21
com.google.appinventor.components.runtime.BarcodeScanner.resultReturned
void resultReturned(int requestCode, int resultCode, Intent data)
Definition: BarcodeScanner.java:130
com.google.appinventor.components.runtime.PermissionResultHandler
Definition: PermissionResultHandler.java:15
com.google.appinventor.components.annotations.androidmanifest
Definition: ActionElement.java:6
com.google.appinventor.components.runtime.EventDispatcher.dispatchEvent
static boolean dispatchEvent(Component component, String eventName, Object...args)
Definition: EventDispatcher.java:188
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_NO_SCANNER_FOUND
static final int ERROR_NO_SCANNER_FOUND
Definition: ErrorMessages.java:157
com.google.appinventor.components.runtime.AndroidNonvisibleComponent
Definition: AndroidNonvisibleComponent.java:17
com.google.appinventor.components.runtime.util.SdkLevel
Definition: SdkLevel.java:19
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime.util.SdkLevel.getLevel
static int getLevel()
Definition: SdkLevel.java:45
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.Component
Definition: Component.java:17
com.google.appinventor.components.runtime.BarcodeScanner.AfterScan
void AfterScan(String result)
Definition: BarcodeScanner.java:145
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.runtime.Form.dispatchErrorOccurredEvent
void dispatchErrorOccurredEvent(final Component component, final String functionName, final int errorNumber, final Object... messageArgs)
Definition: Form.java:1011
com.google.appinventor.components.runtime.ActivityResultListener
Definition: ActivityResultListener.java:16
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google
com
com.google.appinventor.components.runtime.BarcodeScanner.BarcodeScanner
BarcodeScanner(ComponentContainer container)
Definition: BarcodeScanner.java:73
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.runtime.ComponentContainer.$context
Activity $context()
com.google.appinventor.components.annotations.androidmanifest.ActivityElement
Definition: ActivityElement.java:33
com.google.appinventor.components.runtime.Form.registerForActivityResult
int registerForActivityResult(ActivityResultListener listener)
Definition: Form.java:638
com.google.appinventor.components.common.PropertyTypeConstants
Definition: PropertyTypeConstants.java:14
com.google.appinventor.components.annotations
com.google.appinventor.components.annotations.UsesActivities
Definition: UsesActivities.java:24
com.google.appinventor