From 5b7f69473cde1dd3121d9cd9cbe15ba2cf9135fa Mon Sep 17 00:00:00 2001 From: Adithya Abraham Philip Date: Tue, 23 Jun 2015 19:51:19 +0530 Subject: [PATCH] migrated import to KeychainNewService --- .../ui/CreateKeyYubiKeyImportFragment.java | 61 +++++---- .../keychain/ui/ImportKeysProxyActivity.java | 111 +++++++--------- .../keychain/ui/KeyListFragment.java | 86 ++++++------- .../keychain/ui/SafeSlingerActivity.java | 121 ++++++++---------- .../keychain/ui/ViewKeyActivity.java | 87 ++++++------- 5 files changed, 218 insertions(+), 248 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyYubiKeyImportFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyYubiKeyImportFragment.java index 2ab8c5967..298efcc39 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyYubiKeyImportFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/CreateKeyYubiKeyImportFragment.java @@ -40,15 +40,19 @@ import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing; import org.sufficientlysecure.keychain.operations.results.DecryptVerifyResult; import org.sufficientlysecure.keychain.operations.results.ImportKeyResult; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; +import org.sufficientlysecure.keychain.service.ImportKeyringParcel; import org.sufficientlysecure.keychain.service.KeychainService; import org.sufficientlysecure.keychain.service.ServiceProgressHandler; import org.sufficientlysecure.keychain.ui.CreateKeyActivity.FragAction; import org.sufficientlysecure.keychain.ui.CreateKeyActivity.NfcListenerFragment; +import org.sufficientlysecure.keychain.ui.base.CryptoOperationFragment; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.util.Preferences; -public class CreateKeyYubiKeyImportFragment extends Fragment implements NfcListenerFragment { +public class CreateKeyYubiKeyImportFragment + extends CryptoOperationFragment + implements NfcListenerFragment { private static final String ARG_FINGERPRINT = "fingerprint"; public static final String ARG_AID = "aid"; @@ -64,6 +68,10 @@ public class CreateKeyYubiKeyImportFragment extends Fragment implements NfcListe private TextView vSerNo; private TextView vUserId; + // for CryptoOperationFragment key import + private String mKeyserver; + private ArrayList mKeyList; + public static Fragment createInstance(byte[] scannedFingerprints, byte[] nfcAid, String userId) { CreateKeyYubiKeyImportFragment frag = new CreateKeyYubiKeyImportFragment(); @@ -214,38 +222,20 @@ public class CreateKeyYubiKeyImportFragment extends Fragment implements NfcListe } }; - // Send all information needed to service to decrypt in other thread - Intent intent = new Intent(getActivity(), KeychainService.class); - - // fill values for this action - Bundle data = new Bundle(); - - intent.setAction(KeychainService.ACTION_IMPORT_KEYRING); - ArrayList keyList = new ArrayList<>(); keyList.add(new ParcelableKeyRing(mNfcFingerprint, null, null)); - data.putParcelableArrayList(KeychainService.IMPORT_KEY_LIST, keyList); + mKeyList = keyList; { Preferences prefs = Preferences.getPreferences(getActivity()); Preferences.CloudSearchPrefs cloudPrefs = new Preferences.CloudSearchPrefs(true, true, prefs.getPreferredKeyserver()); - data.putString(KeychainService.IMPORT_KEY_SERVER, cloudPrefs.keyserver); + mKeyserver = cloudPrefs.keyserver; } - intent.putExtra(KeychainService.EXTRA_DATA, data); + // TODO: PHILIP make the progress dialog show importing - // Create a new Messenger for the communication back - Messenger messenger = new Messenger(saveHandler); - intent.putExtra(KeychainService.EXTRA_MESSENGER, messenger); - - saveHandler.showProgressDialog( - getString(R.string.progress_importing), - ProgressDialog.STYLE_HORIZONTAL, false - ); - - // start service with intent - getActivity().startService(intent); + cryptoOperation(); } @@ -264,4 +254,29 @@ public class CreateKeyYubiKeyImportFragment extends Fragment implements NfcListe refreshSearch(); } + + @Override + protected ImportKeyringParcel createOperationInput() { + return new ImportKeyringParcel(mKeyList, mKeyserver); + } + + @Override + protected void onCryptoOperationSuccess(ImportKeyResult result) { + long[] masterKeyIds = result.getImportedMasterKeyIds(); + if (masterKeyIds.length == 0) { + super.onCryptoOperationError(result); + return; + } + + Intent intent = new Intent(getActivity(), ViewKeyActivity.class); + // use the imported masterKeyId, not the one from the yubikey, because + // that one might* just have been a subkey of the imported key + intent.setData(KeyRings.buildGenericKeyRingUri(masterKeyIds[0])); + intent.putExtra(ViewKeyActivity.EXTRA_DISPLAY_RESULT, result); + intent.putExtra(ViewKeyActivity.EXTRA_NFC_AID, mNfcAid); + intent.putExtra(ViewKeyActivity.EXTRA_NFC_USER_ID, mNfcUserId); + intent.putExtra(ViewKeyActivity.EXTRA_NFC_FINGERPRINTS, mNfcFingerprints); + startActivity(intent); + getActivity().finish(); + } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysProxyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysProxyActivity.java index 9f3beff43..0a7bd726a 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysProxyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysProxyActivity.java @@ -18,7 +18,6 @@ package org.sufficientlysecure.keychain.ui; import android.annotation.TargetApi; -import android.app.ProgressDialog; import android.content.Intent; import android.content.pm.ActivityInfo; import android.net.Uri; @@ -26,8 +25,6 @@ import android.nfc.NdefMessage; import android.nfc.NfcAdapter; import android.os.Build; import android.os.Bundle; -import android.os.Message; -import android.os.Messenger; import android.os.Parcelable; import android.support.v4.app.FragmentActivity; import android.widget.Toast; @@ -43,8 +40,8 @@ import org.sufficientlysecure.keychain.operations.results.ImportKeyResult; import org.sufficientlysecure.keychain.operations.results.OperationResult; import org.sufficientlysecure.keychain.operations.results.OperationResult.LogType; import org.sufficientlysecure.keychain.operations.results.SingletonResult; -import org.sufficientlysecure.keychain.service.KeychainService; -import org.sufficientlysecure.keychain.service.ServiceProgressHandler; +import org.sufficientlysecure.keychain.service.ImportKeyringParcel; +import org.sufficientlysecure.keychain.ui.base.CryptoOperationHelper; import org.sufficientlysecure.keychain.util.IntentIntegratorSupportV4; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.Preferences; @@ -55,7 +52,8 @@ import java.util.Locale; /** * Proxy activity (just a transparent content view) to scan QR Codes using the Barcode Scanner app */ -public class ImportKeysProxyActivity extends FragmentActivity { +public class ImportKeysProxyActivity extends FragmentActivity + implements CryptoOperationHelper.Callback { public static final String ACTION_QR_CODE_API = OpenKeychainIntents.IMPORT_KEY_FROM_QR_CODE; // implies activity returns scanned fingerprint as extra and does not import @@ -64,6 +62,11 @@ public class ImportKeysProxyActivity extends FragmentActivity { public static final String EXTRA_FINGERPRINT = "fingerprint"; + // for CryptoOperationHelper + private String mKeyserver; + private ArrayList mKeyList; + private CryptoOperationHelper mImportOpHelper; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -106,6 +109,10 @@ public class ImportKeysProxyActivity extends FragmentActivity { @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (mImportOpHelper != null) { + mImportOpHelper.cryptoOperation(); + } + if (requestCode == IntentIntegratorSupportV4.REQUEST_CODE) { IntentResult scanResult = IntentIntegratorSupportV4.parseActivityResult(requestCode, resultCode, data); @@ -205,75 +212,51 @@ public class ImportKeysProxyActivity extends FragmentActivity { private void startImportService(ArrayList keyRings) { - // Message is received after importing is done in KeychainService - ServiceProgressHandler serviceHandler = new ServiceProgressHandler(this) { - @Override - public void handleMessage(Message message) { - // handle messages by standard KeychainIntentServiceHandler first - super.handleMessage(message); - - if (message.arg1 == MessageStatus.OKAY.ordinal()) { - // get returned data bundle - Bundle returnData = message.getData(); - if (returnData == null) { - finish(); - return; - } - final ImportKeyResult result = - returnData.getParcelable(OperationResult.EXTRA_RESULT); - if (result == null) { - Log.e(Constants.TAG, "result == null"); - finish(); - return; - } - - if (!result.success()) { - // only return if no success... - Intent data = new Intent(); - data.putExtras(returnData); - returnResult(data); - return; - } - - Intent certifyIntent = new Intent(ImportKeysProxyActivity.this, - CertifyKeyActivity.class); - certifyIntent.putExtra(CertifyKeyActivity.EXTRA_RESULT, result); - certifyIntent.putExtra(CertifyKeyActivity.EXTRA_KEY_IDS, - result.getImportedMasterKeyIds()); - startActivityForResult(certifyIntent, 0); - } - } - }; - - // fill values for this action - Bundle data = new Bundle(); - // search config { Preferences prefs = Preferences.getPreferences(this); Preferences.CloudSearchPrefs cloudPrefs = new Preferences.CloudSearchPrefs(true, true, prefs.getPreferredKeyserver()); - data.putString(KeychainService.IMPORT_KEY_SERVER, cloudPrefs.keyserver); + mKeyserver = cloudPrefs.keyserver; } - data.putParcelableArrayList(KeychainService.IMPORT_KEY_LIST, keyRings); + mKeyList = keyRings; - // Send all information needed to service to query keys in other thread - Intent intent = new Intent(this, KeychainService.class); - intent.setAction(KeychainService.ACTION_IMPORT_KEYRING); - intent.putExtra(KeychainService.EXTRA_DATA, data); + mImportOpHelper = new CryptoOperationHelper<>(this, this, R.string.progress_importing); - // Create a new Messenger for the communication back - Messenger messenger = new Messenger(serviceHandler); - intent.putExtra(KeychainService.EXTRA_MESSENGER, messenger); + mImportOpHelper.cryptoOperation(); + } - // show progress dialog - serviceHandler.showProgressDialog( - getString(R.string.progress_importing), - ProgressDialog.STYLE_HORIZONTAL, true); - // start service with intent - startService(intent); + // CryptoOperationHelper.Callback methods + + @Override + public ImportKeyringParcel createOperationInput() { + return new ImportKeyringParcel(mKeyList, mKeyserver); + } + + @Override + public void onCryptoOperationSuccess(ImportKeyResult result) { + Intent certifyIntent = new Intent(this, CertifyKeyActivity.class); + certifyIntent.putExtra(CertifyKeyActivity.EXTRA_RESULT, result); + certifyIntent.putExtra(CertifyKeyActivity.EXTRA_KEY_IDS, + result.getImportedMasterKeyIds()); + startActivityForResult(certifyIntent, 0); + } + + @Override + public void onCryptoOperationCancelled() { + + } + + @Override + public void onCryptoOperationError(ImportKeyResult result) { + Bundle returnData = new Bundle(); + returnData.putParcelable(OperationResult.EXTRA_RESULT, result); + Intent data = new Intent(); + data.putExtras(returnData); + returnResult(data); + return; } /** diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java index 780558b27..b52640d86 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/KeyListFragment.java @@ -64,9 +64,11 @@ import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.provider.KeychainDatabase; import org.sufficientlysecure.keychain.provider.ProviderHelper; +import org.sufficientlysecure.keychain.service.ImportKeyringParcel; import org.sufficientlysecure.keychain.service.KeychainService; import org.sufficientlysecure.keychain.service.ServiceProgressHandler; import org.sufficientlysecure.keychain.service.PassphraseCacheService; +import org.sufficientlysecure.keychain.ui.base.CryptoOperationHelper; import org.sufficientlysecure.keychain.ui.dialog.DeleteKeyDialogFragment; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; import org.sufficientlysecure.keychain.ui.adapter.KeyAdapter; @@ -90,7 +92,8 @@ import se.emilsjolander.stickylistheaders.StickyListHeadersListView; */ public class KeyListFragment extends LoaderFragment implements SearchView.OnQueryTextListener, AdapterView.OnItemClickListener, - LoaderManager.LoaderCallbacks, FabContainer { + LoaderManager.LoaderCallbacks, FabContainer, + CryptoOperationHelper.Callback { static final int REQUEST_REPEAT_PASSPHRASE = 1; static final int REQUEST_ACTION = 2; @@ -107,6 +110,11 @@ public class KeyListFragment extends LoaderFragment private FloatingActionsMenu mFab; + // for CryptoOperationHelper import + private ArrayList mKeyList; + private String mKeyserver; + private CryptoOperationHelper mImportOpHelper; + // This ids for multiple key export. private ArrayList mIdsForRepeatAskPassphrase; // This index for remembering the number of master key. @@ -580,64 +588,22 @@ public class KeyListFragment extends LoaderFragment ParcelableKeyRing keyEntry = new ParcelableKeyRing(fingerprint, null, null); keyList.add(keyEntry); } + mKeyList = keyList; } finally { cursor.close(); } - ServiceProgressHandler serviceHandler = new ServiceProgressHandler(getActivity()) { - @Override - public void handleMessage(Message message) { - // handle messages by standard KeychainIntentServiceHandler first - super.handleMessage(message); - - if (message.arg1 == MessageStatus.OKAY.ordinal()) { - // get returned data bundle - Bundle returnData = message.getData(); - if (returnData == null) { - return; - } - final ImportKeyResult result = - returnData.getParcelable(OperationResult.EXTRA_RESULT); - if (result == null) { - Log.e(Constants.TAG, "result == null"); - return; - } - - result.createNotify(getActivity()).show(); - } - } - }; - - // Send all information needed to service to query keys in other thread - Intent intent = new Intent(getActivity(), KeychainService.class); - intent.setAction(KeychainService.ACTION_IMPORT_KEYRING); - - // fill values for this action - Bundle data = new Bundle(); - // search config { Preferences prefs = Preferences.getPreferences(getActivity()); Preferences.CloudSearchPrefs cloudPrefs = new Preferences.CloudSearchPrefs(true, true, prefs.getPreferredKeyserver()); - data.putString(KeychainService.IMPORT_KEY_SERVER, cloudPrefs.keyserver); + mKeyserver = cloudPrefs.keyserver; } - data.putParcelableArrayList(KeychainService.IMPORT_KEY_LIST, keyList); - - intent.putExtra(KeychainService.EXTRA_DATA, data); - - // Create a new Messenger for the communication back - Messenger messenger = new Messenger(serviceHandler); - intent.putExtra(KeychainService.EXTRA_MESSENGER, messenger); - - // show progress dialog - serviceHandler.showProgressDialog( - getString(R.string.progress_updating), - ProgressDialog.STYLE_HORIZONTAL, true); - - // start service with intent - getActivity().startService(intent); + mImportOpHelper = new CryptoOperationHelper<>(this, + this, R.string.progress_updating); + mImportOpHelper.cryptoOperation(); } private void consolidate() { @@ -724,6 +690,9 @@ public class KeyListFragment extends LoaderFragment @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { + if (mImportOpHelper != null) { + mImportOpHelper.handleActivityResult(requestCode, resultCode, data); + } if (requestCode == REQUEST_REPEAT_PASSPHRASE) { if (resultCode != Activity.RESULT_OK) { return; @@ -769,6 +738,27 @@ public class KeyListFragment extends LoaderFragment anim.start(); } + // CryptoOperationHelper.Callback methods + @Override + public ImportKeyringParcel createOperationInput() { + return new ImportKeyringParcel(mKeyList, mKeyserver); + } + + @Override + public void onCryptoOperationSuccess(ImportKeyResult result) { + result.createNotify(getActivity()).show(); + } + + @Override + public void onCryptoOperationCancelled() { + + } + + @Override + public void onCryptoOperationError(ImportKeyResult result) { + result.createNotify(getActivity()).show(); + } + public class KeyListAdapter extends KeyAdapter implements StickyListHeadersAdapter { private HashMap mSelection = new HashMap<>(); diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SafeSlingerActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SafeSlingerActivity.java index 7408135ae..97c44aabf 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SafeSlingerActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/SafeSlingerActivity.java @@ -38,9 +38,11 @@ import org.sufficientlysecure.keychain.operations.results.ImportKeyResult; import org.sufficientlysecure.keychain.operations.results.OperationResult; import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.ProviderHelper; +import org.sufficientlysecure.keychain.service.ImportKeyringParcel; import org.sufficientlysecure.keychain.service.KeychainService; import org.sufficientlysecure.keychain.ui.base.BaseActivity; import org.sufficientlysecure.keychain.service.ServiceProgressHandler; +import org.sufficientlysecure.keychain.ui.base.CryptoOperationHelper; import org.sufficientlysecure.keychain.ui.util.Notify; import org.sufficientlysecure.keychain.util.Log; import org.sufficientlysecure.keychain.util.ParcelableFileCache; @@ -52,7 +54,8 @@ import edu.cmu.cylab.starslinger.exchange.ExchangeActivity; import edu.cmu.cylab.starslinger.exchange.ExchangeConfig; @TargetApi(Build.VERSION_CODES.HONEYCOMB) -public class SafeSlingerActivity extends BaseActivity { +public class SafeSlingerActivity extends BaseActivity + implements CryptoOperationHelper.Callback { private static final int REQUEST_CODE_SAFE_SLINGER = 211; @@ -61,6 +64,12 @@ public class SafeSlingerActivity extends BaseActivity { private long mMasterKeyId; private int mSelectedNumber = 2; + // for CryptoOperationHelper + private ArrayList mKeyList; + private String mKeyserver; + private CryptoOperationHelper mOperationHelper; + + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -116,65 +125,17 @@ public class SafeSlingerActivity extends BaseActivity { @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { + if (mOperationHelper != null) { + mOperationHelper.handleActivityResult(requestCode, resultCode, data); + } + if (requestCode == REQUEST_CODE_SAFE_SLINGER) { if (resultCode == ExchangeActivity.RESULT_EXCHANGE_CANCELED) { return; } - final FragmentActivity activity = SafeSlingerActivity.this; - - // Message is received after importing is done in KeychainService - ServiceProgressHandler saveHandler = new ServiceProgressHandler(activity) { - @Override - public void handleMessage(Message message) { - // handle messages by standard KeychainIntentServiceHandler first - super.handleMessage(message); - - if (message.arg1 == MessageStatus.OKAY.ordinal()) { - // get returned data bundle - Bundle returnData = message.getData(); - if (returnData == null) { - return; - } - final ImportKeyResult result = - returnData.getParcelable(OperationResult.EXTRA_RESULT); - if (result == null) { - Log.e(Constants.TAG, "result == null"); - return; - } - - if (!result.success()) { -// result.createNotify(activity).show(); - // only return if no success... - Intent data = new Intent(); - data.putExtras(returnData); - setResult(RESULT_OK, data); - finish(); - return; - } - -// if (mExchangeMasterKeyId == null) { -// return; -// } - - Intent certifyIntent = new Intent(activity, CertifyKeyActivity.class); - certifyIntent.putExtra(CertifyKeyActivity.EXTRA_RESULT, result); - certifyIntent.putExtra(CertifyKeyActivity.EXTRA_KEY_IDS, result.getImportedMasterKeyIds()); - certifyIntent.putExtra(CertifyKeyActivity.EXTRA_CERTIFY_KEY_ID, mMasterKeyId); - startActivityForResult(certifyIntent, 0); - -// mExchangeMasterKeyId = null; - } - } - }; - Log.d(Constants.TAG, "importKeys started"); - // Send all information needed to service to import key in other thread - Intent intent = new Intent(activity, KeychainService.class); - - intent.setAction(KeychainService.ACTION_IMPORT_KEYRING); - // instead of giving the entries by Intent extra, cache them into a // file to prevent Java Binder problems on heavy imports // read FileImportCache for more info. @@ -185,28 +146,18 @@ public class SafeSlingerActivity extends BaseActivity { // We parcel this iteratively into a file - anything we can // display here, we should be able to import. ParcelableFileCache cache = - new ParcelableFileCache<>(activity, "key_import.pcl"); + new ParcelableFileCache<>(this, "key_import.pcl"); cache.writeCache(it.size(), it.iterator()); - // fill values for this action - Bundle bundle = new Bundle(); - intent.putExtra(KeychainService.EXTRA_DATA, bundle); + mOperationHelper = + new CryptoOperationHelper(this, this, R.string.progress_importing); - // Create a new Messenger for the communication back - Messenger messenger = new Messenger(saveHandler); - intent.putExtra(KeychainService.EXTRA_MESSENGER, messenger); - - // show progress dialog - saveHandler.showProgressDialog( - getString(R.string.progress_importing), - ProgressDialog.STYLE_HORIZONTAL, true - ); - - // start service with intent - activity.startService(intent); + mKeyList = null; + mKeyserver = null; + mOperationHelper.cryptoOperation(); } catch (IOException e) { Log.e(Constants.TAG, "Problem writing cache file", e); - Notify.create(activity, "Problem writing cache file!", Notify.Style.ERROR).show(); + Notify.create(this, "Problem writing cache file!", Notify.Style.ERROR).show(); } } else { // give everything else down to KeyListActivity! @@ -233,4 +184,34 @@ public class SafeSlingerActivity extends BaseActivity { return list; } + // CryptoOperationHelper.Callback functions + + @Override + public ImportKeyringParcel createOperationInput() { + return new ImportKeyringParcel(mKeyList, mKeyserver); + } + + @Override + public void onCryptoOperationSuccess(ImportKeyResult result) { + Intent certifyIntent = new Intent(this, CertifyKeyActivity.class); + certifyIntent.putExtra(CertifyKeyActivity.EXTRA_RESULT, result); + certifyIntent.putExtra(CertifyKeyActivity.EXTRA_KEY_IDS, result.getImportedMasterKeyIds()); + certifyIntent.putExtra(CertifyKeyActivity.EXTRA_CERTIFY_KEY_ID, mMasterKeyId); + startActivityForResult(certifyIntent, 0); + } + + @Override + public void onCryptoOperationCancelled() { + + } + + @Override + public void onCryptoOperationError(ImportKeyResult result) { + Bundle returnData = new Bundle(); + returnData.putParcelable(OperationResult.EXTRA_RESULT, result); + Intent data = new Intent(); + data.putExtras(returnData); + setResult(RESULT_OK, data); + finish(); + } } diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java index 5f6a32e5a..62ee89207 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java @@ -65,11 +65,13 @@ import org.sufficientlysecure.keychain.provider.CachedPublicKeyRing; import org.sufficientlysecure.keychain.provider.KeychainContract; import org.sufficientlysecure.keychain.provider.KeychainContract.KeyRings; import org.sufficientlysecure.keychain.provider.ProviderHelper; +import org.sufficientlysecure.keychain.service.ImportKeyringParcel; import org.sufficientlysecure.keychain.service.KeychainService; import org.sufficientlysecure.keychain.service.ServiceProgressHandler; import org.sufficientlysecure.keychain.service.ServiceProgressHandler.MessageStatus; import org.sufficientlysecure.keychain.service.PassphraseCacheService; import org.sufficientlysecure.keychain.ui.base.BaseNfcActivity; +import org.sufficientlysecure.keychain.ui.base.CryptoOperationHelper; import org.sufficientlysecure.keychain.ui.dialog.DeleteKeyDialogFragment; import org.sufficientlysecure.keychain.ui.util.FormattingUtils; import org.sufficientlysecure.keychain.ui.util.KeyFormattingUtils; @@ -85,11 +87,13 @@ import org.sufficientlysecure.keychain.util.NfcHelper; import org.sufficientlysecure.keychain.util.Preferences; import java.io.IOException; +import java.lang.reflect.Array; import java.util.ArrayList; import java.util.HashMap; public class ViewKeyActivity extends BaseNfcActivity implements - LoaderManager.LoaderCallbacks { + LoaderManager.LoaderCallbacks, + CryptoOperationHelper.Callback { public static final String EXTRA_NFC_USER_ID = "nfc_user_id"; public static final String EXTRA_NFC_AID = "nfc_aid"; @@ -105,6 +109,11 @@ public class ViewKeyActivity extends BaseNfcActivity implements protected Uri mDataUri; + // For CryptoOperationHelper.Callback + private String mKeyserver; + private ArrayList mKeyList; + private CryptoOperationHelper mOperationHelper; + private TextView mName; private TextView mStatusText; private ImageView mStatusImage; @@ -396,7 +405,7 @@ public class ViewKeyActivity extends BaseNfcActivity implements private void certifyImmediate() { 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); } @@ -486,6 +495,10 @@ public class ViewKeyActivity extends BaseNfcActivity implements @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (mOperationHelper != null) { + mOperationHelper.handleActivityResult(requestCode, resultCode, data); + } + if (requestCode == REQUEST_QR_FINGERPRINT && resultCode == Activity.RESULT_OK) { // If there is an EXTRA_RESULT, that's an error. Just show it. @@ -651,56 +664,20 @@ public class ViewKeyActivity extends BaseNfcActivity implements ParcelableKeyRing keyEntry = new ParcelableKeyRing(fingerprint, null, null); ArrayList entries = new ArrayList<>(); entries.add(keyEntry); - - // Message is received after importing is done in KeychainService - ServiceProgressHandler serviceHandler = new ServiceProgressHandler(this) { - @Override - public void handleMessage(Message message) { - // handle messages by standard KeychainIntentServiceHandler first - super.handleMessage(message); - - if (message.arg1 == MessageStatus.OKAY.ordinal()) { - // get returned data bundle - Bundle returnData = message.getData(); - - mIsRefreshing = false; - - if (returnData == null) { - finish(); - return; - } - final ImportKeyResult result = - returnData.getParcelable(OperationResult.EXTRA_RESULT); - result.createNotify(ViewKeyActivity.this).show(); - } - } - }; - - // fill values for this action - Bundle data = new Bundle(); + mKeyList = entries; // search config { Preferences prefs = Preferences.getPreferences(this); Preferences.CloudSearchPrefs cloudPrefs = new Preferences.CloudSearchPrefs(true, true, prefs.getPreferredKeyserver()); - data.putString(KeychainService.IMPORT_KEY_SERVER, cloudPrefs.keyserver); + mKeyserver = cloudPrefs.keyserver; } - data.putParcelableArrayList(KeychainService.IMPORT_KEY_LIST, entries); - - // Send all information needed to service to query keys in other thread - Intent intent = new Intent(this, KeychainService.class); - intent.setAction(KeychainService.ACTION_IMPORT_KEYRING); - intent.putExtra(KeychainService.EXTRA_DATA, data); - - // Create a new Messenger for the communication back - Messenger messenger = new Messenger(serviceHandler); - intent.putExtra(KeychainService.EXTRA_MESSENGER, messenger); - - // start service with intent - startService(intent); + mOperationHelper = new CryptoOperationHelper<>( + this, this, R.string.progress_importing); + mOperationHelper.cryptoOperation(); } private void editKey(Uri dataUri) { @@ -986,4 +963,28 @@ public class ViewKeyActivity extends BaseNfcActivity implements public void onLoaderReset(Loader loader) { } + + // CryptoOperationHelper.Callback functions + + @Override + public ImportKeyringParcel createOperationInput() { + return new ImportKeyringParcel(mKeyList, mKeyserver); + } + + @Override + public void onCryptoOperationSuccess(ImportKeyResult result) { + mIsRefreshing = false; + result.createNotify(this).show(); + } + + @Override + public void onCryptoOperationCancelled() { + mIsRefreshing = false; + } + + @Override + public void onCryptoOperationError(ImportKeyResult result) { + mIsRefreshing = false; + result.createNotify(this).show(); + } } \ No newline at end of file