Search in sources :

Example 1 with SelectedExchangeRateLiveData

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();
        }
    });
}
Also used : IntentFilter(android.content.IntentFilter) BlockStoreException(org.bitcoinj.store.BlockStoreException) BlockChain(org.bitcoinj.core.BlockChain) Wallet(org.bitcoinj.wallet.Wallet) InputStream(java.io.InputStream) SPVBlockStore(org.bitcoinj.store.SPVBlockStore) Stopwatch(com.google.common.base.Stopwatch) Handler(android.os.Handler) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) IOException(java.io.IOException) WalletLiveData(de.schildbach.wallet.data.WalletLiveData) ExchangeRateEntry(de.schildbach.wallet.exchangerate.ExchangeRateEntry) Coin(org.bitcoinj.core.Coin) HandlerThread(android.os.HandlerThread) SelectedExchangeRateLiveData(de.schildbach.wallet.data.SelectedExchangeRateLiveData) File(java.io.File) WalletBalanceLiveData(de.schildbach.wallet.data.WalletBalanceLiveData)

Aggregations

PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 IntentFilter (android.content.IntentFilter)1 Handler (android.os.Handler)1 HandlerThread (android.os.HandlerThread)1 Stopwatch (com.google.common.base.Stopwatch)1 SelectedExchangeRateLiveData (de.schildbach.wallet.data.SelectedExchangeRateLiveData)1 WalletBalanceLiveData (de.schildbach.wallet.data.WalletBalanceLiveData)1 WalletLiveData (de.schildbach.wallet.data.WalletLiveData)1 ExchangeRateEntry (de.schildbach.wallet.exchangerate.ExchangeRateEntry)1 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 BlockChain (org.bitcoinj.core.BlockChain)1 Coin (org.bitcoinj.core.Coin)1 BlockStoreException (org.bitcoinj.store.BlockStoreException)1 SPVBlockStore (org.bitcoinj.store.SPVBlockStore)1 Wallet (org.bitcoinj.wallet.Wallet)1