Send identity destinations to other devices via NFC
This commit is contained in:
1
TODO
1
TODO
@@ -16,7 +16,6 @@ Features:
|
|||||||
- Public address book lookup
|
- Public address book lookup
|
||||||
- Import/export identities
|
- Import/export identities
|
||||||
- Export Destination / make it copyable
|
- Export Destination / make it copyable
|
||||||
-- NFC
|
|
||||||
-- SMS / message
|
-- SMS / message
|
||||||
- Add optional CC: and BCC: fields
|
- Add optional CC: and BCC: fields
|
||||||
- "Empty trash" option in Trash folder
|
- "Empty trash" option in Trash folder
|
||||||
|
|||||||
@@ -1,11 +1,19 @@
|
|||||||
package i2p.bote.android.config;
|
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.os.Bundle;
|
||||||
import android.support.v7.app.ActionBarActivity;
|
import android.support.v7.app.ActionBarActivity;
|
||||||
|
|
||||||
import i2p.bote.android.InitActivities;
|
import i2p.bote.android.InitActivities;
|
||||||
|
|
||||||
public class ViewIdentityActivity extends ActionBarActivity {
|
public class ViewIdentityActivity extends ActionBarActivity {
|
||||||
|
NfcAdapter mNfcAdapter;
|
||||||
|
|
||||||
|
@SuppressLint("NewApi")
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
@@ -26,5 +34,35 @@ public class ViewIdentityActivity extends ActionBarActivity {
|
|||||||
getSupportFragmentManager().beginTransaction()
|
getSupportFragmentManager().beginTransaction()
|
||||||
.add(android.R.id.content, f).commit();
|
.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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ import android.app.Dialog;
|
|||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
|
import android.nfc.NdefMessage;
|
||||||
|
import android.nfc.NdefRecord;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.v4.app.DialogFragment;
|
import android.support.v4.app.DialogFragment;
|
||||||
import android.support.v4.app.Fragment;
|
import android.support.v4.app.Fragment;
|
||||||
@@ -153,4 +156,40 @@ public class ViewIdentityFragment extends Fragment {
|
|||||||
return super.onOptionsItemSelected(item);
|
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()
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user