use of de.schildbach.wallet.data.WalletLiveData in project bitcoin-wallet by bitcoin-wallet.
the class AcceptBluetoothService method onCreate.
@Override
public void onCreate() {
serviceCreatedAt = System.currentTimeMillis();
log.debug(".onCreate()");
super.onCreate();
this.application = (WalletApplication) getApplication();
final BluetoothAdapter bluetoothAdapter = checkNotNull(BluetoothAdapter.getDefaultAdapter());
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName());
wakeLock.acquire();
final NotificationCompat.Builder notification = new NotificationCompat.Builder(this, Constants.NOTIFICATION_CHANNEL_ID_ONGOING);
notification.setColor(getColor(R.color.fg_network_significant));
notification.setSmallIcon(R.drawable.stat_notify_bluetooth_24dp);
notification.setContentTitle(getString(R.string.notification_bluetooth_service_listening));
notification.setWhen(System.currentTimeMillis());
notification.setOngoing(true);
notification.setPriority(NotificationCompat.PRIORITY_LOW);
startForeground(Constants.NOTIFICATION_ID_BLUETOOTH, notification.build());
registerReceiver(bluetoothStateChangeReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
try {
classicThread = new AcceptBluetoothThread.ClassicBluetoothThread(bluetoothAdapter) {
@Override
public boolean handleTx(final Transaction tx) {
return AcceptBluetoothService.this.handleTx(tx);
}
};
paymentProtocolThread = new AcceptBluetoothThread.PaymentProtocolThread(bluetoothAdapter) {
@Override
public boolean handleTx(final Transaction tx) {
return AcceptBluetoothService.this.handleTx(tx);
}
};
} catch (final IOException x) {
new Toast(this).longToast(R.string.error_bluetooth, x.getMessage());
log.warn("problem with listening, stopping service", x);
CrashReporter.saveBackgroundTrace(x, application.packageInfo());
stopSelf();
}
wallet = new WalletLiveData(application);
wallet.observe(this, wallet -> {
classicThread.start();
paymentProtocolThread.start();
});
}
use of de.schildbach.wallet.data.WalletLiveData 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