From d8f6ea0c3fa248a4ff31ea953ec3bda36121d4df Mon Sep 17 00:00:00 2001 From: SomberNight Date: Sat, 24 Nov 2018 03:29:50 +0100 Subject: [PATCH] android: runtime permissions --- .../electrum/qr/SimpleScannerActivity.java | 65 +++++++++++++++---- run_electrum | 10 +++ 2 files changed, 63 insertions(+), 12 deletions(-) diff --git a/electrum/gui/kivy/data/java-classes/org/electrum/qr/SimpleScannerActivity.java b/electrum/gui/kivy/data/java-classes/org/electrum/qr/SimpleScannerActivity.java index 8f4714628770..2d29d7f1c0b6 100644 --- a/electrum/gui/kivy/data/java-classes/org/electrum/qr/SimpleScannerActivity.java +++ b/electrum/gui/kivy/data/java-classes/org/electrum/qr/SimpleScannerActivity.java @@ -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; @@ -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 @@ -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; + } + } + } + } diff --git a/run_electrum b/run_electrum index dbb88d9c1728..5124a510d89b 100755 --- a/run_electrum +++ b/run_electrum @@ -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) @@ -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: