From 4489037619d4d75a475ab3698eaf810507593e40 Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Fri, 31 Jan 2014 21:49:13 -0500 Subject: [PATCH] NFC beam the repo in RepoDetailsActivity This is the framework for easily swapping repos. The idea is that a user can send the URL with the fingerprint for trusted bootstrapping of the repo on a new user's device. This will be essential for p2p repos provided by Bazaar/Kerplapp. The required NFC APIs were introduced in android-14. So android-14 and below skip the NFC stuff. --- AndroidManifest.xml | 30 +++++++- .../fdroid/views/RepoDetailsActivity.java | 75 ++++++++++++++++++- 2 files changed, 100 insertions(+), 5 deletions(-) diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 56149a5c2..d983b6dda 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -20,12 +20,16 @@ + + - + + + + + + + + + + + + + + + + + + - + = 14) + setNfc(); + } + + @TargetApi(14) + private void setNfc() { + NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this); + if (nfcAdapter == null) { + return; + } + nfcAdapter.setNdefPushMessage(new NdefMessage(new NdefRecord[] { + NdefRecord.createUri(getSharingUri()), + }), this); + findViewById(android.R.id.content).post(new Runnable() { + @Override + public void run() { + Log.i(TAG, "Runnable.run()"); + onNewIntent(getIntent()); + } + }); + } + + @Override + public void onResume() { + Log.i(TAG, "onResume"); + super.onResume(); + if (Build.VERSION.SDK_INT >= 9) + processIntent(getIntent()); + } + + @Override + public void onNewIntent(Intent i) { + Log.i(TAG, "onNewIntent"); + Log.i(TAG, "action: " + i.getAction()); + Log.i(TAG, "data: " + i.getData()); + // onResume gets called after this to handle the intent + setIntent(i); + } + + @TargetApi(9) + void processIntent(Intent i) { + if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(i.getAction())) { + Log.i(TAG, "ACTION_NDEF_DISCOVERED"); + Parcelable[] rawMsgs = + i.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); + NdefMessage msg = (NdefMessage) rawMsgs[0]; + String url = new String(msg.getRecords()[0].getPayload()); + Log.i(TAG, "Got this URL: " + url); + Toast.makeText(this, "Got this URL: " + url, Toast.LENGTH_LONG).show(); + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); + String packageName = getPackageName(); + intent.setClassName(packageName, packageName + ".ManageRepo"); + startActivity(intent); + finish(); + } } protected Uri getSharingUri() {