Android Open Source - My-Wallet-Android Amount Calculator 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  ava  2 s . 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 java.math.RoundingMode;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
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 piuk.blockchain.R;
import piuk.blockchain.android.Constants;
import piuk.blockchain.android.ExchangeRatesProvider;

/**
 * @author Andreas Schildbach
 */
public final class AmountCalculatorFragment extends DialogFragment implements LoaderManager.LoaderCallbacks<Cursor>
{
  public static interface Listener
  {
    void use(final BigInteger amount);
  }
  
  public static final String FRAGMENT_TAG = AmountCalculatorFragment.class.getName();

  private LayoutInflater inflater;
  private String exchangeCurrency;
  private Double exchangeRate;
  private boolean exchangeDirection = true;
  private CurrencyAmountView btcAmountView, localAmountView;
  private Listener listener;

  public AmountCalculatorFragment(final Listener listener)
  {
    this.listener = listener;
  }

  @Override
  public Dialog onCreateDialog(final Bundle savedInstanceState)
  {
    final FragmentActivity activity = getActivity();
    inflater = LayoutInflater.from(activity);
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity);
    exchangeCurrency = prefs.getString(Constants.PREFS_KEY_EXCHANGE_CURRENCY, Constants.DEFAULT_EXCHANGE_CURRENCY);

    final Builder dialog = new AlertDialog.Builder(activity).setTitle(R.string.amount_calculator_dialog_title);

    final View view = inflater.inflate(R.layout.amount_calculator_dialog, null);

    btcAmountView = (CurrencyAmountView) view.findViewById(R.id.amount_calculator_row_btc);
    btcAmountView.setListener(new CurrencyAmountView.Listener()
    {
      public void changed()
      {
        if (btcAmountView.getAmount() != null)
        {
          exchangeDirection = true;

          updateAppearance();
        }
        else
        {
          localAmountView.setHint(null);
        }
      }

      public void done()
      {
        AmountCalculatorFragment.this.done();
      }
    });

    localAmountView = (CurrencyAmountView) view.findViewById(R.id.amount_calculator_row_local);
    localAmountView.setCurrencyCode(exchangeCurrency);
    localAmountView.setListener(new CurrencyAmountView.Listener()
    {
      public void changed()
      {
        if (localAmountView.getAmount() != null)
        {
          exchangeDirection = false;

          updateAppearance();
        }
        else
        {
          btcAmountView.setHint(null);
        }
      }

      public void done()
      {
        AmountCalculatorFragment.this.done();
      }
    });

    dialog.setView(view);

    dialog.setPositiveButton(R.string.amount_calculator_dialog_button_use, new DialogInterface.OnClickListener()
    {
      public void onClick(final DialogInterface dialog, final int whichButton)
      {
        done();
      }
    });
    
    dialog.setNegativeButton(R.string.button_cancel, new DialogInterface.OnClickListener()
    {
      public void onClick(final DialogInterface dialog, final int whichButton)
      {
        try {
          dismiss();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    });

    updateAppearance();

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

    return dialog.create();
  }

  private void updateAppearance()
  {
    if (exchangeRate != null)
    {
      localAmountView.setEnabled(true);

      final BigDecimal bdExchangeRate = new BigDecimal(exchangeRate);

      if (exchangeDirection)
      {
        final BigInteger btcAmount = btcAmountView.getAmount();
        if (btcAmount != null)
        {
          localAmountView.setAmount(null);
          localAmountView.setHint(new BigDecimal(btcAmount).multiply(bdExchangeRate).toBigInteger());
          btcAmountView.setHint(null);
        }
      }
      else
      {
        final BigInteger localAmount = localAmountView.getAmount();
        if (localAmount != null)
        {
          btcAmountView.setAmount(null);
          btcAmountView.setHint(new BigDecimal(localAmount).divide(bdExchangeRate, RoundingMode.HALF_UP).toBigInteger());
          localAmountView.setHint(null);
        }
      }
    }
    else
    {
      localAmountView.setEnabled(false);
    }
  }

  private void done()
  {
    final BigInteger amount = exchangeDirection ? btcAmountView.getAmount() : new BigDecimal(localAmountView.getAmount()).divide(
        new BigDecimal(exchangeRate), RoundingMode.HALF_UP).toBigInteger();

    listener.use(amount);

    dismiss();
  }

  public Loader<Cursor> onCreateLoader(final int id, final Bundle args)
  {
    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();
      exchangeRate = data.getDouble(data.getColumnIndexOrThrow(ExchangeRatesProvider.KEY_EXCHANGE_RATE));

      updateAppearance();
    }
  }

  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