diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyHelper.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyHelper.java index e14c8bbdf..7ab7aae3f 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyHelper.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/pgp/PgpKeyHelper.java @@ -539,9 +539,23 @@ public class PgpKeyHelper { * @return array with naming (0), email (1), comment (2) */ public static String[] splitUserId(String userId) { - String[] result = new String[] { "", "", "" }; + String[] result = new String[] { null, null, null }; - Pattern withComment = Pattern.compile("^(.*) \\((.*)\\) <(.*)>$"); + if (userId == null || userId.equals("")) { + return result; + } + + /* + * User ID matching: + * http://fiddle.re/t4p6f + * + * test cases: + * "Max Mustermann (this is a comment) " + * "Max Mustermann " + * "Max Mustermann (this is a comment)" + * "Max Mustermann [this is nothing]" + */ + Pattern withComment = Pattern.compile("^(.*?)(?: \\((.*)\\))?(?: <(.*)>)?$"); Matcher matcher = withComment.matcher(userId); if (matcher.matches()) { result[0] = matcher.group(1); @@ -550,13 +564,6 @@ public class PgpKeyHelper { return result; } - Pattern withoutComment = Pattern.compile("^(.*) <(.*)>$"); - matcher = withoutComment.matcher(userId); - if (matcher.matches()) { - result[0] = matcher.group(1); - result[1] = matcher.group(2); - return result; - } return result; } diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java index bb43719a6..8bb93e32a 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/ViewKeyActivity.java @@ -300,7 +300,7 @@ public class ViewKeyActivity extends SherlockFragmentActivity implements // get name, email, and comment from USER_ID String[] mainUserId = PgpKeyHelper.splitUserId(data .getString(KEYRING_INDEX_USER_ID)); - if (mainUserId[0] != null && mainUserId[0].length() > 0) { + if (mainUserId[0] != null) { setTitle(mainUserId[0]); mName.setText(mainUserId[0]); } else { diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysAdapter.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysAdapter.java index 6229e5202..8170defe4 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysAdapter.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/ImportKeysAdapter.java @@ -93,26 +93,24 @@ public class ImportKeysAdapter extends ArrayAdapter { // main user id String userId = entry.userIds.get(0); - if (userId != null) { - String[] userIdSplit = PgpKeyHelper.splitUserId(userId); + String[] userIdSplit = PgpKeyHelper.splitUserId(userId); - // name - if (userIdSplit[0] != null && userIdSplit[0].length() > 0) { - // show red user id if it is a secret key - if (entry.secretKey) { - userIdSplit[0] = mActivity.getString(R.string.secret_key) + " " + userIdSplit[0]; - mainUserId.setTextColor(Color.RED); - } - mainUserId.setText(userIdSplit[0]); + // name + if (userIdSplit[0] != null) { + // show red user id if it is a secret key + if (entry.secretKey) { + userIdSplit[0] = mActivity.getString(R.string.secret_key) + " " + userIdSplit[0]; + mainUserId.setTextColor(Color.RED); } + mainUserId.setText(userIdSplit[0]); + } - // email - if (userIdSplit[1] != null && userIdSplit[1].length() > 0) { - mainUserIdRest.setText(userIdSplit[1]); - mainUserIdRest.setVisibility(View.VISIBLE); - } else { - mainUserIdRest.setVisibility(View.GONE); - } + // email + if (userIdSplit[1] != null) { + mainUserIdRest.setText(userIdSplit[1]); + mainUserIdRest.setVisibility(View.VISIBLE); + } else { + mainUserIdRest.setVisibility(View.GONE); } keyId.setText(entry.hexKeyId); diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/KeyListPublicAdapter.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/KeyListPublicAdapter.java index d852c16a8..f0e926655 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/KeyListPublicAdapter.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/KeyListPublicAdapter.java @@ -89,16 +89,13 @@ public class KeyListPublicAdapter extends CursorAdapter implements StickyListHea mainUserIdRest.setText(""); String userId = cursor.getString(mIndexUserId); - if (userId != null) { - String[] userIdSplit = PgpKeyHelper.splitUserId(userId); + String[] userIdSplit = PgpKeyHelper.splitUserId(userId); - if (userIdSplit[0] != null && userIdSplit[0].length() > 0) { - mainUserId.setText(userIdSplit[0]); - } - - if (userIdSplit[1] != null && userIdSplit[1].length() > 0) { - mainUserIdRest.setText(userIdSplit[1]); - } + if (userIdSplit[0] != null) { + mainUserId.setText(userIdSplit[0]); + } + if (userIdSplit[1] != null) { + mainUserIdRest.setText(userIdSplit[1]); } } diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/KeyListSecretAdapter.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/KeyListSecretAdapter.java index ec087b46c..ce9b48bff 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/KeyListSecretAdapter.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/KeyListSecretAdapter.java @@ -76,16 +76,13 @@ public class KeyListSecretAdapter extends CursorAdapter { mainUserIdRest.setText(""); String userId = cursor.getString(mIndexUserId); - if (userId != null) { - String[] userIdSplit = PgpKeyHelper.splitUserId(userId); + String[] userIdSplit = PgpKeyHelper.splitUserId(userId); - if (userIdSplit[0] != null && userIdSplit[0].length() > 0) { - mainUserId.setText(userIdSplit[0]); - } - - if (userIdSplit[1] != null && userIdSplit[1].length() > 0) { - mainUserIdRest.setText(userIdSplit[1]); - } + if (userIdSplit[0] != null) { + mainUserId.setText(userIdSplit[0]); + } + if (userIdSplit[1] != null) { + mainUserIdRest.setText(userIdSplit[1]); } } diff --git a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SelectKeyCursorAdapter.java b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SelectKeyCursorAdapter.java index 54c334fd5..7e01faf9b 100644 --- a/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SelectKeyCursorAdapter.java +++ b/OpenPGP-Keychain/src/main/java/org/sufficientlysecure/keychain/ui/adapter/SelectKeyCursorAdapter.java @@ -105,16 +105,13 @@ public class SelectKeyCursorAdapter extends CursorAdapter { status.setText(R.string.unknown_status); String userId = cursor.getString(mIndexUserId); - if (userId != null) { - String[] userIdSplit = PgpKeyHelper.splitUserId(userId); + String[] userIdSplit = PgpKeyHelper.splitUserId(userId); - if (userIdSplit[0] != null && userIdSplit[0].length() > 0) { - mainUserId.setText(userIdSplit[0]); - } - - if (userIdSplit[1] != null && userIdSplit[1].length() > 0) { - mainUserIdRest.setText(userIdSplit[1]); - } + if (userIdSplit[0] != null) { + mainUserId.setText(userIdSplit[0]); + } + if (userIdSplit[1] != null) { + mainUserIdRest.setText(userIdSplit[1]); } long masterKeyId = cursor.getLong(mIndexMasterKeyId);