Android Open Source - My-Wallet-Android Sending Addresses 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.
 *//from   w w w. j  a va2s  .  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.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.text.ClipboardManager;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.TextView;

import com.google.bitcoin.core.Address;
import com.google.bitcoin.uri.BitcoinURI;

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

/**
 * @author Andreas Schildbach
 */
public final class SendingAddressesFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor>
{
  private Activity activity;
  private SimpleCursorAdapter adapter;
  private String walletAddressesSelection;

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

    activity = getActivity();

    setEmptyText(getString(R.string.address_book_empty_text));

    adapter = new SimpleCursorAdapter(activity, R.layout.address_book_row, null, new String[] { AddressBookProvider.KEY_LABEL,
        AddressBookProvider.KEY_ADDRESS }, new int[] { R.id.address_book_row_label, R.id.address_book_row_address }, 0);
    adapter.setViewBinder(new ViewBinder()
    {
      public boolean setViewValue(final View view, final Cursor cursor, final int columnIndex)
      {
        if (!AddressBookProvider.KEY_ADDRESS.equals(cursor.getColumnName(columnIndex)))
          return false;

        ((TextView) view).setText(WalletUtils.formatAddress(cursor.getString(columnIndex), Constants.ADDRESS_FORMAT_GROUP_SIZE,
            Constants.ADDRESS_FORMAT_LINE_SIZE));

        return true;
      }
    });
    setListAdapter(adapter);

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

  @Override
  public void onViewCreated(final View view, final Bundle savedInstanceState)
  {
    super.onViewCreated(view, savedInstanceState);

    registerForContextMenu(getListView());
  }

  @Override
  public void onListItemClick(final ListView l, final View v, final int position, final long id)
  {
    final Cursor cursor = (Cursor) adapter.getItem(position);
    final String address = cursor.getString(cursor.getColumnIndexOrThrow(AddressBookProvider.KEY_ADDRESS));
    handleSend(address);
  }

  @Override
  public void onCreateContextMenu(final ContextMenu menu, final View v, final ContextMenuInfo menuInfo)
  {
    activity.getMenuInflater().inflate(R.menu.sending_addresses_context, menu);
  }

  @Override
  public boolean onContextItemSelected(final MenuItem item)
  {
    final AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();

    switch (item.getItemId())
    {
      case R.id.sending_addresses_context_send:
      {
        final Cursor cursor = (Cursor) adapter.getItem(menuInfo.position);
        final String address = cursor.getString(cursor.getColumnIndexOrThrow(AddressBookProvider.KEY_ADDRESS));
        handleSend(address);
        return true;
      }

      case R.id.sending_addresses_context_edit:
      {
        final Cursor cursor = (Cursor) adapter.getItem(menuInfo.position);
        final String address = cursor.getString(cursor.getColumnIndexOrThrow(AddressBookProvider.KEY_ADDRESS));
        EditAddressBookEntryFragment.edit(getFragmentManager(), address);
        return true;
      }

      case R.id.sending_addresses_context_remove:
      {
        final Cursor cursor = (Cursor) adapter.getItem(menuInfo.position);
        final String address = cursor.getString(cursor.getColumnIndexOrThrow(AddressBookProvider.KEY_ADDRESS));
        handleRemove(address);
        return true;
      }

      case R.id.sending_addresses_context_show_qr:
      {
        final Cursor cursor = (Cursor) adapter.getItem(menuInfo.position);
        Address address;
        try {
          address = new Address(Constants.NETWORK_PARAMETERS, cursor.getString(cursor.getColumnIndexOrThrow(AddressBookProvider.KEY_ADDRESS)));

          final String uri = BitcoinURI.convertToBitcoinURI(address, null, null, null);
          final int size = (int) (256 * getResources().getDisplayMetrics().density);
          new QrDialog(activity, WalletUtils.getQRCodeBitmap(uri, size)).show();
          return true;
        } catch (Exception e) {
          e.printStackTrace();
          return false;
        }
      }

      case R.id.sending_addresses_context_copy_to_clipboard:
      {
        final Cursor cursor = (Cursor) adapter.getItem(menuInfo.position);
        final String address = cursor.getString(cursor.getColumnIndexOrThrow(AddressBookProvider.KEY_ADDRESS));
        handleCopyToClipboard(address);
        return true;
      }

      default:
        return false;
    }
  }

  private void handleSend(final String address)
  {
    final Intent intent = new Intent(activity, SendCoinsActivity.class);
    intent.putExtra(SendCoinsActivity.INTENT_EXTRA_ADDRESS, address);
    startActivity(intent);
  }

  private void handleRemove(final String address)
  {
    final Uri uri = AddressBookProvider.CONTENT_URI.buildUpon().appendPath(address).build();
    activity.getContentResolver().delete(uri, null, null);
  }

  private void handleCopyToClipboard(final String address)
  {
    ClipboardManager clipboardManager = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
    clipboardManager.setText(address);
    ((AbstractWalletActivity) activity).toast(R.string.wallet_address_fragment_clipboard_msg);
  }

  public Loader<Cursor> onCreateLoader(final int id, final Bundle args)
  {
    final Uri uri = AddressBookProvider.CONTENT_URI;
    return new CursorLoader(activity, uri, null, AddressBookProvider.SELECTION_NOTIN,
        new String[] { walletAddressesSelection != null ? walletAddressesSelection : "" }, AddressBookProvider.KEY_LABEL
            + " COLLATE LOCALIZED ASC");
  }

  public void onLoadFinished(final Loader<Cursor> loader, final Cursor data)
  {
    adapter.swapCursor(data);
  }

  public void onLoaderReset(final Loader<Cursor> loader)
  {
    adapter.swapCursor(null);
  }

  public void setWalletAddresses(final ArrayList<Address> addresses)
  {
    final StringBuilder builder = new StringBuilder();
    for (final Address address : addresses)
      builder.append(address.toString()).append(",");
    if (addresses.size() > 0)
      builder.setLength(builder.length() - 1);

    walletAddressesSelection = builder.toString();
  }
}




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