Search in sources :

Example 1 with WalletLiveData

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();
    });
}
Also used : IntentFilter(android.content.IntentFilter) IOException(java.io.IOException) WalletLiveData(de.schildbach.wallet.data.WalletLiveData) PowerManager(android.os.PowerManager) Transaction(org.bitcoinj.core.Transaction) Toast(de.schildbach.wallet.util.Toast) NotificationCompat(androidx.core.app.NotificationCompat) BluetoothAdapter(android.bluetooth.BluetoothAdapter)

Example 2 with WalletLiveData

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();
        }
    });
}
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

IntentFilter (android.content.IntentFilter)2 WalletLiveData (de.schildbach.wallet.data.WalletLiveData)2 IOException (java.io.IOException)2 PendingIntent (android.app.PendingIntent)1 BluetoothAdapter (android.bluetooth.BluetoothAdapter)1 Intent (android.content.Intent)1 Handler (android.os.Handler)1 HandlerThread (android.os.HandlerThread)1 PowerManager (android.os.PowerManager)1 NotificationCompat (androidx.core.app.NotificationCompat)1 Stopwatch (com.google.common.base.Stopwatch)1 SelectedExchangeRateLiveData (de.schildbach.wallet.data.SelectedExchangeRateLiveData)1 WalletBalanceLiveData (de.schildbach.wallet.data.WalletBalanceLiveData)1 ExchangeRateEntry (de.schildbach.wallet.exchangerate.ExchangeRateEntry)1 Toast (de.schildbach.wallet.util.Toast)1 File (java.io.File)1 InputStream (java.io.InputStream)1 BlockChain (org.bitcoinj.core.BlockChain)1 Coin (org.bitcoinj.core.Coin)1 Transaction (org.bitcoinj.core.Transaction)1