Skip to content

Commit

Permalink
Bugfix Service restart after OS kill (#96)
Browse files Browse the repository at this point in the history
* single open wallet

* prevent WalletService & Activity from restarting
removed stray png
  • Loading branch information
m2049r authored Oct 3, 2017
1 parent f282f01 commit e8b749a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 56 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.m2049r.xmrwallet"
minSdkVersion 21
targetSdkVersion 25
versionCode 24
versionName "1.0.1"
versionCode 25
versionName "1.0.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
Expand Down
Binary file removed app/src/main/ic_launcher-web.png
Binary file not shown.
5 changes: 4 additions & 1 deletion app/src/main/java/com/m2049r/xmrwallet/WalletActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate()");
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
// we don't store anything ourselves
// activity restarted
// we don't want that - finish it and fall back to previous activity
finish();
return;
}

setContentView(R.layout.wallet_activity);
Expand Down
54 changes: 27 additions & 27 deletions app/src/main/java/com/m2049r/xmrwallet/model/WalletManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.m2049r.xmrwallet.model;

import android.support.annotation.NonNull;
import android.util.Log;

import java.io.BufferedReader;
Expand Down Expand Up @@ -45,36 +46,36 @@ public static synchronized WalletManager getInstance() {
return WalletManager.Instance;
}

private WalletManager() {
this.managedWallets = new HashMap<>();
}

private Map<String, Wallet> managedWallets;
//private Map<String, Wallet> managedWallets;
private Wallet managedWallet = null;

public Wallet getWallet(String walletId) {
return managedWallets.get(walletId);
public Wallet getWallet() {
return managedWallet;
}

private void manageWallet(String walletId, Wallet wallet) {
if (getWallet(walletId) != null) {
throw new IllegalStateException(walletId + " already under management!");
}
Log.d(TAG, "Managing " + walletId);
managedWallets.put(walletId, wallet);
private void manageWallet(Wallet wallet) {
Log.d(TAG, "Managing " + wallet.getName());
managedWallet = wallet;
}

private void unmanageWallet(String walletId) {
if (getWallet(walletId) == null) {
throw new IllegalStateException(walletId + " not under management!");
private void unmanageWallet(Wallet wallet) {
if (wallet == null) {
throw new IllegalArgumentException("Cannot unmanage null!");
}
if (getWallet() == null) {
throw new IllegalStateException("No wallet under management!");
}
if (getWallet() != wallet) {
throw new IllegalStateException(wallet.getName() + " not under management!");
}
Log.d(TAG, "Unmanaging " + walletId);
managedWallets.remove(walletId);
Log.d(TAG, "Unmanaging " + managedWallet.getName());
managedWallet = null;
}

public Wallet createWallet(File aFile, String password, String language) {
long walletHandle = createWalletJ(aFile.getAbsolutePath(), password, language, isTestNet());
Wallet wallet = new Wallet(walletHandle);
manageWallet(wallet.getName(), wallet);
manageWallet(wallet);
return wallet;
}

Expand All @@ -83,22 +84,22 @@ public Wallet createWallet(File aFile, String password, String language) {
public Wallet openWallet(String path, String password) {
long walletHandle = openWalletJ(path, password, isTestNet());
Wallet wallet = new Wallet(walletHandle);
manageWallet(wallet.getName(), wallet);
manageWallet(wallet);
return wallet;
}

private native long openWalletJ(String path, String password, boolean isTestNet);

public Wallet recoveryWallet(File aFile, String mnemonic) {
Wallet wallet = recoveryWallet(aFile, mnemonic, 0);
manageWallet(wallet.getName(), wallet);
manageWallet(wallet);
return wallet;
}

public Wallet recoveryWallet(File aFile, String mnemonic, long restoreHeight) {
long walletHandle = recoveryWalletJ(aFile.getAbsolutePath(), mnemonic, isTestNet(), restoreHeight);
Wallet wallet = new Wallet(walletHandle);
manageWallet(wallet.getName(), wallet);
manageWallet(wallet);
return wallet;
}

Expand All @@ -109,7 +110,7 @@ public Wallet createWalletFromKeys(File aFile, String language, long restoreHeig
long walletHandle = createWalletFromKeysJ(aFile.getAbsolutePath(), language, isTestNet(), restoreHeight,
addressString, viewKeyString, spendKeyString);
Wallet wallet = new Wallet(walletHandle);
manageWallet(wallet.getName(), wallet);
manageWallet(wallet);
return wallet;
}

Expand All @@ -123,13 +124,12 @@ private native long createWalletFromKeysJ(String path, String language,
public native boolean closeJ(Wallet wallet);

public boolean close(Wallet wallet) {
String walletId = new File(wallet.getFilename()).getName();
unmanageWallet(walletId);
unmanageWallet(wallet);
boolean closed = closeJ(wallet);
if (!closed) {
// in case we could not close it
// we unmanage it
manageWallet(walletId, wallet);
// we manage it again
manageWallet(wallet);
}
return closed;
}
Expand Down
45 changes: 19 additions & 26 deletions app/src/main/java/com/m2049r/xmrwallet/service/WalletService.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,11 @@ public class WalletService extends Service {
private MyWalletListener listener = null;

private class MyWalletListener implements WalletListener {
private Wallet wallet;
boolean updated = true;

Wallet getWallet() {
return wallet;
}

MyWalletListener(Wallet aWallet) {
if (aWallet == null) throw new IllegalArgumentException("Cannot open wallet!");
this.wallet = aWallet;
}

void start() {
Log.d(TAG, "MyWalletListener.start()");
Wallet wallet = getWallet();
if (wallet == null) throw new IllegalStateException("No wallet!");
//acquireWakeLock();
wallet.setListener(this);
Expand All @@ -92,6 +83,7 @@ void start() {

void stop() {
Log.d(TAG, "MyWalletListener.stop()");
Wallet wallet = getWallet();
if (wallet == null) throw new IllegalStateException("No wallet!");
wallet.pauseRefresh();
wallet.setListener(null);
Expand All @@ -115,6 +107,7 @@ public void unconfirmedMoneyReceived(String txId, long amount) {
int lastTxCount = 0;

public void newBlock(long height) {
Wallet wallet = getWallet();
if (wallet == null) throw new IllegalStateException("No wallet!");
// don't flood with an update for every block ...
if (lastBlockTime < System.currentTimeMillis() - 2000) {
Expand All @@ -141,14 +134,16 @@ public void newBlock(long height) {
}

public void updated() {
Log.d(TAG, "updated() " + wallet.getBalance());
Log.d(TAG, "updated()");
Wallet wallet = getWallet();
if (wallet == null) throw new IllegalStateException("No wallet!");
updated = true;
}

public void refreshed() {
Log.d(TAG, "refreshed()");
Wallet wallet = getWallet();
if (wallet == null) throw new IllegalStateException("No wallet!");
Log.d(TAG, "refreshed() " + wallet.getName() + " " + wallet.getBalance() + " sync=" + wallet.isSynchronized() + " with observer " + observer);
if (updated) {
if (observer != null) {
updateDaemonState(wallet, 0);
Expand Down Expand Up @@ -253,8 +248,7 @@ public int getProgressValue() {

//
public Wallet getWallet() {
if (listener == null) throw new IllegalStateException("no listener");
return listener.getWallet();
return WalletManager.getInstance().getWallet();
}

/////////////////////////////////////////////
Expand Down Expand Up @@ -441,10 +435,15 @@ public int onStartCommand(Intent intent, int flags, int startId) {
// start ID so we know which request we're stopping when we finish the job
Message msg = mServiceHandler.obtainMessage();
msg.arg2 = START_SERVICE;
msg.setData(intent.getExtras());
mServiceHandler.sendMessage(msg);
//Log.d(TAG, "onStartCommand() message sent");
return START_STICKY;
if (intent != null) {
msg.setData(intent.getExtras());
mServiceHandler.sendMessage(msg);
return START_STICKY;
} else {
// process restart - don't do anything - let system kill it again
stop();
return START_NOT_STICKY;
}
}

@Override
Expand All @@ -466,9 +465,8 @@ public boolean onUnbind(Intent intent) {
}

private boolean start(String walletName, String walletPassword) {
startNotfication();
// if there is an listener it is always started / syncing
Log.d(TAG, "start()");
startNotfication();
showProgress(getString(R.string.status_wallet_loading));
showProgress(10);
if (listener == null) {
Expand All @@ -478,7 +476,7 @@ private boolean start(String walletName, String walletPassword) {
if (aWallet != null) aWallet.close();
return false;
}
listener = new MyWalletListener(aWallet);
listener = new MyWalletListener();
listener.start();
showProgress(100);
}
Expand All @@ -496,10 +494,6 @@ public void stop() {
if (listener != null) {
listener.stop();
Wallet myWallet = getWallet();
// if (!myWallet.isSynchronized()) { // save only if NOT synced (to continue later)
// Log.d(TAG, "stop() saving");
// myWallet.store();
// }
Log.d(TAG, "stop() closing");
myWallet.close();
Log.d(TAG, "stop() closed");
Expand Down Expand Up @@ -561,6 +555,5 @@ private void startNotfication() {
.setContentIntent(pendingIntent)
.build();
startForeground(NOTIFICATION_ID, notification);

}
}

0 comments on commit e8b749a

Please sign in to comment.