mirror of
https://github.com/moparisthebest/open-keychain
synced 2024-11-27 11:12:15 -05:00
Implemented #1162: Invoke NFC in Advanced View
- Extracted NFC code from ViewKeyActivity to NfcHelper to share code between classes - Changed the private anonymous Handler for NFC into a static private subclass, that uses WeakReference to avoid memory leaks - Added resources needed (retrieved from Graphics) for the NFC button inside ViewKeyAdvShareFragment. - Fixed the ripple boundary of the Share With... button to prevent it from bleeding to other buttons on the right (UX improvement)
This commit is contained in:
parent
cfef53be01
commit
8d6d4328f2
@ -21,18 +21,12 @@ package org.sufficientlysecure.keychain.ui;
|
|||||||
import android.animation.ArgbEvaluator;
|
import android.animation.ArgbEvaluator;
|
||||||
import android.animation.ObjectAnimator;
|
import android.animation.ObjectAnimator;
|
||||||
import android.annotation.SuppressLint;
|
import android.annotation.SuppressLint;
|
||||||
import android.annotation.TargetApi;
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.app.ActivityOptions;
|
import android.app.ActivityOptions;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.pm.PackageManager;
|
|
||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.nfc.NdefMessage;
|
|
||||||
import android.nfc.NdefRecord;
|
|
||||||
import android.nfc.NfcAdapter;
|
|
||||||
import android.nfc.NfcEvent;
|
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
@ -40,7 +34,6 @@ import android.os.Handler;
|
|||||||
import android.os.Message;
|
import android.os.Message;
|
||||||
import android.os.Messenger;
|
import android.os.Messenger;
|
||||||
import android.provider.ContactsContract;
|
import android.provider.ContactsContract;
|
||||||
import android.provider.Settings;
|
|
||||||
import android.support.v4.app.ActivityCompat;
|
import android.support.v4.app.ActivityCompat;
|
||||||
import android.support.v4.app.LoaderManager;
|
import android.support.v4.app.LoaderManager;
|
||||||
import android.support.v4.content.CursorLoader;
|
import android.support.v4.content.CursorLoader;
|
||||||
@ -58,9 +51,7 @@ import android.widget.ImageView;
|
|||||||
import android.widget.RelativeLayout;
|
import android.widget.RelativeLayout;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import com.getbase.floatingactionbutton.FloatingActionButton;
|
import com.getbase.floatingactionbutton.FloatingActionButton;
|
||||||
|
|
||||||
import org.sufficientlysecure.keychain.Constants;
|
import org.sufficientlysecure.keychain.Constants;
|
||||||
import org.sufficientlysecure.keychain.R;
|
import org.sufficientlysecure.keychain.R;
|
||||||
import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing;
|
import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing;
|
||||||
@ -84,6 +75,7 @@ import org.sufficientlysecure.keychain.ui.util.QrCodeUtils;
|
|||||||
import org.sufficientlysecure.keychain.util.ContactHelper;
|
import org.sufficientlysecure.keychain.util.ContactHelper;
|
||||||
import org.sufficientlysecure.keychain.util.ExportHelper;
|
import org.sufficientlysecure.keychain.util.ExportHelper;
|
||||||
import org.sufficientlysecure.keychain.util.Log;
|
import org.sufficientlysecure.keychain.util.Log;
|
||||||
|
import org.sufficientlysecure.keychain.util.NfcHelper;
|
||||||
import org.sufficientlysecure.keychain.util.Preferences;
|
import org.sufficientlysecure.keychain.util.Preferences;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -93,8 +85,8 @@ public class ViewKeyActivity extends BaseActivity implements
|
|||||||
LoaderManager.LoaderCallbacks<Cursor> {
|
LoaderManager.LoaderCallbacks<Cursor> {
|
||||||
|
|
||||||
static final int REQUEST_QR_FINGERPRINT = 1;
|
static final int REQUEST_QR_FINGERPRINT = 1;
|
||||||
static final int REQUEST_DELETE= 2;
|
static final int REQUEST_DELETE = 2;
|
||||||
static final int REQUEST_EXPORT= 3;
|
static final int REQUEST_EXPORT = 3;
|
||||||
|
|
||||||
ExportHelper mExportHelper;
|
ExportHelper mExportHelper;
|
||||||
ProviderHelper mProviderHelper;
|
ProviderHelper mProviderHelper;
|
||||||
@ -115,11 +107,7 @@ public class ViewKeyActivity extends BaseActivity implements
|
|||||||
private CardView mQrCodeLayout;
|
private CardView mQrCodeLayout;
|
||||||
|
|
||||||
// NFC
|
// NFC
|
||||||
private NfcAdapter mNfcAdapter;
|
private NfcHelper mNfcHelper;
|
||||||
private NfcAdapter.CreateNdefMessageCallback mNdefCallback;
|
|
||||||
private NfcAdapter.OnNdefPushCompleteCallback mNdefCompleteCallback;
|
|
||||||
private byte[] mNfcKeyringBytes;
|
|
||||||
private static final int NFC_SENT = 1;
|
|
||||||
|
|
||||||
private static final int LOADER_ID_UNIFIED = 0;
|
private static final int LOADER_ID_UNIFIED = 0;
|
||||||
|
|
||||||
@ -256,7 +244,7 @@ public class ViewKeyActivity extends BaseActivity implements
|
|||||||
mActionNfc.setOnClickListener(new View.OnClickListener() {
|
mActionNfc.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
invokeNfcBeam();
|
mNfcHelper.invokeNfcBeam();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -264,7 +252,8 @@ public class ViewKeyActivity extends BaseActivity implements
|
|||||||
// or start new ones.
|
// or start new ones.
|
||||||
getSupportLoaderManager().initLoader(LOADER_ID_UNIFIED, null, this);
|
getSupportLoaderManager().initLoader(LOADER_ID_UNIFIED, null, this);
|
||||||
|
|
||||||
initNfc(mDataUri);
|
mNfcHelper = new NfcHelper(this, mProviderHelper);
|
||||||
|
mNfcHelper.initNfc(mDataUri);
|
||||||
|
|
||||||
startFragment(savedInstanceState, mDataUri);
|
startFragment(savedInstanceState, mDataUri);
|
||||||
}
|
}
|
||||||
@ -375,41 +364,6 @@ public class ViewKeyActivity extends BaseActivity implements
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
|
||||||
private void invokeNfcBeam() {
|
|
||||||
// Check if device supports NFC
|
|
||||||
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
|
|
||||||
Notify.createNotify(this, R.string.no_nfc_support, Notify.LENGTH_LONG, Notify.Style.ERROR).show();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Check for available NFC Adapter
|
|
||||||
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
|
|
||||||
if (mNfcAdapter == null || !mNfcAdapter.isEnabled()) {
|
|
||||||
Notify.createNotify(this, R.string.error_nfc_needed, Notify.LENGTH_LONG, Notify.Style.ERROR, new Notify.ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void onAction() {
|
|
||||||
Intent intentSettings = new Intent(Settings.ACTION_NFC_SETTINGS);
|
|
||||||
startActivity(intentSettings);
|
|
||||||
}
|
|
||||||
}, R.string.menu_nfc_preferences).show();
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!mNfcAdapter.isNdefPushEnabled()) {
|
|
||||||
Notify.createNotify(this, R.string.error_beam_needed, Notify.LENGTH_LONG, Notify.Style.ERROR, new Notify.ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void onAction() {
|
|
||||||
Intent intentSettings = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);
|
|
||||||
startActivity(intentSettings);
|
|
||||||
}
|
|
||||||
}, R.string.menu_beam_preferences).show();
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
mNfcAdapter.invokeBeam(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void scanQrCode() {
|
private void scanQrCode() {
|
||||||
Intent scanQrCode = new Intent(this, ImportKeysProxyActivity.class);
|
Intent scanQrCode = new Intent(this, ImportKeysProxyActivity.class);
|
||||||
@ -426,7 +380,7 @@ public class ViewKeyActivity extends BaseActivity implements
|
|||||||
|
|
||||||
private void certifyImmediate() {
|
private void certifyImmediate() {
|
||||||
Intent intent = new Intent(this, CertifyKeyActivity.class);
|
Intent intent = new Intent(this, CertifyKeyActivity.class);
|
||||||
intent.putExtra(CertifyKeyActivity.EXTRA_KEY_IDS, new long[]{mMasterKeyId});
|
intent.putExtra(CertifyKeyActivity.EXTRA_KEY_IDS, new long[] {mMasterKeyId});
|
||||||
|
|
||||||
startCertifyIntent(intent);
|
startCertifyIntent(intent);
|
||||||
}
|
}
|
||||||
@ -487,11 +441,11 @@ public class ViewKeyActivity extends BaseActivity implements
|
|||||||
|
|
||||||
HashMap<String, Object> data = providerHelper.getGenericData(
|
HashMap<String, Object> data = providerHelper.getGenericData(
|
||||||
baseUri,
|
baseUri,
|
||||||
new String[]{KeychainContract.Keys.MASTER_KEY_ID, KeychainContract.KeyRings.HAS_SECRET},
|
new String[] {KeychainContract.Keys.MASTER_KEY_ID, KeychainContract.KeyRings.HAS_SECRET},
|
||||||
new int[]{ProviderHelper.FIELD_TYPE_INTEGER, ProviderHelper.FIELD_TYPE_INTEGER});
|
new int[] {ProviderHelper.FIELD_TYPE_INTEGER, ProviderHelper.FIELD_TYPE_INTEGER});
|
||||||
|
|
||||||
exportHelper.showExportKeysDialog(
|
exportHelper.showExportKeysDialog(
|
||||||
new long[]{(Long) data.get(KeychainContract.KeyRings.MASTER_KEY_ID)},
|
new long[] {(Long) data.get(KeychainContract.KeyRings.MASTER_KEY_ID)},
|
||||||
Constants.Path.APP_DIR_FILE, ((Long) data.get(KeychainContract.KeyRings.HAS_SECRET) != 0)
|
Constants.Path.APP_DIR_FILE, ((Long) data.get(KeychainContract.KeyRings.HAS_SECRET) != 0)
|
||||||
);
|
);
|
||||||
} catch (ProviderHelper.NotFoundException e) {
|
} catch (ProviderHelper.NotFoundException e) {
|
||||||
@ -515,7 +469,7 @@ public class ViewKeyActivity extends BaseActivity implements
|
|||||||
// Create a new Messenger for the communication back
|
// Create a new Messenger for the communication back
|
||||||
Messenger messenger = new Messenger(returnHandler);
|
Messenger messenger = new Messenger(returnHandler);
|
||||||
DeleteKeyDialogFragment deleteKeyDialog = DeleteKeyDialogFragment.newInstance(messenger,
|
DeleteKeyDialogFragment deleteKeyDialog = DeleteKeyDialogFragment.newInstance(messenger,
|
||||||
new long[]{ mMasterKeyId });
|
new long[] {mMasterKeyId});
|
||||||
deleteKeyDialog.show(getSupportFragmentManager(), "deleteKeyDialog");
|
deleteKeyDialog.show(getSupportFragmentManager(), "deleteKeyDialog");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -546,11 +500,11 @@ public class ViewKeyActivity extends BaseActivity implements
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (requestCode == REQUEST_DELETE && resultCode == Activity.RESULT_OK){
|
if (requestCode == REQUEST_DELETE && resultCode == Activity.RESULT_OK) {
|
||||||
deleteKey();
|
deleteKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (requestCode == REQUEST_EXPORT && resultCode == Activity.RESULT_OK){
|
if (requestCode == REQUEST_EXPORT && resultCode == Activity.RESULT_OK) {
|
||||||
exportToFile(mDataUri, mExportHelper, mProviderHelper);
|
exportToFile(mDataUri, mExportHelper, mProviderHelper);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -572,7 +526,7 @@ public class ViewKeyActivity extends BaseActivity implements
|
|||||||
long keyId = new ProviderHelper(this)
|
long keyId = new ProviderHelper(this)
|
||||||
.getCachedPublicKeyRing(dataUri)
|
.getCachedPublicKeyRing(dataUri)
|
||||||
.extractOrGetMasterKeyId();
|
.extractOrGetMasterKeyId();
|
||||||
long[] encryptionKeyIds = new long[]{keyId};
|
long[] encryptionKeyIds = new long[] {keyId};
|
||||||
Intent intent;
|
Intent intent;
|
||||||
if (text) {
|
if (text) {
|
||||||
intent = new Intent(this, EncryptTextActivity.class);
|
intent = new Intent(this, EncryptTextActivity.class);
|
||||||
@ -710,98 +664,9 @@ public class ViewKeyActivity extends BaseActivity implements
|
|||||||
loadTask.execute();
|
loadTask.execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* NFC: Initialize NFC sharing if OS and device supports it
|
|
||||||
*/
|
|
||||||
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
|
|
||||||
private void initNfc(final Uri dataUri) {
|
|
||||||
// check if NFC Beam is supported (>= Android 4.1)
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
|
|
||||||
|
|
||||||
// Implementation for the CreateNdefMessageCallback interface
|
|
||||||
mNdefCallback = new NfcAdapter.CreateNdefMessageCallback() {
|
|
||||||
@Override
|
|
||||||
public NdefMessage createNdefMessage(NfcEvent event) {
|
|
||||||
/*
|
|
||||||
* When a device receives a push with an AAR in it, the application specified in the AAR is
|
|
||||||
* guaranteed to run. The AAR overrides the tag dispatch system. You can add it back in to
|
|
||||||
* guarantee that this activity starts when receiving a beamed message. For now, this code
|
|
||||||
* uses the tag dispatch system.
|
|
||||||
*/
|
|
||||||
return new NdefMessage(NdefRecord.createMime(Constants.NFC_MIME,
|
|
||||||
mNfcKeyringBytes), NdefRecord.createApplicationRecord(Constants.PACKAGE_NAME));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Implementation for the OnNdefPushCompleteCallback interface
|
|
||||||
mNdefCompleteCallback = new NfcAdapter.OnNdefPushCompleteCallback() {
|
|
||||||
@Override
|
|
||||||
public void onNdefPushComplete(NfcEvent event) {
|
|
||||||
// A handler is needed to send messages to the activity when this
|
|
||||||
// callback occurs, because it happens from a binder thread
|
|
||||||
mNfcHandler.obtainMessage(NFC_SENT).sendToTarget();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Check for available NFC Adapter
|
|
||||||
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
|
|
||||||
if (mNfcAdapter != null) {
|
|
||||||
/*
|
|
||||||
* Retrieve mNfcKeyringBytes here asynchronously (to not block the UI)
|
|
||||||
* and init nfc adapter afterwards.
|
|
||||||
* mNfcKeyringBytes can not be retrieved in createNdefMessage, because this process
|
|
||||||
* has no permissions to query the Uri.
|
|
||||||
*/
|
|
||||||
AsyncTask<Void, Void, Void> initTask =
|
|
||||||
new AsyncTask<Void, Void, Void>() {
|
|
||||||
protected Void doInBackground(Void... unused) {
|
|
||||||
try {
|
|
||||||
Uri blobUri =
|
|
||||||
KeychainContract.KeyRingData.buildPublicKeyRingUri(dataUri);
|
|
||||||
mNfcKeyringBytes = (byte[]) mProviderHelper.getGenericData(
|
|
||||||
blobUri,
|
|
||||||
KeychainContract.KeyRingData.KEY_RING_DATA,
|
|
||||||
ProviderHelper.FIELD_TYPE_BLOB);
|
|
||||||
} catch (ProviderHelper.NotFoundException e) {
|
|
||||||
Log.e(Constants.TAG, "key not found!", e);
|
|
||||||
}
|
|
||||||
|
|
||||||
// no AsyncTask return (Void)
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void onPostExecute(Void unused) {
|
|
||||||
// Register callback to set NDEF message
|
|
||||||
mNfcAdapter.setNdefPushMessageCallback(mNdefCallback,
|
|
||||||
ViewKeyActivity.this);
|
|
||||||
// Register callback to listen for message-sent success
|
|
||||||
mNfcAdapter.setOnNdefPushCompleteCallback(mNdefCompleteCallback,
|
|
||||||
ViewKeyActivity.this);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
initTask.execute();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* NFC: This handler receives a message from onNdefPushComplete
|
|
||||||
*/
|
|
||||||
private final Handler mNfcHandler = new Handler() {
|
|
||||||
@Override
|
|
||||||
public void handleMessage(Message msg) {
|
|
||||||
switch (msg.what) {
|
|
||||||
case NFC_SENT:
|
|
||||||
Notify.showNotify(
|
|
||||||
ViewKeyActivity.this, R.string.nfc_successful, Notify.Style.INFO);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// These are the rows that we will retrieve.
|
// These are the rows that we will retrieve.
|
||||||
static final String[] PROJECTION = new String[]{
|
static final String[] PROJECTION = new String[] {
|
||||||
KeychainContract.KeyRings._ID,
|
KeychainContract.KeyRings._ID,
|
||||||
KeychainContract.KeyRings.MASTER_KEY_ID,
|
KeychainContract.KeyRings.MASTER_KEY_ID,
|
||||||
KeychainContract.KeyRings.USER_ID,
|
KeychainContract.KeyRings.USER_ID,
|
||||||
|
@ -52,6 +52,7 @@ import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils;
|
|||||||
import org.sufficientlysecure.keychain.ui.util.Notify;
|
import org.sufficientlysecure.keychain.ui.util.Notify;
|
||||||
import org.sufficientlysecure.keychain.ui.util.QrCodeUtils;
|
import org.sufficientlysecure.keychain.ui.util.QrCodeUtils;
|
||||||
import org.sufficientlysecure.keychain.util.Log;
|
import org.sufficientlysecure.keychain.util.Log;
|
||||||
|
import org.sufficientlysecure.keychain.util.NfcHelper;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
@ -68,10 +69,12 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements
|
|||||||
private View mFingerprintClipboardButton;
|
private View mFingerprintClipboardButton;
|
||||||
private View mKeyShareButton;
|
private View mKeyShareButton;
|
||||||
private View mKeyClipboardButton;
|
private View mKeyClipboardButton;
|
||||||
|
private View mKeyNfcButton;
|
||||||
private ImageButton mKeySafeSlingerButton;
|
private ImageButton mKeySafeSlingerButton;
|
||||||
private View mKeyUploadButton;
|
private View mKeyUploadButton;
|
||||||
|
|
||||||
ProviderHelper mProviderHelper;
|
ProviderHelper mProviderHelper;
|
||||||
|
NfcHelper mNfcHelper;
|
||||||
|
|
||||||
private static final int LOADER_ID_UNIFIED = 0;
|
private static final int LOADER_ID_UNIFIED = 0;
|
||||||
|
|
||||||
@ -83,6 +86,7 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements
|
|||||||
View view = inflater.inflate(R.layout.view_key_adv_share_fragment, getContainer());
|
View view = inflater.inflate(R.layout.view_key_adv_share_fragment, getContainer());
|
||||||
|
|
||||||
mProviderHelper = new ProviderHelper(ViewKeyAdvShareFragment.this.getActivity());
|
mProviderHelper = new ProviderHelper(ViewKeyAdvShareFragment.this.getActivity());
|
||||||
|
mNfcHelper = new NfcHelper(getActivity(), mProviderHelper);
|
||||||
|
|
||||||
mFingerprint = (TextView) view.findViewById(R.id.view_key_fingerprint);
|
mFingerprint = (TextView) view.findViewById(R.id.view_key_fingerprint);
|
||||||
mQrCode = (ImageView) view.findViewById(R.id.view_key_qr_code);
|
mQrCode = (ImageView) view.findViewById(R.id.view_key_qr_code);
|
||||||
@ -90,6 +94,7 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements
|
|||||||
mFingerprintShareButton = view.findViewById(R.id.view_key_action_fingerprint_share);
|
mFingerprintShareButton = view.findViewById(R.id.view_key_action_fingerprint_share);
|
||||||
mFingerprintClipboardButton = view.findViewById(R.id.view_key_action_fingerprint_clipboard);
|
mFingerprintClipboardButton = view.findViewById(R.id.view_key_action_fingerprint_clipboard);
|
||||||
mKeyShareButton = view.findViewById(R.id.view_key_action_key_share);
|
mKeyShareButton = view.findViewById(R.id.view_key_action_key_share);
|
||||||
|
mKeyNfcButton = view.findViewById(R.id.view_key_action_key_nfc);
|
||||||
mKeyClipboardButton = view.findViewById(R.id.view_key_action_key_clipboard);
|
mKeyClipboardButton = view.findViewById(R.id.view_key_action_key_clipboard);
|
||||||
mKeySafeSlingerButton = (ImageButton) view.findViewById(R.id.view_key_action_key_safeslinger);
|
mKeySafeSlingerButton = (ImageButton) view.findViewById(R.id.view_key_action_key_safeslinger);
|
||||||
mKeyUploadButton = view.findViewById(R.id.view_key_action_upload);
|
mKeyUploadButton = view.findViewById(R.id.view_key_action_upload);
|
||||||
@ -128,6 +133,14 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements
|
|||||||
share(mDataUri, mProviderHelper, false, true);
|
share(mDataUri, mProviderHelper, false, true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
mKeyNfcButton.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
mNfcHelper.invokeNfcBeam();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
mKeySafeSlingerButton.setOnClickListener(new View.OnClickListener() {
|
mKeySafeSlingerButton.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
@ -255,9 +268,12 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements
|
|||||||
// Prepare the loaders. Either re-connect with an existing ones,
|
// Prepare the loaders. Either re-connect with an existing ones,
|
||||||
// or start new ones.
|
// or start new ones.
|
||||||
getLoaderManager().initLoader(LOADER_ID_UNIFIED, null, this);
|
getLoaderManager().initLoader(LOADER_ID_UNIFIED, null, this);
|
||||||
|
|
||||||
|
// Prepare the NfcHelper
|
||||||
|
mNfcHelper.initNfc(mDataUri);
|
||||||
}
|
}
|
||||||
|
|
||||||
static final String[] UNIFIED_PROJECTION = new String[]{
|
static final String[] UNIFIED_PROJECTION = new String[] {
|
||||||
KeyRings._ID, KeyRings.MASTER_KEY_ID, KeyRings.HAS_ANY_SECRET,
|
KeyRings._ID, KeyRings.MASTER_KEY_ID, KeyRings.HAS_ANY_SECRET,
|
||||||
KeyRings.USER_ID, KeyRings.FINGERPRINT,
|
KeyRings.USER_ID, KeyRings.FINGERPRINT,
|
||||||
KeyRings.ALGORITHM, KeyRings.KEY_SIZE, KeyRings.CREATION, KeyRings.IS_EXPIRED,
|
KeyRings.ALGORITHM, KeyRings.KEY_SIZE, KeyRings.CREATION, KeyRings.IS_EXPIRED,
|
||||||
@ -362,4 +378,5 @@ public class ViewKeyAdvShareFragment extends LoaderFragment implements
|
|||||||
startActivityForResult(uploadIntent, 0);
|
startActivityForResult(uploadIntent, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@ -0,0 +1,204 @@
|
|||||||
|
package org.sufficientlysecure.keychain.util;
|
||||||
|
|
||||||
|
import android.annotation.TargetApi;
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.content.pm.PackageManager;
|
||||||
|
import android.net.Uri;
|
||||||
|
import android.nfc.NdefMessage;
|
||||||
|
import android.nfc.NdefRecord;
|
||||||
|
import android.nfc.NfcAdapter;
|
||||||
|
import android.nfc.NfcEvent;
|
||||||
|
import android.os.AsyncTask;
|
||||||
|
import android.os.Build;
|
||||||
|
import android.os.Handler;
|
||||||
|
import android.os.Message;
|
||||||
|
import android.provider.Settings;
|
||||||
|
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.ui.util.Notify;
|
||||||
|
|
||||||
|
import java.lang.ref.WeakReference;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class contains NFC functionality that can be shared across Fragments or Activities.
|
||||||
|
* <p/>
|
||||||
|
* Created on Mar 20, 2015.
|
||||||
|
*
|
||||||
|
* @author Kent
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class NfcHelper {
|
||||||
|
|
||||||
|
private Activity mActivity;
|
||||||
|
private ProviderHelper mProviderHelper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NFC: This handler receives a message from onNdefPushComplete
|
||||||
|
*/
|
||||||
|
private static NfcHandler mNfcHandler;
|
||||||
|
|
||||||
|
private NfcAdapter mNfcAdapter;
|
||||||
|
private NfcAdapter.CreateNdefMessageCallback mNdefCallback;
|
||||||
|
private NfcAdapter.OnNdefPushCompleteCallback mNdefCompleteCallback;
|
||||||
|
private byte[] mNfcKeyringBytes;
|
||||||
|
private static final int NFC_SENT = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the NfcHelper.
|
||||||
|
*/
|
||||||
|
public NfcHelper(final Activity activity, final ProviderHelper providerHelper) {
|
||||||
|
mActivity = activity;
|
||||||
|
mProviderHelper = providerHelper;
|
||||||
|
|
||||||
|
mNfcHandler = new NfcHandler(mActivity);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return true if the NFC Adapter of this Helper has any features enabled.
|
||||||
|
*
|
||||||
|
* @return true if this NFC Adapter has any features enabled
|
||||||
|
*/
|
||||||
|
public boolean isEnabled() {
|
||||||
|
return mNfcAdapter.isEnabled();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* NFC: Initialize NFC sharing if OS and device supports it
|
||||||
|
*/
|
||||||
|
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
|
||||||
|
public void initNfc(final Uri dataUri) {
|
||||||
|
// check if NFC Beam is supported (>= Android 4.1)
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
|
||||||
|
|
||||||
|
// Implementation for the CreateNdefMessageCallback interface
|
||||||
|
mNdefCallback = new NfcAdapter.CreateNdefMessageCallback() {
|
||||||
|
@Override
|
||||||
|
public NdefMessage createNdefMessage(NfcEvent event) {
|
||||||
|
/*
|
||||||
|
* When a device receives a push with an AAR in it, the application specified in the AAR is
|
||||||
|
* guaranteed to run. The AAR overrides the tag dispatch system. You can add it back in to
|
||||||
|
* guarantee that this activity starts when receiving a beamed message. For now, this code
|
||||||
|
* uses the tag dispatch system.
|
||||||
|
*/
|
||||||
|
return new NdefMessage(NdefRecord.createMime(Constants.NFC_MIME,
|
||||||
|
mNfcKeyringBytes), NdefRecord.createApplicationRecord(Constants.PACKAGE_NAME));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Implementation for the OnNdefPushCompleteCallback interface
|
||||||
|
mNdefCompleteCallback = new NfcAdapter.OnNdefPushCompleteCallback() {
|
||||||
|
@Override
|
||||||
|
public void onNdefPushComplete(NfcEvent event) {
|
||||||
|
// A handler is needed to send messages to the activity when this
|
||||||
|
// callback occurs, because it happens from a binder thread
|
||||||
|
mNfcHandler.obtainMessage(NFC_SENT).sendToTarget();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Check for available NFC Adapter
|
||||||
|
mNfcAdapter = NfcAdapter.getDefaultAdapter(mActivity);
|
||||||
|
if (mNfcAdapter != null) {
|
||||||
|
/*
|
||||||
|
* Retrieve mNfcKeyringBytes here asynchronously (to not block the UI)
|
||||||
|
* and init nfc adapter afterwards.
|
||||||
|
* mNfcKeyringBytes can not be retrieved in createNdefMessage, because this process
|
||||||
|
* has no permissions to query the Uri.
|
||||||
|
*/
|
||||||
|
AsyncTask<Void, Void, Void> initTask =
|
||||||
|
new AsyncTask<Void, Void, Void>() {
|
||||||
|
protected Void doInBackground(Void... unused) {
|
||||||
|
try {
|
||||||
|
Uri blobUri =
|
||||||
|
KeychainContract.KeyRingData.buildPublicKeyRingUri(dataUri);
|
||||||
|
mNfcKeyringBytes = (byte[]) mProviderHelper.getGenericData(
|
||||||
|
blobUri,
|
||||||
|
KeychainContract.KeyRingData.KEY_RING_DATA,
|
||||||
|
ProviderHelper.FIELD_TYPE_BLOB);
|
||||||
|
} catch (ProviderHelper.NotFoundException e) {
|
||||||
|
Log.e(Constants.TAG, "key not found!", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// no AsyncTask return (Void)
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void onPostExecute(Void unused) {
|
||||||
|
// Register callback to set NDEF message
|
||||||
|
mNfcAdapter.setNdefPushMessageCallback(mNdefCallback,
|
||||||
|
mActivity);
|
||||||
|
// Register callback to listen for message-sent success
|
||||||
|
mNfcAdapter.setOnNdefPushCompleteCallback(mNdefCompleteCallback,
|
||||||
|
mActivity);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
initTask.execute();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
|
||||||
|
public void invokeNfcBeam() {
|
||||||
|
// Check if device supports NFC
|
||||||
|
if (!mActivity.getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {
|
||||||
|
Notify.createNotify(mActivity, R.string.no_nfc_support, Notify.LENGTH_LONG, Notify.Style.ERROR).show();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Check for available NFC Adapter
|
||||||
|
mNfcAdapter = NfcAdapter.getDefaultAdapter(mActivity);
|
||||||
|
if (mNfcAdapter == null || !mNfcAdapter.isEnabled()) {
|
||||||
|
Notify.createNotify(mActivity, R.string.error_nfc_needed, Notify.LENGTH_LONG, Notify.Style.ERROR, new Notify.ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void onAction() {
|
||||||
|
Intent intentSettings = new Intent(Settings.ACTION_NFC_SETTINGS);
|
||||||
|
mActivity.startActivity(intentSettings);
|
||||||
|
}
|
||||||
|
}, R.string.menu_nfc_preferences).show();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!mNfcAdapter.isNdefPushEnabled()) {
|
||||||
|
Notify.createNotify(mActivity, R.string.error_beam_needed, Notify.LENGTH_LONG, Notify.Style.ERROR, new Notify.ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void onAction() {
|
||||||
|
Intent intentSettings = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);
|
||||||
|
mActivity.startActivity(intentSettings);
|
||||||
|
}
|
||||||
|
}, R.string.menu_beam_preferences).show();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
mNfcAdapter.invokeBeam(mActivity);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A static subclass of {@link Handler} with a {@link WeakReference} to an {@link Activity} to avoid memory leaks.
|
||||||
|
*/
|
||||||
|
private static class NfcHandler extends Handler {
|
||||||
|
private final WeakReference<Activity> mActivityReference;
|
||||||
|
|
||||||
|
public NfcHandler(Activity activity) {
|
||||||
|
mActivityReference = new WeakReference<>(activity);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleMessage(Message msg) {
|
||||||
|
Activity activity = mActivityReference.get();
|
||||||
|
|
||||||
|
if (activity != null) {
|
||||||
|
switch (msg.what) {
|
||||||
|
case NFC_SENT:
|
||||||
|
Notify.showNotify(
|
||||||
|
activity, R.string.nfc_successful, Notify.Style.INFO);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
BIN
OpenKeychain/src/main/res/drawable-hdpi/ic_nfc_grey_24dp.png
Normal file
BIN
OpenKeychain/src/main/res/drawable-hdpi/ic_nfc_grey_24dp.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.0 KiB |
BIN
OpenKeychain/src/main/res/drawable-mdpi/ic_nfc_grey_24dp.png
Normal file
BIN
OpenKeychain/src/main/res/drawable-mdpi/ic_nfc_grey_24dp.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 701 B |
BIN
OpenKeychain/src/main/res/drawable-xhdpi/ic_nfc_grey_24dp.png
Normal file
BIN
OpenKeychain/src/main/res/drawable-xhdpi/ic_nfc_grey_24dp.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.3 KiB |
BIN
OpenKeychain/src/main/res/drawable-xxhdpi/ic_nfc_grey_24dp.png
Normal file
BIN
OpenKeychain/src/main/res/drawable-xxhdpi/ic_nfc_grey_24dp.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.0 KiB |
BIN
OpenKeychain/src/main/res/drawable-xxxhdpi/ic_nfc_grey_24dp.png
Normal file
BIN
OpenKeychain/src/main/res/drawable-xxxhdpi/ic_nfc_grey_24dp.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.7 KiB |
@ -99,14 +99,12 @@
|
|||||||
android:layout_weight="1" />
|
android:layout_weight="1" />
|
||||||
|
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
android:id="@+id/view_key_action_key_share"
|
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="?android:attr/listPreferredItemHeight"
|
android:layout_height="?android:attr/listPreferredItemHeight"
|
||||||
android:clickable="true"
|
|
||||||
android:background="?android:selectableItemBackground"
|
|
||||||
android:orientation="horizontal">
|
android:orientation="horizontal">
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
|
android:id="@+id/view_key_action_key_share"
|
||||||
android:paddingLeft="8dp"
|
android:paddingLeft="8dp"
|
||||||
android:paddingRight="8dp"
|
android:paddingRight="8dp"
|
||||||
android:textAppearance="?android:attr/textAppearanceMedium"
|
android:textAppearance="?android:attr/textAppearanceMedium"
|
||||||
@ -116,7 +114,25 @@
|
|||||||
android:layout_weight="1"
|
android:layout_weight="1"
|
||||||
android:drawableRight="@drawable/ic_share_grey_24dp"
|
android:drawableRight="@drawable/ic_share_grey_24dp"
|
||||||
android:drawablePadding="8dp"
|
android:drawablePadding="8dp"
|
||||||
android:gravity="center_vertical" />
|
android:gravity="center_vertical"
|
||||||
|
android:background="?android:selectableItemBackground"/>
|
||||||
|
|
||||||
|
<View
|
||||||
|
android:layout_width="1dip"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:gravity="right"
|
||||||
|
android:layout_marginBottom="8dp"
|
||||||
|
android:layout_marginTop="8dp"
|
||||||
|
android:background="?android:attr/listDivider" />
|
||||||
|
|
||||||
|
<ImageButton
|
||||||
|
android:id="@+id/view_key_action_key_nfc"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:padding="8dp"
|
||||||
|
android:src="@drawable/ic_nfc_grey_24dp"
|
||||||
|
android:layout_gravity="center_vertical"
|
||||||
|
android:background="?android:selectableItemBackground" />
|
||||||
|
|
||||||
<View
|
<View
|
||||||
android:layout_width="1dip"
|
android:layout_width="1dip"
|
||||||
@ -154,6 +170,7 @@
|
|||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
|
||||||
<View
|
<View
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="1dip"
|
android:layout_height="1dip"
|
||||||
|
Loading…
Reference in New Issue
Block a user