Ejemplo n.º 1
0
    @Override
    protected Void doInBackground(Void... params) {

      // Simulates a background task
      try {
        Thread.sleep(500);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

      // load more data
      try {
        getDataForContacts(mCurNumItems, mCurNumItems + NUMBER_DATA_GET_MORE);
      } catch (IndexOutOfBoundsException ex) {
        mIsOverData = true;
        final FragmentActivity activity = getActivity();
        activity.runOnUiThread(
            new Runnable() {
              public void run() {
                Toast.makeText(activity, "No more data to load", Toast.LENGTH_SHORT).show();
              }
            });
      }

      return null;
    }
        @Override
        public void update(Observable observable, Object data) {
          Log.i(TAG, "update");
          final FragmentActivity activity = getActivity();
          if (activity == null || WalletFragment.this.isAdded() == false) return;
          activity.runOnUiThread(
              new Runnable() {
                @Override
                public void run() {
                  Log.i(
                      TAG,
                      android.os.Process.myTid()
                          + " TID UI : Refreshing wallet fragment "
                          + walletObservable);

                  final Bitmap IdenticonBitmap = walletObservable.getCurrentIdenticon();
                  if (IdenticonBitmap != null) identicon.setImageBitmap(IdenticonBitmap);
                  final String aliasName1 = walletObservable.getAliasName();
                  if (aliasName1 != null) {
                    aliasNameText.setText(aliasName1);
                    aliasNameText.setVisibility(View.VISIBLE);
                  }

                  if (walletObservable.isSyncedOrPending()) {
                    final Coin walletBalance = walletObservable.getWalletBalance();
                    final Coin walletUnconfirmedBalance =
                        walletObservable.getWalletUnconfirmedBalance();
                    long value1 = walletBalance.getValue();
                    long value2 = walletUnconfirmedBalance.getValue();
                    long value = Math.max(value1, value2); // Optimistic view
                    final String units = String.valueOf(value);
                    final Bitmap QrCodeBitmap = walletObservable.getCurrentQrCode();

                    final String aliasName = walletObservable.getAliasName();
                    final String unconfirmedAliasName = walletObservable.getUnconfirmedAliasName();

                    if (value > 0) {
                      walletWarning.setVisibility(View.GONE);
                    } else {
                      walletWarning.setVisibility(View.VISIBLE);
                    }

                    syncedLayout.setVisibility(View.VISIBLE);
                    syncingLayout.setVisibility(View.GONE);
                    Address current = walletObservable.getCurrent();
                    currentAddressText.setText(current != null ? current.toString() : "");
                    btcBalance.setUnits(units);
                    btcBalance.refreshUI();
                    if (QrCodeBitmap != null) currentQrCode.setImageBitmap(QrCodeBitmap);

                    if (aliasName == null
                        && unconfirmedAliasName == null) { // Alias still to be defined
                      setAliasButton.setVisibility(View.VISIBLE);
                      aliasNameText.setVisibility(View.GONE);
                      aliasNameUnconfirmed.setVisibility(View.GONE);
                    } else if (aliasName == null) { // unconfirmed alias defined!
                      setAliasButton.setVisibility(View.GONE);
                      aliasNameText.setVisibility(View.VISIBLE);
                      aliasNameUnconfirmed.setVisibility(View.VISIBLE);
                      aliasNameText.setText(unconfirmedAliasName);
                    } else { // alias defined and confirmed
                      setAliasButton.setVisibility(View.GONE);
                      aliasNameText.setVisibility(View.VISIBLE);
                      aliasNameUnconfirmed.setVisibility(View.GONE);
                      aliasNameText.setText(aliasName);
                    }

                    if (value1 != value2) {
                      btcBalanceUnconfirmed.setVisibility(View.VISIBLE);
                    } else {
                      btcBalanceUnconfirmed.setVisibility(View.GONE);
                    }
                    payButton.setVisibility(View.VISIBLE);

                    if (walletObservable.getMessagePending() > 0) {
                      messagePending.setVisibility(View.VISIBLE);
                      messagePending.setText(
                          walletObservable.getMessagePending()
                              + " message"
                              + (walletObservable.getMessagePending() > 1 ? "s" : "")
                              + " pending");
                    } else {
                      messagePending.setVisibility(View.GONE);
                    }

                    // refresh actionbar
                    activity.invalidateOptionsMenu();

                    // Load my messages
                    inQueue = null;
                    loadMoreData();

                  } else if (walletObservable.getState() == WalletObservable.State.SYNCING) {
                    if (activity != null && activity.isFinishing()) return;
                    syncedLayout.setVisibility(View.GONE);
                    syncingLayout.setVisibility(View.VISIBLE);
                    setAliasButton.setVisibility(View.INVISIBLE);
                    payButton.setVisibility(View.GONE);
                    if (walletObservable.getPercSync() != null)
                      syncingText.setText(
                          String.format("Syncing (%d%%)", walletObservable.getPercSync()));
                  } else {
                    payButton.setVisibility(View.GONE);
                    FragmentTransaction transaction = getFragmentManager().beginTransaction();
                    final SharedPreferences sharedPref =
                        PreferenceManager.getDefaultSharedPreferences(getActivity());
                    String passphrase = sharedPref.getString(Preferences.PASSPHRASE, null);
                    if (passphrase == null) {
                      transaction.replace(R.id.root_frame, new HelloFragment());
                      transaction.commitAllowingStateLoss();
                      getFragmentManager().executePendingTransactions();
                    } else {; // nothing = transaction.replace(R.id.root_frame, new
                              // WalletFragment());
                    }
                  }
                }
              });
        }