Android Open Source - My-Wallet-Android Nfc Tools






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 ww . jav a 2  s  . co  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.util;

import java.nio.charset.Charset;
import java.util.Arrays;

import piuk.blockchain.android.Constants;

import android.app.Activity;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcManager;

/**
 * @author Andreas Schildbach
 */
public class NfcTools
{
  private static final Charset UTF_8 = Charset.forName("UTF-8");
  private static final Charset US_ASCII = Charset.forName("US-ASCII");
  private static final byte[] RTD_ANDROID_APP = "android.com:pkg".getBytes(US_ASCII);

  public static boolean publishUri(final Object nfcManager, final Activity activity, final String uri)
  {
    final NfcAdapter adapter = ((NfcManager) nfcManager).getDefaultAdapter();
    if (adapter == null)
      return false;

    final NdefRecord uriRecord = wellKnownUriRecord(uri);
    adapter.enableForegroundNdefPush(activity, ndefMessage(uriRecord, true));

    return true;
  }

  public static boolean publishMimeObject(final Object nfcManager, final Activity activity, final String mimeType, final byte[] payload,
      final boolean includeApplicationRecord)
  {
    final NfcAdapter adapter = ((NfcManager) nfcManager).getDefaultAdapter();
    if (adapter == null)
      return false;

    final NdefRecord mimeRecord = mimeRecord(mimeType, payload);
    adapter.enableForegroundNdefPush(activity, ndefMessage(mimeRecord, includeApplicationRecord));

    return true;
  }

  public static void unpublish(final Object nfcManager, final Activity activity)
  {
    final NfcAdapter adapter = ((NfcManager) nfcManager).getDefaultAdapter();
    if (adapter == null)
      return;

    adapter.disableForegroundNdefPush(activity);
  }

  private static NdefMessage ndefMessage(final NdefRecord record, final boolean includeApplicationRecord)
  {
    if (includeApplicationRecord)
    {
      final NdefRecord appRecord = androidApplicationRecord(Constants.PACKAGE_NAME);
      return new NdefMessage(new NdefRecord[] { record, appRecord });
    }
    else
    {
      return new NdefMessage(new NdefRecord[] { record });
    }
  }

  private static NdefRecord absoluteUriRecord(final String uri)
  {
    return new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, NdefRecord.RTD_URI, new byte[0], uri.getBytes(UTF_8));
  }

  private static NdefRecord wellKnownUriRecord(final String uri)
  {
    final byte[] uriBytes = uri.getBytes(UTF_8);
    final byte[] recordBytes = new byte[uriBytes.length + 1];
    recordBytes[0] = (byte) 0x0; // prefix, alway 0 for bitcoin scheme
    System.arraycopy(uriBytes, 0, recordBytes, 1, uriBytes.length);
    return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0], recordBytes);
  }

  private static NdefRecord mimeRecord(final String mimeType, final byte[] payload)
  {
    final byte[] mimeBytes = mimeType.getBytes(US_ASCII);
    final NdefRecord mimeRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, new byte[0], payload);
    return mimeRecord;
  }

  private static NdefRecord androidApplicationRecord(final String packageName)
  {
    return new NdefRecord(NdefRecord.TNF_EXTERNAL_TYPE, RTD_ANDROID_APP, new byte[0], packageName.getBytes(US_ASCII));
  }

  public static byte[] extractMimePayload(final String mimeType, final Object message)
  {
    byte[] mimeBytes = mimeType.getBytes(US_ASCII);

    final NdefMessage ndefMessage = (NdefMessage) message;
    for (final NdefRecord record : ndefMessage.getRecords())
    {
      if (record.getTnf() == NdefRecord.TNF_MIME_MEDIA && Arrays.equals(record.getType(), mimeBytes))
        return record.getPayload();
    }

    return null;
  }
}




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