fix loop bug in key selection

This commit is contained in:
Vincent Breitmoser
2016-12-31 19:12:13 +01:00
parent ac4bb3450f
commit 2ed485fd12
4 changed files with 19 additions and 19 deletions

View File

@@ -131,14 +131,12 @@ public class OpenPgpService extends Service {
} }
private KeyIdResult returnKeyIdsFromEmails(Intent data, String[] encryptionUserIds, boolean isOpportunistic) { private KeyIdResult returnKeyIdsFromEmails(Intent data, String[] encryptionUserIds, boolean isOpportunistic) {
boolean noUserIdsCheck = (encryptionUserIds == null || encryptionUserIds.length == 0); boolean hasUserIds = (encryptionUserIds != null && encryptionUserIds.length > 0);
boolean missingUserIdsCheck = false;
boolean duplicateUserIdsCheck = false;
HashSet<Long> keyIds = new HashSet<>(); HashSet<Long> keyIds = new HashSet<>();
ArrayList<String> missingEmails = new ArrayList<>(); ArrayList<String> missingEmails = new ArrayList<>();
ArrayList<String> duplicateEmails = new ArrayList<>(); ArrayList<String> duplicateEmails = new ArrayList<>();
if (!noUserIdsCheck) { if (hasUserIds) {
for (String rawUserId : encryptionUserIds) { for (String rawUserId : encryptionUserIds) {
OpenPgpUtils.UserId userId = KeyRing.splitUserId(rawUserId); OpenPgpUtils.UserId userId = KeyRing.splitUserId(rawUserId);
String email = userId.email != null ? userId.email : rawUserId; String email = userId.email != null ? userId.email : rawUserId;
@@ -151,19 +149,17 @@ public class OpenPgpService extends Service {
long id = cursor.getLong(cursor.getColumnIndex(KeyRings.MASTER_KEY_ID)); long id = cursor.getLong(cursor.getColumnIndex(KeyRings.MASTER_KEY_ID));
keyIds.add(id); keyIds.add(id);
} else { } else {
missingUserIdsCheck = true;
missingEmails.add(email); missingEmails.add(email);
Log.d(Constants.TAG, "user id missing"); Log.d(Constants.TAG, "user id missing");
} }
// another entry for this email -> two keys with the same email inside user id // another entry for this email -> two keys with the same email inside user id
if (cursor != null && cursor.moveToNext()) { if (cursor != null && cursor.moveToNext()) {
duplicateUserIdsCheck = true;
duplicateEmails.add(email); duplicateEmails.add(email);
// also pre-select // also pre-select
long id = cursor.getLong(cursor.getColumnIndex(KeyRings.MASTER_KEY_ID)); long id = cursor.getLong(cursor.getColumnIndex(KeyRings.MASTER_KEY_ID));
keyIds.add(id); keyIds.add(id);
Log.d(Constants.TAG, "more than one user id with the same email"); Log.d(Constants.TAG, "more than one user id with the same email");
} }
} finally { } finally {
if (cursor != null) { if (cursor != null) {
@@ -173,7 +169,9 @@ public class OpenPgpService extends Service {
} }
} }
if (isOpportunistic && (noUserIdsCheck || missingUserIdsCheck)) { boolean hasMissingUserIds = !missingEmails.isEmpty();
boolean hasDuplicateUserIds = !duplicateEmails.isEmpty();
if (isOpportunistic && (!hasUserIds || hasMissingUserIds)) {
Intent result = new Intent(); Intent result = new Intent();
result.putExtra(OpenPgpApi.RESULT_ERROR, result.putExtra(OpenPgpApi.RESULT_ERROR,
new OpenPgpError(OpenPgpError.OPPORTUNISTIC_MISSING_KEYS, "missing keys in opportunistic mode")); new OpenPgpError(OpenPgpError.OPPORTUNISTIC_MISSING_KEYS, "missing keys in opportunistic mode"));
@@ -181,12 +179,12 @@ public class OpenPgpService extends Service {
return new KeyIdResult(result); return new KeyIdResult(result);
} }
if (noUserIdsCheck || missingUserIdsCheck || duplicateUserIdsCheck) { if (!hasUserIds || hasMissingUserIds || hasDuplicateUserIds) {
// convert ArrayList<Long> to long[] // convert ArrayList<Long> to long[]
long[] keyIdsArray = getUnboxedLongArray(keyIds); long[] keyIdsArray = getUnboxedLongArray(keyIds);
ApiPendingIntentFactory piFactory = new ApiPendingIntentFactory(getBaseContext()); ApiPendingIntentFactory piFactory = new ApiPendingIntentFactory(getBaseContext());
PendingIntent pi = piFactory.createSelectPublicKeyPendingIntent(data, keyIdsArray, PendingIntent pi = piFactory.createSelectPublicKeyPendingIntent(data, keyIdsArray,
missingEmails, duplicateEmails, noUserIdsCheck); missingEmails, duplicateEmails, hasUserIds);
// return PendingIntent to be executed by client // return PendingIntent to be executed by client
Intent result = new Intent(); Intent result = new Intent();
@@ -197,7 +195,7 @@ public class OpenPgpService extends Service {
// everything was easy, we have exactly one key for every email // everything was easy, we have exactly one key for every email
if (keyIds.isEmpty()) { if (keyIds.isEmpty()) {
Log.e(Constants.TAG, "keyIdsArray.length == 0, should never happen!"); throw new AssertionError("keyIdsArray.length == 0, should never happen!");
} }
return new KeyIdResult(keyIds); return new KeyIdResult(keyIds);
@@ -321,9 +319,12 @@ public class OpenPgpService extends Service {
long[] keyIds; long[] keyIds;
{ {
HashSet<Long> encryptKeyIds = new HashSet<>(); HashSet<Long> encryptKeyIds = new HashSet<>();
boolean hasKeysFromSelectPubkeyActivity = data.hasExtra(OpenPgpApi.EXTRA_KEY_IDS_SELECTED);
// get key ids based on given user ids if (hasKeysFromSelectPubkeyActivity) {
if (data.hasExtra(OpenPgpApi.EXTRA_USER_IDS)) { for (long keyId : data.getLongArrayExtra(OpenPgpApi.EXTRA_KEY_IDS_SELECTED)) {
encryptKeyIds.add(keyId);
}
} else if (data.hasExtra(OpenPgpApi.EXTRA_USER_IDS)) {
String[] userIds = data.getStringArrayExtra(OpenPgpApi.EXTRA_USER_IDS); String[] userIds = data.getStringArrayExtra(OpenPgpApi.EXTRA_USER_IDS);
boolean isOpportunistic = data.getBooleanExtra(OpenPgpApi.EXTRA_OPPORTUNISTIC_ENCRYPTION, false); boolean isOpportunistic = data.getBooleanExtra(OpenPgpApi.EXTRA_OPPORTUNISTIC_ENCRYPTION, false);
// give params through to activity... // give params through to activity...

View File

@@ -103,7 +103,7 @@ public class RemoteSelectPubKeyActivity extends BaseActivity {
public void onClick(View v) { public void onClick(View v) {
// add key ids to params Bundle for new request // add key ids to params Bundle for new request
Intent resultData = extras.getParcelable(EXTRA_DATA); Intent resultData = extras.getParcelable(EXTRA_DATA);
resultData.putExtra(OpenPgpApi.EXTRA_KEY_IDS, resultData.putExtra(OpenPgpApi.EXTRA_KEY_IDS_SELECTED,
mSelectFragment.getSelectedMasterKeyIds()); mSelectFragment.getSelectedMasterKeyIds());
RemoteSelectPubKeyActivity.this.setResult(RESULT_OK, resultData); RemoteSelectPubKeyActivity.this.setResult(RESULT_OK, resultData);

View File

@@ -24,7 +24,6 @@ import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;