Skip to content

Commit

Permalink
Merge pull request #287 from tingbob/master
Browse files Browse the repository at this point in the history
Redefine the default activity request code for using switch statement.
  • Loading branch information
rkistner authored Mar 4, 2018
2 parents 8e90d15 + 1817bb1 commit c98a04a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
18 changes: 18 additions & 0 deletions sample/src/main/java/example/zxing/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

public class MainActivity extends AppCompatActivity {

public final int CUSTOMIZED_REQUEST_CODE = 0x0000ffff;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -31,6 +33,10 @@ public void scanBarcode(View view) {
new IntentIntegrator(this).initiateScan();
}

public void scanBarcodeWithCustomizedRequestCode(View view) {
new IntentIntegrator(this, CUSTOMIZED_REQUEST_CODE).initiateScan();
}

public void scanBarcodeInverted(View view){
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.addExtra(Intents.Scan.INVERTED_SCAN, true);
Expand Down Expand Up @@ -87,6 +93,18 @@ public void tabs(View view) {
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case CUSTOMIZED_REQUEST_CODE: {
Toast.makeText(this, "REQUEST_CODE = " + requestCode, Toast.LENGTH_LONG).show();
break;
}
case IntentIntegrator.REQUEST_CODE: {
Toast.makeText(this, "IntentIntegrator default REQUEST_CODE = " + requestCode, Toast.LENGTH_LONG).show();
break;
}
default:
break;
}
if(result != null) {
if(result.getContents() == null) {
Log.d("MainActivity", "Cancelled scan");
Expand Down
6 changes: 6 additions & 0 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
android:text="@string/scan_barcode"
android:onClick="scanBarcode"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/scan_barcode_with_request_code"
android:onClick="scanBarcodeWithCustomizedRequestCode"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
1 change: 1 addition & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

<string name="app_name">ZXing Sample</string>
<string name="scan_barcode">Scan Barcode</string>
<string name="scan_barcode_with_request_code">Scan Barcode with customized request code</string>
<string name="any_orientation">1D Any Orientation</string>
<string name="scan_from_fragment">Scan from fragment</string>
<string name="front_camera">Front Camera</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@
@SuppressWarnings("unused")
public class IntentIntegrator {

public static int REQUEST_CODE = 0x0000c0de; // Only use bottom 16 bits
public static final int REQUEST_CODE = 0x0000c0de; // Only use bottom 16 bits
private static int requestCodeValue;

private static final String TAG = IntentIntegrator.class.getSimpleName();

// supported barcode formats
Expand Down Expand Up @@ -73,8 +75,18 @@ protected Class<?> getDefaultCaptureActivity() {
/**
* @param activity {@link Activity} invoking the integration
*/
public IntentIntegrator(Activity activity, int request_code) {
this.activity = activity;
if (request_code > 0 && request_code <= 0x0000ffff) {
requestCodeValue = request_code;
} else {
requestCodeValue = REQUEST_CODE;
}
}

public IntentIntegrator(Activity activity) {
this.activity = activity;
requestCodeValue = REQUEST_CODE;
}

public Class<?> getCaptureActivity() {
Expand Down Expand Up @@ -199,7 +211,7 @@ public IntentIntegrator setDesiredBarcodeFormats(Collection<String> desiredBarco
* Initiates a scan for all known barcode types with the default camera.
*/
public final void initiateScan() {
startActivityForResult(createScanIntent(), REQUEST_CODE);
startActivityForResult(createScanIntent(), requestCodeValue);
}

/**
Expand Down Expand Up @@ -297,7 +309,7 @@ protected void startActivity(Intent intent) {
* the fields will be null.
*/
public static IntentResult parseActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == REQUEST_CODE) {
if (requestCode == requestCodeValue) {
if (resultCode == Activity.RESULT_OK) {
String contents = intent.getStringExtra(Intents.Scan.RESULT);
String formatName = intent.getStringExtra(Intents.Scan.RESULT_FORMAT);
Expand Down

0 comments on commit c98a04a

Please sign in to comment.