Rewrite ShareKeyHelper
This commit is contained in:
@@ -45,51 +45,24 @@ import java.security.NoSuchAlgorithmException;
|
||||
|
||||
public class ShareKeyHelper {
|
||||
|
||||
private static String getShareKeyContent(Activity activity, UnifiedKeyInfo unifiedKeyInfo, boolean asSshKey)
|
||||
throws KeyRepository.NotFoundException, IOException, PgpGeneralException, NoSuchAlgorithmException {
|
||||
private static String getKeyContent(UnifiedKeyInfo unifiedKeyInfo, KeyRepository keyRepository)
|
||||
throws KeyRepository.NotFoundException, IOException {
|
||||
|
||||
KeyRepository keyRepository = KeyRepository.create(activity);
|
||||
return keyRepository.getPublicKeyRingAsArmoredString(unifiedKeyInfo.master_key_id());
|
||||
}
|
||||
|
||||
private static String getSshKeyContent(UnifiedKeyInfo unifiedKeyInfo, KeyRepository keyRepository)
|
||||
throws KeyRepository.NotFoundException, PgpGeneralException, NoSuchAlgorithmException {
|
||||
|
||||
String content;
|
||||
if (asSshKey) {
|
||||
long authSubKeyId = unifiedKeyInfo.has_auth_key_int();
|
||||
CanonicalizedPublicKey publicKey = keyRepository.getCanonicalizedPublicKeyRing(unifiedKeyInfo.master_key_id())
|
||||
.getPublicKey(authSubKeyId);
|
||||
SshPublicKey sshPublicKey = new SshPublicKey(publicKey);
|
||||
content = sshPublicKey.getEncodedKey();
|
||||
} else {
|
||||
content = keyRepository.getPublicKeyRingAsArmoredString(unifiedKeyInfo.master_key_id());
|
||||
}
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
private static void shareKey(Activity activity, UnifiedKeyInfo unifiedKeyInfo, boolean toClipboard, boolean asSshKey) {
|
||||
if (activity == null || unifiedKeyInfo == null) {
|
||||
return;
|
||||
}
|
||||
if (asSshKey && !unifiedKeyInfo.has_auth_key()) {
|
||||
Notify.create(activity, R.string.authentication_subkey_not_found, Notify.Style.ERROR).show();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
String content = getShareKeyContent(activity, unifiedKeyInfo, asSshKey);
|
||||
|
||||
if (toClipboard) {
|
||||
ClipboardManager clipMan = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
if (clipMan == null) {
|
||||
Notify.create(activity, R.string.error_clipboard_copy, Notify.Style.ERROR).show();
|
||||
return;
|
||||
}
|
||||
|
||||
ClipData clip = ClipData.newPlainText(Constants.CLIPBOARD_LABEL, content);
|
||||
clipMan.setPrimaryClip(clip);
|
||||
|
||||
Notify.create(activity, R.string.key_copied_to_clipboard, Notify.Style.OK).show();
|
||||
return;
|
||||
|
||||
return sshPublicKey.getEncodedKey();
|
||||
}
|
||||
|
||||
private static void shareKeyIntent(Activity activity, UnifiedKeyInfo unifiedKeyInfo, String content) throws IOException {
|
||||
// let user choose application
|
||||
Intent sendIntent = new Intent(Intent.ACTION_SEND);
|
||||
sendIntent.setType(Constants.MIME_TYPE_KEYS);
|
||||
@@ -125,6 +98,58 @@ public class ShareKeyHelper {
|
||||
Intent shareChooser = Intent.createChooser(sendIntent, title);
|
||||
|
||||
activity.startActivity(shareChooser);
|
||||
}
|
||||
|
||||
private static void shareKeyToClipBoard(Activity activity, String content) {
|
||||
ClipboardManager clipMan = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
|
||||
if (clipMan == null) {
|
||||
Notify.create(activity, R.string.error_clipboard_copy, Notify.Style.ERROR).show();
|
||||
return;
|
||||
}
|
||||
|
||||
ClipData clip = ClipData.newPlainText(Constants.CLIPBOARD_LABEL, content);
|
||||
clipMan.setPrimaryClip(clip);
|
||||
|
||||
Notify.create(activity, R.string.key_copied_to_clipboard, Notify.Style.OK).show();
|
||||
}
|
||||
|
||||
private static void shareKey(Activity activity, UnifiedKeyInfo unifiedKeyInfo, boolean toClipboard) {
|
||||
if (activity == null || unifiedKeyInfo == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
String content = getKeyContent(unifiedKeyInfo, KeyRepository.create(activity));
|
||||
if (toClipboard) {
|
||||
shareKeyToClipBoard(activity, content);
|
||||
} else {
|
||||
shareKeyIntent(activity, unifiedKeyInfo, content);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Timber.e(e, "error processing key!");
|
||||
Notify.create(activity, R.string.error_key_processing, Notify.Style.ERROR).show();
|
||||
} catch (KeyRepository.NotFoundException e) {
|
||||
Timber.e(e, "key not found!");
|
||||
Notify.create(activity, R.string.error_key_not_found, Notify.Style.ERROR).show();
|
||||
}
|
||||
}
|
||||
|
||||
private static void shareSshKey(Activity activity, UnifiedKeyInfo unifiedKeyInfo, boolean toClipboard) {
|
||||
if (activity == null || unifiedKeyInfo == null) {
|
||||
return;
|
||||
}
|
||||
if (!unifiedKeyInfo.has_auth_key()) {
|
||||
Notify.create(activity, R.string.authentication_subkey_not_found, Notify.Style.ERROR).show();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
String content = getSshKeyContent(unifiedKeyInfo, KeyRepository.create(activity));
|
||||
if (toClipboard) {
|
||||
shareKeyToClipBoard(activity, content);
|
||||
} else {
|
||||
shareKeyIntent(activity, unifiedKeyInfo, content);
|
||||
}
|
||||
} catch (PgpGeneralException | IOException | NoSuchAlgorithmException e) {
|
||||
Timber.e(e, "error processing key!");
|
||||
Notify.create(activity, R.string.error_key_processing, Notify.Style.ERROR).show();
|
||||
@@ -135,16 +160,16 @@ public class ShareKeyHelper {
|
||||
}
|
||||
|
||||
public static void shareKeyToClipboard(Activity activity, UnifiedKeyInfo unifiedKeyInfo) {
|
||||
shareKey(activity, unifiedKeyInfo, true, false);
|
||||
shareKey(activity, unifiedKeyInfo, true);
|
||||
}
|
||||
public static void shareKey(Activity activity, UnifiedKeyInfo unifiedKeyInfo) {
|
||||
shareKey(activity, unifiedKeyInfo, false, false);
|
||||
shareKey(activity, unifiedKeyInfo, false);
|
||||
}
|
||||
public static void shareSshKey(Activity activity, UnifiedKeyInfo unifiedKeyInfo) {
|
||||
shareKey(activity, unifiedKeyInfo, false, true);
|
||||
shareSshKey(activity, unifiedKeyInfo, false);
|
||||
}
|
||||
public static void shareSshKeyToClipboard(Activity activity, UnifiedKeyInfo unifiedKeyInfo) {
|
||||
shareKey(activity, unifiedKeyInfo, true, true);
|
||||
shareSshKey(activity, unifiedKeyInfo, true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user