Fix and simplify user id parsing
This commit is contained in:
@@ -539,9 +539,23 @@ public class PgpKeyHelper {
|
|||||||
* @return array with naming (0), email (1), comment (2)
|
* @return array with naming (0), email (1), comment (2)
|
||||||
*/
|
*/
|
||||||
public static String[] splitUserId(String userId) {
|
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@example.com>"
|
||||||
|
* "Max Mustermann <max@example.com>"
|
||||||
|
* "Max Mustermann (this is a comment)"
|
||||||
|
* "Max Mustermann [this is nothing]"
|
||||||
|
*/
|
||||||
|
Pattern withComment = Pattern.compile("^(.*?)(?: \\((.*)\\))?(?: <(.*)>)?$");
|
||||||
Matcher matcher = withComment.matcher(userId);
|
Matcher matcher = withComment.matcher(userId);
|
||||||
if (matcher.matches()) {
|
if (matcher.matches()) {
|
||||||
result[0] = matcher.group(1);
|
result[0] = matcher.group(1);
|
||||||
@@ -550,13 +564,6 @@ public class PgpKeyHelper {
|
|||||||
return result;
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -300,7 +300,7 @@ public class ViewKeyActivity extends SherlockFragmentActivity implements
|
|||||||
// get name, email, and comment from USER_ID
|
// get name, email, and comment from USER_ID
|
||||||
String[] mainUserId = PgpKeyHelper.splitUserId(data
|
String[] mainUserId = PgpKeyHelper.splitUserId(data
|
||||||
.getString(KEYRING_INDEX_USER_ID));
|
.getString(KEYRING_INDEX_USER_ID));
|
||||||
if (mainUserId[0] != null && mainUserId[0].length() > 0) {
|
if (mainUserId[0] != null) {
|
||||||
setTitle(mainUserId[0]);
|
setTitle(mainUserId[0]);
|
||||||
mName.setText(mainUserId[0]);
|
mName.setText(mainUserId[0]);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -93,26 +93,24 @@ public class ImportKeysAdapter extends ArrayAdapter<ImportKeysListEntry> {
|
|||||||
|
|
||||||
// main user id
|
// main user id
|
||||||
String userId = entry.userIds.get(0);
|
String userId = entry.userIds.get(0);
|
||||||
if (userId != null) {
|
String[] userIdSplit = PgpKeyHelper.splitUserId(userId);
|
||||||
String[] userIdSplit = PgpKeyHelper.splitUserId(userId);
|
|
||||||
|
|
||||||
// name
|
// name
|
||||||
if (userIdSplit[0] != null && userIdSplit[0].length() > 0) {
|
if (userIdSplit[0] != null) {
|
||||||
// show red user id if it is a secret key
|
// show red user id if it is a secret key
|
||||||
if (entry.secretKey) {
|
if (entry.secretKey) {
|
||||||
userIdSplit[0] = mActivity.getString(R.string.secret_key) + " " + userIdSplit[0];
|
userIdSplit[0] = mActivity.getString(R.string.secret_key) + " " + userIdSplit[0];
|
||||||
mainUserId.setTextColor(Color.RED);
|
mainUserId.setTextColor(Color.RED);
|
||||||
}
|
|
||||||
mainUserId.setText(userIdSplit[0]);
|
|
||||||
}
|
}
|
||||||
|
mainUserId.setText(userIdSplit[0]);
|
||||||
|
}
|
||||||
|
|
||||||
// email
|
// email
|
||||||
if (userIdSplit[1] != null && userIdSplit[1].length() > 0) {
|
if (userIdSplit[1] != null) {
|
||||||
mainUserIdRest.setText(userIdSplit[1]);
|
mainUserIdRest.setText(userIdSplit[1]);
|
||||||
mainUserIdRest.setVisibility(View.VISIBLE);
|
mainUserIdRest.setVisibility(View.VISIBLE);
|
||||||
} else {
|
} else {
|
||||||
mainUserIdRest.setVisibility(View.GONE);
|
mainUserIdRest.setVisibility(View.GONE);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
keyId.setText(entry.hexKeyId);
|
keyId.setText(entry.hexKeyId);
|
||||||
|
|||||||
@@ -89,16 +89,13 @@ public class KeyListPublicAdapter extends CursorAdapter implements StickyListHea
|
|||||||
mainUserIdRest.setText("");
|
mainUserIdRest.setText("");
|
||||||
|
|
||||||
String userId = cursor.getString(mIndexUserId);
|
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) {
|
if (userIdSplit[0] != null) {
|
||||||
mainUserId.setText(userIdSplit[0]);
|
mainUserId.setText(userIdSplit[0]);
|
||||||
}
|
}
|
||||||
|
if (userIdSplit[1] != null) {
|
||||||
if (userIdSplit[1] != null && userIdSplit[1].length() > 0) {
|
mainUserIdRest.setText(userIdSplit[1]);
|
||||||
mainUserIdRest.setText(userIdSplit[1]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -76,16 +76,13 @@ public class KeyListSecretAdapter extends CursorAdapter {
|
|||||||
mainUserIdRest.setText("");
|
mainUserIdRest.setText("");
|
||||||
|
|
||||||
String userId = cursor.getString(mIndexUserId);
|
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) {
|
if (userIdSplit[0] != null) {
|
||||||
mainUserId.setText(userIdSplit[0]);
|
mainUserId.setText(userIdSplit[0]);
|
||||||
}
|
}
|
||||||
|
if (userIdSplit[1] != null) {
|
||||||
if (userIdSplit[1] != null && userIdSplit[1].length() > 0) {
|
mainUserIdRest.setText(userIdSplit[1]);
|
||||||
mainUserIdRest.setText(userIdSplit[1]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -105,16 +105,13 @@ public class SelectKeyCursorAdapter extends CursorAdapter {
|
|||||||
status.setText(R.string.unknown_status);
|
status.setText(R.string.unknown_status);
|
||||||
|
|
||||||
String userId = cursor.getString(mIndexUserId);
|
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) {
|
if (userIdSplit[0] != null) {
|
||||||
mainUserId.setText(userIdSplit[0]);
|
mainUserId.setText(userIdSplit[0]);
|
||||||
}
|
}
|
||||||
|
if (userIdSplit[1] != null) {
|
||||||
if (userIdSplit[1] != null && userIdSplit[1].length() > 0) {
|
mainUserIdRest.setText(userIdSplit[1]);
|
||||||
mainUserIdRest.setText(userIdSplit[1]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
long masterKeyId = cursor.getLong(mIndexMasterKeyId);
|
long masterKeyId = cursor.getLong(mIndexMasterKeyId);
|
||||||
|
|||||||
Reference in New Issue
Block a user