Android Open Source - neoscoin-wallet Sample Activity






From Project

Back to project page neoscoin-wallet.

License

The source code is released under:

GNU General Public License

If you think the Android project neoscoin-wallet 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 2012-2013 the original author or authors.
 */* ww w .j a v  a2s.  c  o  m*/
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.schildbach.wallet.integration.sample;

import org.bitcoin.protocols.payments.Protos;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.TypefaceSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.bitcoin.core.Address;
import com.google.bitcoin.core.AddressFormatException;
import com.google.bitcoin.core.NetworkParameters;
import com.google.bitcoin.script.ScriptBuilder;
import com.google.protobuf.ByteString;

import de.schildbach.wallet.integration.android.BitcoinIntegration;

/**
 * @author Andreas Schildbach
 */
public class SampleActivity extends Activity
{
  private static final long AMOUNT = 500000;
  private static final String[] DONATION_ADDRESSES_MAINNET = { "18CK5k1gajRKKSC7yVSTXT9LUzbheh1XY4", "1PZmMahjbfsTy6DsaRyfStzoWTPppWwDnZ" };
  private static final String[] DONATION_ADDRESSES_TESTNET = { "mkCLjaXncyw8eSWJBcBtnTgviU85z5PfwS", "mwEacn7pYszzxfgcNaVUzYvzL6ypRJzB6A" };
  private static final String MEMO = "Sample donation";
  private static final int REQUEST_CODE = 0;

  private Button donateButton, requestButton;
  private TextView donateMessage;

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

    setContentView(R.layout.sample_activity);

    donateButton = (Button) findViewById(R.id.sample_donate_button);
    donateButton.setOnClickListener(new OnClickListener()
    {
      public void onClick(final View v)
      {
        handleDonate();
      }
    });

    requestButton = (Button) findViewById(R.id.sample_request_button);
    requestButton.setOnClickListener(new OnClickListener()
    {
      public void onClick(final View v)
      {
        handleRequest();
      }
    });

    donateMessage = (TextView) findViewById(R.id.sample_donate_message);
  }

  private String[] donationAddresses()
  {
    final boolean isMainnet = ((RadioButton) findViewById(R.id.sample_network_mainnet)).isChecked();

    return isMainnet ? DONATION_ADDRESSES_MAINNET : DONATION_ADDRESSES_TESTNET;
  }

  private void handleDonate()
  {
    final String[] addresses = donationAddresses();

    BitcoinIntegration.requestForResult(SampleActivity.this, REQUEST_CODE, addresses[0]);
  }

  private void handleRequest()
  {
    try
    {
      final String[] addresses = donationAddresses();
      final NetworkParameters params = Address.getParametersFromAddress(addresses[0]);

      final Protos.Output.Builder output1 = Protos.Output.newBuilder();
      output1.setAmount(AMOUNT);
      output1.setScript(ByteString.copyFrom(ScriptBuilder.createOutputScript(new Address(params, addresses[0])).getProgram()));

      final Protos.Output.Builder output2 = Protos.Output.newBuilder();
      output2.setAmount(AMOUNT);
      output2.setScript(ByteString.copyFrom(ScriptBuilder.createOutputScript(new Address(params, addresses[1])).getProgram()));

      final Protos.PaymentDetails.Builder paymentDetails = Protos.PaymentDetails.newBuilder();
      paymentDetails.setNetwork(params.getPaymentProtocolId());
      paymentDetails.addOutputs(output1);
      paymentDetails.addOutputs(output2);
      paymentDetails.setMemo(MEMO);
      paymentDetails.setTime(System.currentTimeMillis());

      final Protos.PaymentRequest.Builder paymentRequest = Protos.PaymentRequest.newBuilder();
      paymentRequest.setSerializedPaymentDetails(paymentDetails.build().toByteString());

      BitcoinIntegration.requestForResult(SampleActivity.this, REQUEST_CODE, paymentRequest.build().toByteArray());
    }
    catch (final AddressFormatException x)
    {
      throw new RuntimeException(x);
    }
  }

  @Override
  protected void onActivityResult(final int requestCode, final int resultCode, final Intent data)
  {
    if (requestCode == REQUEST_CODE)
    {
      if (resultCode == Activity.RESULT_OK)
      {
        final String txHash = BitcoinIntegration.transactionHashFromResult(data);
        if (txHash != null)
        {
          final SpannableStringBuilder messageBuilder = new SpannableStringBuilder("Transaction hash:\n");
          messageBuilder.append(txHash);
          messageBuilder.setSpan(new TypefaceSpan("monospace"), messageBuilder.length() - txHash.length(), messageBuilder.length(),
              Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

          if (BitcoinIntegration.paymentFromResult(data) != null)
            messageBuilder.append("\n(also a BIP70 payment message was received)");

          donateMessage.setText(messageBuilder);
          donateMessage.setVisibility(View.VISIBLE);
        }

        Toast.makeText(this, "Thank you!", Toast.LENGTH_LONG).show();
      }
      else if (resultCode == Activity.RESULT_CANCELED)
      {
        Toast.makeText(this, "Cancelled.", Toast.LENGTH_LONG).show();
      }
      else
      {
        Toast.makeText(this, "Unknown result.", Toast.LENGTH_LONG).show();
      }
    }
  }
}




Java Source Code List

de.schildbach.wallet.AddressBookProvider.java
de.schildbach.wallet.Configuration.java
de.schildbach.wallet.Constants.java
de.schildbach.wallet.ExchangeRatesProvider.java
de.schildbach.wallet.PaymentIntent.java
de.schildbach.wallet.WalletApplication.java
de.schildbach.wallet.WalletBalanceWidgetProvider.java
de.schildbach.wallet.camera.CameraManager.java
de.schildbach.wallet.integration.android.BitcoinIntegration.java
de.schildbach.wallet.integration.sample.SampleActivity.java
de.schildbach.wallet.offline.AcceptBluetoothService.java
de.schildbach.wallet.offline.AcceptBluetoothThread.java
de.schildbach.wallet.offline.DirectPaymentTask.java
de.schildbach.wallet.service.AutosyncReceiver.java
de.schildbach.wallet.service.BlockchainServiceImpl.java
de.schildbach.wallet.service.BlockchainService.java
de.schildbach.wallet.ui.AboutActivity.java
de.schildbach.wallet.ui.AbstractBindServiceActivity.java
de.schildbach.wallet.ui.AbstractOnDemandServiceActivity.java
de.schildbach.wallet.ui.AbstractWalletActivity.java
de.schildbach.wallet.ui.AddressAndLabel.java
de.schildbach.wallet.ui.AddressBookActivity.java
de.schildbach.wallet.ui.BlockListFragment.java
de.schildbach.wallet.ui.CurrencyAmountView.java
de.schildbach.wallet.ui.CurrencyCalculatorLink.java
de.schildbach.wallet.ui.CurrencySymbolDrawable.java
de.schildbach.wallet.ui.CurrencyTextView.java
de.schildbach.wallet.ui.DialogBuilder.java
de.schildbach.wallet.ui.EditAddressBookEntryFragment.java
de.schildbach.wallet.ui.ExchangeRateLoader.java
de.schildbach.wallet.ui.ExchangeRatesActivity.java
de.schildbach.wallet.ui.ExchangeRatesFragment.java
de.schildbach.wallet.ui.FileAdapter.java
de.schildbach.wallet.ui.HelpDialogFragment.java
de.schildbach.wallet.ui.ImportDialogButtonEnablerListener.java
de.schildbach.wallet.ui.ImportKeysActivity.java
de.schildbach.wallet.ui.InputParser.java
de.schildbach.wallet.ui.NetworkMonitorActivity.java
de.schildbach.wallet.ui.PeerListFragment.java
de.schildbach.wallet.ui.PreferencesActivity.java
de.schildbach.wallet.ui.ProgressDialogFragment.java
de.schildbach.wallet.ui.ReportIssueDialogBuilder.java
de.schildbach.wallet.ui.RequestCoinsActivity.java
de.schildbach.wallet.ui.RequestCoinsFragment.java
de.schildbach.wallet.ui.RequestPaymentRequestTask.java
de.schildbach.wallet.ui.ScanActivity.java
de.schildbach.wallet.ui.ScannerView.java
de.schildbach.wallet.ui.SendCoinsActivity.java
de.schildbach.wallet.ui.SendCoinsFragment.java
de.schildbach.wallet.ui.SendCoinsOfflineTask.java
de.schildbach.wallet.ui.SendCoinsQrActivity.java
de.schildbach.wallet.ui.SendingAddressesFragment.java
de.schildbach.wallet.ui.ShowPasswordCheckListener.java
de.schildbach.wallet.ui.TransactionActivity.java
de.schildbach.wallet.ui.TransactionFragment.java
de.schildbach.wallet.ui.TransactionsListAdapter.java
de.schildbach.wallet.ui.TransactionsListFragment.java
de.schildbach.wallet.ui.WalletActionsFragment.java
de.schildbach.wallet.ui.WalletActivity.java
de.schildbach.wallet.ui.WalletAddressFragment.java
de.schildbach.wallet.ui.WalletAddressesAdapter.java
de.schildbach.wallet.ui.WalletAddressesFragment.java
de.schildbach.wallet.ui.WalletBalanceFragment.java
de.schildbach.wallet.ui.WalletBalanceLoader.java
de.schildbach.wallet.ui.WalletDisclaimerFragment.java
de.schildbach.wallet.ui.WalletTransactionsFragment.java
de.schildbach.wallet.util.AbstractClipboardManager.java
de.schildbach.wallet.util.Base43.java
de.schildbach.wallet.util.BitmapFragment.java
de.schildbach.wallet.util.Bluetooth.java
de.schildbach.wallet.util.CircularProgressView.java
de.schildbach.wallet.util.CrashReporter.java
de.schildbach.wallet.util.Crypto.java
de.schildbach.wallet.util.GenericUtils.java
de.schildbach.wallet.util.HttpGetThread.java
de.schildbach.wallet.util.Io.java
de.schildbach.wallet.util.Iso8601Format.java
de.schildbach.wallet.util.LinuxSecureRandom.java
de.schildbach.wallet.util.Nfc.java
de.schildbach.wallet.util.PaymentProtocol.java
de.schildbach.wallet.util.Qr.java
de.schildbach.wallet.util.ThrottlingWalletChangeListener.java
de.schildbach.wallet.util.ViewPagerTabs.java
de.schildbach.wallet.util.WalletUtils.java