Android Open Source - My-Wallet-Android Wallet Balance Fragment






From Project

Back to project page My-Wallet-Android.

License

The source code is released under:

GNU General Public License

If you think the Android project My-Wallet-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2011-2012 the original author or authors.
 */*w w  w  .  j  av a 2s  .c o  m*/
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package piuk.blockchain.android.ui;

import java.math.BigDecimal;
import java.math.BigInteger;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.database.Cursor;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.bitcoin.core.AbstractWalletEventListener;
import com.google.bitcoin.core.Wallet;
import com.google.bitcoin.core.Wallet.BalanceType;
import com.google.bitcoin.core.WalletEventListener;

import piuk.blockchain.R;
import piuk.blockchain.android.Constants;
import piuk.blockchain.android.ExchangeRatesProvider;
import piuk.blockchain.android.WalletApplication;
import piuk.blockchain.android.util.WalletUtils;

/**
 * @author Andreas Schildbach
 */
public final class WalletBalanceFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>
{
  private WalletApplication application;
  private SharedPreferences prefs;
  private final Handler handler = new Handler();

  private CurrencyAmountView viewBalance;
  private TextView viewBalanceLocal;

  private final WalletEventListener walletEventListener = new AbstractWalletEventListener()
  {
    @Override
    public void onChange(Wallet wallet)
    {
      try {
        getActivity().runOnUiThread(new Runnable()
        {
          public void run()
          {
            try {
              updateView();
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        });
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  };

  @Override
  public void onCreate(final Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);

    final Activity activity = getActivity();
    application = (WalletApplication) activity.getApplication();
    prefs = PreferenceManager.getDefaultSharedPreferences(activity);

    application.getWallet().addEventListener(walletEventListener);
  }

  @Override
  public void onActivityCreated(final Bundle savedInstanceState)
  {
    super.onActivityCreated(savedInstanceState);

    getLoaderManager().initLoader(0, null, this);
  }

  @Override
  public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState)
  {
    final View view = inflater.inflate(R.layout.wallet_balance_fragment, container, false);
    viewBalance = (CurrencyAmountView) view.findViewById(R.id.wallet_balance);
    viewBalanceLocal = (TextView) view.findViewById(R.id.wallet_balance_local);

    if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) < Configuration.SCREENLAYOUT_SIZE_LARGE)
    {
      view.setOnClickListener(new OnClickListener() 
      {
        public void onClick(final View v)
        {
          final FragmentManager fm = getFragmentManager();
          final FragmentTransaction ft = fm.beginTransaction();
          ft.hide(fm.findFragmentById(R.id.wallet_transactions_fragment));
          ft.show(fm.findFragmentById(R.id.exchange_rates_fragment));
          ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
          ft.addToBackStack(null);
          ft.commit();
        }
      });
    }

    return view;
  }

  @Override
  public void onResume()
  {
    super.onResume();

    updateView();
  }

  @Override
  public void onDestroy()
  {
    application.getWallet().removeEventListener(walletEventListener);

    super.onDestroy();
  }

  public void updateView()
  {
    viewBalance.setAmount(application.getWallet().getBalance(BalanceType.ESTIMATED));

    getLoaderManager().restartLoader(0, null, this);
  }

  private Runnable resetColorRunnable = new Runnable()
  {
    public void run()
    {
      viewBalanceLocal.setTextColor(Color.parseColor("#888888"));
    }
  };

  public void flashLocal()
  {
    viewBalanceLocal.setTextColor(Color.parseColor("#cc5500"));
    handler.removeCallbacks(resetColorRunnable);
    handler.postDelayed(resetColorRunnable, 500);
  }

  public Loader<Cursor> onCreateLoader(final int id, final Bundle args)
  {
    final String exchangeCurrency = prefs.getString(Constants.PREFS_KEY_EXCHANGE_CURRENCY, Constants.DEFAULT_EXCHANGE_CURRENCY);
    return new CursorLoader(getActivity(), ExchangeRatesProvider.CONTENT_URI, null, ExchangeRatesProvider.KEY_CURRENCY_CODE,
        new String[] { exchangeCurrency }, null);
  }

  public void onLoadFinished(final Loader<Cursor> loader, final Cursor data)
  {
    if (data != null)
    {
      data.moveToFirst();
      final Double exchangeRate = data.getDouble(data.getColumnIndexOrThrow(ExchangeRatesProvider.KEY_EXCHANGE_RATE));

      viewBalanceLocal.setVisibility(View.GONE);
      if (application.getWallet().getBalance(BalanceType.ESTIMATED).signum() > 0 && exchangeRate != null)
      {
        final String exchangeCurrency = prefs.getString(Constants.PREFS_KEY_EXCHANGE_CURRENCY, Constants.DEFAULT_EXCHANGE_CURRENCY);
        final BigInteger balance = application.getWallet().getBalance(BalanceType.ESTIMATED);
        final BigInteger valueLocal = new BigDecimal(balance).multiply(new BigDecimal(exchangeRate)).toBigInteger();
        viewBalanceLocal.setVisibility(View.VISIBLE);
        viewBalanceLocal.setText(getString(R.string.wallet_balance_fragment_local_value, exchangeCurrency,
            WalletUtils.formatValue(valueLocal)));
        if (Constants.TEST)
          viewBalanceLocal.setPaintFlags(viewBalanceLocal.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
      }
    }
  }

  public void onLoaderReset(final Loader<Cursor> loader)
  {
  }
}




Java Source Code List

piuk.BitcoinAddress.java
piuk.BitcoinScript.java
piuk.Hash.java
piuk.MyBlockChain.java
piuk.MyRemoteWallet.java
piuk.MyTransactionConfidence.java
piuk.MyTransactionInput.java
piuk.MyTransactionOutPoint.java
piuk.MyTransactionOutput.java
piuk.MyTransaction.java
piuk.MyWallet.java
piuk.blockchain.android.AddressBookProvider.java
piuk.blockchain.android.BlockchainService.java
piuk.blockchain.android.Constants.java
piuk.blockchain.android.DetermineFirstSeenThread.java
piuk.blockchain.android.ExchangeRatesProvider.java
piuk.blockchain.android.WalletApplication.java
piuk.blockchain.android.WalletBalanceWidgetProvider.java
piuk.blockchain.android.ui.AbstractWalletActivity.java
piuk.blockchain.android.ui.AmountCalculatorFragment.java
piuk.blockchain.android.ui.CurrencyAmountView.java
piuk.blockchain.android.ui.CurrencyCodeDrawable.java
piuk.blockchain.android.ui.EditAddressBookEntryFragment.java
piuk.blockchain.android.ui.ExchangeRatesFragment.java
piuk.blockchain.android.ui.NewAccountFragment.java
piuk.blockchain.android.ui.PairWalletActivity.java
piuk.blockchain.android.ui.PreferencesActivity.java
piuk.blockchain.android.ui.RequestCoinsActivity.java
piuk.blockchain.android.ui.RequestCoinsFragment.java
piuk.blockchain.android.ui.SecondPasswordFragment.java
piuk.blockchain.android.ui.SendCoinsActivity.java
piuk.blockchain.android.ui.SendCoinsFragment.java
piuk.blockchain.android.ui.SendingAddressesFragment.java
piuk.blockchain.android.ui.TransactionActivity.java
piuk.blockchain.android.ui.TransactionFragment.java
piuk.blockchain.android.ui.WalletActivity.java
piuk.blockchain.android.ui.WalletAddressesFragment.java
piuk.blockchain.android.ui.WalletBalanceFragment.java
piuk.blockchain.android.ui.WalletTransactionsFragment.java
piuk.blockchain.android.ui.WelcomeFragment.java
piuk.blockchain.android.util.ActionBarFragment.java
piuk.blockchain.android.util.Base43.java
piuk.blockchain.android.util.CircularProgressView.java
piuk.blockchain.android.util.ErrorReporter.java
piuk.blockchain.android.util.IOUtils.java
piuk.blockchain.android.util.Iso8601Format.java
piuk.blockchain.android.util.NfcTools.java
piuk.blockchain.android.util.QrDialog.java
piuk.blockchain.android.util.ViewPagerTabs.java
piuk.blockchain.android.util.WalletUtils.java