use of de.schildbach.wallet.data.SelectedExchangeRateLiveData in project bitcoin-wallet by bitcoin-wallet.
the class BlockchainService method onCreate.
@Override
public void onCreate() {
serviceUpTime = Stopwatch.createStarted();
log.debug(".onCreate()");
super.onCreate();
application = (WalletApplication) getApplication();
config = application.getConfiguration();
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
log.info("acquiring {}", wakeLock);
wakeLock.acquire();
connectivityNotification.setColor(getColor(R.color.fg_network_significant));
connectivityNotification.setContentTitle(getString(config.isTrustedPeersOnly() ? R.string.notification_connectivity_syncing_trusted_peer : R.string.notification_connectivity_syncing_message));
connectivityNotification.setContentIntent(PendingIntent.getActivity(BlockchainService.this, 0, new Intent(BlockchainService.this, WalletActivity.class), 0));
connectivityNotification.setWhen(System.currentTimeMillis());
connectivityNotification.setOngoing(true);
connectivityNotification.setPriority(NotificationCompat.PRIORITY_LOW);
startForeground(0);
backgroundThread = new HandlerThread("backgroundThread", Process.THREAD_PRIORITY_BACKGROUND);
backgroundThread.start();
backgroundHandler = new Handler(backgroundThread.getLooper());
addressBookDao = AddressBookDatabase.getDatabase(application).addressBookDao();
blockChainFile = new File(getDir("blockstore", Context.MODE_PRIVATE), Constants.Files.BLOCKCHAIN_FILENAME);
config.registerOnSharedPreferenceChangeListener(preferenceChangeListener);
registerReceiver(deviceIdleModeReceiver, new IntentFilter(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED));
peerConnectivityListener = new PeerConnectivityListener();
broadcastPeerState(0);
final WalletBalanceLiveData walletBalance = new WalletBalanceLiveData(application);
final SelectedExchangeRateLiveData exchangeRate = new SelectedExchangeRateLiveData(application);
walletBalance.observe(this, balance -> {
final ExchangeRateEntry rate = exchangeRate.getValue();
if (balance != null)
WalletBalanceWidgetProvider.updateWidgets(BlockchainService.this, balance, rate != null ? rate.exchangeRate() : null);
});
exchangeRate.observe(this, rate -> {
final Coin balance = walletBalance.getValue();
if (balance != null)
WalletBalanceWidgetProvider.updateWidgets(BlockchainService.this, balance, rate != null ? rate.exchangeRate() : null);
});
wallet = new WalletLiveData(application);
wallet.observe(this, new Observer<Wallet>() {
@Override
public void onChanged(final Wallet wallet) {
BlockchainService.this.wallet.removeObserver(this);
final boolean blockChainFileExists = blockChainFile.exists();
if (!blockChainFileExists) {
log.info("blockchain does not exist, resetting wallet");
wallet.reset();
}
try {
blockStore = new SPVBlockStore(Constants.NETWORK_PARAMETERS, blockChainFile, Constants.Files.BLOCKCHAIN_STORE_CAPACITY, true);
// detect corruptions as early as possible
blockStore.getChainHead();
final long earliestKeyCreationTimeSecs = wallet.getEarliestKeyCreationTime();
if (!blockChainFileExists && earliestKeyCreationTimeSecs > 0) {
try {
log.info("loading checkpoints for birthdate {} from '{}'", Utils.dateTimeFormat(earliestKeyCreationTimeSecs * 1000), Constants.Files.CHECKPOINTS_ASSET);
final Stopwatch watch = Stopwatch.createStarted();
final InputStream checkpointsInputStream = getAssets().open(Constants.Files.CHECKPOINTS_ASSET);
CheckpointManager.checkpoint(Constants.NETWORK_PARAMETERS, checkpointsInputStream, blockStore, earliestKeyCreationTimeSecs);
watch.stop();
log.info("checkpoints loaded, took {}", watch);
} catch (final IOException x) {
log.error("problem reading checkpoints, continuing without", x);
}
}
} catch (final BlockStoreException x) {
blockChainFile.delete();
final String msg = "blockstore cannot be created";
log.error(msg, x);
throw new Error(msg, x);
}
try {
blockChain = new BlockChain(Constants.NETWORK_PARAMETERS, wallet, blockStore);
} catch (final BlockStoreException x) {
throw new Error("blockchain cannot be created", x);
}
observeLiveDatasThatAreDependentOnWalletAndBlockchain();
}
});
}
Aggregations