Skip to content

Commit

Permalink
android: runtime permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
SomberNight committed Nov 24, 2018
1 parent 9b278e5 commit d8f6ea0
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import android.os.Bundle;
import android.util.Log;
import android.content.Intent;
import android.support.v4.app.ActivityCompat;
import android.Manifest;
import android.content.pm.PackageManager;

import java.util.Arrays;

Expand All @@ -13,28 +16,35 @@
import com.google.zxing.BarcodeFormat;

public class SimpleScannerActivity extends Activity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;
final String TAG = "org.electrum.SimpleScannerActivity";
private static final int MY_PERMISSIONS_CAMERA = 1002;

@Override
public void onCreate(Bundle state) {
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
}
private ZXingScannerView mScannerView = null;
final String TAG = "org.electrum.SimpleScannerActivity";

@Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this); // Register ourselves as a handler for scan results.
mScannerView.startCamera(); // Start camera on resume
if (this.hasPermission()) {
this.startCamera();
} else {
this.requestPermission();
}
}

@Override
public void 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
Expand All @@ -45,4 +55,35 @@ public void handleResult(Result rawResult) {
setResult(Activity.RESULT_OK, resultIntent);
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;
}
}
}

}
10 changes: 10 additions & 0 deletions run_electrum
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
# SOFTWARE.
import os
import sys
import time

script_dir = os.path.dirname(os.path.realpath(__file__))
is_bundle = getattr(sys, 'frozen', False)
Expand Down Expand Up @@ -331,6 +332,15 @@ if __name__ == '__main__':
elif config.get('simnet'):
constants.set_simnet()

# if android, wait until we have storage R/W permissions.
# the user is being prompted for these in a separate thread handled by p4a
if is_android:
fdir = util.get_headers_dir(config)
while True:
try: os.listdir(fdir)
except OSError: time.sleep(0.1)
else: break

if cmdname == 'gui':
fd, server = daemon.get_fd_or_server(config)
if fd is not None:
Expand Down

0 comments on commit d8f6ea0

Please sign in to comment.