Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
[android] modify playground app camera runtime permission request beh…
Browse files Browse the repository at this point in the history
…avior (#1221)
  • Loading branch information
shawnlinboy authored and YorkShen committed Jan 8, 2019
1 parent 8b9ce52 commit b874c1f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,14 @@ public boolean onOptionsItemSelected(MenuItem item) {
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == CAMERA_PERMISSION_REQUEST_CODE && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startActivity(new Intent(this, CaptureActivity.class));
} else if (requestCode == WRITE_EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
Toast.makeText(this, "request camara permission fail!", Toast.LENGTH_SHORT).show();
if (requestCode == CAMERA_PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startActivity(new Intent(this, CaptureActivity.class));
} else {
Toast.makeText(this, "request camara permission fail!", Toast.LENGTH_SHORT).show();
}
} else if (requestCode == WRITE_EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE
&& grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.Toast;

Expand Down Expand Up @@ -57,8 +60,8 @@ public void openURL(String url) {
if ("weex://go/scan".equals(url)) {
if (ContextCompat.checkSelfPermission(mWXSDKInstance.getContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) mWXSDKInstance.getContext(), Manifest.permission.CAMERA)) {
Toast.makeText(mWXSDKInstance.getContext(), "Weex playground need the camera permission to scan QR code", Toast.LENGTH_SHORT).show();
} else {
showCameraPermissionRationale();
} else{
ActivityCompat.requestPermissions((Activity) mWXSDKInstance.getContext(), new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
}
} else {
Expand Down Expand Up @@ -89,6 +92,21 @@ public void openURL(String url) {
mWXSDKInstance.fireModuleEvent("event", this, params);
}
}

private void showCameraPermissionRationale() {
if (mWXSDKInstance.getContext() instanceof AppCompatActivity) {
AlertDialog.Builder builder = new AlertDialog.Builder(((AppCompatActivity) mWXSDKInstance.getContext()));
builder.setMessage("Weex playground need the camera permission to scan QR code");
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions((Activity) mWXSDKInstance.getContext(), new String[]{Manifest.permission.CAMERA}, CAMERA_PERMISSION_REQUEST_CODE);
}
});
builder.create().show();
}
}

/*
* a test method for macaca case, you can fire globalEvent when download finish、device shaked and so on.
* @param event event name
Expand All @@ -108,9 +126,13 @@ public void fireNativeGlobalEvent(String event, JSCallback callback) {

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == CAMERA_PERMISSION_REQUEST_CODE && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mWXSDKInstance.getContext().startActivity(new Intent(mWXSDKInstance.getContext(), CaptureActivity.class));
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == CAMERA_PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mWXSDKInstance.getContext().startActivity(new Intent(mWXSDKInstance.getContext(), CaptureActivity.class));
} else {
Toast.makeText(mWXSDKInstance.getContext(), "request camara permission fail!", Toast.LENGTH_SHORT).show();
}
}
}
}

0 comments on commit b874c1f

Please sign in to comment.