From 19b62d67b33ae172322ca8d6502973c3f9cdae0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Wed, 9 Apr 2014 16:05:47 +0200 Subject: [PATCH] Fix NFC --- .../keychain/ui/ImportKeysActivity.java | 17 +++-- .../keychain/ui/ViewKeyActivityJB.java | 74 +++++++++---------- 2 files changed, 48 insertions(+), 43 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java index 6ea79473a..46da6257a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -93,6 +93,11 @@ public class ImportKeysActivity extends ActionBarActivity implements ActionBar.O ImportKeysClipboardFragment.class, ImportKeysNFCFragment.class }; + private static final int NAV_SERVER = 0; + private static final int NAV_FILE = 1; + private static final int NAV_QR_CODE = 2; + private static final int NAV_CLIPBOARD = 3; + private static final int NAV_NFC = 4; private int mCurrentNavPosition = -1; @@ -152,7 +157,7 @@ public class ImportKeysActivity extends ActionBarActivity implements ActionBar.O /* Keychain's own Actions */ // display file fragment - loadNavFragment(1, null); + loadNavFragment(NAV_FILE, null); if (dataUri != null) { // action: directly load data @@ -187,7 +192,7 @@ public class ImportKeysActivity extends ActionBarActivity implements ActionBar.O // display keyserver fragment with query Bundle args = new Bundle(); args.putString(ImportKeysServerFragment.ARG_QUERY, query); - loadNavFragment(0, args); + loadNavFragment(NAV_SERVER, args); // action: search immediately startListFragment(savedInstanceState, null, null, query); @@ -212,7 +217,7 @@ public class ImportKeysActivity extends ActionBarActivity implements ActionBar.O } else if (ACTION_IMPORT_KEY_FROM_FILE.equals(action)) { // NOTE: this only displays the appropriate fragment, no actions are taken - loadNavFragment(1, null); + loadNavFragment(NAV_FILE, null); // no immediate actions! startListFragment(savedInstanceState, null, null, null); @@ -220,14 +225,14 @@ public class ImportKeysActivity extends ActionBarActivity implements ActionBar.O // also exposed in AndroidManifest // NOTE: this only displays the appropriate fragment, no actions are taken - loadNavFragment(2, null); + loadNavFragment(NAV_QR_CODE, null); // no immediate actions! startListFragment(savedInstanceState, null, null, null); } else if (ACTION_IMPORT_KEY_FROM_NFC.equals(action)) { // NOTE: this only displays the appropriate fragment, no actions are taken - loadNavFragment(3, null); + loadNavFragment(NAV_NFC, null); // no immediate actions! startListFragment(savedInstanceState, null, null, null); @@ -327,7 +332,7 @@ public class ImportKeysActivity extends ActionBarActivity implements ActionBar.O Bundle args = new Bundle(); args.putString(ImportKeysServerFragment.ARG_QUERY, query); args.putBoolean(ImportKeysServerFragment.ARG_DISABLE_QUERY_EDIT, true); - loadNavFragment(0, args); + loadNavFragment(NAV_SERVER, args); // action: search directly startListFragment(savedInstanceState, null, null, query); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivityJB.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivityJB.java index 14a5c1a33..d535195f0 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivityJB.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivityJB.java @@ -18,6 +18,7 @@ package org.sufficientlysecure.keychain.ui; import android.annotation.TargetApi; +import android.net.Uri; import android.nfc.NdefMessage; import android.nfc.NdefRecord; import android.nfc.NfcAdapter; @@ -29,12 +30,12 @@ import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Message; -import android.widget.Toast; import com.devspark.appmsg.AppMsg; import org.sufficientlysecure.keychain.Constants; import org.sufficientlysecure.keychain.R; +import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.ProviderHelper; import org.sufficientlysecure.keychain.util.Log; @@ -63,29 +64,40 @@ public class ViewKeyActivityJB extends ViewKeyActivity implements CreateNdefMess if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { // Check for available NFC Adapter mNfcAdapter = NfcAdapter.getDefaultAdapter(this); + if (mNfcAdapter != null) { + /* + * Retrieve mSharedKeyringBytes here asynchronously (to not block the UI) + * and init nfc adapter afterwards. + * mSharedKeyringBytes can not be retrieved in createNdefMessage, because this process + * has no permissions to query the Uri. + */ + AsyncTask initTask = + new AsyncTask() { + protected Void doInBackground(NfcAdapter... adapter) { + try { + Uri dataUri = KeychainContract.KeyRingData.buildPublicKeyRingUri(mDataUri); + mSharedKeyringBytes = ProviderHelper.getPGPKeyRing( + ViewKeyActivityJB.this, dataUri).getEncoded(); + } catch (IOException e) { + Log.e(Constants.TAG, "Error parsing keyring", e); + } catch (ProviderHelper.NotFoundException e) { + Log.e(Constants.TAG, "key not found!", e); + } + return null; + } -// AsyncTask registerTask = -// new AsyncTask() { -// protected Void doInBackground(NfcAdapter... adapter) { -// if (adapter != null) { -// // init nfc -// // Register callback to set NDEF message -// adapter.setNdefPushMessageCallback(ViewKeyActivityJB.this, ViewKeyActivityJB.this); -// // Register callback to listen for message-sent success -// adapter.setOnNdefPushCompleteCallback(this, this); -// } -// return null; -// } -// -// protected void onProgressUpdate() { -// } -// -// protected void onPostExecute(Void result) { -// -// } -// }; - + protected void onPostExecute(Void result) { + // Register callback to set NDEF message + mNfcAdapter.setNdefPushMessageCallback(ViewKeyActivityJB.this + , ViewKeyActivityJB.this); + // Register callback to listen for message-sent success + mNfcAdapter.setOnNdefPushCompleteCallback(ViewKeyActivityJB.this, + ViewKeyActivityJB.this); + } + }; + initTask.execute(); + } } } @@ -100,21 +112,9 @@ public class ViewKeyActivityJB extends ViewKeyActivity implements CreateNdefMess * guarantee that this activity starts when receiving a beamed message. For now, this code * uses the tag dispatch system. */ -// try { - // get public keyring as byte array -// event.nfcAdapter. -// mSharedKeyringBytes = ProviderHelper.getPGPKeyRing(this, mDataUri).getEncoded(); -// -// NdefMessage msg = new NdefMessage(NdefRecord.createMime(Constants.NFC_MIME, -// mSharedKeyringBytes), NdefRecord.createApplicationRecord(Constants.PACKAGE_NAME)); -// return msg; -// } catch(IOException e) { -// Log.e(Constants.TAG, "Error parsing keyring", e); -// return null; -// } catch (ProviderHelper.NotFoundException e) { -// Log.e(Constants.TAG, "key not found!", e); - return null; -// } + NdefMessage msg = new NdefMessage(NdefRecord.createMime(Constants.NFC_MIME, + mSharedKeyringBytes), NdefRecord.createApplicationRecord(Constants.PACKAGE_NAME)); + return msg; } /**