| 1 | package tw.nekomimi.nekogram; |
| 2 | |
| 3 | import android.annotation.SuppressLint; |
| 4 | import android.content.Context; |
| 5 | |
| 6 | import org.telegram.messenger.AndroidUtilities; |
| 7 | import org.telegram.messenger.BaseController; |
| 8 | import org.telegram.messenger.LocaleController; |
| 9 | import org.telegram.messenger.MessageObject; |
| 10 | import org.telegram.messenger.R; |
| 11 | import org.telegram.messenger.UserConfig; |
| 12 | import org.telegram.tgnet.ConnectionsManager; |
| 13 | import org.telegram.tgnet.TLRPC; |
| 14 | import org.telegram.ui.ActionBar.AlertDialog; |
| 15 | import org.telegram.ui.Cells.ChatMessageCell; |
| 16 | |
| 17 | import java.util.ArrayList; |
| 18 | |
| 19 | import tw.nekomimi.nekogram.settings.NekoGeneralSettingsActivity; |
| 20 | import tw.nekomimi.nekogram.translator.TranslateBottomSheet; |
| 21 | import tw.nekomimi.nekogram.translator.Translator; |
| 22 | |
| 23 | public class MessageHelper extends BaseController { |
| 24 | |
| 25 | private static volatile MessageHelper[] Instance = new MessageHelper[UserConfig.MAX_ACCOUNT_COUNT]; |
| 26 | @SuppressLint("StaticFieldLeak") |
| 27 | private static AlertDialog progressDialog; |
| 28 | private int lastReqId; |
| 29 | |
| 30 | public MessageHelper(int num) { |
| 31 | super(num); |
| 32 | } |
| 33 | |
| 34 | public static void setMessageContent(MessageObject messageObject, ChatMessageCell chatMessageCell, String message) { |
| 35 | messageObject.messageOwner.message = message; |
| 36 | if (messageObject.caption != null) { |
| 37 | messageObject.caption = null; |
| 38 | messageObject.generateCaption(); |
| 39 | messageObject.forceUpdate = true; |
| 40 | } |
| 41 | messageObject.applyNewText(); |
| 42 | messageObject.resetLayout(); |
| 43 | chatMessageCell.requestLayout(); |
| 44 | chatMessageCell.invalidate(); |
| 45 | } |
| 46 | |
| 47 | public static void showTranslateDialog(Context context, String query) { |
| 48 | if (NekoConfig.translationProvider < 0) { |
| 49 | TranslateBottomSheet.show(context, query); |
| 50 | } else { |
| 51 | if (progressDialog != null) { |
| 52 | progressDialog.dismiss(); |
| 53 | } |
| 54 | progressDialog = new AlertDialog(context, 3); |
| 55 | progressDialog.showDelayed(400); |
| 56 | Translator.translate(query, new Translator.TranslateCallBack() { |
| 57 | @Override |
| 58 | public void onSuccess(String translation) { |
| 59 | if (progressDialog != null) { |
| 60 | progressDialog.dismiss(); |
| 61 | } |
| 62 | AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 63 | builder.setMessage(translation); |
| 64 | builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), null); |
| 65 | builder.setNeutralButton(LocaleController.getString("Copy", R.string.Copy), (dialog, which) -> AndroidUtilities.addToClipboard(translation)); |
| 66 | builder.show(); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void onError() { |
| 71 | if (progressDialog != null) { |
| 72 | progressDialog.dismiss(); |
| 73 | } |
| 74 | AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 75 | builder.setMessage(LocaleController.getString("TranslateFailed", R.string.TranslateFailed)); |
| 76 | builder.setNeutralButton(LocaleController.getString("TranslationProvider", R.string.TranslationProvider), (dialog, which) -> NekoGeneralSettingsActivity.getTranslationProviderAlert(context).show()); |
| 77 | builder.setPositiveButton(LocaleController.getString("Retry", R.string.Retry), (dialog, which) -> showTranslateDialog(context, query)); |
| 78 | builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null); |
| 79 | builder.show(); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void onUnsupported() { |
| 84 | if (progressDialog != null) { |
| 85 | progressDialog.dismiss(); |
| 86 | } |
| 87 | AlertDialog.Builder builder = new AlertDialog.Builder(context); |
| 88 | builder.setMessage(LocaleController.getString("TranslateApiUnsupported", R.string.TranslateApiUnsupported)); |
| 89 | builder.setPositiveButton(LocaleController.getString("TranslationProvider", R.string.TranslationProvider), (dialog, which) -> NekoGeneralSettingsActivity.getTranslationProviderAlert(context).show()); |
| 90 | builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null); |
| 91 | builder.show(); |
| 92 | } |
| 93 | }); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | public static MessageHelper getInstance(int num) { |
| 98 | MessageHelper localInstance = Instance[num]; |
| 99 | if (localInstance == null) { |
| 100 | synchronized (MessageHelper.class) { |
| 101 | localInstance = Instance[num]; |
| 102 | if (localInstance == null) { |
| 103 | Instance[num] = localInstance = new MessageHelper(num); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | return localInstance; |
| 108 | } |
| 109 | |
| 110 | public void deleteUserChannelHistoryWithSearch(final long dialog_id, final TLRPC.User user) { |
| 111 | deleteUserChannelHistoryWithSearch(dialog_id, user, 0); |
| 112 | } |
| 113 | |
| 114 | public void deleteUserChannelHistoryWithSearch(final long dialog_id, final TLRPC.User user, final int offset_id) { |
| 115 | final TLRPC.TL_messages_search req = new TLRPC.TL_messages_search(); |
| 116 | req.peer = getMessagesController().getInputPeer((int) dialog_id); |
| 117 | if (req.peer == null) { |
| 118 | return; |
| 119 | } |
| 120 | req.limit = 100; |
| 121 | req.q = ""; |
| 122 | req.offset_id = offset_id; |
| 123 | if (user != null) { |
| 124 | req.from_id = getMessagesController().getInputUser(user); |
| 125 | req.flags |= 1; |
| 126 | } |
| 127 | req.filter = new TLRPC.TL_inputMessagesFilterEmpty(); |
| 128 | final int currentReqId = ++lastReqId; |
| 129 | getConnectionsManager().sendRequest(req, (response, error) -> AndroidUtilities.runOnUIThread(() -> { |
| 130 | if (error == null) { |
| 131 | int lastMessageId = offset_id; |
| 132 | if (currentReqId == lastReqId) { |
| 133 | if (response != null) { |
| 134 | TLRPC.messages_Messages res = (TLRPC.messages_Messages) response; |
| 135 | int size = res.messages.size(); |
| 136 | if (size == 0) { |
| 137 | return; |
| 138 | } |
| 139 | ArrayList<Integer> ids = new ArrayList<>(); |
| 140 | ArrayList<Long> random_ids = new ArrayList<>(); |
| 141 | int channelId = 0; |
| 142 | for (int a = 0; a < res.messages.size(); a++) { |
| 143 | TLRPC.Message message = res.messages.get(a); |
| 144 | ids.add(message.id); |
| 145 | if (message.random_id != 0) { |
| 146 | random_ids.add(message.random_id); |
| 147 | } |
| 148 | if (message.to_id.channel_id != 0) { |
| 149 | channelId = message.to_id.channel_id; |
| 150 | } |
| 151 | if (message.id > lastMessageId) { |
| 152 | lastMessageId = message.id; |
| 153 | } |
| 154 | } |
| 155 | getMessagesController().deleteMessages(ids, random_ids, null, dialog_id, channelId, true, false); |
| 156 | deleteUserChannelHistoryWithSearch(dialog_id, user, lastMessageId); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | }), ConnectionsManager.RequestFlagFailOnServerErrors); |
| 161 | } |
| 162 | } |
| 163 | |