diff --git a/TODO b/TODO index 50ee329..256458b 100644 --- a/TODO +++ b/TODO @@ -16,7 +16,6 @@ Features: - Public address book lookup - Import/export identities - Export Destination / make it copyable --- NFC -- SMS / message - Add optional CC: and BCC: fields - "Empty trash" option in Trash folder diff --git a/app/src/main/java/i2p/bote/android/config/ViewIdentityActivity.java b/app/src/main/java/i2p/bote/android/config/ViewIdentityActivity.java index 2bf0df5..fbff5af 100644 --- a/app/src/main/java/i2p/bote/android/config/ViewIdentityActivity.java +++ b/app/src/main/java/i2p/bote/android/config/ViewIdentityActivity.java @@ -1,11 +1,19 @@ package i2p.bote.android.config; +import android.annotation.SuppressLint; +import android.nfc.NdefMessage; +import android.nfc.NfcAdapter; +import android.nfc.NfcEvent; +import android.os.Build; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import i2p.bote.android.InitActivities; public class ViewIdentityActivity extends ActionBarActivity { + NfcAdapter mNfcAdapter; + + @SuppressLint("NewApi") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -26,5 +34,35 @@ public class ViewIdentityActivity extends ActionBarActivity { getSupportFragmentManager().beginTransaction() .add(android.R.id.content, f).commit(); } + + // NFC send only works on API 10+ + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD_MR1) { + mNfcAdapter = NfcAdapter.getDefaultAdapter(this); + if (mNfcAdapter != null && + Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) + mNfcAdapter.setNdefPushMessageCallback(new NfcAdapter.CreateNdefMessageCallback() { + @Override + public NdefMessage createNdefMessage(NfcEvent nfcEvent) { + return getNdefMessage(); + } + }, this); + } + } + + @SuppressLint("NewApi") + @Override + public void onResume() { + super.onResume(); + + if (mNfcAdapter != null && + Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) { + mNfcAdapter.enableForegroundNdefPush(this, getNdefMessage()); + } + } + + private NdefMessage getNdefMessage() { + ViewIdentityFragment f = (ViewIdentityFragment) getSupportFragmentManager() + .findFragmentById(android.R.id.content); + return f.createNdefMessage(); } } diff --git a/app/src/main/java/i2p/bote/android/config/ViewIdentityFragment.java b/app/src/main/java/i2p/bote/android/config/ViewIdentityFragment.java index 30faf17..ab2d85d 100644 --- a/app/src/main/java/i2p/bote/android/config/ViewIdentityFragment.java +++ b/app/src/main/java/i2p/bote/android/config/ViewIdentityFragment.java @@ -13,6 +13,9 @@ import android.app.Dialog; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Bitmap; +import android.nfc.NdefMessage; +import android.nfc.NdefRecord; +import android.os.Build; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; @@ -153,4 +156,40 @@ public class ViewIdentityFragment extends Fragment { return super.onOptionsItemSelected(item); } } + + public NdefMessage createNdefMessage() { + NdefMessage msg = new NdefMessage(new NdefRecord[]{ + createNameRecord(), + createDestinationRecord() + }); + return msg; + } + + private NdefRecord createNameRecord() { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) + return new NdefRecord( + NdefRecord.TNF_EXTERNAL_TYPE, + "i2p.bote:contact".getBytes(), + new byte[0], + mIdentity.getPublicName().getBytes() + ); + else + return NdefRecord.createExternal( + "i2p.bote", "contact", mIdentity.getPublicName().getBytes() + ); + } + + private NdefRecord createDestinationRecord() { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) + return new NdefRecord( + NdefRecord.TNF_EXTERNAL_TYPE, + "i2p.bote:contactDestination".getBytes(), + new byte[0], + mIdentity.getKey().getBytes() + ); + else + return NdefRecord.createExternal( + "i2p.bote", "contactDestination", mIdentity.getKey().getBytes() + ); + } }