From 497d6019e14dfc0ccb992b8cbab26e3734f39c90 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Fri, 14 Feb 2020 19:02:43 +0100 Subject: [PATCH] kivy/android: ask for STORAGE permission at runtime --- electrum/gui/kivy/main_window.py | 19 +++++++++++++++++-- electrum/util.py | 7 +++---- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/electrum/gui/kivy/main_window.py b/electrum/gui/kivy/main_window.py index e19b45ce2..fbd23e236 100644 --- a/electrum/gui/kivy/main_window.py +++ b/electrum/gui/kivy/main_window.py @@ -7,7 +7,7 @@ import traceback from decimal import Decimal import threading import asyncio -from typing import TYPE_CHECKING, Optional, Union, Callable +from typing import TYPE_CHECKING, Optional, Union, Callable, Sequence from electrum.storage import WalletStorage, StorageReadWriteError from electrum.wallet_db import WalletDB @@ -1223,11 +1223,26 @@ class ElectrumWindow(App): self._password_dialog.open() def save_backup(self): + if platform != 'android': + self._save_backup() + return + + from android.permissions import request_permissions, Permission + def cb(permissions, grant_results: Sequence[bool]): + if not grant_results or not grant_results[0]: + self.show_error(_("Cannot save backup without STORAGE permission")) + return + # note: Clock.schedule_once is a hack so that we get called on a non-daemon thread + # (needed for WalletDB.write) + Clock.schedule_once(lambda dt: self._save_backup()) + request_permissions([Permission.WRITE_EXTERNAL_STORAGE], cb) + + def _save_backup(self): new_path = self.wallet.save_backup(pin_code=self.pin_code) if new_path: self.show_info(_("Backup saved:") + f"\n{new_path}") else: - self.show_error(_("Backup directory not configured")) + self.show_error(_("Backup NOT saved. Backup directory or password not configured.")) def export_private_keys(self, pk_label, addr): if self.wallet.is_watching_only(): diff --git a/electrum/util.py b/electrum/util.py index 9e6d613dd..932602ac8 100644 --- a/electrum/util.py +++ b/electrum/util.py @@ -426,12 +426,11 @@ def profiler(func): def android_ext_dir(): - import jnius - env = jnius.autoclass('android.os.Environment') - return env.getExternalStorageDirectory().getPath() + from android.storage import primary_external_storage_path + return primary_external_storage_path() def android_backup_dir(): - d = android_ext_dir() + '/org.electrum.electrum' + d = os.path.join(android_ext_dir(), 'org.electrum.electrum') if not os.path.exists(d): os.mkdir(d) return d