From ddd022ef6f6db0c02923b9efef38ab68b5e643bc Mon Sep 17 00:00:00 2001 From: DrKLO Date: Wed, 5 Mar 2014 20:42:10 +0400 Subject: [PATCH] Update to 1.3.26 (184) https://github.com/DrKLO/Telegram/pull/216 Bug fixes --- TMessagesProj/src/main/AndroidManifest.xml | 2 +- .../telegram/messenger/BuffersStorage.java | 62 +++++----- .../messenger/ConnectionsManager.java | 20 ++- .../messenger/ContactsController.java | 73 +++++++---- .../messenger/MessagesController.java | 44 ++++--- .../org/telegram/messenger/TcpConnection.java | 2 +- .../telegram/ui/LoginActivityPhoneView.java | 117 ++++++++++-------- .../org/telegram/ui/PhotoCropActivity.java | 6 + TMessagesProj/src/main/res/values/strings.xml | 38 +++--- 9 files changed, 205 insertions(+), 159 deletions(-) diff --git a/TMessagesProj/src/main/AndroidManifest.xml b/TMessagesProj/src/main/AndroidManifest.xml index 3f7fa05c0..8f6bfd84b 100644 --- a/TMessagesProj/src/main/AndroidManifest.xml +++ b/TMessagesProj/src/main/AndroidManifest.xml @@ -1,7 +1,7 @@ freeBuffers128; - private final ConcurrentLinkedQueue freeBuffers1024; - private final ConcurrentLinkedQueue freeBuffers4096; - private final ConcurrentLinkedQueue freeBuffers16384; - private final ConcurrentLinkedQueue freeBuffers32768; + private final ArrayList freeBuffers128; + private final ArrayList freeBuffers1024; + private final ArrayList freeBuffers4096; + private final ArrayList freeBuffers16384; + private final ArrayList freeBuffers32768; public BuffersStorage() { - freeBuffers128 = new ConcurrentLinkedQueue(); - freeBuffers1024 = new ConcurrentLinkedQueue(); - freeBuffers4096 = new ConcurrentLinkedQueue(); - freeBuffers16384 = new ConcurrentLinkedQueue(); - freeBuffers32768 = new ConcurrentLinkedQueue(); + freeBuffers128 = new ArrayList(); + freeBuffers1024 = new ArrayList(); + freeBuffers4096 = new ArrayList(); + freeBuffers16384 = new ArrayList(); + freeBuffers32768 = new ArrayList(); for (int a = 0; a < 5; a++) { freeBuffers128.add(new ByteBufferDesc(128)); @@ -47,7 +47,10 @@ public class BuffersStorage { ByteBufferDesc buffer = null; if (size <= 128) { synchronized (freeBuffers128) { - buffer = freeBuffers128.poll(); + if (freeBuffers128.size() > 0) { + buffer = freeBuffers128.get(0); + freeBuffers128.remove(0); + } } if (buffer == null) { buffer = new ByteBufferDesc(128); @@ -55,7 +58,10 @@ public class BuffersStorage { } } else if (size <= 1024 + 200) { synchronized (freeBuffers1024) { - buffer = freeBuffers1024.poll(); + if (freeBuffers1024.size() > 0) { + buffer = freeBuffers1024.get(0); + freeBuffers1024.remove(0); + } } if (buffer == null) { buffer = new ByteBufferDesc(1024 + 200); @@ -63,7 +69,10 @@ public class BuffersStorage { } } else if (size <= 4096 + 200) { synchronized (freeBuffers4096) { - buffer = freeBuffers4096.poll(); + if (freeBuffers4096.size() > 0) { + buffer = freeBuffers4096.get(0); + freeBuffers4096.remove(0); + } } if (buffer == null) { buffer = new ByteBufferDesc(4096 + 200); @@ -71,7 +80,10 @@ public class BuffersStorage { } } else if (size <= 16384 + 200) { synchronized (freeBuffers16384) { - buffer = freeBuffers16384.poll(); + if (freeBuffers16384.size() > 0) { + buffer = freeBuffers16384.get(0); + freeBuffers16384.remove(0); + } } if (buffer == null) { buffer = new ByteBufferDesc(16384 + 200); @@ -79,7 +91,10 @@ public class BuffersStorage { } } else if (size <= 40000) { synchronized (freeBuffers32768) { - buffer = freeBuffers32768.poll(); + if (freeBuffers32768.size() > 0) { + buffer = freeBuffers32768.get(0); + freeBuffers32768.remove(0); + } } if (buffer == null) { buffer = new ByteBufferDesc(40000); @@ -98,37 +113,22 @@ public class BuffersStorage { } if (buffer.buffer.capacity() == 128) { synchronized (freeBuffers128) { - if (freeBuffers128.contains(buffer)) { - throw new RuntimeException("already containing buffer! 0"); - } freeBuffers128.add(buffer); } } else if (buffer.buffer.capacity() == 1024 + 200) { synchronized (freeBuffers1024) { - if (freeBuffers1024.contains(buffer)) { - throw new RuntimeException("already containing buffer! 1"); - } freeBuffers1024.add(buffer); } } else if (buffer.buffer.capacity() == 4096 + 200) { synchronized (freeBuffers4096) { - if (freeBuffers4096.contains(buffer)) { - throw new RuntimeException("already containing buffer! 2"); - } freeBuffers4096.add(buffer); } } else if (buffer.buffer.capacity() == 16384 + 200) { synchronized (freeBuffers16384) { - if (freeBuffers16384.contains(buffer)) { - throw new RuntimeException("already containing buffer! 3"); - } freeBuffers16384.add(buffer); } } else if (buffer.buffer.capacity() == 40000) { synchronized (freeBuffers32768) { - if (freeBuffers32768.contains(buffer)) { - throw new RuntimeException("already containing buffer! 4"); - } freeBuffers32768.add(buffer); } } diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/ConnectionsManager.java b/TMessagesProj/src/main/java/org/telegram/messenger/ConnectionsManager.java index 159e37259..51cbac368 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/ConnectionsManager.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/ConnectionsManager.java @@ -919,20 +919,16 @@ public class ConnectionsManager implements Action.ActionDelegate, TcpConnection. final ArrayList unauthorizedDatacenterIds = new ArrayList(); int currentTime = (int)(System.currentTimeMillis() / 1000); - for (RPCRequest request : runningRequests) { + for (int i = 0; i < runningRequests.size(); i++) { + RPCRequest request = runningRequests.get(i); + if (updatingDcSettings && datacenters.size() > 1 && request.rawRequest instanceof TLRPC.TL_help_getConfig) { if (updatingDcStartTime < currentTime - 60) { - updatingDcStartTime = currentTime; - ArrayList allDc = new ArrayList(datacenters.values()); - for (int a = 0; a < allDc.size(); a++) { - Datacenter dc = allDc.get(a); - if (dc.datacenterId == request.runningDatacenterId) { - allDc.remove(a); - break; - } - } - Datacenter newDc = allDc.get(Math.abs(MessagesController.random.nextInt()) % allDc.size()); - request.runningDatacenterId = newDc.datacenterId; + FileLog.e("tmessages", "move TL_help_getConfig to requestQueue"); + requestQueue.add(request); + runningRequests.remove(i); + i--; + continue; } } diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/ContactsController.java b/TMessagesProj/src/main/java/org/telegram/messenger/ContactsController.java index a789191a7..5122ae2af 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/ContactsController.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/ContactsController.java @@ -501,16 +501,16 @@ public class ContactsController { Utilities.RunOnUIThread(new Runnable() { @Override public void run() { - if (ConnectionsManager.DEBUG_VERSION) { - FileLog.e("tmessages", "need delete contacts"); - for (HashMap.Entry c : contactHashMap.entrySet()) { - Contact contact = c.getValue(); - FileLog.e("tmessages", "delete contact " + contact.first_name + " " + contact.last_name); - for (String phone : contact.phones) { - FileLog.e("tmessages", phone); - } - } - } +// if (ConnectionsManager.DEBUG_VERSION) { +// FileLog.e("tmessages", "need delete contacts"); +// for (HashMap.Entry c : contactHashMap.entrySet()) { +// Contact contact = c.getValue(); +// FileLog.e("tmessages", "delete contact " + contact.first_name + " " + contact.last_name); +// for (String phone : contact.phones) { +// FileLog.e("tmessages", phone); +// } +// } +// } final ArrayList toDelete = new ArrayList(); if (contactHashMap != null && !contactHashMap.isEmpty()) { @@ -577,12 +577,12 @@ public class ContactsController { if (request) { if (!toImport.isEmpty()) { - if (ConnectionsManager.DEBUG_VERSION) { - FileLog.e("tmessages", "start import contacts"); - for (TLRPC.TL_inputPhoneContact contact : toImport) { - FileLog.e("tmessages", "add contact " + contact.first_name + " " + contact.last_name + " " + contact.phone); - } - } +// if (ConnectionsManager.DEBUG_VERSION) { +// FileLog.e("tmessages", "start import contacts"); +// for (TLRPC.TL_inputPhoneContact contact : toImport) { +// FileLog.e("tmessages", "add contact " + contact.first_name + " " + contact.last_name + " " + contact.phone); +// } +// } final int count = (int)Math.ceil(toImport.size() / 500.0f); for (int a = 0; a < count; a++) { ArrayList finalToImport = new ArrayList(); @@ -598,15 +598,6 @@ public class ContactsController { FileLog.e("tmessages", "contacts imported"); if (isLastQuery && !contactsMap.isEmpty()) { MessagesStorage.Instance.putCachedPhoneBook(contactsMap); - Utilities.stageQueue.postRunnable(new Runnable() { - @Override - public void run() { - contactsBookSPhones = contactsBookShort; - contactsBook = contactsMap; - contactsSyncInProgress = false; - contactsBookLoaded = true; - } - }); } TLRPC.TL_contacts_importedContacts res = (TLRPC.TL_contacts_importedContacts)response; MessagesStorage.Instance.putUsersAndChats(res.users, null, true, true); @@ -620,6 +611,24 @@ public class ContactsController { } else { FileLog.e("tmessages", "import contacts error " + error.text); } + if (isLastQuery) { + Utilities.stageQueue.postRunnable(new Runnable() { + @Override + public void run() { + contactsBookSPhones = contactsBookShort; + contactsBook = contactsMap; + contactsSyncInProgress = false; + contactsBookLoaded = true; + if (first) { + contactsLoaded = true; + } + if (!delayedContactsUpdate.isEmpty() && contactsLoaded && contactsBookLoaded) { + applyContactsUpdates(delayedContactsUpdate, null, null, null); + delayedContactsUpdate.clear(); + } + } + }); + } } }, null, true, RPCRequest.RPCRequestClassGeneric | RPCRequest.RPCRequestClassFailOnServerErrors | RPCRequest.RPCRequestClassCanCompress); } @@ -631,6 +640,13 @@ public class ContactsController { contactsBook = contactsMap; contactsSyncInProgress = false; contactsBookLoaded = true; + if (first) { + contactsLoaded = true; + } + if (!delayedContactsUpdate.isEmpty() && contactsLoaded && contactsBookLoaded) { + applyContactsUpdates(delayedContactsUpdate, null, null, null); + delayedContactsUpdate.clear(); + } } }); Utilities.RunOnUIThread(new Runnable() { @@ -649,6 +665,13 @@ public class ContactsController { contactsBook = contactsMap; contactsSyncInProgress = false; contactsBookLoaded = true; + if (first) { + contactsLoaded = true; + } + if (!delayedContactsUpdate.isEmpty() && contactsLoaded && contactsBookLoaded) { + applyContactsUpdates(delayedContactsUpdate, null, null, null); + delayedContactsUpdate.clear(); + } } }); if (!contactsMap.isEmpty()) { diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java b/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java index b7b40b8a0..903e71a44 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/MessagesController.java @@ -4418,18 +4418,20 @@ public class MessagesController implements NotificationCenter.NotificationCenter private void updateInterfaceWithMessages(long uid, ArrayList messages) { MessageObject lastMessage = null; - int lastDate = 0; TLRPC.TL_dialog dialog = dialogs_dict.get(uid); + boolean isEncryptedChat = ((int)uid) == 0; + NotificationCenter.Instance.postNotificationName(didReceivedNewMessages, uid, messages); for (MessageObject message : messages) { - if (lastMessage == null || message.messageOwner.date > lastDate) { + if (lastMessage == null || (!isEncryptedChat && message.messageOwner.id > lastMessage.messageOwner.id || isEncryptedChat && message.messageOwner.id < lastMessage.messageOwner.id) || message.messageOwner.date > lastMessage.messageOwner.date) { lastMessage = message; - lastDate = message.messageOwner.date; } } + boolean changed = false; + if (dialog == null) { dialog = new TLRPC.TL_dialog(); dialog.id = uid; @@ -4439,33 +4441,37 @@ public class MessagesController implements NotificationCenter.NotificationCenter dialogs_dict.put(uid, dialog); dialogs.add(dialog); dialogMessage.put(lastMessage.messageOwner.id, lastMessage); + changed = true; } else { - dialogMessage.remove(dialog.top_message); if (dialog.top_message > 0 && lastMessage.messageOwner.id > 0 && lastMessage.messageOwner.id > dialog.top_message || dialog.top_message < 0 && lastMessage.messageOwner.id < 0 && lastMessage.messageOwner.id < dialog.top_message || dialog.last_message_date < lastMessage.messageOwner.date) { + dialogMessage.remove(dialog.top_message); dialog.top_message = lastMessage.messageOwner.id; dialog.last_message_date = lastMessage.messageOwner.date; dialogMessage.put(lastMessage.messageOwner.id, lastMessage); + changed = true; } } - dialogsServerOnly.clear(); - Collections.sort(dialogs, new Comparator() { - @Override - public int compare(TLRPC.TL_dialog tl_dialog, TLRPC.TL_dialog tl_dialog2) { - if (tl_dialog.last_message_date == tl_dialog2.last_message_date) { - return 0; - } else if (tl_dialog.last_message_date < tl_dialog2.last_message_date) { - return 1; - } else { - return -1; + if (changed) { + dialogsServerOnly.clear(); + Collections.sort(dialogs, new Comparator() { + @Override + public int compare(TLRPC.TL_dialog tl_dialog, TLRPC.TL_dialog tl_dialog2) { + if (tl_dialog.last_message_date == tl_dialog2.last_message_date) { + return 0; + } else if (tl_dialog.last_message_date < tl_dialog2.last_message_date) { + return 1; + } else { + return -1; + } + } + }); + for (TLRPC.TL_dialog d : dialogs) { + if ((int)d.id != 0) { + dialogsServerOnly.add(d); } - } - }); - for (TLRPC.TL_dialog d : dialogs) { - if ((int)d.id != 0) { - dialogsServerOnly.add(d); } } } diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/TcpConnection.java b/TMessagesProj/src/main/java/org/telegram/messenger/TcpConnection.java index 8f1aa83e7..301360979 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/TcpConnection.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/TcpConnection.java @@ -439,7 +439,7 @@ public class TcpConnection extends PyroClientAdapter { } if (currentPacketLength % 4 != 0 || currentPacketLength > 2 * 1024 * 1024) { - //FileLog.e("tmessages", "Invalid packet length"); + FileLog.e("tmessages", "Invalid packet length"); reconnect(); return; } diff --git a/TMessagesProj/src/main/java/org/telegram/ui/LoginActivityPhoneView.java b/TMessagesProj/src/main/java/org/telegram/ui/LoginActivityPhoneView.java index 457368b2d..4bdd64574 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/LoginActivityPhoneView.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/LoginActivityPhoneView.java @@ -48,6 +48,8 @@ public class LoginActivityPhoneView extends SlideView implements AdapterView.OnI private EditText phoneField; private TextView countryButton; + private int countryState = 0; + private ArrayList countriesArray = new ArrayList(); private HashMap countriesMap = new HashMap(); private HashMap codesMap = new HashMap(); @@ -104,17 +106,29 @@ public class LoginActivityPhoneView extends SlideView implements AdapterView.OnI ignoreOnTextChange = true; String text = PhoneFormat.stripExceptNumbers(codeField.getText().toString()); codeField.setText(text); - String country = codesMap.get(text); - if (country != null) { - int index = countriesArray.indexOf(country); - if (index != -1) { - ignoreSelection = true; - countryButton.setText(countriesArray.get(index)); + if (text.length() == 0) { + countryButton.setText(R.string.ChooseCountry); + countryState = 1; + } else { + String country = codesMap.get(text); + if (country != null) { + int index = countriesArray.indexOf(country); + if (index != -1) { + ignoreSelection = true; + countryButton.setText(countriesArray.get(index)); - updatePhoneField(); + updatePhoneField(); + countryState = 0; + } else { + countryButton.setText(R.string.WrongCountry); + countryState = 2; + } + } else { + countryButton.setText(R.string.WrongCountry); + countryState = 2; } + codeField.setSelection(codeField.getText().length()); } - codeField.setSelection(codeField.getText().length()); } }); codeField.setOnEditorActionListener(new TextView.OnEditorActionListener() { @@ -198,56 +212,50 @@ public class LoginActivityPhoneView extends SlideView implements AdapterView.OnI } }); - boolean codeProceed = false; + String country = null; - if (!codeProceed) { - String country = "RU"; - - try { - TelephonyManager telephonyManager = (TelephonyManager)ApplicationLoader.applicationContext.getSystemService(Context.TELEPHONY_SERVICE); - if (telephonyManager != null) { - country = telephonyManager.getSimCountryIso().toUpperCase(); - } - } catch (Exception e) { - FileLog.e("tmessages", e); - } - - if (country == null || country.length() == 0) { - try { - Locale current = ApplicationLoader.applicationContext.getResources().getConfiguration().locale; - country = current.getCountry().toUpperCase(); - } catch (Exception e) { - FileLog.e("tmessages", e); - } - } - if (country == null || country.length() == 0) { - country = "RU"; - } - - String countryName = languageMap.get(country); - if (countryName == null) { - countryName = "Russia"; - } - - int index = countriesArray.indexOf(countryName); - if (index != -1) { - codeField.setText(countriesMap.get(countryName)); + try { + TelephonyManager telephonyManager = (TelephonyManager)ApplicationLoader.applicationContext.getSystemService(Context.TELEPHONY_SERVICE); + if (telephonyManager != null) { + country = telephonyManager.getSimCountryIso().toUpperCase(); } + } catch (Exception e) { + FileLog.e("tmessages", e); } + if (country != null) { + String countryName = languageMap.get(country); + if (countryName != null) { + int index = countriesArray.indexOf(countryName); + if (index != -1) { + codeField.setText(countriesMap.get(countryName)); + countryState = 0; + } + } + } + if (codeField.length() == 0) { + countryButton.setText(R.string.ChooseCountry); + countryState = 1; + } + } + + if (codeField.length() != 0) { Utilities.showKeyboard(phoneField); phoneField.requestFocus(); - phoneField.setOnEditorActionListener(new TextView.OnEditorActionListener() { - @Override - public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) { - if (i == EditorInfo.IME_ACTION_NEXT) { - delegate.onNextAction(); - return true; - } - return false; - } - }); + } else { + Utilities.showKeyboard(codeField); + codeField.requestFocus(); } + phoneField.setOnEditorActionListener(new TextView.OnEditorActionListener() { + @Override + public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) { + if (i == EditorInfo.IME_ACTION_NEXT) { + delegate.onNextAction(); + return true; + } + return false; + } + }); } public void selectCountry(String name) { @@ -301,6 +309,13 @@ public class LoginActivityPhoneView extends SlideView implements AdapterView.OnI @Override public void onNextPressed() { + if (countryState == 1) { + delegate.needShowAlert(ApplicationLoader.applicationContext.getString(R.string.ChooseCountry)); + return; + } else if (countryState == 2) { + delegate.needShowAlert(ApplicationLoader.applicationContext.getString(R.string.WrongCountry)); + return; + } if (codeField.length() == 0 || phoneField.length() == 0) { delegate.needShowAlert(ApplicationLoader.applicationContext.getString(R.string.InvalidPhoneNumber)); return; diff --git a/TMessagesProj/src/main/java/org/telegram/ui/PhotoCropActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/PhotoCropActivity.java index f013d9681..a0831589b 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/PhotoCropActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/PhotoCropActivity.java @@ -232,6 +232,12 @@ public class PhotoCropActivity extends BaseFragment { int x = (int)(percX * imageToCrop.getWidth()); int y = (int)(percY * imageToCrop.getHeight()); int size = (int)(percSize * imageToCrop.getWidth()); + if (x + size > imageToCrop.getWidth()) { + size = imageToCrop.getWidth() - x; + } + if (y + size > imageToCrop.getHeight()) { + size = imageToCrop.getHeight() - y; + } try { return Bitmap.createBitmap(imageToCrop, x, y, size, size); } catch (Exception e) { diff --git a/TMessagesProj/src/main/res/values/strings.xml b/TMessagesProj/src/main/res/values/strings.xml index 612f1feee..979a73254 100644 --- a/TMessagesProj/src/main/res/values/strings.xml +++ b/TMessagesProj/src/main/res/values/strings.xml @@ -1,6 +1,6 @@ - + Telegram @@ -48,7 +48,7 @@ %s joined your secret chat. You joined the secret chat. Clear History - Delete and Exit + Delete and exit Hidden Name Select Chat @@ -60,7 +60,7 @@ No files yet... File size shouldn\'t be greater than %1$s Storage not mounted - Usb transfer active + USB transfer active Internal Storage External Storage System Root @@ -102,9 +102,9 @@ Do not allow forwarding %1$d new message %1$d new messages - You were kicked from this group + You were removed from this group You left this group - Delete this Group + Delete this group SLIDE TO CANCEL @@ -126,7 +126,7 @@ %1$s sent you a photo %1$s sent you a video %1$s shared a contact with you - %1$s sent you a map + %1$s sent you a location %1$s sent you a document %1$s sent you an audio %1$s @ %2$s: %3$s @@ -134,15 +134,15 @@ %1$s sent a photo to the group %2$s %1$s sent a video to the group %2$s %1$s shared a contact in the group %2$s - %1$s sent a map to the group %2$s + %1$s sent a location to the group %2$s %1$s sent a document to the group %2$s %1$s sent an audio to the group %2$s %1$s invited you to the group %2$s %1$s edited the group\'s %2$s name %1$s edited the group\'s %2$s photo %1$s invited %3$s to the group %2$s - %1$s kicked %3$s from the group %2$s - %1$s kicked you from the group %2$s + %1$s removed %3$s from the group %2$s + %1$s removed you from the group %2$s %1$s has left the group %2$s %1$s joined Telegram! %1$s,\nWe detected a login into your account from a new device on %2$s\n\nDevice: %3$s\nLocation: %4$s\n\nIf this wasn’t you, you can go to Settings – Terminate all sessions.\n\nThanks,\nThe Telegram Team @@ -178,7 +178,7 @@ Add member Delete and leave group Notifications - Kick from group + Remove from group Share @@ -290,21 +290,21 @@ OK - un1 kicked un2 + un1 removed un2 un1 left group un1 added un2 - un1 removed group photo - un1 changed group photo - un1 changed group name to un2 + un1 removed the group photo + un1 changed the group photo + un1 changed the group name to un2 un1 created the group - You kicked un2 + You removed un2 You left group You added un2 - You removed group photo - You changed group photo - You changed group name to un2 + You removed the group photo + You changed the group photo + You changed the group name to un2 You created the group - un1 kicked you + un1 removed you un1 added you This message is not supported on your version of Telegram. Photo