Esempio n. 1
0
  public void rescanBlockchain(long rescanTime) {
    mLogger.info(String.format("RESCANNING from %d", rescanTime));

    // Make sure we are in a good state for this.
    if (mState != State.READY) {
      mLogger.warn("can't rescan until the wallet is ready");
      return;
    }

    switch (mSyncState) {
      case SYNCHRONIZED:
        mSyncState = SyncState.RESCAN;
        break;
      default:
        mSyncState = SyncState.RERESCAN;
        break;
    }

    // Remove our wallet event listener.
    mKit.wallet().removeEventListener(mWalletListener);

    // Persist and remove our HDWallet.
    //
    // NOTE - It's best not to clear the balances here.  When the
    // transactions are filling in on the transactions screen it's
    // disturbing to see negative historical balances.  They'll
    // get completely refigured when the sync is done anyway ...
    //
    mHDWallet.persist(mApp);
    mHDWallet = null;

    mLogger.info("resetting wallet state");
    mKit.wallet().clearTransactions(0);
    mKit.wallet().setLastBlockSeenHeight(-1); // magic value
    mKit.wallet().setLastBlockSeenHash(null);

    mLogger.info("shutting kit down");
    try {
      mKit.shutDown();
      mKit = null;
    } catch (Exception ex) {
      mLogger.error("kit shutdown failed: " + ex.toString());
      return;
    }

    File dir = mApp.getWalletDir();
    String spvpath = mApp.getWalletPrefix() + ".spvchain";
    mLogger.info("removing spvchain file " + dir + spvpath);
    File chainFile = new File(dir, spvpath);
    if (!chainFile.delete()) mLogger.error("delete of spvchain file failed");

    mLogger.info("restarting wallet");

    mKeyCrypter = mApp.mKeyCrypter;
    mAesKey = mApp.mAesKey;

    setState(State.SETUP);
    mTask = new SetupWalletTask();
    mTask.execute(rescanTime);
  }
Esempio n. 2
0
  @Override
  public void onCreate() {
    mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mLBM = LocalBroadcastManager.getInstance(this);

    mLogger.info("WalletService created");

    mApp = (WalletApplication) getApplicationContext();

    mContext = getApplicationContext();
    mRes = mContext.getResources();

    mTimeoutWorker = Executors.newSingleThreadScheduledExecutor();

    final String lockName = getPackageName() + " blockchain sync";
    final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, lockName);

    mPrefs = PreferenceManager.getDefaultSharedPreferences(this);

    String fiatRateSource = mPrefs.getString(SettingsActivity.KEY_FIAT_RATE_SOURCE, "");
    setFiatRateSource(fiatRateSource);

    // Register for future preference changes.
    mPrefs.registerOnSharedPreferenceChangeListener(this);

    // Register with the WalletApplication.
    mApp.setWalletService(this);
  }
Esempio n. 3
0
        @Override
        public void onCoinsSent(
            Wallet wallet, Transaction tx, BigInteger prevBalance, BigInteger newBalance) {
          BigInteger amt = prevBalance.subtract(newBalance);
          final long amount = amt.longValue();

          WalletApplication app = (WalletApplication) getApplicationContext();
          final BTCFmt btcfmt = app.getBTCFmt();

          // We allocate a new notification id for each receive.
          // We use it on both the receive and confirm so it
          // will replace the receive note with the confirm ...
          final int noteId = ++mNoteId;

          mLogger.info(String.format("showing notification send %d", amount));

          showEventNotification(
              noteId,
              R.drawable.ic_note_bc_red_lt,
              mRes.getString(R.string.wallet_service_note_sent_title, btcfmt.unitStr()),
              mRes.getString(
                  R.string.wallet_service_note_sent_msg, btcfmt.format(amount), btcfmt.unitStr()));

          final TransactionConfidence txconf = tx.getConfidence();

          final TransactionConfidence.Listener listener =
              new TransactionConfidence.Listener() {
                @Override
                public void onConfidenceChanged(Transaction tx, ChangeReason reason) {
                  // Wait until it's not pending anymore.
                  if (tx.isPending()) return;

                  ConfidenceType ct = tx.getConfidence().getConfidenceType();

                  if (ct == ConfidenceType.BUILDING) {
                    mLogger.info(String.format("send %d confirm", amount));

                    // Show no longer pending.
                    showEventNotification(
                        noteId,
                        R.drawable.ic_note_bc_red,
                        mRes.getString(R.string.wallet_service_note_scnf_title, btcfmt.unitStr()),
                        mRes.getString(
                            R.string.wallet_service_note_scnf_msg,
                            btcfmt.format(amount),
                            btcfmt.unitStr()));
                  } else if (ct == ConfidenceType.DEAD) {
                    mLogger.info(String.format("send %d dead", amount));
                    // Notify dead.
                    showEventNotification(
                        noteId,
                        R.drawable.ic_note_bc_gray,
                        mRes.getString(R.string.wallet_service_note_sdead_title, btcfmt.unitStr()),
                        mRes.getString(
                            R.string.wallet_service_note_sdead_msg,
                            btcfmt.format(amount),
                            btcfmt.unitStr()));

                  } else {
                    mLogger.info(String.format("send %d unknown", amount));
                  }

                  // We're all done listening ...
                  txconf.removeEventListener(this);
                }
              };

          txconf.addEventListener(listener);
        }