| 1 | package tw.nekomimi.nekogram; |
| 2 | |
| 3 | import android.os.Build; |
| 4 | |
| 5 | import org.telegram.messenger.AccountInstance; |
| 6 | import org.telegram.messenger.BuildConfig; |
| 7 | import org.telegram.messenger.BuildVars; |
| 8 | import org.telegram.messenger.FileLog; |
| 9 | import org.telegram.messenger.UserConfig; |
| 10 | import org.telegram.tgnet.TLRPC; |
| 11 | import org.webrtc.EglBase; |
| 12 | |
| 13 | import java.util.ArrayList; |
| 14 | import java.util.Collections; |
| 15 | import java.util.List; |
| 16 | import java.util.Locale; |
| 17 | import java.util.function.Consumer; |
| 18 | |
| 19 | import tw.nekomimi.nekogram.utils.FileUtil; |
| 20 | |
| 21 | //TODO use UpdateAppAlertDialog / BlockingUpdateView? |
| 22 | |
| 23 | public class InternalUpdater { |
| 24 | |
| 25 | static final int UPDATE_METADATA_START_FROM = 0; |
| 26 | static final int MAX_READ_COUNT = 20; |
| 27 | static final long CHANNEL_METADATA_ID = 1787231606; |
| 28 | static final String CHANNEL_METADATA_NAME = "pagergram_update_metadata"; |
| 29 | static final long CHANNEL_APKS_ID = 1780943907; |
| 30 | static final String CHANNEL_APKS_NAME = "PagerGram_Apks"; |
| 31 | |
| 32 | static void retrieveUpdateMetadata(retrieveUpdateMetadataCallback callback) { |
| 33 | final int localVersionCode = BuildVars.BUILD_VERSION; |
| 34 | AccountInstance accountInstance = AccountInstance.getInstance(UserConfig.selectedAccount); |
| 35 | TLRPC.TL_messages_getHistory req = new TLRPC.TL_messages_getHistory(); |
| 36 | req.peer = accountInstance.getMessagesController().getInputPeer(-CHANNEL_METADATA_ID); |
| 37 | req.offset_id = 0; |
| 38 | req.limit = MAX_READ_COUNT; |
| 39 | Runnable sendReq = () -> accountInstance.getConnectionsManager().sendRequest(req, (response, error) -> { |
| 40 | if (error != null) { |
| 41 | FileLog.e("Error when retrieving update metadata from channel " + error); |
| 42 | callback.apply(null, true); |
| 43 | return; |
| 44 | } |
| 45 | try { |
| 46 | TLRPC.messages_Messages res = (TLRPC.messages_Messages) response; |
| 47 | List<UpdateMetadata> metas = new ArrayList<>(); |
| 48 | for (TLRPC.Message message : res.messages) { |
| 49 | if (!(message instanceof TLRPC.TL_message)) continue; |
| 50 | if (!message.message.startsWith("v")) continue; |
| 51 | String[] split = message.message.split(","); |
| 52 | if (split.length < 4) continue; |
| 53 | UpdateMetadata metaData = new UpdateMetadata(message.id, split); |
| 54 | metas.add(metaData); |
| 55 | } |
| 56 | Collections.sort(metas, (o1, o2) -> o2.versionCode - o1.versionCode); // versionCode Desc |
| 57 | UpdateMetadata found = null; |
| 58 | for (UpdateMetadata metaData : metas) { |
| 59 | if (metaData.versionCode <= localVersionCode) break; |
| 60 | if (NekoXConfig.autoUpdateReleaseChannel < 3 && metaData.versionName.contains("preview")) |
| 61 | continue; |
| 62 | if (NekoXConfig.autoUpdateReleaseChannel < 2 && metaData.versionName.contains("rc")) |
| 63 | continue; |
| 64 | found = metaData; |
| 65 | break; |
| 66 | } |
| 67 | if (found != null) { |
| 68 | for (TLRPC.Message message : res.messages) { |
| 69 | if (!(message instanceof TLRPC.TL_message)) continue; |
| 70 | if (message.id == found.UpdateLogMessageID) { |
| 71 | found.updateLog = message.message; |
| 72 | found.updateLogEntities = message.entities; |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | if (found == null) { |
| 78 | FileLog.d("Cannot find Update Metadata"); |
| 79 | callback.apply(null, false); |
| 80 | return; |
| 81 | } |
| 82 | FileLog.w("Found Update Metadata " + found.versionName + " " + found.versionCode); |
| 83 | callback.apply(found, false); |
| 84 | } catch (Exception e) { |
| 85 | FileLog.e(e); |
| 86 | callback.apply(null, true); |
| 87 | } |
| 88 | }); |
| 89 | if (req.peer.access_hash != 0) sendReq.run(); |
| 90 | else { |
| 91 | TLRPC.TL_contacts_resolveUsername resolve = new TLRPC.TL_contacts_resolveUsername(); |
| 92 | resolve.username = CHANNEL_METADATA_NAME; |
| 93 | accountInstance.getConnectionsManager().sendRequest(resolve, (response1, error1) -> { |
| 94 | if (error1 != null) { |
| 95 | FileLog.e("Error when checking update, unable to resolve metadata channel " + error1.text); |
| 96 | callback.apply(null, true); |
| 97 | return; |
| 98 | } |
| 99 | if (!(response1 instanceof TLRPC.TL_contacts_resolvedPeer)) { |
| 100 | FileLog.e("Error when checking update, unable to resolve metadata channel, unexpected responseType " + response1.getClass().getName()); |
| 101 | callback.apply(null, true); |
| 102 | return; |
| 103 | } |
| 104 | TLRPC.TL_contacts_resolvedPeer resolvedPeer = (TLRPC.TL_contacts_resolvedPeer) response1; |
| 105 | accountInstance.getMessagesController().putUsers(resolvedPeer.users, false); |
| 106 | accountInstance.getMessagesController().putChats(resolvedPeer.chats, false); |
| 107 | accountInstance.getMessagesStorage().putUsersAndChats(resolvedPeer.users, resolvedPeer.chats, false, true); |
| 108 | if ((resolvedPeer.chats == null || resolvedPeer.chats.size() == 0)) { |
| 109 | FileLog.e("Error when checking update, unable to resolve metadata channel, unexpected resolvedChat "); |
| 110 | callback.apply(null, true); |
| 111 | return; |
| 112 | } |
| 113 | req.peer = new TLRPC.TL_inputPeerChannel(); |
| 114 | req.peer.channel_id = resolvedPeer.chats.get(0).id; |
| 115 | req.peer.access_hash = resolvedPeer.chats.get(0).access_hash; |
| 116 | sendReq.run(); |
| 117 | }); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | public static void checkUpdate(checkUpdateCallback callback) { |
| 122 | AccountInstance accountInstance = AccountInstance.getInstance(UserConfig.selectedAccount); |
| 123 | retrieveUpdateMetadata((metadata, err) -> { |
| 124 | if (metadata == null) { |
| 125 | callback.apply(null, err); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | TLRPC.TL_messages_getHistory req = new TLRPC.TL_messages_getHistory(); |
| 130 | req.peer = accountInstance.getMessagesController().getInputPeer(-CHANNEL_APKS_ID); |
| 131 | req.min_id = metadata.apkChannelMessageID; |
| 132 | req.limit = MAX_READ_COUNT; |
| 133 | |
| 134 | Runnable sendReq = () -> accountInstance.getConnectionsManager().sendRequest(req, (response, error) -> { |
| 135 | if (error != null) { |
| 136 | FileLog.e("Error when getting update document " + error.text); |
| 137 | callback.apply(null, true); |
| 138 | return; |
| 139 | } |
| 140 | try { |
| 141 | TLRPC.messages_Messages res = (TLRPC.messages_Messages) response; |
| 142 | FileLog.d("Retrieve update messages, size:" + res.messages.size()); |
| 143 | final String target = metadata.versionName + "(" + metadata.versionCode + ")" + "-" + Build.SUPPORTED_ABIS[0].toLowerCase(Locale.ROOT) + |
| 144 | for (int i = 0; i < res.messages.size(); i++) { |
| 145 | if (res.messages.get(i).media == null) continue; |
| 146 | |
| 147 | TLRPC.Document apkDocument = res.messages.get(i).media.document; |
| 148 | if (apkDocument.attributes == null) continue; |
| 149 | String fileName = apkDocument.attributes.size() == 0 ? "" : apkDocument.attributes.get(0).file_name; |
| 150 | if (!fileName.contains(target)) |
| 151 | continue; |
| 152 | TLRPC.TL_help_appUpdate update = new TLRPC.TL_help_appUpdate(); |
| 153 | update.version = metadata.versionName; |
| 154 | update.document = apkDocument; |
| 155 | update.can_not_skip = false; |
| 156 | update.flags |= 2; |
| 157 | if (metadata.updateLog != null) { |
| 158 | update.text = metadata.updateLog; |
| 159 | update.entities = metadata.updateLogEntities; |
| 160 | } |
| 161 | callback.apply(update, false); |
| 162 | return; |
| 163 | } |
| 164 | callback.apply(null, false); |
| 165 | } catch (Exception e) { |
| 166 | FileLog.e(e); |
| 167 | callback.apply(null, true); |
| 168 | } |
| 169 | }); |
| 170 | if (req.peer.access_hash != 0) sendReq.run(); |
| 171 | else { |
| 172 | TLRPC.TL_contacts_resolveUsername resolve = new TLRPC.TL_contacts_resolveUsername(); |
| 173 | resolve.username = CHANNEL_APKS_NAME; |
| 174 | accountInstance.getConnectionsManager().sendRequest(resolve, (response1, error1) -> { |
| 175 | if (error1 != null) { |
| 176 | FileLog.e("Error when checking update, unable to resolve metadata channel " + error1); |
| 177 | callback.apply(null, true); |
| 178 | return; |
| 179 | } |
| 180 | if (!(response1 instanceof TLRPC.TL_contacts_resolvedPeer)) { |
| 181 | FileLog.e("Error when checking update, unable to resolve metadata channel, unexpected responseType " + response1.getClass().getName()); |
| 182 | callback.apply(null, true); |
| 183 | return; |
| 184 | } |
| 185 | TLRPC.TL_contacts_resolvedPeer resolvedPeer = (TLRPC.TL_contacts_resolvedPeer) response1; |
| 186 | accountInstance.getMessagesController().putUsers(resolvedPeer.users, false); |
| 187 | accountInstance.getMessagesController().putChats(resolvedPeer.chats, false); |
| 188 | accountInstance.getMessagesStorage().putUsersAndChats(resolvedPeer.users, resolvedPeer.chats, false, true); |
| 189 | if ((resolvedPeer.chats == null || resolvedPeer.chats.size() == 0)) { |
| 190 | FileLog.e("Error when checking update, unable to resolve metadata channel, unexpected resolvedChat "); |
| 191 | callback.apply(null, true); |
| 192 | return; |
| 193 | } |
| 194 | req.peer = new TLRPC.TL_inputPeerChannel(); |
| 195 | req.peer.channel_id = resolvedPeer.chats.get(0).id; |
| 196 | req.peer.access_hash = resolvedPeer.chats.get(0).access_hash; |
| 197 | sendReq.run(); |
| 198 | }); |
| 199 | } |
| 200 | }); |
| 201 | |
| 202 | |
| 203 | } |
| 204 | |
| 205 | public interface retrieveUpdateMetadataCallback { |
| 206 | void apply(UpdateMetadata metadata, boolean error); |
| 207 | } |
| 208 | |
| 209 | public interface checkUpdateCallback { |
| 210 | void apply(TLRPC.TL_help_appUpdate resp, boolean error); |
| 211 | } |
| 212 | |
| 213 | static class UpdateMetadata { |
| 214 | int messageID; |
| 215 | String versionName; |
| 216 | int versionCode; |
| 217 | int apkChannelMessageID; |
| 218 | int UpdateLogMessageID; |
| 219 | String updateLog = null; |
| 220 | ArrayList<TLRPC.MessageEntity> updateLogEntities = null; |
| 221 | |
| 222 | UpdateMetadata(int messageID, String[] split) { |
| 223 | this.messageID = messageID; |
| 224 | versionName = split[0]; |
| 225 | versionCode = Integer.parseInt(split[1]); |
| 226 | apkChannelMessageID = Integer.parseInt(split[2]); |
| 227 | UpdateLogMessageID = Integer.parseInt(split[3]); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | } |
| 232 | |