Android Open Source - My-Wallet-Android Transaction 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  a va2 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.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.text.DateFormat;
import java.util.Date;
import java.util.zip.GZIPOutputStream;

import android.app.Activity;
import android.content.ContentResolver;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.bitcoin.core.Address;
import com.google.bitcoin.core.ScriptException;
import com.google.bitcoin.core.Transaction;
import com.google.bitcoin.core.TransactionConfidence.ConfidenceType;
import com.google.bitcoin.core.Wallet;

import piuk.blockchain.R;
import piuk.blockchain.android.AddressBookProvider;
import piuk.blockchain.android.Constants;
import piuk.blockchain.android.WalletApplication;
import piuk.blockchain.android.util.Base43;
import piuk.blockchain.android.util.QrDialog;
import piuk.blockchain.android.util.WalletUtils;

/**
 * @author Andreas Schildbach
 */
public final class TransactionFragment extends Fragment
{
  public static final String FRAGMENT_TAG = TransactionFragment.class.getName();

  private FragmentActivity activity;

  private DateFormat dateFormat;
  private DateFormat timeFormat;

  @Override
  public void onAttach(final Activity activity)
  {
    super.onAttach(activity);

    this.activity = (FragmentActivity) activity;

    dateFormat = android.text.format.DateFormat.getDateFormat(activity);
    timeFormat = android.text.format.DateFormat.getTimeFormat(activity);
  }

  @Override
  public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState)
  {
    return inflater.inflate(R.layout.transaction_fragment, null);
  }

  public void update(final Transaction tx)
  {
    final Wallet wallet = ((WalletApplication) activity.getApplication()).getWallet();

    final byte[] serializedTx = tx.unsafeBitcoinSerialize();

    Address from = null;
    boolean fromMine = false;
    try
    {
      from = tx.getInputs().get(0).getFromAddress();
      fromMine = wallet.isPubKeyHashMine(from.getHash160());
    }
    catch (final ScriptException x)
    {
      x.printStackTrace();
    }

    Address to = null;
    boolean toMine = false;
    try
    {
      to = tx.getOutputs().get(0).getScriptPubKey().getToAddress();
      toMine = wallet.isPubKeyHashMine(to.getHash160());
    }
    catch (final ScriptException x)
    {
      x.printStackTrace();
    }

    final ContentResolver contentResolver = activity.getContentResolver();

    final View view = getView();

    final Date time = tx.getUpdateTime();
    view.findViewById(R.id.transaction_fragment_time_row).setVisibility(time != null ? View.VISIBLE : View.GONE);
    if (time != null)
    {
      final TextView viewDate = (TextView) view.findViewById(R.id.transaction_fragment_time);
      viewDate.setText((DateUtils.isToday(time.getTime()) ? getString(R.string.transaction_fragment_time_today) : dateFormat.format(time))
          + ", " + timeFormat.format(time));
    }

    try
    {
      final BigInteger amountSent = tx.getValueSentFromMe(wallet);
      view.findViewById(R.id.transaction_fragment_amount_sent_row).setVisibility(amountSent.signum() != 0 ? View.VISIBLE : View.GONE);
      if (amountSent.signum() != 0)
      {
        final TextView viewAmountSent = (TextView) view.findViewById(R.id.transaction_fragment_amount_sent);
        viewAmountSent.setText(Constants.CURRENCY_MINUS_SIGN + WalletUtils.formatValue(amountSent));
      }
    }
    catch (final ScriptException x)
    {
      x.printStackTrace();
    }

    final BigInteger amountReceived = tx.getValueSentToMe(wallet);
    view.findViewById(R.id.transaction_fragment_amount_received_row).setVisibility(amountReceived.signum() != 0 ? View.VISIBLE : View.GONE);
    if (amountReceived.signum() != 0)
    {
      final TextView viewAmountReceived = (TextView) view.findViewById(R.id.transaction_fragment_amount_received);
      viewAmountReceived.setText(Constants.CURRENCY_PLUS_SIGN + WalletUtils.formatValue(amountReceived));
    }

    final View viewFromButton = view.findViewById(R.id.transaction_fragment_from_button);
    final TextView viewFromLabel = (TextView) view.findViewById(R.id.transaction_fragment_from_label);
    if (from != null)
    {
      final String label = AddressBookProvider.resolveLabel(contentResolver, from.toString());
      final StringBuilder builder = new StringBuilder();

      if (fromMine)
        builder.append(getString(R.string.transaction_fragment_you)).append(", ");

      if (label != null)
      {
        builder.append(label);
      }
      else
      {
        builder.append(from.toString());
        viewFromLabel.setTypeface(Typeface.MONOSPACE);
      }

      viewFromLabel.setText(builder.toString());

      final String addressStr = from.toString();
      viewFromButton.setOnClickListener(new OnClickListener()
      {
        public void onClick(final View v)
        {
          EditAddressBookEntryFragment.edit(getFragmentManager(), addressStr);
        }
      });
    }
    else
    {
      viewFromLabel.setText(null);
    }

    final View viewToButton = view.findViewById(R.id.transaction_fragment_to_button);
    final TextView viewToLabel = (TextView) view.findViewById(R.id.transaction_fragment_to_label);
    if (to != null)
    {
      final String label = AddressBookProvider.resolveLabel(contentResolver, to.toString());
      final StringBuilder builder = new StringBuilder();

      if (toMine)
        builder.append(getString(R.string.transaction_fragment_you)).append(", ");

      if (label != null)
      {
        builder.append(label);
      }
      else
      {
        builder.append(to.toString());
        viewToLabel.setTypeface(Typeface.MONOSPACE);
      }

      viewToLabel.setText(builder.toString());

      final String addressStr = to.toString();
      viewToButton.setOnClickListener(new OnClickListener()
      {
        public void onClick(final View v)
        {
          EditAddressBookEntryFragment.edit(getFragmentManager(), addressStr);
        }
      });
    }
    else
    {
      viewToLabel.setText(null);
    }

    final TextView viewStatus = (TextView) view.findViewById(R.id.transaction_fragment_status);
    final ConfidenceType confidenceType = tx.getConfidence().getConfidenceType();
    if (confidenceType == ConfidenceType.OVERRIDDEN_BY_DOUBLE_SPEND || confidenceType == ConfidenceType.NOT_IN_BEST_CHAIN)
      viewStatus.setText(R.string.transaction_fragment_status_dead);
    else if (confidenceType == ConfidenceType.NOT_SEEN_IN_CHAIN)
      viewStatus.setText(R.string.transaction_fragment_status_pending);
    else if (confidenceType == ConfidenceType.BUILDING)
      viewStatus.setText(R.string.transaction_fragment_status_confirmed);
    else
      viewStatus.setText(R.string.transaction_fragment_status_unknown);

    final TextView viewHash = (TextView) view.findViewById(R.id.transaction_fragment_hash);
    viewHash.setText(tx.getHash().toString());

    final TextView viewLength = (TextView) view.findViewById(R.id.transaction_fragment_length);
    viewLength.setText(Integer.toString(serializedTx.length));

    final ImageView viewQr = (ImageView) view.findViewById(R.id.transaction_fragment_qr);

    try
    {
      // encode transaction URI
      final ByteArrayOutputStream bos = new ByteArrayOutputStream(serializedTx.length);
      final GZIPOutputStream gos = new GZIPOutputStream(bos);
      gos.write(serializedTx);
      gos.close();

      final byte[] gzippedSerializedTx = bos.toByteArray();
      final boolean useCompressioon = gzippedSerializedTx.length < serializedTx.length;

      final StringBuilder txStr = new StringBuilder("btctx:");
      txStr.append(useCompressioon ? 'Z' : '-');
      txStr.append(Base43.encode(useCompressioon ? gzippedSerializedTx : serializedTx));

      final Bitmap qrCodeBitmap = WalletUtils.getQRCodeBitmap(txStr.toString().toUpperCase(), 512);
      viewQr.setImageBitmap(qrCodeBitmap);
      viewQr.setOnClickListener(new OnClickListener()
      {
        public void onClick(final View v)
        {
          new QrDialog(activity, qrCodeBitmap).show();
        }
      });
    }
    catch (final IOException x)
    {
      throw new RuntimeException(x);
    }
  }
}




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