Skip to content

Commit

Permalink
Merge pull request #408 from mycelium-com/logAnrFix
Browse files Browse the repository at this point in the history
Prevented potential deadlock as we don't need to sync on whole class
  • Loading branch information
Giszmo authored Jul 26, 2018
2 parents b44bab9 + 8b1e81a commit 62f62c4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 27 deletions.
41 changes: 17 additions & 24 deletions mbw/src/main/java/com/mycelium/wallet/MbwManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import com.google.common.cache.CacheBuilder;
import com.google.common.collect.EvictingQueue;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Queues;
import com.google.common.collect.Sets;
import com.google.common.primitives.Ints;
import com.mrd.bitlib.crypto.Bip39;
Expand Down Expand Up @@ -134,6 +135,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Queue;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;
Expand Down Expand Up @@ -217,12 +219,14 @@ public static synchronized MbwManager getInstance(Context context) {
private TorManager _torManager;
public final BlockExplorerManager _blockExplorerManager;

private EvictingQueue<LogEntry> _wapiLogs = EvictingQueue.create(100);
private final Queue<LogEntry> _wapiLogs;
private Cache<String, Object> _semiPersistingBackgroundObjects = CacheBuilder.newBuilder().maximumSize(10).build();

private WalletConfiguration configuration;

private MbwManager(Context evilContext) {
Queue<LogEntry> unsafeWapiLogs = EvictingQueue.create(100);
_wapiLogs = Queues.synchronizedQueue(unsafeWapiLogs);
_applicationContext = Preconditions.checkNotNull(evilContext.getApplicationContext());
_environment = MbwEnvironment.verifyEnvironment();
String version = VersionManager.determineVersion(_applicationContext);
Expand Down Expand Up @@ -380,7 +384,6 @@ public InMemoryPrivateKey deriveKey(int accountIndex, String site) {
}
}


private Optional<ColuManager> createColuManager(final Context context) {
// Create persisted account backing
// we never talk directly to this class. Instead, we use SecureKeyValueStore API
Expand Down Expand Up @@ -428,7 +431,7 @@ public void logInfo(String message) {
});
}

private synchronized void retainLog(Level level, String message) {
private void retainLog(Level level, String message) {
_wapiLogs.add(new LogEntry(message, level, new Date()));
}

Expand Down Expand Up @@ -788,7 +791,7 @@ public void pinEntered(PinDialog dialog, Pin pin) {
}
} else {
Toast.makeText(activity, R.string.pin_codes_dont_match, Toast.LENGTH_LONG).show();
MbwManager.this.vibrate(500);
MbwManager.this.vibrate();
dialog.dismiss();
if (afterDialogClosed.isPresent()) {
afterDialogClosed.get().run();
Expand Down Expand Up @@ -860,7 +863,7 @@ public void pinEntered(final PinDialog pinDialog, Pin pin) {
Thread.sleep(millis);
} catch (InterruptedException ignored) {
Toast.makeText(activity, "Something weird is happening. avoid getting to pin check", Toast.LENGTH_LONG).show();
vibrate(500);
vibrate();
pinDialog.dismiss();
return;
}
Expand Down Expand Up @@ -903,7 +906,7 @@ public void onClick(DialogInterface dialogInterface, int i) {
} else {
// This pin is not resettable, you are out of luck
Toast.makeText(activity, R.string.pin_invalid_pin, Toast.LENGTH_LONG).show();
vibrate(500);
vibrate();
pinDialog.dismiss();
}
}
Expand Down Expand Up @@ -933,13 +936,9 @@ public Optional<Integer> getResetPinRemainingBlocksCount() {
}

public void vibrate() {
vibrate(500);
}

private void vibrate(int milliseconds) {
Vibrator v = (Vibrator) _applicationContext.getSystemService(Context.VIBRATOR_SERVICE);
if (v != null) {
v.vibrate(milliseconds);
v.vibrate(500);
}
}

Expand Down Expand Up @@ -1278,7 +1277,7 @@ public WapiClient getWapi() {
return _wapi;
}

public EvictingQueue<LogEntry> getWapiLogs() {
public Queue<LogEntry> getWapiLogs() {
return _wapiLogs;
}

Expand Down Expand Up @@ -1335,17 +1334,15 @@ public CoinapultManager getCoinapultManager() {
}
}

public ColuManager getColuManager() {
public synchronized ColuManager getColuManager() {
if(_coluManager != null && _coluManager.isPresent()) {
return _coluManager.get();
} else {
synchronized (this) {
_coluManager = createColuManager(_applicationContext);
if (_coluManager.isPresent()) {
return _coluManager.get();
} else {
throw new IllegalStateException("Tried to obtain colu manager without having created one.");
}
_coluManager = createColuManager(_applicationContext);
if (_coluManager.isPresent()) {
return _coluManager.get();
} else {
throw new IllegalStateException("Tried to obtain colu manager without having created one.");
}
}
}
Expand All @@ -1354,10 +1351,6 @@ public Cache<String, Object> getBackgroundObjectsCache() {
return _semiPersistingBackgroundObjects;
}

private void switchServer() {
_environment.getWapiEndpoints().switchToNextEndpoint();
}

public void stopWatchingAddress(){
if (_addressWatchTimer != null){
_addressWatchTimer.cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,5 @@ public boolean onLongClick(View view) {
return true;
}
});

}


}

0 comments on commit 62f62c4

Please sign in to comment.