Browse Source

android: runtime permission dialog for camera

3.3.3.1
SomberNight 6 years ago
parent
commit
29b697df1a
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 65
      electrum/gui/kivy/data/java-classes/org/electrum/qr/SimpleScannerActivity.java

65
electrum/gui/kivy/data/java-classes/org/electrum/qr/SimpleScannerActivity.java

@ -4,6 +4,9 @@ import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log; import android.util.Log;
import android.content.Intent; import android.content.Intent;
import android.support.v4.app.ActivityCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import java.util.Arrays; import java.util.Arrays;
@ -13,28 +16,35 @@ import com.google.zxing.Result;
import com.google.zxing.BarcodeFormat; import com.google.zxing.BarcodeFormat;
public class SimpleScannerActivity extends Activity implements ZXingScannerView.ResultHandler { public class SimpleScannerActivity extends Activity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView; private static final int MY_PERMISSIONS_CAMERA = 1002;
final String TAG = "org.electrum.SimpleScannerActivity";
@Override private ZXingScannerView mScannerView = null;
public void onCreate(Bundle state) { final String TAG = "org.electrum.SimpleScannerActivity";
super.onCreate(state);
mScannerView = new ZXingScannerView(this); // Programmatically initialize the scanner view
mScannerView.setFormats(Arrays.asList(BarcodeFormat.QR_CODE));
setContentView(mScannerView); // Set the scanner view as the content view
}
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results. if (this.hasPermission()) {
mScannerView.startCamera(); // Start camera on resume this.startCamera();
} else {
this.requestPermission();
}
} }
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
mScannerView.stopCamera(); // Stop camera on pause if (null != mScannerView) {
mScannerView.stopCamera(); // Stop camera on pause
}
}
private void startCamera() {
mScannerView = new ZXingScannerView(this); // Programmatically initialize the scanner view
mScannerView.setFormats(Arrays.asList(BarcodeFormat.QR_CODE));
setContentView(mScannerView); // Set the scanner view as the content view
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera on resume
} }
@Override @Override
@ -45,4 +55,35 @@ public class SimpleScannerActivity extends Activity implements ZXingScannerView.
setResult(Activity.RESULT_OK, resultIntent); setResult(Activity.RESULT_OK, resultIntent);
this.finish(); this.finish();
} }
private boolean hasPermission() {
return (ActivityCompat.checkSelfPermission(this,
Manifest.permission.CAMERA)
== PackageManager.PERMISSION_GRANTED);
}
private void requestPermission() {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_CAMERA);
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_CAMERA: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay!
this.startCamera();
} else {
// permission denied
this.finish();
}
return;
}
}
}
} }

Loading…
Cancel
Save