Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ShouldShowRequestPermissionRationale #722

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions zxing-android-embedded/res-orig/values/zxing_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@
<string name="zxing_button_ok">OK</string>
<string name="zxing_msg_camera_framework_bug">Sorry, the Android camera encountered a problem. You may need to restart the device.</string>
<string name="zxing_msg_default_status">Place a barcode inside the viewfinder rectangle to scan it.</string>
<string name="camera_permission_request_explanation">To make app function properly, you should give camera permission. Would you like to give permission?</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;
import android.view.Display;
import android.view.Surface;
Expand All @@ -38,10 +41,10 @@
* Manages barcode scanning for a CaptureActivity. This class may be used to have a custom Activity
* (e.g. with a customized look and feel, or a different superclass), but not the barcode scanning
* process itself.
*
* <p>
* This is intended for an Activity that is dedicated to capturing a single barcode and returning
* it via setResult(). For other use cases, use DefaultBarcodeScannerView or BarcodeView directly.
*
* <p>
* The following is managed by this class:
* - Orientation lock
* - InactivityTimer
Expand Down Expand Up @@ -138,7 +141,7 @@ public CaptureManager(Activity activity, DecoratedBarcodeView barcodeView) {
/**
* Perform initialization, according to preferences set in the intent.
*
* @param intent the intent containing the scanning preferences
* @param intent the intent containing the scanning preferences
* @param savedInstanceState saved state, containing orientation lock
*/
public void initializeFromIntent(Intent intent, Bundle savedInstanceState) {
Expand Down Expand Up @@ -242,20 +245,47 @@ private void openCameraWithPermission() {
== PackageManager.PERMISSION_GRANTED) {
barcodeView.resume();
} else if (!askedPermission) {
showPermissionRequiredDialog();
askedPermission = true;
} // else wait for permission result
}

private void showPermissionRequiredDialog() {
Dialog dialog = new AlertDialog.Builder(activity)
.setMessage(R.string.camera_permission_request_explanation)
.setPositiveButton(android.R.string.ok, (dialogInterface, i) -> {
dialogInterface.cancel();
requestPermissionOrNavigateToSettings();
})
.setNegativeButton(android.R.string.no, (dialogInterface, i) -> {
dialogInterface.cancel();
activity.finish();
})
.create();
dialog.show();
}

private void requestPermissionOrNavigateToSettings() {
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.CAMERA)) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse("package:" + activity.getPackageName()));
activity.startActivity(intent);
} else {
ActivityCompat.requestPermissions(this.activity,
new String[]{Manifest.permission.CAMERA},
cameraPermissionReqCode);
askedPermission = true;
} // else wait for permission result
}
}

/**
* Call from Activity#onRequestPermissionsResult
* @param requestCode The request code passed in {@link androidx.core.app.ActivityCompat#requestPermissions(Activity, String[], int)}.
* @param permissions The requested permissions.
*
* @param requestCode The request code passed in {@link androidx.core.app.ActivityCompat#requestPermissions(Activity, String[], int)}.
* @param permissions The requested permissions.
* @param grantResults The grant results for the corresponding permissions
* which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED}
* or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null.
* which is either {@link android.content.pm.PackageManager#PERMISSION_GRANTED}
* or {@link android.content.pm.PackageManager#PERMISSION_DENIED}. Never null.
*/
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (requestCode == cameraPermissionReqCode) {
Expand Down Expand Up @@ -302,7 +332,7 @@ public void onSaveInstanceState(Bundle outState) {
/**
* Create a intent to return as the Activity result.
*
* @param rawResult the BarcodeResult, must not be null.
* @param rawResult the BarcodeResult, must not be null.
* @param barcodeImagePath a path to an exported file of the Barcode Image, can be null.
* @return the Intent
*/
Expand Down