From 3de0366e62e79ad5ca8839187aa534815b182e33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Mon, 20 Jul 2020 21:14:39 +0800 Subject: [PATCH] Add share my public key option --- .../java/org/telegram/ui/ChatActivity.java | 159 ++++++++++++++---- .../ui/Components/ChatActivityEnterView.java | 48 +++--- .../src/main/res/values/strings_nekox.xml | 1 + .../org/openintents/openpgp/OpenPgpError.java | 5 + 4 files changed, 152 insertions(+), 61 deletions(-) diff --git a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java index 33517e8d7..42bf4ca26 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java @@ -99,8 +99,7 @@ import androidx.recyclerview.widget.RecyclerView; import com.google.android.exoplayer2.ui.AspectRatioFrameLayout; -import org.openintents.openpgp.OpenPgpDecryptionResult; -import org.openintents.openpgp.OpenPgpSignatureResult; +import org.openintents.openpgp.OpenPgpError; import org.openintents.openpgp.util.OpenPgpApi; import org.sufficientlysecure.keychain.pgp.PgpHelper; import org.telegram.PhoneFormat.PhoneFormat; @@ -214,15 +213,8 @@ import org.telegram.ui.Components.URLSpanUserMention; import org.telegram.ui.Components.UndoView; import org.telegram.ui.Components.voip.VoIPHelper; -import cn.hutool.core.io.IoUtil; -import tw.nekomimi.nekogram.JalaliCalendar; -import tw.nekomimi.nekogram.MessageDetailsActivity; -import tw.nekomimi.nekogram.MessageHelper; -import tw.nekomimi.nekogram.NekoConfig; -import tw.nekomimi.nekogram.settings.NekoGeneralSettingsActivity; - import java.io.BufferedWriter; -import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.FileWriter; @@ -237,6 +229,7 @@ import java.util.regex.Pattern; import cn.hutool.core.util.StrUtil; import kotlin.Unit; +import tw.nekomimi.nekogram.JalaliCalendar; import tw.nekomimi.nekogram.MessageDetailsActivity; import tw.nekomimi.nekogram.MessageHelper; import tw.nekomimi.nekogram.NekoConfig; @@ -271,6 +264,7 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not private ActionBarMenuItem searchItem; private RadialProgressView progressBar; private ActionBarMenuSubItem addContactItem; + private ActionBarMenuSubItem shareKeyItem; private ClippingImageView animatingImageView; public RecyclerListView chatListView; private ChatListItemAnimator chatListItemAniamtor; @@ -950,8 +944,7 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not private final static int add_shortcut = 24; private final static int show_pinned = 25; private final static int translate = 101; - - private final static int delete_all = 28; + private final static int share_key = 28; private final static int bot_help = 30; private final static int bot_settings = 31; @@ -1559,7 +1552,7 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not return; } showDialog(AlertsCreator.createTTLAlert(getParentActivity(), currentEncryptedChat).create()); - } else if (id == delete_history || id == delete_all) { + } else if (id == delete_history) { AlertDialog.Builder builder = new AlertDialog.Builder(context); TextView messageTextView = new TextView(context); messageTextView.setTextColor(Theme.getColor(Theme.key_dialogTextBlack)); @@ -1585,27 +1578,17 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not textView.setEllipsize(TextUtils.TruncateAt.END); if (id == delete_history) { textView.setText(LocaleController.getString("DeleteAllFromSelf", R.string.DeleteAllFromSelf)); - } else { - textView.setText(LocaleController.getString("DeleteAllInChat", R.string.DeleteAllInChat)); } frameLayout.addView(textView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, (LocaleController.isRTL ? 21 : 76), 11, (LocaleController.isRTL ? 76 : 21), 0)); frameLayout.addView(messageTextView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, 24, 57, 24, 9)); - if (id == delete_history) { - messageTextView.setText(AndroidUtilities.replaceTags(LocaleController.getString("DeleteAllFromSelfAlert", R.string.DeleteAllFromSelfAlert))); - builder.setPositiveButton(LocaleController.getString("DeleteAll", R.string.DeleteAll), (dialogInterface, i) -> { - if (ChatObject.canUserDoAction(currentChat, ChatObject.ACTION_DELETE_MESSAGES)) { - getMessagesController().deleteUserChannelHistory(currentChat, UserConfig.getInstance(currentAccount).getCurrentUser(), 0); - } else { - MessageHelper.getInstance(currentAccount).deleteUserChannelHistoryWithSearch(dialog_id, UserConfig.getInstance(currentAccount).getCurrentUser()); - } - }); - } else { - messageTextView.setText(AndroidUtilities.replaceTags(LocaleController.getString("DeleteAllInChatAlert", R.string.DeleteAllInChatAlert))); - builder.setPositiveButton(LocaleController.getString("DeleteAllInChat", R.string.DeleteAllInChat), (dialogInterface, i) -> { - MessageHelper.getInstance(currentAccount).deleteChannelHistory(dialog_id, currentChat, 0); - }); - } - + messageTextView.setText(AndroidUtilities.replaceTags(LocaleController.getString("DeleteAllFromSelfAlert", R.string.DeleteAllFromSelfAlert))); + builder.setPositiveButton(LocaleController.getString("DeleteAll", R.string.DeleteAll), (dialogInterface, i) -> { + if (ChatObject.canUserDoAction(currentChat, ChatObject.ACTION_DELETE_MESSAGES)) { + getMessagesController().deleteUserChannelHistory(currentChat, UserConfig.getInstance(currentAccount).getCurrentUser(), 0); + } else { + MessageHelper.getInstance(currentAccount).deleteUserChannelHistoryWithSearch(dialog_id, UserConfig.getInstance(currentAccount).getCurrentUser()); + } + }); builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null); AlertDialog alertDialog = builder.create(); showDialog(alertDialog); @@ -1677,6 +1660,10 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not args.putBoolean("addContact", true); presentFragment(new ContactAddActivity(args)); } + } else if (id == share_key) { + + selectAndShareMyKey(new Intent()); + } else if (id == mute) { toggleMute(false); } else if (id == add_shortcut) { @@ -2001,6 +1988,9 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not if (currentUser != null) { addContactItem = headerItem.addSubItem(share_contact, R.drawable.msg_addcontact, ""); } + + shareKeyItem = headerItem.addSubItem(share_key, R.drawable.baseline_vpn_key_24, LocaleController.getString("ShareMyKey", R.string.ShareMyKey)); + if (currentEncryptedChat != null) { timeItem2 = headerItem.addSubItem(chat_enc_timer, R.drawable.msg_timer, LocaleController.getString("SetTimer", R.string.SetTimer)); } @@ -2013,10 +2003,6 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not if (ChatObject.isChannel(currentChat) && currentChat.megagroup) { headerItem.addSubItem(delete_history, R.drawable.baseline_delete_24, LocaleController.getString("DeleteAllFromSelf", R.string.DeleteAllFromSelf)); } - - if (ChatObject.isChannel(currentChat) && ChatObject.canUserDoAction(currentChat, ChatObject.ACTION_DELETE_MESSAGES)) { - headerItem.addSubItem(delete_all, R.drawable.baseline_delete_24, LocaleController.getString("DeleteAllInChat", R.string.DeleteAllInChat)); - } if (ChatObject.isChannel(currentChat)) { if (!ChatObject.isNotInChat(currentChat)) { if (currentChat.megagroup) { @@ -6834,6 +6820,92 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not showDialog(builder.create()); } + private void selectAndShareMyKey(Intent intent) { + + intent.setAction(OpenPgpApi.ACTION_GET_SIGN_KEY_ID); + + PGPUtil.post(() -> PGPUtil.api.executeApiAsync(intent, null, null, result -> { + + switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) { + + case OpenPgpApi.RESULT_CODE_SUCCESS: { + + result.putExtra(OpenPgpApi.EXTRA_KEY_ID, result.getLongExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, 0L)); + + shareMyKey(result); + + break; + + } + case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: { + + PendingIntent pi = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT); + try { + Activity act = (Activity) getParentActivity(); + act.startIntentSenderFromChild(act, pi.getIntentSender(), 117, null, 0, 0, 0); + } catch (IntentSender.SendIntentException e) { + Log.e(OpenPgpApi.TAG, "SendIntentException", e); + } + break; + } + case OpenPgpApi.RESULT_CODE_ERROR: { + OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR); + AlertUtil.showToast(error.getMessage()); + break; + } + } + + })); + + } + + private void shareMyKey(Intent intent) { + + intent.setAction(OpenPgpApi.ACTION_GET_KEY); + intent.putExtra(OpenPgpApi.EXTRA_REQUEST_ASCII_ARMOR, true); + + ByteArrayOutputStream os = new ByteArrayOutputStream(); + + PGPUtil.post(() -> PGPUtil.api.executeApiAsync(intent, null, os, result -> { + + switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) { + + case OpenPgpApi.RESULT_CODE_SUCCESS: { + + String str = StrUtil.utf8Str(os.toByteArray()); + if (StrUtil.isBlank(str)) return; + getSendMessagesHelper().sendMessage(str, dialog_id, null, null, false, null, null, null, true, 0); + if (!inScheduleMode) { + moveScrollToLastMessage(); + } + hideFieldPanel(false); + break; + + } + + case OpenPgpApi.RESULT_CODE_USER_INTERACTION_REQUIRED: { + + PendingIntent pi = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT); + try { + getParentActivity().startIntentSenderFromChild(getParentActivity(), pi.getIntentSender(), 118, null, 0, 0, 0); + } catch (Exception e) { + FileLog.e(e); + AlertUtil.showToast(e); + } + break; + } + case OpenPgpApi.RESULT_CODE_ERROR: { + OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR); + if (error == null) return; + AlertUtil.showToast(error.toString()); + break; + } + } + + })); + + } + private void hideVoiceHint() { if (voiceHintTextView == null) { return; @@ -9985,6 +10057,10 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not showAttachmentError(); } afterMessageSend(); + } else if (requestCode == 117) { + selectAndShareMyKey(data); + } else if (requestCode == 118) { + shareMyKey(data); } } } @@ -13810,6 +13886,13 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not } addToContactsButton.setVisibility(View.GONE); } + + if (StrUtil.isBlank(NekoConfig.openPGPApp)) { + shareKeyItem.setVisibility(View.GONE); + } else { + shareKeyItem.setVisibility(View.VISIBLE); + } + if (userBlocked || addToContactsButton.getVisibility() == View.GONE && reportSpamButton.getVisibility() == View.GONE) { show = false; } @@ -16246,7 +16329,8 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not }, null); break; } - case 200: case 201: { + case 200: + case 201: { Intent open = new Intent(Intent.ACTION_SEND); open.setType("application/pgp-message"); @@ -16278,7 +16362,8 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not break; } - case 202: case 203: { + case 202: + case 203: { Intent open = new Intent(NekoConfig.openPGPApp + ".action.IMPORT_KEY"); open.putExtra(NekoConfig.openPGPApp + ".EXTRA_KEY_BYTES", StrUtil.utf8Bytes(selectedObject.messageOwner.message)); diff --git a/TMessagesProj/src/main/java/org/telegram/ui/Components/ChatActivityEnterView.java b/TMessagesProj/src/main/java/org/telegram/ui/Components/ChatActivityEnterView.java index bfbf458de..8a648fefa 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/Components/ChatActivityEnterView.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/Components/ChatActivityEnterView.java @@ -20,7 +20,6 @@ import android.app.PendingIntent; import android.content.ClipDescription; import android.content.Context; import android.content.Intent; -import android.content.IntentSender; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.graphics.Bitmap; @@ -51,7 +50,6 @@ import android.text.StaticLayout; import android.text.TextPaint; import android.text.TextWatcher; import android.text.style.ImageSpan; -import android.util.Log; import android.util.Property; import android.util.TypedValue; import android.view.ActionMode; @@ -2999,14 +2997,14 @@ public class ChatActivityEnterView extends FrameLayout implements NotificationCe if (sendPopupWindow != null && sendPopupWindow.isShowing()) { sendPopupWindow.dismiss(); } - signComment(NekoConfig.openPGPKeyId); + signComment(true); }); cell.setOnLongClickListener(v -> { if (sendPopupWindow != null && sendPopupWindow.isShowing()) { sendPopupWindow.dismiss(); } - signComment(1L); + signComment(false); return true; }); cell.setMinimumWidth(AndroidUtilities.dp(196)); @@ -3107,31 +3105,33 @@ public class ChatActivityEnterView extends FrameLayout implements NotificationCe return false; } - private void signComment(long signKeyId) { + private void signComment(boolean save) { + + Intent intent = new Intent(); + + if (NekoConfig.openPGPKeyId != 0L && save) intent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, NekoConfig.openPGPKeyId); + + signComment(intent, save); + + } + + private void signComment(Intent intent, boolean save) { if (parentActivity instanceof LaunchActivity) { ((LaunchActivity) parentActivity).callbacks.put(115, result -> { - long keyId = signKeyId; + long keyId = result.getLongExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, 0L); - if (signKeyId == 0L || signKeyId == 1L) { + if (save && keyId != 0L) NekoConfig.setOpenPGPKeyId(keyId); - keyId = result.getLongExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, 0L); - - if (signKeyId == 0L) NekoConfig.setOpenPGPKeyId(keyId); - - } - - signComment(keyId); + signComment(result, save); }); } - Intent intent = new Intent(OpenPgpApi.ACTION_CLEARTEXT_SIGN); - - if (signKeyId < 0L) intent.putExtra(OpenPgpApi.EXTRA_SIGN_KEY_ID, signKeyId); + intent.setAction(OpenPgpApi.ACTION_CLEARTEXT_SIGN); ByteArrayInputStream is = IoUtil.toUtf8Stream(messageEditText.getText().toString()); ByteArrayOutputStream os = new ByteArrayOutputStream(); @@ -3143,9 +3143,7 @@ public class ChatActivityEnterView extends FrameLayout implements NotificationCe case OpenPgpApi.RESULT_CODE_SUCCESS: { String str = StrUtil.utf8Str(os.toByteArray()); - if (StrUtil.isNotBlank(str)) messageEditText.setText(str); - break; } @@ -3155,18 +3153,20 @@ public class ChatActivityEnterView extends FrameLayout implements NotificationCe PendingIntent pi = result.getParcelableExtra(OpenPgpApi.RESULT_INTENT); try { parentActivity.startIntentSenderFromChild(parentActivity, pi.getIntentSender(), 115, null, 0, 0, 0); - } catch (IntentSender.SendIntentException e) { - Log.e(OpenPgpApi.TAG, "SendIntentException", e); + } catch (Exception e) { + FileLog.e(e); + AlertUtil.showToast(e); } break; } case OpenPgpApi.RESULT_CODE_ERROR: { OpenPgpError error = result.getParcelableExtra(OpenPgpApi.RESULT_ERROR); - if (error.getMessage().contains("not found") && signKeyId < 0L) { + if (error == null) return; + if (error.getMessage() != null && error.getMessage().contains("not found") && save) { NekoConfig.setOpenPGPKeyId(0L); - signComment(0L); + signComment(new Intent(), true); } else { - AlertUtil.showToast(error.getMessage()); + AlertUtil.showToast(error.toString()); } break; } diff --git a/TMessagesProj/src/main/res/values/strings_nekox.xml b/TMessagesProj/src/main/res/values/strings_nekox.xml index 19013d084..40b4c025d 100644 --- a/TMessagesProj/src/main/res/values/strings_nekox.xml +++ b/TMessagesProj/src/main/res/values/strings_nekox.xml @@ -231,5 +231,6 @@ Verify Message Import Public Key Import Private Key + Share My Public Key \ No newline at end of file diff --git a/openpgp-api/src/main/java/org/openintents/openpgp/OpenPgpError.java b/openpgp-api/src/main/java/org/openintents/openpgp/OpenPgpError.java index a149be45f..bb9322cec 100644 --- a/openpgp-api/src/main/java/org/openintents/openpgp/OpenPgpError.java +++ b/openpgp-api/src/main/java/org/openintents/openpgp/OpenPgpError.java @@ -113,4 +113,9 @@ public class OpenPgpError implements Parcelable { return new OpenPgpError[size]; } }; + + @Override + public String toString() { + return errorId + ": " + message; + } }