Skip to content

Commit

Permalink
android: Use LabelSync plugin
Browse files Browse the repository at this point in the history
Explicitly setting the "use_labels" setting in the Android code might be
antithetical to "plugins" but since there is no way to plug in or out
modules in runtime in Android we might as well hardcode when needed.

There are a couple of things to fix/investigate:
* Currently the application has to be restarted when the settings have
  changed.
* run_hook is not runned on main thread and a warning is printed. Is
  this a problem here?
* Is a force upload/download option needed?
* Should the 'android' part of the plugin, including the hooks, be more
  generically named? There isn't any Android specific things and other
  clients (iOS?) could use it...
  • Loading branch information
jonas-lundqvist committed May 24, 2021
1 parent f61b6d3 commit 9d51b73
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 3 deletions.
1 change: 1 addition & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ android {
exclude "electroncash/locale/"
include "electroncash_gui/__init__.py"
include "electroncash_plugins/__init__.py"
include "electroncash_plugins/labels/"

// From src/main/python
include "chaquopy/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ fun initSettings(): PyObject {
settings.getBoolean("cashaddr_format").observeForever {
clsAddress.callAttr("show_cashaddr", it)
}
settings.getBoolean("synchronize_labels").observeForever {
settings.getBoolean("use_labels").setValue(it)
}
settings.getString("base_unit").observeForever {
unitName = it!!
val places = libUtil.get("base_units")!!.callAttr("get", it)
Expand Down Expand Up @@ -70,6 +73,7 @@ fun setDefaultValues(sp: SharedPreferences) {
// Appearance
setDefaultValue(sp, "cashaddr_format",
clsAddress.get("FMT_UI") == clsAddress.get("FMT_CASHADDR"))
setDefaultValue(sp, "use_labels", false)
setDefaultValue(sp, "base_unit", libUtil.get("DEFAULT_BASE_UNIT")!!.toString())
setDefaultValue(sp, "block_explorer", libWeb.callAttr("BE_default_explorer")!!.toString())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from electroncash import commands, daemon, interface, keystore, storage, util
from electroncash.i18n import _
from electroncash.plugins import Plugins, run_hook
from electroncash.storage import WalletStorage
from electroncash.wallet import (ImportedAddressWallet, ImportedPrivkeyWallet, Standard_Wallet,
Wallet)
Expand Down Expand Up @@ -80,7 +81,8 @@ def __init__(self, config):

# Create daemon here rather than in start() so the DaemonModel has a chance to register
# its callback before the daemon threads start.
self.daemon = daemon.Daemon(self.config, fd, is_gui=False, plugins=None)
plugins = Plugins(config, "android")
self.daemon = daemon.Daemon(self.config, fd, is_gui=False, plugins=plugins)
self.daemon_running = False

self.gui_callback = None
Expand Down Expand Up @@ -128,11 +130,14 @@ def load_wallet(self, name, password=None):
wallet = Wallet(storage)
wallet.start_threads(self.network)
self.daemon.add_wallet(wallet)
run_hook('start_android_wallet', wallet)

def close_wallet(self, name=None):
"""Close a wallet"""
path = self._wallet_path(name)
run_hook('stop_android_wallet', self.daemon.get_wallet(path))
self._assert_daemon_running()
self.daemon.stop_wallet(self._wallet_path(name))
self.daemon.stop_wallet(path)

def create(self, name, password, seed=None, passphrase="", bip39_derivation=None,
master=None, addresses=None, privkeys=None):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
_("Show seed")
_("Size")
_("Signed transaction")
_("Synchronize labels")
_("The string you entered has been broadcast. Please check your transactions for confirmation.")
_("Transaction not found")
_("%1$d tx (%2$d unverified)")
Expand Down
3 changes: 3 additions & 0 deletions android/app/src/main/res/xml/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<SwitchPreference
android:key="cashaddr_format"
android:title="@string/cashaddr_address"/>
<SwitchPreference
android:key="synchronize_labels"
android:title="@string/synchronize_labels"/>
<ListPreference
android:key="base_unit"
android:title="@string/base_unit"
Expand Down
2 changes: 1 addition & 1 deletion electroncash_plugins/labels/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
_("Save your wallet labels on a remote server, and synchronize them across multiple devices where you use Electron Cash."),
_("Labels, transactions IDs and addresses are encrypted before they are sent to the remote server.")
]
available_for = [ 'qt' ]
available_for = ['qt', 'android']
15 changes: 15 additions & 0 deletions electroncash_plugins/labels/android.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from electroncash.plugins import hook

from .labels import LabelsPlugin

class Plugin(LabelsPlugin):
def __init__(self, *args):
LabelsPlugin.__init__(self, *args)

@hook
def start_android_wallet(self, wallet):
self.start_wallet(wallet)

@hook
def stop_android_wallet(self, wallet):
self.stop_wallet(wallet)

0 comments on commit 9d51b73

Please sign in to comment.