From abf8f6f6199dfcc0df3cc315ce5ba1e4edeb9991 Mon Sep 17 00:00:00 2001 From: DrKLO Date: Thu, 23 Oct 2014 19:30:35 +0400 Subject: [PATCH] Bug fixes --- README.md | 21 +- TMessagesProj/build.gradle | 4 +- .../telegram/android/AndroidUtilities.java | 2 +- .../telegram/android/MessagesController.java | 8 +- .../org/telegram/android/MessagesStorage.java | 780 ++++++------------ .../telegram/android/SendMessagesHelper.java | 10 +- .../ContactsActivitySearchAdapter.java | 32 +- .../java/org/telegram/ui/ChatActivity.java | 2 +- .../org/telegram/ui/ChatProfileActivity.java | 2 +- .../telegram/ui/GroupCreateFinalActivity.java | 5 +- .../org/telegram/ui/MessagesActivity.java | 80 +- .../java/org/telegram/ui/PhotoViewer.java | 2 + .../ui/SettingsChangeUsernameActivity.java | 3 + .../org/telegram/ui/VideoEditorActivity.java | 12 +- .../ui/Views/ChatActivityEnterView.java | 26 +- TMessagesProj/src/main/res/anim/scale_in.xml | 4 +- TMessagesProj/src/main/res/anim/scale_out.xml | 4 +- 17 files changed, 368 insertions(+), 629 deletions(-) diff --git a/README.md b/README.md index d4affdf22..7b569217a 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,30 @@ ## Telegram messenger for Android [Telegram](http://telegram.org) is a messaging app with a focus on speed and security. It’s superfast, simple and free. +This repo contains the official source code for [Telegram App for Android](https://play.google.com/store/apps/details?id=org.telegram.messenger). -This repo contains official [Telegram App for Android](https://play.google.com/store/apps/details?id=org.telegram.messenger) source code. +##Creating your Telegram Application + +We welcome all developers to use our API and source code to create applications on our platform. +There are several things we require from **all developers** for the moment. + +1. [**Obtain your own api_id**](https://core.telegram.org/api/obtaining_api_id) for your application. +2. Please **do not** use the name Telegram for your app — or make sure your users understand that it is unofficial. +3. Kindly **do not** use our standard logo (white paper plane in a blue circle) as your app's logo. +3. Please study our [**security guidelines**](https://core.telegram.org/mtproto/security_guidelines) and take good care of your users' data and privacy. +4. Please remember to publish **your** code too in order to comply with the licences. ### API, Protocol documentation -Documentation for Telegram API is available here: http://core.telegram.org/api +Telegram API manuals: http://core.telegram.org/api -Documentation for MTproto protocol is available here: http://core.telegram.org/mtproto +MTproto protocol manuals: http://core.telegram.org/mtproto ### Usage -**Beware of using dev branch and uploading it to any markets, in most cases it will work as you expecting** - -First of all your should take a look to **src/main/java/org/telegram/messenger/BuildVars.java** and fill it with correct values. +**Beware of using the dev branch and uploading it to any markets, in many cases it not will work as expected**. +First of all, take a look at **src/main/java/org/telegram/messenger/BuildVars.java** and fill it with correct values. Import the root folder into your IDE (tested on Android Studio), then run project. ### Localization diff --git a/TMessagesProj/build.gradle b/TMessagesProj/build.gradle index 56b601084..13c41af8e 100644 --- a/TMessagesProj/build.gradle +++ b/TMessagesProj/build.gradle @@ -25,7 +25,7 @@ dependencies { android { compileSdkVersion 21 - buildToolsVersion '21.0.1' + buildToolsVersion '21.0.2' signingConfigs { debug { @@ -80,7 +80,7 @@ android { defaultConfig { minSdkVersion 8 targetSdkVersion 19 - versionCode 372 + versionCode 374 versionName "1.9.6" } } diff --git a/TMessagesProj/src/main/java/org/telegram/android/AndroidUtilities.java b/TMessagesProj/src/main/java/org/telegram/android/AndroidUtilities.java index ed9a09e90..3b346fd00 100644 --- a/TMessagesProj/src/main/java/org/telegram/android/AndroidUtilities.java +++ b/TMessagesProj/src/main/java/org/telegram/android/AndroidUtilities.java @@ -218,7 +218,7 @@ public class AndroidUtilities { } public static File getCacheDir() { - if (Environment.getExternalStorageState().startsWith(Environment.MEDIA_MOUNTED)) { + if (Environment.getExternalStorageState() == null || Environment.getExternalStorageState().startsWith(Environment.MEDIA_MOUNTED)) { try { File file = ApplicationLoader.applicationContext.getExternalCacheDir(); if (file != null) { diff --git a/TMessagesProj/src/main/java/org/telegram/android/MessagesController.java b/TMessagesProj/src/main/java/org/telegram/android/MessagesController.java index eb95c9a56..80838222c 100644 --- a/TMessagesProj/src/main/java/org/telegram/android/MessagesController.java +++ b/TMessagesProj/src/main/java/org/telegram/android/MessagesController.java @@ -1711,7 +1711,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter } public void markMessageAsRead(final long dialog_id, final long random_id, int ttl) { - if (random_id == 0 || dialog_id == 0) { + if (random_id == 0 || dialog_id == 0 || ttl <= 0) { return; } int lower_part = (int)dialog_id; @@ -1726,10 +1726,8 @@ public class MessagesController implements NotificationCenter.NotificationCenter ArrayList random_ids = new ArrayList(); random_ids.add(random_id); SendMessagesHelper.getInstance().sendMessagesReadMessage(chat, random_ids, null); - if (ttl > 0) { - int time = ConnectionsManager.getInstance().getCurrentTime(); - MessagesStorage.getInstance().createTaskForSecretChat(chat.id, time, time, 0, random_ids); - } + int time = ConnectionsManager.getInstance().getCurrentTime(); + MessagesStorage.getInstance().createTaskForSecretChat(chat.id, time, time, 0, random_ids); } public void markDialogAsRead(final long dialog_id, final int max_id, final int max_positive_id, final int offset, final int max_date, final boolean was, final boolean popup) { diff --git a/TMessagesProj/src/main/java/org/telegram/android/MessagesStorage.java b/TMessagesProj/src/main/java/org/telegram/android/MessagesStorage.java index 479401bf6..4b8598b7a 100644 --- a/TMessagesProj/src/main/java/org/telegram/android/MessagesStorage.java +++ b/TMessagesProj/src/main/java/org/telegram/android/MessagesStorage.java @@ -499,58 +499,15 @@ public class MessagesStorage { cursor.dispose(); if (!encryptedChatIds.isEmpty()) { - String stringToLoad = TextUtils.join(",", encryptedChatIds); - cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, user, g, authkey, ttl, layer, seq_in, seq_out FROM enc_chats WHERE uid IN(%s)", stringToLoad)); - while (cursor.next()) { - ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); - if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { - TLRPC.EncryptedChat chat = (TLRPC.EncryptedChat)TLClassStore.Instance().TLdeserialize(data, data.readInt32()); - encryptedChats.add(chat); - chat.user_id = cursor.intValue(1); - if (!userIds.contains(chat.user_id)) { - userIds.add(chat.user_id); - } - chat.a_or_b = cursor.byteArrayValue(2); - chat.auth_key = cursor.byteArrayValue(3); - chat.ttl = cursor.intValue(4); - chat.layer = cursor.intValue(5); - chat.seq_in = cursor.intValue(6); - chat.seq_out = cursor.intValue(7); - } - buffersStorage.reuseFreeBuffer(data); - } - cursor.dispose(); + getEncryptedChatsInternal(TextUtils.join(",", encryptedChatIds), encryptedChats, userIds); } if (!userIds.isEmpty()) { - String stringToLoad = TextUtils.join(",", userIds); - cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, status FROM users WHERE uid IN(%s)", stringToLoad)); - while (cursor.next()) { - ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); - if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { - TLRPC.User user = (TLRPC.User) TLClassStore.Instance().TLdeserialize(data, data.readInt32()); - if (user.status != null) { - user.status.expires = cursor.intValue(1); - } - users.add(user); - } - buffersStorage.reuseFreeBuffer(data); - } - cursor.dispose(); + getUsersInternal(TextUtils.join(",", userIds), users); } if (!chatIds.isEmpty()) { - String stringToLoad = TextUtils.join(",", chatIds); - cursor = database.queryFinalized(String.format(Locale.US, "SELECT data FROM chats WHERE uid IN(%s)", stringToLoad)); - while (cursor.next()) { - ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); - if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { - TLRPC.Chat chat = (TLRPC.Chat) TLClassStore.Instance().TLdeserialize(data, data.readInt32()); - chats.add(chat); - } - buffersStorage.reuseFreeBuffer(data); - } - cursor.dispose(); + getChatsInternal(TextUtils.join(",", chatIds), chats); } } Collections.reverse(messages); @@ -639,19 +596,7 @@ public class MessagesStorage { cursor.dispose(); if (usersToLoad.length() != 0) { - cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, status FROM users WHERE uid IN(%s)", usersToLoad.toString())); - while (cursor.next()) { - ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); - if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { - TLRPC.User user = (TLRPC.User)TLClassStore.Instance().TLdeserialize(data, data.readInt32()); - users.add(user); - if (user.status != null) { - user.status.expires = cursor.intValue(1); - } - } - buffersStorage.reuseFreeBuffer(data); - } - cursor.dispose(); + getUsersInternal(usersToLoad.toString(), users); } MessagesController.getInstance().processLoadedBlockedUsers(ids, users, true); @@ -872,8 +817,12 @@ public class MessagesStorage { cursor = database.queryFinalized(String.format(Locale.US, "SELECT m.mid, m.ttl FROM messages as m INNER JOIN randoms as r ON m.mid = r.mid WHERE r.random_id IN (%s)", ids)); } while (cursor.next()) { + int ttl = cursor.intValue(1); + if (ttl <= 0) { + continue; + } int mid = cursor.intValue(0); - int date = Math.min(readTime, time) + cursor.intValue(1); + int date = Math.min(readTime, time) + ttl; minDate = Math.min(minDate, date); ArrayList arr = messages.get(date); if (arr == null) { @@ -1126,19 +1075,7 @@ public class MessagesStorage { } } if (usersToLoad.length() != 0) { - cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, status FROM users WHERE uid IN(%s)", usersToLoad.toString())); - while (cursor.next()) { - ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); - if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { - TLRPC.User user = (TLRPC.User)TLClassStore.Instance().TLdeserialize(data, data.readInt32()); - loadedUsers.add(user); - if (user.status != null) { - user.status.expires = cursor.intValue(1); - } - } - buffersStorage.reuseFreeBuffer(data); - } - cursor.dispose(); + getUsersInternal(usersToLoad.toString(), loadedUsers); } if (modified) { updateChatInfo(chat_id, info, false); @@ -1225,7 +1162,18 @@ public class MessagesStorage { SQLiteCursor cursor = database.queryFinalized("SELECT u.data, u.status, u.name FROM users as u INNER JOIN contacts as c ON u.uid = c.uid"); while (cursor.next()) { String name = cursor.stringValue(2); + String username = null; + int usernamePos = name.lastIndexOf(";;;"); + if (usernamePos != -1) { + username = name.substring(usernamePos + 3); + } + int found = 0; if (name.startsWith(q) || name.contains(" " + q)) { + found = 1; + } else if (username != null && username.startsWith(q)) { + found = 2; + } + if (found != 0) { ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { TLRPC.User user = (TLRPC.User)TLClassStore.Instance().TLdeserialize(data, data.readInt32()); @@ -1233,7 +1181,11 @@ public class MessagesStorage { if (user.status != null) { user.status.expires = cursor.intValue(1); } - resultArrayNames.add(Utilities.generateSearchName(user.first_name, user.last_name, q)); + if (found == 1) { + resultArrayNames.add(Utilities.generateSearchName(user.first_name, user.last_name, q)); + } else { + resultArrayNames.add(Utilities.generateSearchName("@" + user.username, null, "@" + q)); + } resultArray.add(user); } } @@ -1246,7 +1198,20 @@ public class MessagesStorage { cursor = database.queryFinalized("SELECT q.data, u.name, q.user, q.g, q.authkey, q.ttl, u.data, u.status, q.layer, q.seq_in, q.seq_out FROM enc_chats as q INNER JOIN dialogs as d ON (q.uid << 32) = d.did INNER JOIN users as u ON q.user = u.uid"); while (cursor.next()) { String name = cursor.stringValue(1); + + String username = null; + int usernamePos = name.lastIndexOf(";;;"); + if (usernamePos != -1) { + username = name.substring(usernamePos + 2); + } + int found = 0; if (name.startsWith(q) || name.contains(" " + q)) { + found = 1; + } else if (username != null && username.startsWith(q)) { + found = 2; + } + + if (found != 0) { ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); ByteBufferDesc data2 = buffersStorage.getFreeBuffer(cursor.byteArrayLength(6)); if (data != null && cursor.byteBufferValue(0, data.buffer) != 0 && cursor.byteBufferValue(6, data2.buffer) != 0) { @@ -1263,7 +1228,11 @@ public class MessagesStorage { if (user.status != null) { user.status.expires = cursor.intValue(7); } - resultArrayNames.add(Html.fromHtml("" + ContactsController.formatName(user.first_name, user.last_name) + "")); + if (found == 1) { + resultArrayNames.add(Html.fromHtml("" + ContactsController.formatName(user.first_name, user.last_name) + "")); + } else { + resultArrayNames.add(Utilities.generateSearchName("@" + user.username, null, "@" + q)); + } resultArray.add(chat); encUsers.add(user); } @@ -1473,19 +1442,7 @@ public class MessagesStorage { cursor.dispose(); if (uids.length() != 0) { - cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, status FROM users WHERE uid IN(%s)", uids.toString())); - while (cursor.next()) { - ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); - if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { - TLRPC.User user = (TLRPC.User)TLClassStore.Instance().TLdeserialize(data, data.readInt32()); - users.add(user); - if (user.status != null) { - user.status.expires = cursor.intValue(1); - } - } - buffersStorage.reuseFreeBuffer(data); - } - cursor.dispose(); + getUsersInternal(uids.toString(), users); } } catch (Exception e) { contacts.clear(); @@ -1597,20 +1554,7 @@ public class MessagesStorage { } } if (usersToLoad.length() != 0) { - cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, status FROM users WHERE uid IN(%s)", usersToLoad.toString())); - while (cursor.next()) { - ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); - if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { - TLRPC.User user = (TLRPC.User)TLClassStore.Instance().TLdeserialize(data, data.readInt32()); - loadedUsers.add(user.id); - if (user.status != null) { - user.status.expires = cursor.intValue(1); - } - res.users.add(user); - } - buffersStorage.reuseFreeBuffer(data); - } - cursor.dispose(); + getUsersInternal(usersToLoad.toString(), res.users); } } catch (Exception e) { res.messages.clear(); @@ -1737,45 +1681,11 @@ public class MessagesStorage { if (!encryptedChatIds.isEmpty()) { - String stringToLoad = TextUtils.join(",", encryptedChatIds); - cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, user, g, authkey, ttl, layer, seq_in, seq_out FROM enc_chats WHERE uid IN(%s)", stringToLoad)); - while (cursor.next()) { - ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); - if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { - TLRPC.EncryptedChat chat = (TLRPC.EncryptedChat)TLClassStore.Instance().TLdeserialize(data, data.readInt32()); - encryptedChats.add(chat); - chat.user_id = cursor.intValue(1); - if (!userIds.contains(chat.user_id)) { - userIds.add(chat.user_id); - } - chat.a_or_b = cursor.byteArrayValue(2); - chat.auth_key = cursor.byteArrayValue(3); - chat.ttl = cursor.intValue(4); - chat.layer = cursor.intValue(5); - chat.seq_in = cursor.intValue(6); - chat.seq_out = cursor.intValue(7); - } - buffersStorage.reuseFreeBuffer(data); - } - cursor.dispose(); + getEncryptedChatsInternal(TextUtils.join(",", encryptedChatIds), encryptedChats, userIds); } - if (!userIds.isEmpty()) { - String stringToLoad = TextUtils.join(",", userIds); - cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, status FROM users WHERE uid IN(%s)", stringToLoad)); - while (cursor.next()) { - ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); - if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { - TLRPC.User user = (TLRPC.User) TLClassStore.Instance().TLdeserialize(data, data.readInt32()); - if (user.status != null) { - user.status.expires = cursor.intValue(1); - } - users.add(user); - } - buffersStorage.reuseFreeBuffer(data); - } - cursor.dispose(); + getUsersInternal(TextUtils.join(",", userIds), users); } if (!chatIds.isEmpty() || !broadcastIds.isEmpty()) { @@ -1792,16 +1702,7 @@ public class MessagesStorage { } stringToLoad.append(-cid); } - cursor = database.queryFinalized(String.format(Locale.US, "SELECT data FROM chats WHERE uid IN(%s)", stringToLoad.toString())); - while (cursor.next()) { - ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); - if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { - TLRPC.Chat chat = (TLRPC.Chat) TLClassStore.Instance().TLdeserialize(data, data.readInt32()); - chats.add(chat); - } - buffersStorage.reuseFreeBuffer(data); - } - cursor.dispose(); + getChatsInternal(stringToLoad.toString(), chats); } SendMessagesHelper.getInstance().processUnsentMessages(messages, users, chats, encryptedChats); @@ -2017,20 +1918,7 @@ public class MessagesStorage { } } if (usersToLoad.length() != 0) { - cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, status FROM users WHERE uid IN(%s)", usersToLoad.toString())); - while (cursor.next()) { - ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); - if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { - TLRPC.User user = (TLRPC.User)TLClassStore.Instance().TLdeserialize(data, data.readInt32()); - loadedUsers.add(user.id); - if (user.status != null) { - user.status.expires = cursor.intValue(1); - } - res.users.add(user); - } - buffersStorage.reuseFreeBuffer(data); - } - cursor.dispose(); + getUsersInternal(usersToLoad.toString(), res.users); } } catch (Exception e) { res.messages.clear(); @@ -2270,42 +2158,15 @@ public class MessagesStorage { @Override public void run() { try { - int userToLoad = 0; - SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, user, g, authkey, ttl, layer, seq_in, seq_out FROM enc_chats WHERE uid = %d", chat_id)); - if (cursor.next()) { - ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); - if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { - TLRPC.EncryptedChat chat = (TLRPC.EncryptedChat)TLClassStore.Instance().TLdeserialize(data, data.readInt32()); - result.add(chat); - chat.user_id = cursor.intValue(1); - userToLoad = chat.user_id; - chat.a_or_b = cursor.byteArrayValue(2); - chat.auth_key = cursor.byteArrayValue(3); - chat.ttl = cursor.intValue(4); - chat.layer = cursor.intValue(5); - chat.seq_in = cursor.intValue(6); - chat.seq_out = cursor.intValue(7); - } - buffersStorage.reuseFreeBuffer(data); - } - cursor.dispose(); - if (userToLoad != 0) { - cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, status FROM users WHERE uid = %d", userToLoad)); - if (cursor.next()) { - ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); - if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { - TLRPC.User user = (TLRPC.User)TLClassStore.Instance().TLdeserialize(data, data.readInt32()); - if (user.status != null) { - user.status.expires = cursor.intValue(1); - } - result.add(user); - } - buffersStorage.reuseFreeBuffer(data); - } - cursor.dispose(); - - if (result.size() != 2) { - result.clear(); + ArrayList usersToLoad = new ArrayList(); + ArrayList encryptedChats = new ArrayList(); + getEncryptedChatsInternal("" + chat_id, encryptedChats, usersToLoad); + if (!encryptedChats.isEmpty() && !usersToLoad.isEmpty()) { + ArrayList users = new ArrayList(); + getUsersInternal(TextUtils.join(",", usersToLoad), users); + if (!users.isEmpty()) { + result.add(encryptedChats.get(0)); + result.add(users.get(0)); } } } catch (Exception e) { @@ -2333,12 +2194,7 @@ public class MessagesStorage { chat.serializeToStream(data); state.bindInteger(1, chat.id); state.bindInteger(2, user.id); - if (user.first_name != null && user.last_name != null) { - String name = (user.first_name + " " + user.last_name).toLowerCase(); - state.bindString(3, name); - } else { - state.bindString(3, ""); - } + state.bindString(3, formatUserSearchName(user)); state.bindByteBuffer(4, data.buffer); if (chat.a_or_b != null) { data2.writeRaw(chat.a_or_b); @@ -2374,54 +2230,157 @@ public class MessagesStorage { }); } + private String formatUserSearchName(TLRPC.User user) { + StringBuilder str = new StringBuilder(""); + if (user.first_name != null && user.first_name.length() > 0) { + str.append(user.first_name); + } + if (user.last_name != null && user.last_name.length() > 0) { + if (str.length() > 0) { + str.append(" "); + } + str.append(user.last_name); + } + str.append(";;;"); + if (user.username != null && user.username.length() > 0) { + str.append(user.username); + } + return str.toString().toLowerCase(); + } + + private void putUsersInternal(ArrayList users) throws Exception { + if (users == null || users.isEmpty()) { + return; + } + SQLitePreparedStatement state = database.executeFast("REPLACE INTO users VALUES(?, ?, ?, ?)"); + for (TLRPC.User user : users) { + state.requery(); + ByteBufferDesc data = buffersStorage.getFreeBuffer(user.getObjectSize()); + user.serializeToStream(data); + state.bindInteger(1, user.id); + state.bindString(2, formatUserSearchName(user)); + if (user.status != null) { + state.bindInteger(3, user.status.expires); + } else { + state.bindInteger(3, 0); + } + state.bindByteBuffer(4, data.buffer); + state.step(); + buffersStorage.reuseFreeBuffer(data); + } + state.dispose(); + } + + private void putChatsInternal(ArrayList chats) throws Exception { + if (chats == null || chats.isEmpty()) { + return; + } + SQLitePreparedStatement state = database.executeFast("REPLACE INTO chats VALUES(?, ?, ?)"); + for (TLRPC.Chat chat : chats) { + state.requery(); + ByteBufferDesc data = buffersStorage.getFreeBuffer(chat.getObjectSize()); + chat.serializeToStream(data); + state.bindInteger(1, chat.id); + if (chat.title != null) { + String name = chat.title.toLowerCase(); + state.bindString(2, name); + } else { + state.bindString(2, ""); + } + state.bindByteBuffer(3, data.buffer); + state.step(); + buffersStorage.reuseFreeBuffer(data); + } + state.dispose(); + } + + private void getUsersInternal(String usersToLoad, ArrayList result) throws Exception { + if (usersToLoad == null || usersToLoad.length() == 0 || result == null) { + return; + } + SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, status FROM users WHERE uid IN(%s)", usersToLoad)); + while (cursor.next()) { + try { + ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); + if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { + TLRPC.User user = (TLRPC.User)TLClassStore.Instance().TLdeserialize(data, data.readInt32()); + if (user != null) { + if (user.status != null) { + user.status.expires = cursor.intValue(1); + } + result.add(user); + } + } + buffersStorage.reuseFreeBuffer(data); + } catch (Exception e) { + FileLog.e("tmessages", e); + } + } + cursor.dispose(); + } + + private void getChatsInternal(String chatsToLoad, ArrayList result) throws Exception { + if (chatsToLoad == null || chatsToLoad.length() == 0 || result == null) { + return; + } + SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT data FROM chats WHERE uid IN(%s)", chatsToLoad)); + while (cursor.next()) { + try { + ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); + if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { + TLRPC.Chat chat = (TLRPC.Chat)TLClassStore.Instance().TLdeserialize(data, data.readInt32()); + if (chat != null) { + result.add(chat); + } + } + buffersStorage.reuseFreeBuffer(data); + } catch (Exception e) { + FileLog.e("tmessages", e); + } + } + cursor.dispose(); + } + + private void getEncryptedChatsInternal(String chatsToLoad, ArrayList result, ArrayList usersToLoad) throws Exception { + if (chatsToLoad == null || chatsToLoad.length() == 0 || result == null) { + return; + } + + SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, user, g, authkey, ttl, layer, seq_in, seq_out FROM enc_chats WHERE uid IN(%s)", chatsToLoad)); + while (cursor.next()) { + try { + ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); + if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { + TLRPC.EncryptedChat chat = (TLRPC.EncryptedChat)TLClassStore.Instance().TLdeserialize(data, data.readInt32()); + if (chat != null) { + chat.user_id = cursor.intValue(1); + if (usersToLoad != null && !usersToLoad.contains(chat.user_id)) { + usersToLoad.add(chat.user_id); + } + chat.a_or_b = cursor.byteArrayValue(2); + chat.auth_key = cursor.byteArrayValue(3); + chat.ttl = cursor.intValue(4); + chat.layer = cursor.intValue(5); + chat.seq_in = cursor.intValue(6); + chat.seq_out = cursor.intValue(7); + result.add(chat); + } + } + buffersStorage.reuseFreeBuffer(data); + } catch (Exception e) { + FileLog.e("tmessages", e); + } + } + cursor.dispose(); + } + private void putUsersAndChatsInternal(final ArrayList users, final ArrayList chats, final boolean withTransaction) { try { if (withTransaction) { database.beginTransaction(); } - if (users != null && !users.isEmpty()) { - SQLitePreparedStatement state = database.executeFast("REPLACE INTO users VALUES(?, ?, ?, ?)"); - for (TLRPC.User user : users) { - state.requery(); - ByteBufferDesc data = buffersStorage.getFreeBuffer(user.getObjectSize()); - user.serializeToStream(data); - state.bindInteger(1, user.id); - if (user.first_name != null && user.last_name != null) { - String name = (user.first_name + " " + user.last_name).toLowerCase(); - state.bindString(2, name); - } else { - state.bindString(2, ""); - } - if (user.status != null) { - state.bindInteger(3, user.status.expires); - } else { - state.bindInteger(3, 0); - } - state.bindByteBuffer(4, data.buffer); - state.step(); - buffersStorage.reuseFreeBuffer(data); - } - state.dispose(); - } - if (chats != null && !chats.isEmpty()) { - SQLitePreparedStatement state = database.executeFast("REPLACE INTO chats VALUES(?, ?, ?)"); - for (TLRPC.Chat chat : chats) { - state.requery(); - ByteBufferDesc data = buffersStorage.getFreeBuffer(chat.getObjectSize()); - chat.serializeToStream(data); - state.bindInteger(1, chat.id); - if (chat.title != null) { - String name = chat.title.toLowerCase(); - state.bindString(2, name); - } else { - state.bindString(2, ""); - } - state.bindByteBuffer(3, data.buffer); - state.step(); - buffersStorage.reuseFreeBuffer(data); - } - state.dispose(); - } + putUsersInternal(users); + putChatsInternal(chats); if (withTransaction) { database.commitTransaction(); } @@ -2999,16 +2958,10 @@ public class MessagesStorage { usersDict.put(user.id, user); } ArrayList loadedUsers = new ArrayList(); - SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, status FROM users WHERE uid IN(%s)", ids.toString())); - while (cursor.next()) { - ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); - if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { - TLRPC.User user = (TLRPC.User)TLClassStore.Instance().TLdeserialize(data, data.readInt32()); - loadedUsers.add(user); - if (user.status != null) { - user.status.expires = cursor.intValue(1); - } - TLRPC.User updateUser = usersDict.get(user.id); + getUsersInternal(ids.toString(), loadedUsers); + for (TLRPC.User user : loadedUsers) { + TLRPC.User updateUser = usersDict.get(user.id); + if (updateUser != null) { if (updateUser.first_name != null && updateUser.last_name != null) { user.first_name = updateUser.first_name; user.last_name = updateUser.last_name; @@ -3017,35 +2970,13 @@ public class MessagesStorage { user.photo = updateUser.photo; } } - buffersStorage.reuseFreeBuffer(data); } - cursor.dispose(); + if (!loadedUsers.isEmpty()) { if (withTransaction) { database.beginTransaction(); } - SQLitePreparedStatement state = database.executeFast("REPLACE INTO users VALUES(?, ?, ?, ?)"); - for (TLRPC.User user : loadedUsers) { - state.requery(); - ByteBufferDesc data = buffersStorage.getFreeBuffer(user.getObjectSize()); - user.serializeToStream(data); - state.bindInteger(1, user.id); - if (user.first_name != null && user.last_name != null) { - String name = (user.first_name + " " + user.last_name).toLowerCase(); - state.bindString(2, name); - } else { - state.bindString(2, ""); - } - if (user.status != null) { - state.bindInteger(3, user.status.expires); - } else { - state.bindInteger(3, 0); - } - state.bindByteBuffer(4, data.buffer); - state.step(); - buffersStorage.reuseFreeBuffer(data); - } - state.dispose(); + putUsersInternal(loadedUsers); if (withTransaction) { database.commitTransaction(); } @@ -3308,58 +3239,15 @@ public class MessagesStorage { cursor.dispose(); if (!encryptedToLoad.isEmpty()) { - String toLoad = TextUtils.join(",", encryptedToLoad); - cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, user, g, authkey, ttl, layer, seq_in, seq_out FROM enc_chats WHERE uid IN(%s)", toLoad)); - while (cursor.next()) { - ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); - if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { - TLRPC.EncryptedChat chat = (TLRPC.EncryptedChat)TLClassStore.Instance().TLdeserialize(data, data.readInt32()); - encryptedChats.add(chat); - chat.user_id = cursor.intValue(1); - if (!usersToLoad.contains(chat.user_id)) { - usersToLoad.add(chat.user_id); - } - chat.a_or_b = cursor.byteArrayValue(2); - chat.auth_key = cursor.byteArrayValue(3); - chat.ttl = cursor.intValue(4); - chat.layer = cursor.intValue(5); - chat.seq_in = cursor.intValue(6); - chat.seq_out = cursor.intValue(7); - } - buffersStorage.reuseFreeBuffer(data); - } - cursor.dispose(); + getEncryptedChatsInternal(TextUtils.join(",", encryptedToLoad), encryptedChats, usersToLoad); } if (!chatsToLoad.isEmpty()) { - String toLoad = TextUtils.join(",", chatsToLoad); - cursor = database.queryFinalized(String.format(Locale.US, "SELECT data FROM chats WHERE uid IN(%s)", toLoad)); - while (cursor.next()) { - ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); - if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { - TLRPC.Chat chat = (TLRPC.Chat)TLClassStore.Instance().TLdeserialize(data, data.readInt32()); - dialogs.chats.add(chat); - } - buffersStorage.reuseFreeBuffer(data); - } - cursor.dispose(); + getChatsInternal(TextUtils.join(",", chatsToLoad), dialogs.chats); } if (!usersToLoad.isEmpty()) { - String toLoad = TextUtils.join(",", usersToLoad); - cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, status FROM users WHERE uid IN(%s)", toLoad)); - while (cursor.next()) { - ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); - if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { - TLRPC.User user = (TLRPC.User)TLClassStore.Instance().TLdeserialize(data, data.readInt32()); - if (user.status != null) { - user.status.expires = cursor.intValue(1); - } - dialogs.users.add(user); - } - buffersStorage.reuseFreeBuffer(data); - } - cursor.dispose(); + getUsersInternal(TextUtils.join(",", usersToLoad), dialogs.users); } if (!dialogs.dialogs.isEmpty() || !encryptedChats.isEmpty()) { @@ -3442,49 +3330,8 @@ public class MessagesStorage { state.dispose(); state2.dispose(); } - if (!messages.users.isEmpty()) { - SQLitePreparedStatement state = database.executeFast("REPLACE INTO users VALUES(?, ?, ?, ?)"); - for (TLRPC.User user : messages.users) { - state.requery(); - ByteBufferDesc data = buffersStorage.getFreeBuffer(user.getObjectSize()); - user.serializeToStream(data); - state.bindInteger(1, user.id); - if (user.first_name != null && user.last_name != null) { - String name = (user.first_name + " " + user.last_name).toLowerCase(); - state.bindString(2, name); - } else { - state.bindString(2, ""); - } - if (user.status != null) { - state.bindInteger(3, user.status.expires); - } else { - state.bindInteger(3, 0); - } - state.bindByteBuffer(4, data.buffer); - state.step(); - buffersStorage.reuseFreeBuffer(data); - } - state.dispose(); - } - if (!messages.chats.isEmpty()) { - SQLitePreparedStatement state = database.executeFast("REPLACE INTO chats VALUES(?, ?, ?)"); - for (TLRPC.Chat chat : messages.chats) { - state.requery(); - ByteBufferDesc data = buffersStorage.getFreeBuffer(chat.getObjectSize()); - chat.serializeToStream(data); - state.bindInteger(1, chat.id); - if (chat.title != null) { - String name = chat.title.toLowerCase(); - state.bindString(2, name); - } else { - state.bindString(2, ""); - } - state.bindByteBuffer(3, data.buffer); - state.step(); - buffersStorage.reuseFreeBuffer(data); - } - state.dispose(); - } + putUsersInternal(messages.users); + putChatsInternal(messages.chats); database.commitTransaction(); } catch (Exception e) { @@ -3567,76 +3414,15 @@ public class MessagesStorage { cursor.dispose(); if (!encryptedToLoad.isEmpty()) { - String toLoad = TextUtils.join(",", encryptedToLoad); - cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, user, g, authkey, ttl, layer, seq_in, seq_out FROM enc_chats WHERE uid IN(%s)", toLoad)); - while (cursor.next()) { - try { - ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); - if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { - TLRPC.EncryptedChat chat = (TLRPC.EncryptedChat)TLClassStore.Instance().TLdeserialize(data, data.readInt32()); - if (chat != null) { - encryptedChats.add(chat); - chat.user_id = cursor.intValue(1); - if (!usersToLoad.contains(chat.user_id)) { - usersToLoad.add(chat.user_id); - } - chat.a_or_b = cursor.byteArrayValue(2); - chat.auth_key = cursor.byteArrayValue(3); - chat.ttl = cursor.intValue(4); - chat.layer = cursor.intValue(5); - chat.seq_in = cursor.intValue(6); - chat.seq_out = cursor.intValue(7); - } - } - buffersStorage.reuseFreeBuffer(data); - } catch (Exception e) { - FileLog.e("tmessages", e); - } - } - cursor.dispose(); + getEncryptedChatsInternal(TextUtils.join(",", encryptedToLoad), encryptedChats, usersToLoad); } if (!chatsToLoad.isEmpty()) { - String toLoad = TextUtils.join(",", chatsToLoad); - cursor = database.queryFinalized(String.format(Locale.US, "SELECT data FROM chats WHERE uid IN(%s)", toLoad)); - while (cursor.next()) { - try { - ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); - if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { - TLRPC.Chat chat = (TLRPC.Chat)TLClassStore.Instance().TLdeserialize(data, data.readInt32()); - if (chat != null) { - dialogs.chats.add(chat); - } - } - buffersStorage.reuseFreeBuffer(data); - } catch (Exception e) { - FileLog.e("tmessages", e); - } - } - cursor.dispose(); + getChatsInternal(TextUtils.join(",", chatsToLoad), dialogs.chats); } if (!usersToLoad.isEmpty()) { - String toLoad = TextUtils.join(",", usersToLoad); - cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, status FROM users WHERE uid IN(%s)", toLoad)); - while (cursor.next()) { - try { - ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); - if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { - TLRPC.User user = (TLRPC.User)TLClassStore.Instance().TLdeserialize(data, data.readInt32()); - if (user != null) { - if (user.status != null) { - user.status.expires = cursor.intValue(1); - } - dialogs.users.add(user); - } - } - buffersStorage.reuseFreeBuffer(data); - } catch (Exception e) { - FileLog.e("tmessages", e); - } - } - cursor.dispose(); + getUsersInternal(TextUtils.join(",", usersToLoad), dialogs.users); } MessagesController.getInstance().processLoadedDialogs(dialogs, encryptedChats, offset, serverOffset, count, true, false); } catch (Exception e) { @@ -3725,50 +3511,8 @@ public class MessagesStorage { state4.dispose(); } - if (!dialogs.users.isEmpty()) { - SQLitePreparedStatement state = database.executeFast("REPLACE INTO users VALUES(?, ?, ?, ?)"); - for (TLRPC.User user : dialogs.users) { - state.requery(); - ByteBufferDesc data = buffersStorage.getFreeBuffer(user.getObjectSize()); - user.serializeToStream(data); - state.bindInteger(1, user.id); - if (user.first_name != null && user.last_name != null) { - String name = (user.first_name + " " + user.last_name).toLowerCase(); - state.bindString(2, name); - } else { - state.bindString(2, ""); - } - if (user.status != null) { - state.bindInteger(3, user.status.expires); - } else { - state.bindInteger(3, 0); - } - state.bindByteBuffer(4, data.buffer); - state.step(); - buffersStorage.reuseFreeBuffer(data); - } - state.dispose(); - } - - if (!dialogs.chats.isEmpty()) { - SQLitePreparedStatement state = database.executeFast("REPLACE INTO chats VALUES(?, ?, ?)"); - for (TLRPC.Chat chat : dialogs.chats) { - state.requery(); - ByteBufferDesc data = buffersStorage.getFreeBuffer(chat.getObjectSize()); - chat.serializeToStream(data); - state.bindInteger(1, chat.id); - if (chat.title != null) { - String name = chat.title.toLowerCase(); - state.bindString(2, name); - } else { - state.bindString(2, ""); - } - state.bindByteBuffer(3, data.buffer); - state.step(); - buffersStorage.reuseFreeBuffer(data); - } - state.dispose(); - } + putUsersInternal(dialogs.users); + putChatsInternal(dialogs.chats); database.commitTransaction(); @@ -3783,56 +3527,23 @@ public class MessagesStorage { public TLRPC.User getUser(final int user_id) { TLRPC.User user = null; try { - SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, status FROM users WHERE uid = %d", user_id)); - if (cursor.next()) { - ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); - if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { - user = (TLRPC.User) TLClassStore.Instance().TLdeserialize(data, data.readInt32()); - if (user != null) { - if (user.status != null) { - user.status.expires = cursor.intValue(1); - } - } - } - buffersStorage.reuseFreeBuffer(data); + ArrayList users = new ArrayList(); + getUsersInternal("" + user_id, users); + if (!users.isEmpty()) { + user = users.get(0); } - cursor.dispose(); } catch (Exception e) { FileLog.e("tmessages", e); } return user; } - public ArrayList getUsers(final ArrayList uids, final boolean[] error) { + public ArrayList getUsers(final ArrayList uids) { ArrayList users = new ArrayList(); try { - String uidsStr = TextUtils.join(",", uids); - - SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, status FROM users WHERE uid IN (%s)", uidsStr)); - while (cursor.next()) { - ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); - if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { - TLRPC.User user = (TLRPC.User) TLClassStore.Instance().TLdeserialize(data, data.readInt32()); - if (user != null) { - if (user.status != null) { - user.status.expires = cursor.intValue(1); - } - users.add(user); - } else { - error[0] = true; - buffersStorage.reuseFreeBuffer(data); - break; - } - } else { - error[0] = true; - buffersStorage.reuseFreeBuffer(data); - break; - } - buffersStorage.reuseFreeBuffer(data); - } - cursor.dispose(); + getUsersInternal(TextUtils.join(",", uids), users); } catch (Exception e) { - error[0] = true; + users.clear(); FileLog.e("tmessages", e); } return users; @@ -3841,15 +3552,11 @@ public class MessagesStorage { public TLRPC.Chat getChat(final int chat_id) { TLRPC.Chat chat = null; try { - SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT data FROM chats WHERE uid = %d", chat_id)); - if (cursor.next()) { - ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); - if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { - chat = (TLRPC.Chat) TLClassStore.Instance().TLdeserialize(data, data.readInt32()); - } - buffersStorage.reuseFreeBuffer(data); + ArrayList chats = new ArrayList(); + getChatsInternal("" + chat_id, chats); + if (!chats.isEmpty()) { + chat = chats.get(0); } - cursor.dispose(); } catch (Exception e) { FileLog.e("tmessages", e); } @@ -3859,24 +3566,11 @@ public class MessagesStorage { public TLRPC.EncryptedChat getEncryptedChat(final int chat_id) { TLRPC.EncryptedChat chat = null; try { - SQLiteCursor cursor = database.queryFinalized(String.format(Locale.US, "SELECT data, user, g, authkey, ttl, layer, seq_in, seq_out FROM enc_chats WHERE uid = %d", chat_id)); - if (cursor.next()) { - ByteBufferDesc data = buffersStorage.getFreeBuffer(cursor.byteArrayLength(0)); - if (data != null && cursor.byteBufferValue(0, data.buffer) != 0) { - chat = (TLRPC.EncryptedChat) TLClassStore.Instance().TLdeserialize(data, data.readInt32()); - if (chat != null) { - chat.user_id = cursor.intValue(1); - chat.a_or_b = cursor.byteArrayValue(2); - chat.auth_key = cursor.byteArrayValue(3); - chat.ttl = cursor.intValue(4); - chat.layer = cursor.intValue(5); - chat.seq_in = cursor.intValue(6); - chat.seq_out = cursor.intValue(7); - } - } - buffersStorage.reuseFreeBuffer(data); + ArrayList encryptedChats = new ArrayList(); + getEncryptedChatsInternal("" + chat_id, encryptedChats, null); + if (!encryptedChats.isEmpty()) { + chat = encryptedChats.get(0); } - cursor.dispose(); } catch (Exception e) { FileLog.e("tmessages", e); } diff --git a/TMessagesProj/src/main/java/org/telegram/android/SendMessagesHelper.java b/TMessagesProj/src/main/java/org/telegram/android/SendMessagesHelper.java index 27cdd0368..770a38c55 100644 --- a/TMessagesProj/src/main/java/org/telegram/android/SendMessagesHelper.java +++ b/TMessagesProj/src/main/java/org/telegram/android/SendMessagesHelper.java @@ -626,10 +626,12 @@ public class SendMessagesHelper implements NotificationCenter.NotificationCenter newMsg.to_id.user_id = encryptedChat.participant_id; } newMsg.ttl = encryptedChat.ttl; - if (newMsg.media instanceof TLRPC.TL_messageMediaAudio) { - newMsg.ttl = Math.max(encryptedChat.ttl, newMsg.media.audio.duration + 1); - } else if (newMsg.media instanceof TLRPC.TL_messageMediaVideo) { - newMsg.ttl = Math.max(encryptedChat.ttl, newMsg.media.video.duration + 1); + if (newMsg.ttl != 0) { + if (newMsg.media instanceof TLRPC.TL_messageMediaAudio) { + newMsg.ttl = Math.max(encryptedChat.ttl, newMsg.media.audio.duration + 1); + } else if (newMsg.media instanceof TLRPC.TL_messageMediaVideo) { + newMsg.ttl = Math.max(encryptedChat.ttl, newMsg.media.video.duration + 1); + } } } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/Adapters/ContactsActivitySearchAdapter.java b/TMessagesProj/src/main/java/org/telegram/ui/Adapters/ContactsActivitySearchAdapter.java index cab7b6471..d58d86bdf 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/Adapters/ContactsActivitySearchAdapter.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/Adapters/ContactsActivitySearchAdapter.java @@ -99,12 +99,25 @@ public class ContactsActivitySearchAdapter extends BaseContactsSearchAdapter { for (TLRPC.TL_contact contact : contactsCopy) { TLRPC.User user = MessagesController.getInstance().getUser(contact.user_id); + if (user.id == UserConfig.getClientUserId()) { + continue; + } + String name = ContactsController.formatName(user.first_name, user.last_name).toLowerCase(); + + int found = 0; if (name.startsWith(q) || name.contains(" " + q)) { - if (user.id == UserConfig.getClientUserId()) { - continue; + found = 1; + } else if (user.username != null && user.username.startsWith(q)) { + found = 2; + } + + if (found != 0) { + if (found == 1) { + resultArrayNames.add(Utilities.generateSearchName(user.first_name, user.last_name, q)); + } else { + resultArrayNames.add(Utilities.generateSearchName("@" + user.username, null, "@" + q)); } - resultArrayNames.add(Utilities.generateSearchName(user.first_name, user.last_name, q)); resultArray.add(user); } } @@ -197,7 +210,16 @@ public class ContactsActivitySearchAdapter extends BaseContactsSearchAdapter { TLRPC.User user = getItem(i); if (user != null) { CharSequence username = null; - if (i > searchResult.size() && user.username != null) { + CharSequence name = null; + if (i < searchResult.size()) { + name = searchResultNames.get(i); + if (name != null && user != null && user.username != null && user.username.length() > 0) { + if (name.toString().startsWith("@" + user.username)) { + username = name; + name = null; + } + } + } else if (i > searchResult.size() && user.username != null) { try { username = Html.fromHtml(String.format("@%s%s", user.username.substring(0, lastFoundUsername.length()), user.username.substring(lastFoundUsername.length()))); } catch (Exception e) { @@ -206,7 +228,7 @@ public class ContactsActivitySearchAdapter extends BaseContactsSearchAdapter { } } - ((ChatOrUserCell) view).setData(user, null, null, i < searchResult.size() ? searchResultNames.get(i) : null, username); + ((ChatOrUserCell) view).setData(user, null, null, name, username); if (ignoreUsers != null) { if (ignoreUsers.containsKey(user.id)) { diff --git a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java index e5918f4bc..c4af50a4c 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java @@ -1040,7 +1040,7 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not } private boolean sendSecretMessageRead(MessageObject messageObject) { - if (messageObject == null || messageObject.isOut() || !messageObject.isSecretMedia() || messageObject.messageOwner.destroyTime != 0) { + if (messageObject == null || messageObject.isOut() || !messageObject.isSecretMedia() || messageObject.messageOwner.destroyTime != 0 || messageObject.messageOwner.ttl <= 0) { return false; } MessagesController.getInstance().markMessageAsRead(dialog_id, messageObject.messageOwner.random_id, messageObject.messageOwner.ttl); diff --git a/TMessagesProj/src/main/java/org/telegram/ui/ChatProfileActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/ChatProfileActivity.java index dc7dd128d..f8f556b48 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/ChatProfileActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/ChatProfileActivity.java @@ -477,7 +477,7 @@ public class ChatProfileActivity extends BaseFragment implements NotificationCen args.putBoolean("destroyAfterSelect", true); args.putBoolean("usersAsSections", true); args.putBoolean("returnAsResult", true); - args.putBoolean("allowUsernameSearch", false); + //args.putBoolean("allowUsernameSearch", false); if (chat_id > 0) { args.putString("selectAlertString", LocaleController.getString("AddToTheGroup", R.string.AddToTheGroup)); } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/GroupCreateFinalActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/GroupCreateFinalActivity.java index 972f87c4d..331dc705f 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/GroupCreateFinalActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/GroupCreateFinalActivity.java @@ -82,11 +82,10 @@ public class GroupCreateFinalActivity extends BaseFragment implements Notificati if (!usersToLoad.isEmpty()) { final Semaphore semaphore = new Semaphore(0); final ArrayList users = new ArrayList(); - final boolean[] error = new boolean[1]; MessagesStorage.getInstance().storageQueue.postRunnable(new Runnable() { @Override public void run() { - users.addAll(MessagesStorage.getInstance().getUsers(usersToLoad, error)); + users.addAll(MessagesStorage.getInstance().getUsers(usersToLoad)); semaphore.release(); } }); @@ -95,7 +94,7 @@ public class GroupCreateFinalActivity extends BaseFragment implements Notificati } catch (Exception e) { FileLog.e("tmessages", e); } - if (error[0]) { + if (usersToLoad.size() != users.size()) { return false; } if (!users.isEmpty()) { diff --git a/TMessagesProj/src/main/java/org/telegram/ui/MessagesActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/MessagesActivity.java index 6f2b8ec6b..477a519a5 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/MessagesActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/MessagesActivity.java @@ -775,17 +775,19 @@ public class MessagesActivity extends BaseFragment implements NotificationCenter @Override public View getView(int i, View view, ViewGroup viewGroup) { - if (searching && searchWas) { - if (i == searchResult.size()) { - if (view == null) { - view = new SettingsSectionLayout(mContext); - ((SettingsSectionLayout) view).setText(LocaleController.getString("GlobalSearch", R.string.GlobalSearch)); - view.setPadding(AndroidUtilities.dp(11), 0, AndroidUtilities.dp(11), 0); - } - } else { - if (view == null) { - view = new ChatOrUserCell(mContext); - } + int type = getItemViewType(i); + + if (type == 3) { + if (view == null) { + view = new SettingsSectionLayout(mContext); + ((SettingsSectionLayout) view).setText(LocaleController.getString("GlobalSearch", R.string.GlobalSearch)); + view.setPadding(AndroidUtilities.dp(11), 0, AndroidUtilities.dp(11), 0); + } + } else if (type == 2) { + if (view == null) { + view = new ChatOrUserCell(mContext); + } + if (searching && searchWas) { TLRPC.User user = null; TLRPC.Chat chat = null; TLRPC.EncryptedChat encryptedChat = null; @@ -795,7 +797,7 @@ public class MessagesActivity extends BaseFragment implements NotificationCenter if (obj instanceof TLRPC.User) { user = MessagesController.getInstance().getUser(((TLRPC.User) obj).id); if (user == null) { - user = (TLRPC.User)obj; + user = (TLRPC.User) obj; } } else if (obj instanceof TLRPC.Chat) { chat = MessagesController.getInstance().getChat(((TLRPC.Chat) obj).id); @@ -805,7 +807,16 @@ public class MessagesActivity extends BaseFragment implements NotificationCenter } CharSequence username = null; - if (i > searchResult.size() && user != null && user.username != null) { + CharSequence name = null; + if (i < searchResult.size()) { + name = searchResultNames.get(i); + if (name != null && user != null && user.username != null && user.username.length() > 0) { + if (name.toString().startsWith("@" + user.username)) { + username = name; + name = null; + } + } + } else if (i > searchResult.size() && user != null && user.username != null) { try { username = Html.fromHtml(String.format("@%s%s", user.username.substring(0, lastFoundUsername.length()), user.username.substring(lastFoundUsername.length()))); } catch (Exception e) { @@ -814,36 +825,31 @@ public class MessagesActivity extends BaseFragment implements NotificationCenter } } - ((ChatOrUserCell) view).setData(user, chat, encryptedChat, i < searchResult.size() ? searchResultNames.get(i) : null, username); + ((ChatOrUserCell) view).setData(user, chat, encryptedChat, name, username); } - - return view; - } - int type = getItemViewType(i); - if (type == 1) { + } else if (type == 1) { if (view == null) { LayoutInflater li = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = li.inflate(R.layout.loading_more_layout, viewGroup, false); } - return view; - } - - if (view == null) { - view = new DialogCell(mContext); - } - ((DialogCell) view).useSeparator = (i != getCount() - 1); - if (serverOnly) { - ((DialogCell)view).setDialog(MessagesController.getInstance().dialogsServerOnly.get(i)); - } else { - TLRPC.TL_dialog dialog = MessagesController.getInstance().dialogs.get(i); - if (AndroidUtilities.isTablet()) { - if (dialog.id == openedDialogId) { - view.setBackgroundColor(0x0f000000); - } else { - view.setBackgroundColor(0); - } + } else if (type == 0) { + if (view == null) { + view = new DialogCell(mContext); + } + ((DialogCell) view).useSeparator = (i != getCount() - 1); + if (serverOnly) { + ((DialogCell) view).setDialog(MessagesController.getInstance().dialogsServerOnly.get(i)); + } else { + TLRPC.TL_dialog dialog = MessagesController.getInstance().dialogs.get(i); + if (AndroidUtilities.isTablet()) { + if (dialog.id == openedDialogId) { + view.setBackgroundColor(0x0f000000); + } else { + view.setBackgroundColor(0); + } + } + ((DialogCell) view).setDialog(dialog); } - ((DialogCell)view).setDialog(dialog); } return view; diff --git a/TMessagesProj/src/main/java/org/telegram/ui/PhotoViewer.java b/TMessagesProj/src/main/java/org/telegram/ui/PhotoViewer.java index a0a349618..c566a2ba3 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/PhotoViewer.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/PhotoViewer.java @@ -1173,6 +1173,7 @@ public class PhotoViewer implements NotificationCenter.NotificationCenterDelegat menuItem.hideSubItem(gallery_menu_save); shareButton.setVisibility(View.GONE); } else { + menuItem.showSubItem(gallery_menu_save); shareButton.setVisibility(View.VISIBLE); } setImageIndex(0, true); @@ -1215,6 +1216,7 @@ public class PhotoViewer implements NotificationCenter.NotificationCenterDelegat menuItem.hideSubItem(gallery_menu_save); shareButton.setVisibility(View.GONE); } else { + menuItem.showSubItem(gallery_menu_save); shareButton.setVisibility(View.VISIBLE); } opennedFromMedia = true; diff --git a/TMessagesProj/src/main/java/org/telegram/ui/SettingsChangeUsernameActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/SettingsChangeUsernameActivity.java index d5fdfb342..3f893ca79 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/SettingsChangeUsernameActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/SettingsChangeUsernameActivity.java @@ -194,6 +194,9 @@ public class SettingsChangeUsernameActivity extends BaseFragment { } private void showErrorAlert(String error) { + if (getParentActivity() == null) { + return; + } AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity()); builder.setTitle(LocaleController.getString("AppName", R.string.AppName)); if (error.equals("USERNAME_INVALID")) { diff --git a/TMessagesProj/src/main/java/org/telegram/ui/VideoEditorActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/VideoEditorActivity.java index 6a9c7027c..18ed3fd96 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/VideoEditorActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/VideoEditorActivity.java @@ -457,11 +457,17 @@ public class VideoEditorActivity extends BaseFragment implements TextureView.Sur } private void onPlayComplete() { - playButton.setImageResource(R.drawable.video_play); - videoSeekBarView.setProgress(videoTimelineView.getLeftProgress()); + if (playButton != null) { + playButton.setImageResource(R.drawable.video_play); + } + if (videoSeekBarView != null && videoTimelineView != null) { + videoSeekBarView.setProgress(videoTimelineView.getLeftProgress()); + } try { if (videoPlayer != null) { - videoPlayer.seekTo((int) (videoTimelineView.getLeftProgress() * videoDuration)); + if (videoTimelineView != null) { + videoPlayer.seekTo((int) (videoTimelineView.getLeftProgress() * videoDuration)); + } } } catch (Exception e) { FileLog.e("tmessages", e); diff --git a/TMessagesProj/src/main/java/org/telegram/ui/Views/ChatActivityEnterView.java b/TMessagesProj/src/main/java/org/telegram/ui/Views/ChatActivityEnterView.java index 96c92a174..5dbd1fdbb 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/Views/ChatActivityEnterView.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/Views/ChatActivityEnterView.java @@ -597,24 +597,22 @@ public class ChatActivityEnterView implements NotificationCenter.NotificationCen messsageEditText.dispatchKeyEvent(new KeyEvent(0, 67)); } - public void onEmojiSelected(String paramAnonymousString) { + public void onEmojiSelected(String symbol) { int i = messsageEditText.getSelectionEnd(); - CharSequence localCharSequence = Emoji.replaceEmoji(paramAnonymousString, messsageEditText.getPaint().getFontMetricsInt(), AndroidUtilities.dp(20)); - messsageEditText.setText(messsageEditText.getText().insert(i, localCharSequence)); - int j = i + localCharSequence.length(); - messsageEditText.setSelection(j, j); + if (i < 0) { + i = 0; + } + try { + CharSequence localCharSequence = Emoji.replaceEmoji(symbol, messsageEditText.getPaint().getFontMetricsInt(), AndroidUtilities.dp(20)); + messsageEditText.setText(messsageEditText.getText().insert(i, localCharSequence)); + int j = i + localCharSequence.length(); + messsageEditText.setSelection(j, j); + } catch (Exception e) { + FileLog.e("tmessages", e); + } } }); emojiPopup = new PopupWindow(emojiView); - - /*try { - Method method = emojiPopup.getClass().getMethod("setWindowLayoutType", int.class); - if (method != null) { - method.invoke(emojiPopup, WindowManager.LayoutParams.LAST_SUB_WINDOW); - } - } catch (Exception e) { - //don't promt - }*/ } public void setDelegate(ChatActivityEnterViewDelegate delegate) { diff --git a/TMessagesProj/src/main/res/anim/scale_in.xml b/TMessagesProj/src/main/res/anim/scale_in.xml index 406fd345a..a0aa6293b 100644 --- a/TMessagesProj/src/main/res/anim/scale_in.xml +++ b/TMessagesProj/src/main/res/anim/scale_in.xml @@ -20,12 +20,12 @@ android:fromAlpha="0.0" android:toAlpha="1.0" android:interpolator="@anim/decelerate_cubic" - android:duration="220"/> + android:duration="150"/> + android:duration="150"/> \ No newline at end of file diff --git a/TMessagesProj/src/main/res/anim/scale_out.xml b/TMessagesProj/src/main/res/anim/scale_out.xml index 17bd0e7cd..11135b976 100644 --- a/TMessagesProj/src/main/res/anim/scale_out.xml +++ b/TMessagesProj/src/main/res/anim/scale_out.xml @@ -20,7 +20,7 @@ android:fromAlpha="1.0" android:toAlpha="0.0" android:interpolator="@anim/decelerate_cubic" - android:duration="220"/> + android:duration="150"/> + android:duration="150"/> \ No newline at end of file