From a45aaa2277422b2bc1521f9237f0ec6c5684bd0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Wed, 6 May 2015 04:10:27 +0200 Subject: [PATCH] Fix import of keyring with pub+sec key with same key id --- .../keychain/service/CloudImportService.java | 19 ++- .../keychain/ui/ImportKeysActivity.java | 123 ++++++++++-------- 2 files changed, 80 insertions(+), 62 deletions(-) diff --git a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/CloudImportService.java b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/CloudImportService.java index 180109297..249586f6d 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/CloudImportService.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/service/CloudImportService.java @@ -50,18 +50,18 @@ import java.util.concurrent.atomic.AtomicBoolean; */ public class CloudImportService extends Service implements Progressable { - //required as extras from intent + // required as extras from intent public static final String EXTRA_MESSENGER = "messenger"; public static final String EXTRA_DATA = "data"; - //required by data bundle + // required by data bundle public static final String IMPORT_KEY_LIST = "import_key_list"; public static final String IMPORT_KEY_SERVER = "import_key_server"; // indicates a request to cancel the import public static final String ACTION_CANCEL = Constants.INTENT_PREFIX + "CANCEL"; - //tells the spawned threads whether the user has requested a cancel + // tells the spawned threads whether the user has requested a cancel private static AtomicBoolean mActionCancelled = new AtomicBoolean(false); @Override @@ -86,7 +86,7 @@ public class CloudImportService extends Service implements Progressable { public KeyImportAccumulator(int totalKeys) { mTotalKeys = totalKeys; - //ignore updates from ImportExportOperation for now + // ignore updates from ImportExportOperation for now mImportProgressable = new Progressable() { @Override public void setProgress(String message, int current, int total) { @@ -131,20 +131,17 @@ public class CloudImportService extends Service implements Progressable { mSecret += result.mSecret; long[] masterKeyIds = result.getImportedMasterKeyIds(); - for (int i = 0; i < masterKeyIds.length; i++) { - mImportedMasterKeyIds.add(masterKeyIds[i]); + for (long masterKeyId : masterKeyIds) { + mImportedMasterKeyIds.add(masterKeyId); } // if any key import has been cancelled, set result type to cancelled // resultType is added to in getConsolidatedKayImport to account for remaining factors mResultType |= result.getResult() & ImportKeyResult.RESULT_CANCELLED; - } /** * returns accumulated result of all imports so far - * - * @return */ public ImportKeyResult getConsolidatedImportKeyResult() { @@ -205,7 +202,7 @@ public class CloudImportService extends Service implements Progressable { Bundle data = extras.getBundle(EXTRA_DATA); final String keyServer = data.getString(IMPORT_KEY_SERVER); - //keyList being null (in case key list to be reaad from cache) is checked by importKeys + // keyList being null (in case key list to be reaad from cache) is checked by importKeys final ArrayList keyList = data.getParcelableArrayList(IMPORT_KEY_LIST); // Adding keys to the ThreadPoolExecutor takes time, we don't want to block the main thread @@ -225,7 +222,7 @@ public class CloudImportService extends Service implements Progressable { new ParcelableFileCache<>(this, "key_import.pcl"); int totKeys = 0; Iterator keyListIterator = null; - //either keyList or cache must be null, no guarantees otherwise + // either keyList or cache must be null, no guarantees otherwise if (keyList == null) {//export from cache, copied from ImportExportOperation.importKeyRings try { 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 4eb01a76c..4cba62d5b 100644 --- a/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java +++ b/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/ui/ImportKeysActivity.java @@ -35,6 +35,7 @@ import org.sufficientlysecure.keychain.keyimport.ImportKeysListEntry; import org.sufficientlysecure.keychain.keyimport.ParcelableKeyRing; import org.sufficientlysecure.keychain.operations.results.ImportKeyResult; import org.sufficientlysecure.keychain.operations.results.OperationResult; +import org.sufficientlysecure.keychain.service.KeychainIntentService; import org.sufficientlysecure.keychain.ui.base.BaseNfcActivity; import org.sufficientlysecure.keychain.service.CloudImportService; import org.sufficientlysecure.keychain.service.ServiceProgressHandler; @@ -345,60 +346,66 @@ public class ImportKeysActivity extends BaseNfcActivity { mListFragment.loadNew(loaderState); } + private void handleMessage(Message message) { + if (message.arg1 == ServiceProgressHandler.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 (ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN_RESULT.equals(getIntent().getAction()) + || ACTION_IMPORT_KEY_FROM_FILE_AND_RETURN.equals(getIntent().getAction())) { + Intent intent = new Intent(); + intent.putExtra(ImportKeyResult.EXTRA_RESULT, result); + ImportKeysActivity.this.setResult(RESULT_OK, intent); + ImportKeysActivity.this.finish(); + return; + } + if (ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN_TO_SERVICE.equals(getIntent().getAction())) { + ImportKeysActivity.this.setResult(RESULT_OK, mPendingIntentData); + ImportKeysActivity.this.finish(); + return; + } + + result.createNotify(ImportKeysActivity.this) + .show((ViewGroup) findViewById(R.id.import_snackbar)); + } + } + /** * Import keys with mImportData */ public void importKeys() { - // Message is received after importing is done in CloudImportService - ServiceProgressHandler saveHandler = new ServiceProgressHandler( - this, - getString(R.string.progress_importing), - ProgressDialog.STYLE_HORIZONTAL, - true, - ProgressDialogFragment.ServiceType.CLOUD_IMPORT) { - 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 (ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN_RESULT.equals(getIntent().getAction()) - || ACTION_IMPORT_KEY_FROM_FILE_AND_RETURN.equals(getIntent().getAction())) { - Intent intent = new Intent(); - intent.putExtra(ImportKeyResult.EXTRA_RESULT, result); - ImportKeysActivity.this.setResult(RESULT_OK, intent); - ImportKeysActivity.this.finish(); - return; - } - if (ACTION_IMPORT_KEY_FROM_KEYSERVER_AND_RETURN_TO_SERVICE.equals(getIntent().getAction())) { - ImportKeysActivity.this.setResult(RESULT_OK, mPendingIntentData); - ImportKeysActivity.this.finish(); - return; - } - - result.createNotify(ImportKeysActivity.this) - .show((ViewGroup) findViewById(R.id.import_snackbar)); - } - } - }; - ImportKeysListFragment.LoaderState ls = mListFragment.getLoaderState(); if (ls instanceof ImportKeysListFragment.BytesLoaderState) { Log.d(Constants.TAG, "importKeys started"); + ServiceProgressHandler serviceHandler = new ServiceProgressHandler( + this, + getString(R.string.progress_importing), + ProgressDialog.STYLE_HORIZONTAL, + true, + ProgressDialogFragment.ServiceType.KEYCHAIN_INTENT) { + public void handleMessage(Message message) { + // handle messages by standard KeychainIntentServiceHandler first + super.handleMessage(message); + + ImportKeysActivity.this.handleMessage(message); + } + }; + + // TODO: Currently not using CloudImport here due to https://github.com/open-keychain/open-keychain/issues/1221 // Send all information needed to service to import key in other thread - Intent intent = new Intent(this, CloudImportService.class); + Intent intent = new Intent(this, KeychainIntentService.class); + + intent.setAction(KeychainIntentService.ACTION_IMPORT_KEYRING); // fill values for this action Bundle data = new Bundle(); @@ -416,14 +423,14 @@ public class ImportKeysActivity extends BaseNfcActivity { new ParcelableFileCache<>(this, "key_import.pcl"); cache.writeCache(selectedEntries); - intent.putExtra(CloudImportService.EXTRA_DATA, data); + intent.putExtra(KeychainIntentService.EXTRA_DATA, data); // Create a new Messenger for the communication back - Messenger messenger = new Messenger(saveHandler); - intent.putExtra(CloudImportService.EXTRA_MESSENGER, messenger); + Messenger messenger = new Messenger(serviceHandler); + intent.putExtra(KeychainIntentService.EXTRA_MESSENGER, messenger); // show progress dialog - saveHandler.showProgressDialog(this); + serviceHandler.showProgressDialog(this); // start service with intent startService(intent); @@ -435,6 +442,20 @@ public class ImportKeysActivity extends BaseNfcActivity { } else if (ls instanceof ImportKeysListFragment.CloudLoaderState) { ImportKeysListFragment.CloudLoaderState sls = (ImportKeysListFragment.CloudLoaderState) ls; + ServiceProgressHandler serviceHandler = new ServiceProgressHandler( + this, + getString(R.string.progress_importing), + ProgressDialog.STYLE_HORIZONTAL, + true, + ProgressDialogFragment.ServiceType.CLOUD_IMPORT) { + public void handleMessage(Message message) { + // handle messages by standard KeychainIntentServiceHandler first + super.handleMessage(message); + + ImportKeysActivity.this.handleMessage(message); + } + }; + // Send all information needed to service to query keys in other thread Intent intent = new Intent(this, CloudImportService.class); @@ -459,11 +480,11 @@ public class ImportKeysActivity extends BaseNfcActivity { intent.putExtra(CloudImportService.EXTRA_DATA, data); // Create a new Messenger for the communication back - Messenger messenger = new Messenger(saveHandler); + Messenger messenger = new Messenger(serviceHandler); intent.putExtra(CloudImportService.EXTRA_MESSENGER, messenger); // show progress dialog - saveHandler.showProgressDialog(this); + serviceHandler.showProgressDialog(this); // start service with intent startService(intent);