2013-10-25 17:19:00 +02:00
|
|
|
/*
|
2013-12-20 20:25:49 +01:00
|
|
|
* This is the source code of Telegram for Android v. 1.3.2.
|
2013-10-25 17:19:00 +02:00
|
|
|
* It is licensed under GNU GPL v. 2 or later.
|
|
|
|
* You should have received a copy of the license in this archive (see LICENSE).
|
|
|
|
*
|
|
|
|
* Copyright Nikolai Kudashov, 2013.
|
|
|
|
*/
|
|
|
|
|
2014-07-03 00:39:05 +02:00
|
|
|
package org.telegram.android;
|
2013-10-25 17:19:00 +02:00
|
|
|
|
2014-02-04 19:36:55 +01:00
|
|
|
import android.app.Activity;
|
2015-02-27 20:57:58 +01:00
|
|
|
import android.app.AlertDialog;
|
2013-10-25 17:19:00 +02:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.content.pm.PackageInfo;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.text.Html;
|
2015-02-27 20:57:58 +01:00
|
|
|
import android.util.Base64;
|
2014-10-10 19:16:39 +02:00
|
|
|
import android.util.SparseArray;
|
2013-10-25 17:19:00 +02:00
|
|
|
|
2014-07-03 00:39:05 +02:00
|
|
|
import org.telegram.messenger.ConnectionsManager;
|
|
|
|
import org.telegram.messenger.FileLoader;
|
|
|
|
import org.telegram.messenger.FileLog;
|
|
|
|
import org.telegram.messenger.R;
|
|
|
|
import org.telegram.messenger.RPCRequest;
|
2015-02-27 20:57:58 +01:00
|
|
|
import org.telegram.messenger.SerializedData;
|
|
|
|
import org.telegram.messenger.TLClassStore;
|
2014-07-03 00:39:05 +02:00
|
|
|
import org.telegram.messenger.TLObject;
|
|
|
|
import org.telegram.messenger.TLRPC;
|
|
|
|
import org.telegram.messenger.UserConfig;
|
|
|
|
import org.telegram.messenger.Utilities;
|
2014-12-01 18:56:31 +01:00
|
|
|
import org.telegram.messenger.ApplicationLoader;
|
2015-02-27 20:57:58 +01:00
|
|
|
import org.telegram.ui.ActionBar.BaseFragment;
|
2013-10-25 17:19:00 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
import java.util.concurrent.Semaphore;
|
|
|
|
|
|
|
|
public class MessagesController implements NotificationCenter.NotificationCenterDelegate {
|
2014-12-01 18:56:31 +01:00
|
|
|
|
2015-01-02 23:15:07 +01:00
|
|
|
private ConcurrentHashMap<Integer, TLRPC.Chat> chats = new ConcurrentHashMap<>(100, 1.0f, 2);
|
|
|
|
private ConcurrentHashMap<Integer, TLRPC.EncryptedChat> encryptedChats = new ConcurrentHashMap<>(10, 1.0f, 2);
|
|
|
|
private ConcurrentHashMap<Integer, TLRPC.User> users = new ConcurrentHashMap<>(100, 1.0f, 2);
|
2015-03-19 00:09:45 +01:00
|
|
|
private ConcurrentHashMap<String, TLRPC.User> usersByUsernames = new ConcurrentHashMap<>(100, 1.0f, 2);
|
2015-01-02 23:15:07 +01:00
|
|
|
|
|
|
|
public ArrayList<TLRPC.TL_dialog> dialogs = new ArrayList<>();
|
|
|
|
public ArrayList<TLRPC.TL_dialog> dialogsServerOnly = new ArrayList<>();
|
|
|
|
public ConcurrentHashMap<Long, TLRPC.TL_dialog> dialogs_dict = new ConcurrentHashMap<>(100, 1.0f, 2);
|
|
|
|
public HashMap<Integer, MessageObject> dialogMessage = new HashMap<>();
|
|
|
|
public ConcurrentHashMap<Long, ArrayList<PrintingUser>> printingUsers = new ConcurrentHashMap<>(20, 1.0f, 2);
|
|
|
|
public HashMap<Long, CharSequence> printingStrings = new HashMap<>();
|
|
|
|
public HashMap<Long, Boolean> sendingTypings = new HashMap<>();
|
|
|
|
public ConcurrentHashMap<Integer, Integer> onlinePrivacy = new ConcurrentHashMap<>(20, 1.0f, 2);
|
2014-05-20 22:22:44 +02:00
|
|
|
private int lastPrintingStringCount = 0;
|
2013-10-25 17:19:00 +02:00
|
|
|
|
2014-08-22 16:24:33 +02:00
|
|
|
public boolean loadingBlockedUsers = false;
|
2015-01-02 23:15:07 +01:00
|
|
|
public ArrayList<Integer> blockedUsers = new ArrayList<>();
|
2014-08-22 16:24:33 +02:00
|
|
|
|
2015-03-19 00:09:45 +01:00
|
|
|
private ArrayList<TLRPC.Updates> updatesQueueSeq = new ArrayList<>();
|
|
|
|
private ArrayList<TLRPC.Updates> updatesQueuePts = new ArrayList<>();
|
|
|
|
private ArrayList<TLRPC.Updates> updatesQueueQts = new ArrayList<>();
|
|
|
|
private long updatesStartWaitTimeSeq = 0;
|
|
|
|
private long updatesStartWaitTimePts = 0;
|
|
|
|
private long updatesStartWaitTimeQts = 0;
|
2015-01-02 23:15:07 +01:00
|
|
|
private ArrayList<Integer> loadingFullUsers = new ArrayList<>();
|
|
|
|
private ArrayList<Integer> loadedFullUsers = new ArrayList<>();
|
|
|
|
private ArrayList<Integer> loadingFullChats = new ArrayList<>();
|
|
|
|
private ArrayList<Integer> loadedFullChats = new ArrayList<>();
|
2013-10-25 17:19:00 +02:00
|
|
|
|
2015-01-09 13:50:15 +01:00
|
|
|
private ArrayList<Integer> reloadingMessages = new ArrayList<>();
|
|
|
|
|
2013-10-25 17:19:00 +02:00
|
|
|
private boolean gettingNewDeleteTask = false;
|
|
|
|
private int currentDeletingTaskTime = 0;
|
|
|
|
private ArrayList<Integer> currentDeletingTaskMids = null;
|
2014-10-14 10:13:16 +02:00
|
|
|
private Runnable currentDeleteTaskRunnable = null;
|
2013-10-25 17:19:00 +02:00
|
|
|
|
|
|
|
public int totalDialogsCount = 0;
|
|
|
|
public boolean loadingDialogs = false;
|
|
|
|
public boolean dialogsEndReached = false;
|
|
|
|
public boolean gettingDifference = false;
|
2013-12-20 20:25:49 +01:00
|
|
|
public boolean gettingDifferenceAgain = false;
|
2013-10-25 17:19:00 +02:00
|
|
|
public boolean updatingState = false;
|
|
|
|
public boolean firstGettingTask = false;
|
|
|
|
public boolean registeringForPush = false;
|
2014-07-03 00:39:05 +02:00
|
|
|
|
2013-10-25 17:19:00 +02:00
|
|
|
private long lastStatusUpdateTime = 0;
|
2014-02-28 23:28:25 +01:00
|
|
|
private long statusRequest = 0;
|
|
|
|
private int statusSettingState = 0;
|
|
|
|
private boolean offlineSent = false;
|
2013-10-25 17:19:00 +02:00
|
|
|
private String uploadingAvatar = null;
|
2014-07-03 00:39:05 +02:00
|
|
|
|
2014-02-04 19:36:55 +01:00
|
|
|
public boolean enableJoined = true;
|
2014-07-03 00:39:05 +02:00
|
|
|
public int fontSize = AndroidUtilities.dp(16);
|
2014-08-06 01:17:40 +02:00
|
|
|
public int maxGroupCount = 200;
|
|
|
|
public int maxBroadcastCount = 100;
|
2015-02-27 20:57:58 +01:00
|
|
|
public int groupBigSize;
|
|
|
|
private ArrayList<TLRPC.TL_disabledFeature> disabledFeatures = new ArrayList<>();
|
2014-02-04 19:36:55 +01:00
|
|
|
|
2015-03-19 00:09:45 +01:00
|
|
|
private class UserActionUpdatesSeq extends TLRPC.Updates {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private class UserActionUpdatesPts extends TLRPC.Updates {
|
2014-02-04 19:36:55 +01:00
|
|
|
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
|
|
|
|
public static final int UPDATE_MASK_NAME = 1;
|
|
|
|
public static final int UPDATE_MASK_AVATAR = 2;
|
|
|
|
public static final int UPDATE_MASK_STATUS = 4;
|
2014-02-04 19:36:55 +01:00
|
|
|
public static final int UPDATE_MASK_CHAT_AVATAR = 8;
|
|
|
|
public static final int UPDATE_MASK_CHAT_NAME = 16;
|
|
|
|
public static final int UPDATE_MASK_CHAT_MEMBERS = 32;
|
|
|
|
public static final int UPDATE_MASK_USER_PRINT = 64;
|
|
|
|
public static final int UPDATE_MASK_USER_PHONE = 128;
|
2014-03-22 23:31:55 +01:00
|
|
|
public static final int UPDATE_MASK_READ_DIALOG_MESSAGE = 256;
|
2014-11-11 23:16:17 +01:00
|
|
|
public static final int UPDATE_MASK_SELECT_DIALOG = 512;
|
2014-12-01 18:56:31 +01:00
|
|
|
public static final int UPDATE_MASK_PHONE = 1024;
|
2015-02-26 02:32:51 +01:00
|
|
|
public static final int UPDATE_MASK_NEW_MESSAGE = 2048;
|
|
|
|
public static final int UPDATE_MASK_SEND_STATE = 4096;
|
2014-12-01 18:56:31 +01:00
|
|
|
public static final int UPDATE_MASK_ALL = UPDATE_MASK_AVATAR | UPDATE_MASK_STATUS | UPDATE_MASK_NAME | UPDATE_MASK_CHAT_AVATAR | UPDATE_MASK_CHAT_NAME | UPDATE_MASK_CHAT_MEMBERS | UPDATE_MASK_USER_PRINT | UPDATE_MASK_USER_PHONE | UPDATE_MASK_READ_DIALOG_MESSAGE | UPDATE_MASK_PHONE;
|
2013-10-25 17:19:00 +02:00
|
|
|
|
|
|
|
public static class PrintingUser {
|
|
|
|
public long lastTime;
|
|
|
|
public int userId;
|
|
|
|
}
|
|
|
|
|
2014-03-22 23:31:55 +01:00
|
|
|
private static volatile MessagesController Instance = null;
|
|
|
|
public static MessagesController getInstance() {
|
|
|
|
MessagesController localInstance = Instance;
|
|
|
|
if (localInstance == null) {
|
|
|
|
synchronized (MessagesController.class) {
|
|
|
|
localInstance = Instance;
|
|
|
|
if (localInstance == null) {
|
|
|
|
Instance = localInstance = new MessagesController();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return localInstance;
|
|
|
|
}
|
|
|
|
|
2013-10-25 17:19:00 +02:00
|
|
|
public MessagesController() {
|
2014-08-22 16:24:33 +02:00
|
|
|
ImageLoader.getInstance();
|
|
|
|
MessagesStorage.getInstance();
|
|
|
|
NotificationCenter.getInstance().addObserver(this, NotificationCenter.FileDidUpload);
|
|
|
|
NotificationCenter.getInstance().addObserver(this, NotificationCenter.FileDidFailUpload);
|
|
|
|
NotificationCenter.getInstance().addObserver(this, NotificationCenter.FileDidLoaded);
|
|
|
|
NotificationCenter.getInstance().addObserver(this, NotificationCenter.FileDidFailedLoad);
|
|
|
|
NotificationCenter.getInstance().addObserver(this, NotificationCenter.messageReceivedByServer);
|
2013-10-25 17:19:00 +02:00
|
|
|
addSupportUser();
|
2014-02-04 19:36:55 +01:00
|
|
|
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Activity.MODE_PRIVATE);
|
|
|
|
enableJoined = preferences.getBoolean("EnableContactJoined", true);
|
2014-08-22 16:24:33 +02:00
|
|
|
|
2014-02-28 23:28:25 +01:00
|
|
|
preferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE);
|
2014-08-06 01:17:40 +02:00
|
|
|
maxGroupCount = preferences.getInt("maxGroupCount", 200);
|
|
|
|
maxBroadcastCount = preferences.getInt("maxBroadcastCount", 100);
|
2015-02-27 20:57:58 +01:00
|
|
|
groupBigSize = preferences.getInt("groupBigSize", 10);
|
2014-09-24 04:17:27 +02:00
|
|
|
fontSize = preferences.getInt("fons_size", AndroidUtilities.isTablet() ? 18 : 16);
|
2015-02-27 20:57:58 +01:00
|
|
|
String disabledFeaturesString = preferences.getString("disabledFeatures", null);
|
|
|
|
if (disabledFeaturesString != null && disabledFeaturesString.length() != 0) {
|
|
|
|
try {
|
|
|
|
byte[] bytes = Base64.decode(disabledFeaturesString, Base64.DEFAULT);
|
|
|
|
if (bytes != null) {
|
|
|
|
SerializedData data = new SerializedData(bytes);
|
|
|
|
int count = data.readInt32();
|
|
|
|
for (int a = 0; a < count; a++) {
|
|
|
|
TLRPC.TL_disabledFeature feature = (TLRPC.TL_disabledFeature) TLClassStore.Instance().TLdeserialize(data, data.readInt32());
|
|
|
|
if (feature != null && feature.feature != null && feature.description != null) {
|
|
|
|
disabledFeatures.add(feature);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
FileLog.e("tmessages", e);
|
|
|
|
}
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
|
2014-08-06 01:17:40 +02:00
|
|
|
public void updateConfig(final TLRPC.TL_config config) {
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-08-06 01:17:40 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
maxBroadcastCount = config.broadcast_size_max;
|
|
|
|
maxGroupCount = config.chat_size_max;
|
2015-02-27 20:57:58 +01:00
|
|
|
groupBigSize = config.chat_big_size;
|
|
|
|
disabledFeatures = config.disabled_features;
|
|
|
|
|
2014-08-06 01:17:40 +02:00
|
|
|
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE);
|
|
|
|
SharedPreferences.Editor editor = preferences.edit();
|
|
|
|
editor.putInt("maxGroupCount", maxGroupCount);
|
|
|
|
editor.putInt("maxBroadcastCount", maxBroadcastCount);
|
2015-02-27 20:57:58 +01:00
|
|
|
editor.putInt("groupBigSize", groupBigSize);
|
|
|
|
try {
|
|
|
|
SerializedData data = new SerializedData();
|
|
|
|
data.writeInt32(disabledFeatures.size());
|
|
|
|
for (TLRPC.TL_disabledFeature disabledFeature : disabledFeatures) {
|
|
|
|
disabledFeature.serializeToStream(data);
|
|
|
|
}
|
|
|
|
String string = Base64.encodeToString(data.toByteArray(), Base64.DEFAULT);
|
|
|
|
if (string != null && string.length() != 0) {
|
|
|
|
editor.putString("disabledFeatures", string);
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
editor.remove("disabledFeatures");
|
|
|
|
FileLog.e("tmessages", e);
|
|
|
|
}
|
2014-08-06 01:17:40 +02:00
|
|
|
editor.commit();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-02-27 20:57:58 +01:00
|
|
|
public static boolean isFeatureEnabled(String feature, BaseFragment fragment) {
|
|
|
|
if (feature == null || feature.length() == 0 || getInstance().disabledFeatures.isEmpty() || fragment == null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (TLRPC.TL_disabledFeature disabledFeature : getInstance().disabledFeatures) {
|
|
|
|
if (disabledFeature.feature.equals(feature)) {
|
|
|
|
if (fragment.getParentActivity() != null) {
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(fragment.getParentActivity());
|
|
|
|
builder.setTitle("Oops!");
|
2015-03-19 00:09:45 +01:00
|
|
|
builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), null);
|
2015-02-27 20:57:58 +01:00
|
|
|
builder.setMessage(disabledFeature.description);
|
|
|
|
fragment.showAlertDialog(builder);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-25 17:19:00 +02:00
|
|
|
public void addSupportUser() {
|
|
|
|
TLRPC.TL_userForeign user = new TLRPC.TL_userForeign();
|
|
|
|
user.phone = "333";
|
|
|
|
user.id = 333000;
|
|
|
|
user.first_name = "Telegram";
|
|
|
|
user.last_name = "";
|
|
|
|
user.status = null;
|
|
|
|
user.photo = new TLRPC.TL_userProfilePhotoEmpty();
|
2014-08-22 16:24:33 +02:00
|
|
|
putUser(user, true);
|
2014-08-29 23:06:04 +02:00
|
|
|
|
|
|
|
user = new TLRPC.TL_userForeign();
|
|
|
|
user.phone = "42777";
|
|
|
|
user.id = 777000;
|
|
|
|
user.first_name = "Telegram";
|
|
|
|
user.last_name = "Notifications";
|
|
|
|
user.status = null;
|
|
|
|
user.photo = new TLRPC.TL_userProfilePhotoEmpty();
|
|
|
|
putUser(user, true);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
|
2014-02-04 19:36:55 +01:00
|
|
|
public static TLRPC.InputUser getInputUser(TLRPC.User user) {
|
|
|
|
if (user == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
TLRPC.InputUser inputUser = null;
|
2014-06-13 12:42:21 +02:00
|
|
|
if (user.id == UserConfig.getClientUserId()) {
|
2014-02-04 19:36:55 +01:00
|
|
|
inputUser = new TLRPC.TL_inputUserSelf();
|
|
|
|
} else if (user instanceof TLRPC.TL_userForeign || user instanceof TLRPC.TL_userRequest) {
|
|
|
|
inputUser = new TLRPC.TL_inputUserForeign();
|
|
|
|
inputUser.user_id = user.id;
|
|
|
|
inputUser.access_hash = user.access_hash;
|
|
|
|
} else {
|
|
|
|
inputUser = new TLRPC.TL_inputUserContact();
|
|
|
|
inputUser.user_id = user.id;
|
|
|
|
}
|
|
|
|
return inputUser;
|
|
|
|
}
|
|
|
|
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void didReceivedNotification(int id, Object... args) {
|
2014-08-22 16:24:33 +02:00
|
|
|
if (id == NotificationCenter.FileDidUpload) {
|
2014-07-03 00:39:05 +02:00
|
|
|
final String location = (String)args[0];
|
|
|
|
final TLRPC.InputFile file = (TLRPC.InputFile)args[1];
|
|
|
|
final TLRPC.InputEncryptedFile encryptedFile = (TLRPC.InputEncryptedFile)args[2];
|
|
|
|
|
|
|
|
if (uploadingAvatar != null && uploadingAvatar.equals(location)) {
|
|
|
|
TLRPC.TL_photos_uploadProfilePhoto req = new TLRPC.TL_photos_uploadProfilePhoto();
|
|
|
|
req.caption = "";
|
|
|
|
req.crop = new TLRPC.TL_inputPhotoCropAuto();
|
|
|
|
req.file = file;
|
|
|
|
req.geo_point = new TLRPC.TL_inputGeoPointEmpty();
|
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error == null) {
|
2014-08-22 16:24:33 +02:00
|
|
|
TLRPC.User user = getUser(UserConfig.getClientUserId());
|
2014-07-03 00:39:05 +02:00
|
|
|
if (user == null) {
|
|
|
|
user = UserConfig.getCurrentUser();
|
2014-08-22 16:24:33 +02:00
|
|
|
putUser(user, true);
|
2014-07-03 00:39:05 +02:00
|
|
|
} else {
|
|
|
|
UserConfig.setCurrentUser(user);
|
|
|
|
}
|
|
|
|
if (user == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
TLRPC.TL_photos_photo photo = (TLRPC.TL_photos_photo) response;
|
|
|
|
ArrayList<TLRPC.PhotoSize> sizes = photo.photo.sizes;
|
2014-10-01 21:55:24 +02:00
|
|
|
TLRPC.PhotoSize smallSize = FileLoader.getClosestPhotoSizeWithSize(sizes, 100);
|
|
|
|
TLRPC.PhotoSize bigSize = FileLoader.getClosestPhotoSizeWithSize(sizes, 1000);
|
2014-07-03 00:39:05 +02:00
|
|
|
user.photo = new TLRPC.TL_userProfilePhoto();
|
|
|
|
user.photo.photo_id = photo.photo.id;
|
|
|
|
if (smallSize != null) {
|
|
|
|
user.photo.photo_small = smallSize.location;
|
|
|
|
}
|
|
|
|
if (bigSize != null) {
|
|
|
|
user.photo.photo_big = bigSize.location;
|
|
|
|
} else if (smallSize != null) {
|
|
|
|
user.photo.photo_small = smallSize.location;
|
|
|
|
}
|
|
|
|
MessagesStorage.getInstance().clearUserPhotos(user.id);
|
2015-01-02 23:15:07 +01:00
|
|
|
ArrayList<TLRPC.User> users = new ArrayList<>();
|
2014-07-03 00:39:05 +02:00
|
|
|
users.add(user);
|
|
|
|
MessagesStorage.getInstance().putUsersAndChats(users, null, false, true);
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-07-03 00:39:05 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_AVATAR);
|
2014-07-03 00:39:05 +02:00
|
|
|
UserConfig.saveConfig(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-08-22 16:24:33 +02:00
|
|
|
} else if (id == NotificationCenter.FileDidFailUpload) {
|
2014-07-03 00:39:05 +02:00
|
|
|
final String location = (String) args[0];
|
|
|
|
final boolean enc = (Boolean) args[1];
|
|
|
|
|
|
|
|
if (uploadingAvatar != null && uploadingAvatar.equals(location)) {
|
|
|
|
uploadingAvatar = null;
|
|
|
|
}
|
2014-08-22 16:24:33 +02:00
|
|
|
} else if (id == NotificationCenter.messageReceivedByServer) {
|
2013-10-25 17:19:00 +02:00
|
|
|
Integer msgId = (Integer)args[0];
|
|
|
|
MessageObject obj = dialogMessage.get(msgId);
|
|
|
|
if (obj != null) {
|
|
|
|
Integer newMsgId = (Integer)args[1];
|
|
|
|
dialogMessage.remove(msgId);
|
|
|
|
dialogMessage.put(newMsgId, obj);
|
|
|
|
obj.messageOwner.id = newMsgId;
|
2014-08-22 16:24:33 +02:00
|
|
|
obj.messageOwner.send_state = MessageObject.MESSAGE_SEND_STATE_SENT;
|
2013-10-25 17:19:00 +02:00
|
|
|
|
|
|
|
long uid;
|
|
|
|
if (obj.messageOwner.to_id.chat_id != 0) {
|
|
|
|
uid = -obj.messageOwner.to_id.chat_id;
|
|
|
|
} else {
|
2014-06-13 12:42:21 +02:00
|
|
|
if (obj.messageOwner.to_id.user_id == UserConfig.getClientUserId()) {
|
2013-10-25 17:19:00 +02:00
|
|
|
obj.messageOwner.to_id.user_id = obj.messageOwner.from_id;
|
|
|
|
}
|
|
|
|
uid = obj.messageOwner.to_id.user_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
TLRPC.TL_dialog dialog = dialogs_dict.get(uid);
|
|
|
|
if (dialog != null) {
|
|
|
|
if (dialog.top_message == msgId) {
|
|
|
|
dialog.top_message = newMsgId;
|
|
|
|
}
|
|
|
|
}
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.dialogsNeedReload);
|
2014-07-27 00:28:33 +02:00
|
|
|
}
|
2014-08-22 16:24:33 +02:00
|
|
|
} else {
|
|
|
|
NotificationCenter.getInstance().addObserver(this, NotificationCenter.FileDidLoaded);
|
|
|
|
NotificationCenter.getInstance().addObserver(this, NotificationCenter.FileDidFailedLoad);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cleanUp() {
|
2014-03-22 23:31:55 +01:00
|
|
|
ContactsController.getInstance().cleanup();
|
|
|
|
MediaController.getInstance().cleanup();
|
2014-07-10 02:15:58 +02:00
|
|
|
NotificationsController.getInstance().cleanup();
|
2014-08-22 16:24:33 +02:00
|
|
|
SendMessagesHelper.getInstance().cleanUp();
|
2014-12-01 18:56:31 +01:00
|
|
|
SecretChatHelper.getInstance().cleanUp();
|
2014-02-04 19:36:55 +01:00
|
|
|
|
2013-10-25 17:19:00 +02:00
|
|
|
dialogs_dict.clear();
|
|
|
|
dialogs.clear();
|
|
|
|
dialogsServerOnly.clear();
|
|
|
|
users.clear();
|
2015-03-19 00:09:45 +01:00
|
|
|
usersByUsernames.clear();
|
2013-10-25 17:19:00 +02:00
|
|
|
chats.clear();
|
|
|
|
dialogMessage.clear();
|
|
|
|
printingUsers.clear();
|
|
|
|
printingStrings.clear();
|
2014-11-21 01:14:44 +01:00
|
|
|
onlinePrivacy.clear();
|
2013-10-25 17:19:00 +02:00
|
|
|
totalDialogsCount = 0;
|
2014-05-20 22:22:44 +02:00
|
|
|
lastPrintingStringCount = 0;
|
2015-03-19 00:09:45 +01:00
|
|
|
updatesQueueSeq.clear();
|
|
|
|
updatesQueuePts.clear();
|
|
|
|
updatesQueueQts.clear();
|
2014-08-22 16:24:33 +02:00
|
|
|
blockedUsers.clear();
|
2014-08-23 01:22:33 +02:00
|
|
|
sendingTypings.clear();
|
2014-10-17 20:29:13 +02:00
|
|
|
loadingFullUsers.clear();
|
|
|
|
loadedFullUsers.clear();
|
2015-01-09 13:50:15 +01:00
|
|
|
reloadingMessages.clear();
|
2015-02-01 19:51:02 +01:00
|
|
|
loadingFullChats.clear();
|
|
|
|
loadedFullChats.clear();
|
2013-10-25 17:19:00 +02:00
|
|
|
|
2015-03-19 00:09:45 +01:00
|
|
|
updatesStartWaitTimeSeq = 0;
|
|
|
|
updatesStartWaitTimePts = 0;
|
|
|
|
updatesStartWaitTimeQts = 0;
|
2013-10-25 17:19:00 +02:00
|
|
|
currentDeletingTaskTime = 0;
|
|
|
|
currentDeletingTaskMids = null;
|
|
|
|
gettingNewDeleteTask = false;
|
|
|
|
loadingDialogs = false;
|
|
|
|
dialogsEndReached = false;
|
|
|
|
gettingDifference = false;
|
2013-12-20 20:25:49 +01:00
|
|
|
gettingDifferenceAgain = false;
|
2014-08-22 16:24:33 +02:00
|
|
|
loadingBlockedUsers = false;
|
2013-10-25 17:19:00 +02:00
|
|
|
firstGettingTask = false;
|
|
|
|
updatingState = false;
|
|
|
|
lastStatusUpdateTime = 0;
|
2014-02-28 23:28:25 +01:00
|
|
|
offlineSent = false;
|
2013-10-25 17:19:00 +02:00
|
|
|
registeringForPush = false;
|
|
|
|
uploadingAvatar = null;
|
2014-02-28 23:28:25 +01:00
|
|
|
statusRequest = 0;
|
|
|
|
statusSettingState = 0;
|
2014-10-14 10:13:16 +02:00
|
|
|
|
|
|
|
if (currentDeleteTaskRunnable != null) {
|
|
|
|
Utilities.stageQueue.cancelRunnable(currentDeleteTaskRunnable);
|
|
|
|
currentDeleteTaskRunnable = null;
|
|
|
|
}
|
|
|
|
|
2013-10-25 17:19:00 +02:00
|
|
|
addSupportUser();
|
|
|
|
}
|
|
|
|
|
2014-08-22 16:24:33 +02:00
|
|
|
public TLRPC.User getUser(Integer id) {
|
|
|
|
return users.get(id);
|
|
|
|
}
|
|
|
|
|
2015-03-19 00:09:45 +01:00
|
|
|
public TLRPC.User getUser(String username) {
|
|
|
|
return usersByUsernames.get(username);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ConcurrentHashMap<Integer, TLRPC.User> getUsers() {
|
|
|
|
return users;
|
|
|
|
}
|
|
|
|
|
2014-08-22 16:24:33 +02:00
|
|
|
public TLRPC.Chat getChat(Integer id) {
|
|
|
|
return chats.get(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public TLRPC.EncryptedChat getEncryptedChat(Integer id) {
|
|
|
|
return encryptedChats.get(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public TLRPC.EncryptedChat getEncryptedChatDB(int chat_id) {
|
|
|
|
TLRPC.EncryptedChat chat = encryptedChats.get(chat_id);
|
|
|
|
if (chat == null) {
|
|
|
|
Semaphore semaphore = new Semaphore(0);
|
2015-01-02 23:15:07 +01:00
|
|
|
ArrayList<TLObject> result = new ArrayList<>();
|
2014-08-22 16:24:33 +02:00
|
|
|
MessagesStorage.getInstance().getEncryptedChat(chat_id, semaphore, result);
|
|
|
|
try {
|
|
|
|
semaphore.acquire();
|
|
|
|
} catch (Exception e) {
|
|
|
|
FileLog.e("tmessages", e);
|
|
|
|
}
|
|
|
|
if (result.size() == 2) {
|
|
|
|
chat = (TLRPC.EncryptedChat)result.get(0);
|
|
|
|
TLRPC.User user = (TLRPC.User)result.get(1);
|
|
|
|
putEncryptedChat(chat, false);
|
|
|
|
putUser(user, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return chat;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean putUser(TLRPC.User user, boolean fromCache) {
|
|
|
|
if (user == null) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-11-17 03:44:57 +01:00
|
|
|
fromCache = fromCache && user.id / 1000 != 333 && user.id != 777000;
|
2014-08-22 16:24:33 +02:00
|
|
|
TLRPC.User oldUser = users.get(user.id);
|
2015-03-19 00:09:45 +01:00
|
|
|
if (oldUser != null && oldUser.username != null && oldUser.username.length() > 0) {
|
|
|
|
usersByUsernames.remove(oldUser.username);
|
|
|
|
}
|
|
|
|
if (user.username != null && user.username.length() > 0) {
|
|
|
|
usersByUsernames.put(user.username, user);
|
|
|
|
}
|
2014-08-22 16:24:33 +02:00
|
|
|
if (!fromCache) {
|
|
|
|
users.put(user.id, user);
|
|
|
|
if (user.id == UserConfig.getClientUserId()) {
|
|
|
|
UserConfig.setCurrentUser(user);
|
2014-10-17 20:29:13 +02:00
|
|
|
UserConfig.saveConfig(true);
|
2014-08-22 16:24:33 +02:00
|
|
|
}
|
|
|
|
if (oldUser != null && user.status != null && oldUser.status != null && user.status.expires != oldUser.status.expires) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (oldUser == null) {
|
|
|
|
users.put(user.id, user);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void putUsers(ArrayList<TLRPC.User> users, boolean fromCache) {
|
|
|
|
if (users == null || users.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
boolean updateStatus = false;
|
|
|
|
for (TLRPC.User user : users) {
|
|
|
|
if (putUser(user, fromCache)) {
|
|
|
|
updateStatus = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (updateStatus) {
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-08-22 16:24:33 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_STATUS);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void putChat(TLRPC.Chat chat, boolean fromCache) {
|
|
|
|
if (chat == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fromCache) {
|
|
|
|
chats.putIfAbsent(chat.id, chat);
|
|
|
|
} else {
|
|
|
|
chats.put(chat.id, chat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void putChats(ArrayList<TLRPC.Chat> chats, boolean fromCache) {
|
|
|
|
if (chats == null || chats.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (TLRPC.Chat chat : chats) {
|
|
|
|
putChat(chat, fromCache);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void putEncryptedChat(TLRPC.EncryptedChat encryptedChat, boolean fromCache) {
|
|
|
|
if (encryptedChat == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (fromCache) {
|
|
|
|
encryptedChats.putIfAbsent(encryptedChat.id, encryptedChat);
|
|
|
|
} else {
|
|
|
|
encryptedChats.put(encryptedChat.id, encryptedChat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void putEncryptedChats(ArrayList<TLRPC.EncryptedChat> encryptedChats, boolean fromCache) {
|
|
|
|
if (encryptedChats == null || encryptedChats.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (TLRPC.EncryptedChat encryptedChat : encryptedChats) {
|
|
|
|
putEncryptedChat(encryptedChat, fromCache);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-17 20:29:13 +02:00
|
|
|
public void cancelLoadFullUser(int uid) {
|
|
|
|
loadingFullUsers.remove((Integer) uid);
|
|
|
|
}
|
|
|
|
|
2014-11-19 16:17:24 +01:00
|
|
|
public void cancelLoadFullChat(int cid) {
|
|
|
|
loadingFullChats.remove((Integer) cid);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void clearFullUsers() {
|
|
|
|
loadedFullUsers.clear();
|
|
|
|
loadedFullChats.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void loadFullChat(final int chat_id, final int classGuid) {
|
|
|
|
if (loadingFullChats.contains(chat_id) || loadedFullChats.contains(chat_id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
loadingFullChats.add(chat_id);
|
|
|
|
TLRPC.TL_messages_getFullChat req = new TLRPC.TL_messages_getFullChat();
|
|
|
|
req.chat_id = chat_id;
|
|
|
|
long reqId = ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error == null) {
|
|
|
|
final TLRPC.TL_messages_chatFull res = (TLRPC.TL_messages_chatFull) response;
|
|
|
|
MessagesStorage.getInstance().putUsersAndChats(res.users, res.chats, true, true);
|
|
|
|
MessagesStorage.getInstance().updateChatInfo(chat_id, res.full_chat.participants, false);
|
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
loadingFullChats.remove((Integer)chat_id);
|
|
|
|
loadedFullChats.add(chat_id);
|
|
|
|
|
|
|
|
putUsers(res.users, false);
|
|
|
|
putChats(res.chats, false);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.chatInfoDidLoaded, chat_id, res.full_chat.participants);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
loadingFullChats.remove((Integer) chat_id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (classGuid != 0) {
|
|
|
|
ConnectionsManager.getInstance().bindRequestToGuid(reqId, classGuid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 16:24:33 +02:00
|
|
|
public void loadFullUser(final TLRPC.User user, final int classGuid) {
|
2014-10-17 20:29:13 +02:00
|
|
|
if (user == null || loadingFullUsers.contains(user.id) || loadedFullUsers.contains(user.id)) {
|
2014-08-22 16:24:33 +02:00
|
|
|
return;
|
|
|
|
}
|
2014-10-17 20:29:13 +02:00
|
|
|
loadingFullUsers.add(user.id);
|
2014-08-22 16:24:33 +02:00
|
|
|
TLRPC.TL_users_getFullUser req = new TLRPC.TL_users_getFullUser();
|
|
|
|
req.id = getInputUser(user);
|
|
|
|
long reqId = ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
|
|
|
@Override
|
|
|
|
public void run(final TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error == null) {
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-08-22 16:24:33 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-10-17 20:29:13 +02:00
|
|
|
loadingFullUsers.remove((Integer)user.id);
|
|
|
|
loadedFullUsers.add(user.id);
|
|
|
|
String names = user.first_name + user.last_name + user.username;
|
2014-08-22 16:24:33 +02:00
|
|
|
TLRPC.TL_userFull userFull = (TLRPC.TL_userFull)response;
|
2015-01-02 23:15:07 +01:00
|
|
|
ArrayList<TLRPC.User> users = new ArrayList<>();
|
2014-08-22 16:24:33 +02:00
|
|
|
users.add(userFull.user);
|
|
|
|
putUsers(users, false);
|
2014-10-17 20:29:13 +02:00
|
|
|
MessagesStorage.getInstance().putUsersAndChats(users, null, false, true);
|
|
|
|
if (!names.equals(userFull.user.first_name + userFull.user.last_name + userFull.user.username)) {
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_NAME);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-10-17 20:29:13 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
loadingFullUsers.remove((Integer)user.id);
|
2014-08-22 16:24:33 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ConnectionsManager.getInstance().bindRequestToGuid(reqId, classGuid);
|
|
|
|
}
|
|
|
|
|
2015-01-09 13:50:15 +01:00
|
|
|
private void reloadMessages(final ArrayList<Integer> mids, final long dialog_id) {
|
|
|
|
final TLRPC.TL_messages_getMessages req = new TLRPC.TL_messages_getMessages();
|
|
|
|
for (Integer mid : mids) {
|
|
|
|
if (reloadingMessages.contains(mid)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
req.id.add(mid);
|
|
|
|
}
|
|
|
|
if (req.id.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
reloadingMessages.addAll(req.id);
|
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error == null) {
|
|
|
|
TLRPC.messages_Messages messagesRes = (TLRPC.messages_Messages) response;
|
2015-02-01 19:51:02 +01:00
|
|
|
ImageLoader.saveMessagesThumbs(messagesRes.messages);
|
2015-01-09 13:50:15 +01:00
|
|
|
MessagesStorage.getInstance().putMessages(messagesRes, dialog_id);
|
|
|
|
|
|
|
|
final ArrayList<MessageObject> objects = new ArrayList<>();
|
|
|
|
ArrayList<Integer> messagesToReload = null;
|
|
|
|
for (TLRPC.Message message : messagesRes.messages) {
|
|
|
|
message.dialog_id = dialog_id;
|
|
|
|
final HashMap<Integer, TLRPC.User> usersLocal = new HashMap<>();
|
|
|
|
for (TLRPC.User u : messagesRes.users) {
|
|
|
|
usersLocal.put(u.id, u);
|
|
|
|
}
|
2015-02-01 19:51:02 +01:00
|
|
|
objects.add(new MessageObject(message, usersLocal, true));
|
2015-01-09 13:50:15 +01:00
|
|
|
}
|
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.replaceMessagesObjects, dialog_id, objects);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
reloadingMessages.removeAll(req.id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-03-19 00:09:45 +01:00
|
|
|
protected void processNewDifferenceParams(int seq, int pts, int date, int pts_count) {
|
2015-03-20 00:03:19 +01:00
|
|
|
FileLog.e("tmessages", "processNewDifferenceParams seq = " + seq + " pts = " + pts + " date = " + date + " pts_count = " + pts_count);
|
2015-03-19 00:09:45 +01:00
|
|
|
if (pts != -1) {
|
|
|
|
if (MessagesStorage.lastPtsValue + pts_count == pts) {
|
2015-03-20 00:03:19 +01:00
|
|
|
FileLog.e("tmessages", "APPLY PTS");
|
2014-08-22 16:24:33 +02:00
|
|
|
MessagesStorage.lastPtsValue = pts;
|
2015-03-19 00:09:45 +01:00
|
|
|
MessagesStorage.getInstance().saveDiffParams(MessagesStorage.lastSeqValue, MessagesStorage.lastPtsValue, MessagesStorage.lastDateValue, MessagesStorage.lastQtsValue);
|
2015-03-20 00:03:19 +01:00
|
|
|
} else if (MessagesStorage.lastPtsValue != pts) {
|
2015-03-19 00:09:45 +01:00
|
|
|
if (gettingDifference || updatesStartWaitTimePts == 0 || updatesStartWaitTimePts != 0 && updatesStartWaitTimePts + 1500 > System.currentTimeMillis()) {
|
2015-03-20 00:03:19 +01:00
|
|
|
FileLog.e("tmessages", "ADD UPDATE TO QUEUE pts = " + pts + " pts_count = " + pts_count);
|
2015-03-19 00:09:45 +01:00
|
|
|
if (updatesStartWaitTimePts == 0) {
|
|
|
|
updatesStartWaitTimePts = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
UserActionUpdatesPts updates = new UserActionUpdatesPts();
|
2015-03-20 00:03:19 +01:00
|
|
|
updates.pts = pts;
|
|
|
|
updates.pts_count = pts_count;
|
2015-03-19 00:09:45 +01:00
|
|
|
updatesQueuePts.add(updates);
|
|
|
|
} else {
|
|
|
|
getDifference();
|
|
|
|
}
|
2014-08-22 16:24:33 +02:00
|
|
|
}
|
2015-03-19 00:09:45 +01:00
|
|
|
}
|
|
|
|
if (seq != -1) {
|
|
|
|
if (MessagesStorage.lastSeqValue + 1 == seq) {
|
2015-03-20 00:03:19 +01:00
|
|
|
FileLog.e("tmessages", "APPLY SEQ");
|
2015-03-19 00:09:45 +01:00
|
|
|
MessagesStorage.lastSeqValue = seq;
|
|
|
|
if (date != -1) {
|
|
|
|
MessagesStorage.lastDateValue = date;
|
|
|
|
}
|
|
|
|
MessagesStorage.getInstance().saveDiffParams(MessagesStorage.lastSeqValue, MessagesStorage.lastPtsValue, MessagesStorage.lastDateValue, MessagesStorage.lastQtsValue);
|
|
|
|
} else if (MessagesStorage.lastSeqValue != seq) {
|
|
|
|
if (gettingDifference || updatesStartWaitTimeSeq == 0 || updatesStartWaitTimeSeq != 0 && updatesStartWaitTimeSeq + 1500 > System.currentTimeMillis()) {
|
2015-03-20 00:03:19 +01:00
|
|
|
FileLog.e("tmessages", "ADD UPDATE TO QUEUE seq = " + seq);
|
2015-03-19 00:09:45 +01:00
|
|
|
if (updatesStartWaitTimeSeq == 0) {
|
|
|
|
updatesStartWaitTimeSeq = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
UserActionUpdatesSeq updates = new UserActionUpdatesSeq();
|
|
|
|
updates.seq = seq;
|
|
|
|
updatesQueueSeq.add(updates);
|
|
|
|
} else {
|
|
|
|
getDifference();
|
2014-08-22 16:24:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-10 19:16:39 +02:00
|
|
|
public void didAddedNewTask(final int minDate, final SparseArray<ArrayList<Integer>> mids) {
|
2013-10-25 17:19:00 +02:00
|
|
|
Utilities.stageQueue.postRunnable(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-10-08 22:22:22 +02:00
|
|
|
if (currentDeletingTaskMids == null && !gettingNewDeleteTask || currentDeletingTaskTime != 0 && minDate < currentDeletingTaskTime) {
|
2013-10-25 17:19:00 +02:00
|
|
|
getNewDeleteTask(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-10-10 19:16:39 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.didCreatedNewDeleteTask, mids);
|
|
|
|
}
|
|
|
|
});
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
|
2014-10-08 22:22:22 +02:00
|
|
|
public void getNewDeleteTask(final ArrayList<Integer> oldTask) {
|
2013-10-25 17:19:00 +02:00
|
|
|
Utilities.stageQueue.postRunnable(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
gettingNewDeleteTask = true;
|
2014-03-22 23:31:55 +01:00
|
|
|
MessagesStorage.getInstance().getNewTask(oldTask);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-14 10:13:16 +02:00
|
|
|
private boolean checkDeletingTask(boolean runnable) {
|
2014-03-22 23:31:55 +01:00
|
|
|
int currentServerTime = ConnectionsManager.getInstance().getCurrentTime();
|
2013-10-25 17:19:00 +02:00
|
|
|
|
2014-10-14 10:13:16 +02:00
|
|
|
if (currentDeletingTaskMids != null && (runnable || currentDeletingTaskTime != 0 && currentDeletingTaskTime <= currentServerTime)) {
|
2013-10-25 17:19:00 +02:00
|
|
|
currentDeletingTaskTime = 0;
|
2014-10-14 10:13:16 +02:00
|
|
|
if (currentDeleteTaskRunnable != null && !runnable) {
|
|
|
|
Utilities.stageQueue.cancelRunnable(currentDeleteTaskRunnable);
|
|
|
|
}
|
|
|
|
currentDeleteTaskRunnable = null;
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-03-22 23:31:55 +01:00
|
|
|
deleteMessages(currentDeletingTaskMids, null, null);
|
2013-10-25 17:19:00 +02:00
|
|
|
|
|
|
|
Utilities.stageQueue.postRunnable(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-10-08 22:22:22 +02:00
|
|
|
getNewDeleteTask(currentDeletingTaskMids);
|
2013-10-25 17:19:00 +02:00
|
|
|
currentDeletingTaskTime = 0;
|
2014-10-08 22:22:22 +02:00
|
|
|
currentDeletingTaskMids = null;
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2014-10-14 10:13:16 +02:00
|
|
|
return true;
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-10-14 10:13:16 +02:00
|
|
|
return false;
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
|
2014-10-08 22:22:22 +02:00
|
|
|
public void processLoadedDeleteTask(final int taskTime, final ArrayList<Integer> messages) {
|
2013-10-25 17:19:00 +02:00
|
|
|
Utilities.stageQueue.postRunnable(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
gettingNewDeleteTask = false;
|
2014-10-08 22:22:22 +02:00
|
|
|
if (messages != null) {
|
2013-10-25 17:19:00 +02:00
|
|
|
currentDeletingTaskTime = taskTime;
|
|
|
|
currentDeletingTaskMids = messages;
|
|
|
|
|
2014-10-14 10:13:16 +02:00
|
|
|
if (currentDeleteTaskRunnable != null) {
|
|
|
|
Utilities.stageQueue.cancelRunnable(currentDeleteTaskRunnable);
|
|
|
|
currentDeleteTaskRunnable = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!checkDeletingTask(false)) {
|
|
|
|
currentDeleteTaskRunnable = new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
checkDeletingTask(true);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
int currentServerTime = ConnectionsManager.getInstance().getCurrentTime();
|
|
|
|
Utilities.stageQueue.postRunnable(currentDeleteTaskRunnable, (long)Math.abs(currentServerTime - currentDeletingTaskTime) * 1000);
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
} else {
|
|
|
|
currentDeletingTaskTime = 0;
|
|
|
|
currentDeletingTaskMids = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-12-20 20:25:49 +01:00
|
|
|
public void loadUserPhotos(final int uid, final int offset, final int count, final long max_id, final boolean fromCache, final int classGuid) {
|
|
|
|
if (fromCache) {
|
2014-03-22 23:31:55 +01:00
|
|
|
MessagesStorage.getInstance().getUserPhotos(uid, offset, count, max_id, classGuid);
|
2013-12-20 20:25:49 +01:00
|
|
|
} else {
|
2014-08-22 16:24:33 +02:00
|
|
|
TLRPC.User user = getUser(uid);
|
2013-12-20 20:25:49 +01:00
|
|
|
if (user == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
TLRPC.TL_photos_getUserPhotos req = new TLRPC.TL_photos_getUserPhotos();
|
|
|
|
req.limit = count;
|
|
|
|
req.offset = offset;
|
|
|
|
req.max_id = (int)max_id;
|
2014-02-04 19:36:55 +01:00
|
|
|
req.user_id = getInputUser(user);
|
2014-03-22 23:31:55 +01:00
|
|
|
long reqId = ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-12-20 20:25:49 +01:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error == null) {
|
2014-03-22 23:31:55 +01:00
|
|
|
TLRPC.photos_Photos res = (TLRPC.photos_Photos) response;
|
2013-12-20 20:25:49 +01:00
|
|
|
processLoadedUserPhotos(res, uid, offset, count, max_id, fromCache, classGuid);
|
|
|
|
}
|
|
|
|
}
|
2014-07-03 00:39:05 +02:00
|
|
|
});
|
2014-03-22 23:31:55 +01:00
|
|
|
ConnectionsManager.getInstance().bindRequestToGuid(reqId, classGuid);
|
2013-12-20 20:25:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 16:24:33 +02:00
|
|
|
public void blockUser(int user_id) {
|
|
|
|
final TLRPC.User user = getUser(user_id);
|
|
|
|
if (user == null || MessagesController.getInstance().blockedUsers.contains(user_id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
blockedUsers.add(user_id);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.blockedUsersDidLoaded);
|
|
|
|
TLRPC.TL_contacts_block req = new TLRPC.TL_contacts_block();
|
|
|
|
req.id = MessagesController.getInputUser(user);
|
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error == null) {
|
2015-01-02 23:15:07 +01:00
|
|
|
ArrayList<Integer> ids = new ArrayList<>();
|
2014-08-22 16:24:33 +02:00
|
|
|
ids.add(user.id);
|
|
|
|
MessagesStorage.getInstance().putBlockedUsers(ids, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void unblockUser(int user_id) {
|
|
|
|
TLRPC.TL_contacts_unblock req = new TLRPC.TL_contacts_unblock();
|
|
|
|
final TLRPC.User user = MessagesController.getInstance().getUser(user_id);
|
|
|
|
if (user == null) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-19 00:09:45 +01:00
|
|
|
blockedUsers.remove((Integer) user.id);
|
2014-08-22 16:24:33 +02:00
|
|
|
req.id = MessagesController.getInputUser(user);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.blockedUsersDidLoaded);
|
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
MessagesStorage.getInstance().deleteBlockedUser(user.id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void getBlockedUsers(boolean cache) {
|
|
|
|
if (!UserConfig.isClientActivated() || loadingBlockedUsers) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
loadingBlockedUsers = true;
|
|
|
|
if (cache) {
|
|
|
|
MessagesStorage.getInstance().getBlockedUsers();
|
|
|
|
} else {
|
|
|
|
TLRPC.TL_contacts_getBlocked req = new TLRPC.TL_contacts_getBlocked();
|
|
|
|
req.offset = 0;
|
|
|
|
req.limit = 200;
|
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
2015-01-02 23:15:07 +01:00
|
|
|
ArrayList<Integer> blocked = new ArrayList<>();
|
2014-08-22 16:24:33 +02:00
|
|
|
ArrayList<TLRPC.User> users = null;
|
|
|
|
if (error == null) {
|
|
|
|
final TLRPC.contacts_Blocked res = (TLRPC.contacts_Blocked)response;
|
|
|
|
for (TLRPC.TL_contactBlocked contactBlocked : res.blocked) {
|
|
|
|
blocked.add(contactBlocked.user_id);
|
|
|
|
}
|
|
|
|
users = res.users;
|
|
|
|
MessagesStorage.getInstance().putUsersAndChats(res.users, null, true, true);
|
|
|
|
MessagesStorage.getInstance().putBlockedUsers(blocked, true);
|
|
|
|
}
|
|
|
|
processLoadedBlockedUsers(blocked, users, false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void processLoadedBlockedUsers(final ArrayList<Integer> ids, final ArrayList<TLRPC.User> users, final boolean cache) {
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-08-22 16:24:33 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (users != null) {
|
|
|
|
MessagesController.getInstance().putUsers(users, cache);
|
|
|
|
}
|
|
|
|
loadingBlockedUsers = false;
|
|
|
|
if (ids.isEmpty() && cache && !UserConfig.blockedUsersLoaded) {
|
|
|
|
getBlockedUsers(false);
|
|
|
|
return;
|
|
|
|
} else if (!cache) {
|
|
|
|
UserConfig.blockedUsersLoaded = true;
|
|
|
|
UserConfig.saveConfig(false);
|
|
|
|
}
|
|
|
|
blockedUsers = ids;
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.blockedUsersDidLoaded);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deleteUserPhoto(TLRPC.InputPhoto photo) {
|
|
|
|
if (photo == null) {
|
|
|
|
TLRPC.TL_photos_updateProfilePhoto req = new TLRPC.TL_photos_updateProfilePhoto();
|
|
|
|
req.id = new TLRPC.TL_inputPhotoEmpty();
|
|
|
|
req.crop = new TLRPC.TL_inputPhotoCropAuto();
|
|
|
|
UserConfig.getCurrentUser().photo = new TLRPC.TL_userProfilePhotoEmpty();
|
|
|
|
TLRPC.User user = MessagesController.getInstance().getUser(UserConfig.getClientUserId());
|
|
|
|
if (user == null) {
|
|
|
|
user = UserConfig.getCurrentUser();
|
|
|
|
}
|
|
|
|
if (user == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (user != null) {
|
|
|
|
user.photo = UserConfig.getCurrentUser().photo;
|
|
|
|
}
|
2014-11-17 03:44:57 +01:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.mainUserInfoChanged);
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, MessagesController.UPDATE_MASK_ALL);
|
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error == null) {
|
|
|
|
TLRPC.User user = MessagesController.getInstance().getUser(UserConfig.getClientUserId());
|
|
|
|
if (user == null) {
|
|
|
|
user = UserConfig.getCurrentUser();
|
|
|
|
MessagesController.getInstance().putUser(user, false);
|
|
|
|
} else {
|
|
|
|
UserConfig.setCurrentUser(user);
|
|
|
|
}
|
|
|
|
if (user == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MessagesStorage.getInstance().clearUserPhotos(user.id);
|
2015-01-02 23:15:07 +01:00
|
|
|
ArrayList<TLRPC.User> users = new ArrayList<>();
|
2014-08-22 16:24:33 +02:00
|
|
|
users.add(user);
|
|
|
|
MessagesStorage.getInstance().putUsersAndChats(users, null, false, true);
|
2015-03-19 00:09:45 +01:00
|
|
|
user.photo = (TLRPC.UserProfilePhoto) response;
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-08-22 16:24:33 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2015-02-26 02:32:51 +01:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.mainUserInfoChanged);
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, MessagesController.UPDATE_MASK_ALL);
|
|
|
|
UserConfig.saveConfig(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
TLRPC.TL_photos_deletePhotos req = new TLRPC.TL_photos_deletePhotos();
|
|
|
|
req.id.add(photo);
|
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 20:25:49 +01:00
|
|
|
public void processLoadedUserPhotos(final TLRPC.photos_Photos res, final int uid, final int offset, final int count, final long max_id, final boolean fromCache, final int classGuid) {
|
|
|
|
if (!fromCache) {
|
2014-07-14 23:27:26 +02:00
|
|
|
MessagesStorage.getInstance().putUsersAndChats(res.users, null, true, true);
|
2014-03-22 23:31:55 +01:00
|
|
|
MessagesStorage.getInstance().putUserPhotos(uid, res);
|
2013-12-20 20:25:49 +01:00
|
|
|
} else if (res == null || res.photos.isEmpty()) {
|
|
|
|
loadUserPhotos(uid, offset, count, max_id, false, classGuid);
|
|
|
|
return;
|
|
|
|
}
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2013-12-20 20:25:49 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-08-22 16:24:33 +02:00
|
|
|
putUsers(res.users, fromCache);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.userPhotosLoaded, uid, offset, count, fromCache, classGuid, res.photos);
|
2013-12-20 20:25:49 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-10-25 17:19:00 +02:00
|
|
|
public void uploadAndApplyUserAvatar(TLRPC.PhotoSize bigPhoto) {
|
|
|
|
if (bigPhoto != null) {
|
2014-09-25 05:54:35 +02:00
|
|
|
uploadingAvatar = FileLoader.getInstance().getDirectory(FileLoader.MEDIA_DIR_CACHE) + "/" + bigPhoto.location.volume_id + "_" + bigPhoto.location.local_id + ".jpg";
|
2014-09-01 17:43:03 +02:00
|
|
|
FileLoader.getInstance().uploadFile(uploadingAvatar, false, true);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-22 23:31:55 +01:00
|
|
|
public void deleteMessages(ArrayList<Integer> messages, ArrayList<Long> randoms, TLRPC.EncryptedChat encryptedChat) {
|
2014-09-14 01:40:36 +02:00
|
|
|
if (messages == null) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
for (Integer id : messages) {
|
|
|
|
MessageObject obj = dialogMessage.get(id);
|
|
|
|
if (obj != null) {
|
|
|
|
obj.deleted = true;
|
|
|
|
}
|
|
|
|
}
|
2014-03-22 23:31:55 +01:00
|
|
|
MessagesStorage.getInstance().markMessagesAsDeleted(messages, true);
|
|
|
|
MessagesStorage.getInstance().updateDialogsWithDeletedMessages(messages, true);
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.messagesDeleted, messages);
|
2014-03-22 23:31:55 +01:00
|
|
|
|
|
|
|
if (randoms != null && encryptedChat != null && !randoms.isEmpty()) {
|
2014-12-01 18:56:31 +01:00
|
|
|
SecretChatHelper.getInstance().sendMessagesDeleteMessage(encryptedChat, randoms, null);
|
2014-03-22 23:31:55 +01:00
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
|
2015-01-02 23:15:07 +01:00
|
|
|
ArrayList<Integer> toSend = new ArrayList<>();
|
2013-10-25 17:19:00 +02:00
|
|
|
for (Integer mid : messages) {
|
|
|
|
if (mid > 0) {
|
|
|
|
toSend.add(mid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (toSend.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
TLRPC.TL_messages_deleteMessages req = new TLRPC.TL_messages_deleteMessages();
|
|
|
|
req.id = messages;
|
2014-03-22 23:31:55 +01:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
2015-03-19 00:09:45 +01:00
|
|
|
if (error == null) {
|
|
|
|
TLRPC.TL_messages_affectedMessages res = (TLRPC.TL_messages_affectedMessages) response;
|
|
|
|
processNewDifferenceParams(-1, res.pts, -1, res.pts_count);
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-07-03 00:39:05 +02:00
|
|
|
});
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void deleteDialog(final long did, int offset, final boolean onlyHistory) {
|
2014-11-20 15:45:33 +01:00
|
|
|
int lower_part = (int)did;
|
|
|
|
int high_id = (int)(did >> 32);
|
|
|
|
|
|
|
|
if (offset == 0) {
|
|
|
|
TLRPC.TL_dialog dialog = dialogs_dict.get(did);
|
2014-11-21 01:14:44 +01:00
|
|
|
if (dialog != null) {
|
|
|
|
if (!onlyHistory) {
|
|
|
|
dialogs.remove(dialog);
|
|
|
|
dialogsServerOnly.remove(dialog);
|
|
|
|
dialogs_dict.remove(did);
|
|
|
|
totalDialogsCount--;
|
|
|
|
} else {
|
|
|
|
dialog.unread_count = 0;
|
|
|
|
}
|
|
|
|
dialogMessage.remove(dialog.top_message);
|
2015-02-26 02:32:51 +01:00
|
|
|
dialog.top_message = 0;
|
2014-07-27 12:53:00 +02:00
|
|
|
}
|
2015-02-26 02:32:51 +01:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.dialogsNeedReload);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.removeAllMessagesFromDialog, did);
|
2014-11-20 15:45:33 +01:00
|
|
|
MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
NotificationsController.getInstance().processReadMessages(null, did, 0, Integer.MAX_VALUE, false);
|
2015-01-02 23:15:07 +01:00
|
|
|
HashMap<Long, Integer> dialogsToUpdate = new HashMap<>();
|
2014-11-20 15:45:33 +01:00
|
|
|
dialogsToUpdate.put(did, 0);
|
|
|
|
NotificationsController.getInstance().processDialogsUpdateRead(dialogsToUpdate);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2015-02-26 02:32:51 +01:00
|
|
|
|
|
|
|
MessagesStorage.getInstance().deleteDialog(did, onlyHistory);
|
2014-11-20 15:45:33 +01:00
|
|
|
}
|
2014-07-27 12:53:00 +02:00
|
|
|
|
2014-11-20 15:45:33 +01:00
|
|
|
if (high_id == 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lower_part != 0) {
|
|
|
|
TLRPC.TL_messages_deleteHistory req = new TLRPC.TL_messages_deleteHistory();
|
|
|
|
req.offset = offset;
|
|
|
|
if (did < 0) {
|
|
|
|
req.peer = new TLRPC.TL_inputPeerChat();
|
|
|
|
req.peer.chat_id = -lower_part;
|
|
|
|
} else {
|
|
|
|
TLRPC.User user = getUser(lower_part);
|
|
|
|
if (user instanceof TLRPC.TL_userForeign || user instanceof TLRPC.TL_userRequest) {
|
|
|
|
req.peer = new TLRPC.TL_inputPeerForeign();
|
|
|
|
req.peer.access_hash = user.access_hash;
|
2013-10-25 17:19:00 +02:00
|
|
|
} else {
|
2014-11-20 15:45:33 +01:00
|
|
|
req.peer = new TLRPC.TL_inputPeerContact();
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-11-20 15:45:33 +01:00
|
|
|
req.peer.user_id = lower_part;
|
|
|
|
}
|
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error == null) {
|
|
|
|
TLRPC.TL_messages_affectedHistory res = (TLRPC.TL_messages_affectedHistory) response;
|
|
|
|
if (res.offset > 0) {
|
|
|
|
deleteDialog(did, res.offset, onlyHistory);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2015-03-19 00:09:45 +01:00
|
|
|
processNewDifferenceParams(-1, res.pts, -1, res.pts_count);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-03-22 23:31:55 +01:00
|
|
|
}
|
2014-11-20 15:45:33 +01:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (onlyHistory) {
|
2014-12-01 18:56:31 +01:00
|
|
|
SecretChatHelper.getInstance().sendClearHistoryMessage(getEncryptedChat(high_id), null);
|
2014-11-20 15:45:33 +01:00
|
|
|
} else {
|
2014-12-01 18:56:31 +01:00
|
|
|
SecretChatHelper.getInstance().declineSecretChat(high_id);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-08 12:17:06 +02:00
|
|
|
public void loadChatInfo(final int chat_id, Semaphore semaphore) {
|
|
|
|
MessagesStorage.getInstance().loadChatInfo(chat_id, semaphore);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void processChatInfo(final int chat_id, final TLRPC.ChatParticipants info, final ArrayList<TLRPC.User> usersArr, final boolean fromCache) {
|
2014-11-19 16:17:24 +01:00
|
|
|
if (fromCache && chat_id > 0) {
|
|
|
|
loadFullChat(chat_id, 0);
|
|
|
|
}
|
|
|
|
if (info != null) {
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-08-22 16:24:33 +02:00
|
|
|
putUsers(usersArr, fromCache);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.chatInfoDidLoaded, chat_id, info);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateTimerProc() {
|
|
|
|
long currentTime = System.currentTimeMillis();
|
|
|
|
|
2014-10-14 10:13:16 +02:00
|
|
|
checkDeletingTask(false);
|
2013-10-25 17:19:00 +02:00
|
|
|
|
2014-06-13 12:42:21 +02:00
|
|
|
if (UserConfig.isClientActivated()) {
|
2014-07-27 00:28:33 +02:00
|
|
|
if (ConnectionsManager.getInstance().getPauseTime() == 0 && ApplicationLoader.isScreenOn && !ApplicationLoader.mainInterfacePaused) {
|
2014-02-28 23:28:25 +01:00
|
|
|
if (statusSettingState != 1 && (lastStatusUpdateTime == 0 || lastStatusUpdateTime <= System.currentTimeMillis() - 55000 || offlineSent)) {
|
|
|
|
statusSettingState = 1;
|
|
|
|
|
|
|
|
if (statusRequest != 0) {
|
2014-03-22 23:31:55 +01:00
|
|
|
ConnectionsManager.getInstance().cancelRpc(statusRequest, true);
|
2014-02-28 23:28:25 +01:00
|
|
|
}
|
|
|
|
|
2013-10-25 17:19:00 +02:00
|
|
|
TLRPC.TL_account_updateStatus req = new TLRPC.TL_account_updateStatus();
|
|
|
|
req.offline = false;
|
2014-03-22 23:31:55 +01:00
|
|
|
statusRequest = ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
2014-02-28 23:28:25 +01:00
|
|
|
if (error == null) {
|
|
|
|
lastStatusUpdateTime = System.currentTimeMillis();
|
|
|
|
offlineSent = false;
|
|
|
|
statusSettingState = 0;
|
|
|
|
} else {
|
|
|
|
if (lastStatusUpdateTime != 0) {
|
|
|
|
lastStatusUpdateTime += 5000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
statusRequest = 0;
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-07-03 00:39:05 +02:00
|
|
|
});
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-07-03 00:39:05 +02:00
|
|
|
} else if (statusSettingState != 2 && !offlineSent && ConnectionsManager.getInstance().getPauseTime() <= System.currentTimeMillis() - 2000) {
|
2014-02-28 23:28:25 +01:00
|
|
|
statusSettingState = 2;
|
|
|
|
if (statusRequest != 0) {
|
2014-03-22 23:31:55 +01:00
|
|
|
ConnectionsManager.getInstance().cancelRpc(statusRequest, true);
|
2014-02-28 23:28:25 +01:00
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
TLRPC.TL_account_updateStatus req = new TLRPC.TL_account_updateStatus();
|
|
|
|
req.offline = true;
|
2014-03-22 23:31:55 +01:00
|
|
|
statusRequest = ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
2014-02-28 23:28:25 +01:00
|
|
|
if (error == null) {
|
|
|
|
offlineSent = true;
|
|
|
|
} else {
|
|
|
|
if (lastStatusUpdateTime != 0) {
|
|
|
|
lastStatusUpdateTime += 5000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
statusRequest = 0;
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-07-03 00:39:05 +02:00
|
|
|
});
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-02-04 19:36:55 +01:00
|
|
|
|
2015-03-19 00:09:45 +01:00
|
|
|
for (int a = 0; a < 3; a++) {
|
|
|
|
if (getUpdatesStartTime(a) != 0 && getUpdatesStartTime(a) + 1500 < currentTime) {
|
|
|
|
FileLog.e("tmessages", a + " QUEUE UPDATES WAIT TIMEOUT - CHECK QUEUE");
|
|
|
|
processUpdatesQueue(a, 0);
|
|
|
|
}
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-11-21 01:14:44 +01:00
|
|
|
if (!onlinePrivacy.isEmpty()) {
|
|
|
|
ArrayList<Integer> toRemove = null;
|
|
|
|
int currentServerTime = ConnectionsManager.getInstance().getCurrentTime();
|
|
|
|
for (ConcurrentHashMap.Entry<Integer, Integer> entry : onlinePrivacy.entrySet()) {
|
|
|
|
if (entry.getValue() < currentServerTime - 30) {
|
|
|
|
if (toRemove == null) {
|
2015-01-02 23:15:07 +01:00
|
|
|
toRemove = new ArrayList<>();
|
2014-11-21 01:14:44 +01:00
|
|
|
}
|
|
|
|
toRemove.add(entry.getKey());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (toRemove != null) {
|
|
|
|
for (Integer uid : toRemove) {
|
|
|
|
onlinePrivacy.remove(uid);
|
|
|
|
}
|
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_STATUS);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2014-05-20 22:22:44 +02:00
|
|
|
if (!printingUsers.isEmpty() || lastPrintingStringCount != printingUsers.size()) {
|
2014-02-04 19:36:55 +01:00
|
|
|
boolean updated = false;
|
2015-01-02 23:15:07 +01:00
|
|
|
ArrayList<Long> keys = new ArrayList<>(printingUsers.keySet());
|
2013-10-25 17:19:00 +02:00
|
|
|
for (int b = 0; b < keys.size(); b++) {
|
|
|
|
Long key = keys.get(b);
|
|
|
|
ArrayList<PrintingUser> arr = printingUsers.get(key);
|
|
|
|
for (int a = 0; a < arr.size(); a++) {
|
|
|
|
PrintingUser user = arr.get(a);
|
|
|
|
if (user.lastTime + 5900 < currentTime) {
|
2014-02-04 19:36:55 +01:00
|
|
|
updated = true;
|
2013-10-25 17:19:00 +02:00
|
|
|
arr.remove(user);
|
|
|
|
a--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (arr.isEmpty()) {
|
|
|
|
printingUsers.remove(key);
|
|
|
|
keys.remove(b);
|
|
|
|
b--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updatePrintingStrings();
|
|
|
|
|
2014-02-04 19:36:55 +01:00
|
|
|
if (updated) {
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_USER_PRINT);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updatePrintingStrings() {
|
2015-01-02 23:15:07 +01:00
|
|
|
final HashMap<Long, CharSequence> newPrintingStrings = new HashMap<>();
|
2013-10-25 17:19:00 +02:00
|
|
|
|
2015-01-02 23:15:07 +01:00
|
|
|
ArrayList<Long> keys = new ArrayList<>(printingUsers.keySet());
|
2013-10-25 17:19:00 +02:00
|
|
|
for (Long key : keys) {
|
2014-06-11 01:05:54 +02:00
|
|
|
if (key > 0 || key.intValue() == 0) {
|
2014-03-22 23:31:55 +01:00
|
|
|
newPrintingStrings.put(key, LocaleController.getString("Typing", R.string.Typing));
|
2013-10-25 17:19:00 +02:00
|
|
|
} else {
|
|
|
|
ArrayList<PrintingUser> arr = printingUsers.get(key);
|
|
|
|
int count = 0;
|
|
|
|
String label = "";
|
|
|
|
for (PrintingUser pu : arr) {
|
2014-08-22 16:24:33 +02:00
|
|
|
TLRPC.User user = getUser(pu.userId);
|
2013-10-25 17:19:00 +02:00
|
|
|
if (user != null) {
|
|
|
|
if (label.length() != 0) {
|
|
|
|
label += ", ";
|
|
|
|
}
|
2015-03-26 18:34:47 +01:00
|
|
|
if (user.first_name != null && user.first_name.length() > 0) {
|
|
|
|
label += user.first_name;
|
|
|
|
} else if (user.last_name != null && user.last_name.length() > 0) {
|
|
|
|
label += user.last_name;
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
count++;
|
|
|
|
}
|
|
|
|
if (count == 2) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (label.length() != 0) {
|
|
|
|
if (count > 1) {
|
|
|
|
if (arr.size() > 2) {
|
2014-07-03 16:55:04 +02:00
|
|
|
newPrintingStrings.put(key, Html.fromHtml(String.format("%s %s", label, LocaleController.formatPluralString("AndMoreTyping", arr.size() - 2))));
|
2013-10-25 17:19:00 +02:00
|
|
|
} else {
|
2014-03-22 23:31:55 +01:00
|
|
|
newPrintingStrings.put(key, Html.fromHtml(String.format("%s %s", label, LocaleController.getString("AreTyping", R.string.AreTyping))));
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
} else {
|
2014-03-22 23:31:55 +01:00
|
|
|
newPrintingStrings.put(key, Html.fromHtml(String.format("%s %s", label, LocaleController.getString("IsTyping", R.string.IsTyping))));
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-20 22:22:44 +02:00
|
|
|
lastPrintingStringCount = newPrintingStrings.size();
|
|
|
|
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
printingStrings = newPrintingStrings;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-08-23 01:22:33 +02:00
|
|
|
public void cancelTyping(long dialog_id) {
|
|
|
|
sendingTypings.remove(dialog_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sendTyping(final long dialog_id, int classGuid) {
|
2013-10-25 17:19:00 +02:00
|
|
|
if (dialog_id == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-23 01:22:33 +02:00
|
|
|
if (sendingTypings.get(dialog_id) != null) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
int lower_part = (int)dialog_id;
|
2014-07-27 12:53:00 +02:00
|
|
|
int high_id = (int)(dialog_id >> 32);
|
2013-10-25 17:19:00 +02:00
|
|
|
if (lower_part != 0) {
|
2014-07-27 12:53:00 +02:00
|
|
|
if (high_id == 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-25 17:19:00 +02:00
|
|
|
TLRPC.TL_messages_setTyping req = new TLRPC.TL_messages_setTyping();
|
|
|
|
if (lower_part < 0) {
|
|
|
|
req.peer = new TLRPC.TL_inputPeerChat();
|
|
|
|
req.peer.chat_id = -lower_part;
|
|
|
|
} else {
|
2014-08-22 16:24:33 +02:00
|
|
|
TLRPC.User user = getUser(lower_part);
|
2013-12-20 20:25:49 +01:00
|
|
|
if (user != null) {
|
|
|
|
if (user instanceof TLRPC.TL_userForeign || user instanceof TLRPC.TL_userRequest) {
|
|
|
|
req.peer = new TLRPC.TL_inputPeerForeign();
|
|
|
|
req.peer.user_id = user.id;
|
|
|
|
req.peer.access_hash = user.access_hash;
|
|
|
|
} else {
|
|
|
|
req.peer = new TLRPC.TL_inputPeerContact();
|
|
|
|
req.peer.user_id = user.id;
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
} else {
|
2013-12-20 20:25:49 +01:00
|
|
|
return;
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
2014-10-07 22:14:27 +02:00
|
|
|
req.action = new TLRPC.TL_sendMessageTypingAction();
|
2014-08-23 01:22:33 +02:00
|
|
|
sendingTypings.put(dialog_id, true);
|
2014-03-22 23:31:55 +01:00
|
|
|
long reqId = ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-08-23 01:22:33 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
sendingTypings.remove(dialog_id);
|
|
|
|
}
|
|
|
|
});
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-07-12 17:38:37 +02:00
|
|
|
}, true, RPCRequest.RPCRequestClassGeneric | RPCRequest.RPCRequestClassFailOnServerErrors);
|
2014-03-22 23:31:55 +01:00
|
|
|
ConnectionsManager.getInstance().bindRequestToGuid(reqId, classGuid);
|
2013-10-25 17:19:00 +02:00
|
|
|
} else {
|
2014-08-22 16:24:33 +02:00
|
|
|
TLRPC.EncryptedChat chat = getEncryptedChat(high_id);
|
2014-07-27 12:53:00 +02:00
|
|
|
if (chat.auth_key != null && chat.auth_key.length > 1 && chat instanceof TLRPC.TL_encryptedChat) {
|
|
|
|
TLRPC.TL_messages_setEncryptedTyping req = new TLRPC.TL_messages_setEncryptedTyping();
|
|
|
|
req.peer = new TLRPC.TL_inputEncryptedChat();
|
|
|
|
req.peer.chat_id = chat.id;
|
|
|
|
req.peer.access_hash = chat.access_hash;
|
|
|
|
req.typing = true;
|
2014-08-23 01:22:33 +02:00
|
|
|
sendingTypings.put(dialog_id, true);
|
2014-07-27 12:53:00 +02:00
|
|
|
long reqId = ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
2014-08-23 01:22:33 +02:00
|
|
|
sendingTypings.remove(dialog_id);
|
2014-07-27 12:53:00 +02:00
|
|
|
}
|
|
|
|
}, true, RPCRequest.RPCRequestClassGeneric | RPCRequest.RPCRequestClassFailOnServerErrors);
|
|
|
|
ConnectionsManager.getInstance().bindRequestToGuid(reqId, classGuid);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-19 02:23:46 +01:00
|
|
|
public void loadMessages(final long dialog_id, final int count, final int max_id, boolean fromCache, int midDate, final int classGuid, final int load_type, final int last_message_id, final int first_message_id, final boolean allowCache) {
|
2013-10-25 17:19:00 +02:00
|
|
|
int lower_part = (int)dialog_id;
|
|
|
|
if (fromCache || lower_part == 0) {
|
2014-10-31 20:02:29 +01:00
|
|
|
MessagesStorage.getInstance().getMessages(dialog_id, count, max_id, midDate, classGuid, load_type);
|
2013-10-25 17:19:00 +02:00
|
|
|
} else {
|
|
|
|
TLRPC.TL_messages_getHistory req = new TLRPC.TL_messages_getHistory();
|
|
|
|
if (lower_part < 0) {
|
|
|
|
req.peer = new TLRPC.TL_inputPeerChat();
|
|
|
|
req.peer.chat_id = -lower_part;
|
|
|
|
} else {
|
2014-08-22 16:24:33 +02:00
|
|
|
TLRPC.User user = getUser(lower_part);
|
2013-10-25 17:19:00 +02:00
|
|
|
if (user instanceof TLRPC.TL_userForeign || user instanceof TLRPC.TL_userRequest) {
|
|
|
|
req.peer = new TLRPC.TL_inputPeerForeign();
|
|
|
|
req.peer.user_id = user.id;
|
|
|
|
req.peer.access_hash = user.access_hash;
|
|
|
|
} else {
|
|
|
|
req.peer = new TLRPC.TL_inputPeerContact();
|
|
|
|
req.peer.user_id = user.id;
|
|
|
|
}
|
|
|
|
}
|
2014-11-18 06:01:04 +01:00
|
|
|
if (load_type == 3) {
|
|
|
|
req.offset = -count / 2;
|
2014-11-19 02:23:46 +01:00
|
|
|
} else if (load_type == 1) {
|
|
|
|
req.offset = -count - 1;
|
2014-11-18 06:01:04 +01:00
|
|
|
} else {
|
|
|
|
req.offset = 0;
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
req.limit = count;
|
|
|
|
req.max_id = max_id;
|
2014-03-22 23:31:55 +01:00
|
|
|
long reqId = ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error == null) {
|
2014-03-22 23:31:55 +01:00
|
|
|
final TLRPC.messages_Messages res = (TLRPC.messages_Messages) response;
|
2014-11-19 02:23:46 +01:00
|
|
|
processLoadedMessages(res, dialog_id, count, max_id, false, classGuid, 0, last_message_id, first_message_id, 0, 0, load_type, allowCache);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-03 00:39:05 +02:00
|
|
|
});
|
2014-03-22 23:31:55 +01:00
|
|
|
ConnectionsManager.getInstance().bindRequestToGuid(reqId, classGuid);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-19 02:23:46 +01:00
|
|
|
public void processLoadedMessages(final TLRPC.messages_Messages messagesRes, final long dialog_id, final int count, final int max_id, final boolean isCache, final int classGuid,
|
|
|
|
final int first_unread, final int last_message_id, final int first_message_id, final int unread_count, final int last_date, final int load_type, final boolean allowCache) {
|
2013-10-25 17:19:00 +02:00
|
|
|
Utilities.stageQueue.postRunnable(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2015-03-19 00:09:45 +01:00
|
|
|
int lower_id = (int) dialog_id;
|
|
|
|
int high_id = (int) (dialog_id >> 32);
|
2015-02-01 19:51:02 +01:00
|
|
|
if (!isCache) {
|
|
|
|
ImageLoader.saveMessagesThumbs(messagesRes.messages);
|
|
|
|
}
|
2014-11-19 02:23:46 +01:00
|
|
|
if (!isCache && allowCache) {
|
2014-03-22 23:31:55 +01:00
|
|
|
MessagesStorage.getInstance().putMessages(messagesRes, dialog_id);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-11-17 23:04:31 +01:00
|
|
|
if (high_id != 1 && lower_id != 0 && isCache && messagesRes.messages.size() == 0 && (load_type == 0 || load_type == 2 || load_type == 3)) {
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-11-19 02:23:46 +01:00
|
|
|
loadMessages(dialog_id, count, max_id, false, 0, classGuid, load_type, last_message_id, first_message_id, allowCache);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2015-01-02 23:15:07 +01:00
|
|
|
final HashMap<Integer, TLRPC.User> usersLocal = new HashMap<>();
|
2013-10-25 17:19:00 +02:00
|
|
|
for (TLRPC.User u : messagesRes.users) {
|
|
|
|
usersLocal.put(u.id, u);
|
|
|
|
}
|
2015-01-02 23:15:07 +01:00
|
|
|
final ArrayList<MessageObject> objects = new ArrayList<>();
|
2015-01-09 13:50:15 +01:00
|
|
|
ArrayList<Integer> messagesToReload = null;
|
2013-10-25 17:19:00 +02:00
|
|
|
for (TLRPC.Message message : messagesRes.messages) {
|
|
|
|
message.dialog_id = dialog_id;
|
2015-02-01 19:51:02 +01:00
|
|
|
objects.add(new MessageObject(message, usersLocal, true));
|
2015-01-09 13:50:15 +01:00
|
|
|
if (isCache && message.media instanceof TLRPC.TL_messageMediaUnsupported) {
|
|
|
|
if (message.media.bytes.length == 0 || message.media.bytes.length == 1 && message.media.bytes[0] < TLRPC.LAYER) {
|
|
|
|
if (messagesToReload == null) {
|
|
|
|
messagesToReload = new ArrayList<>();
|
|
|
|
}
|
|
|
|
messagesToReload.add(message.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (messagesToReload != null) {
|
|
|
|
reloadMessages(messagesToReload, dialog_id);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-10-31 20:02:29 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
putUsers(messagesRes.users, isCache);
|
|
|
|
putChats(messagesRes.chats, isCache);
|
2014-11-19 02:23:46 +01:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.messagesDidLoaded, dialog_id, count, objects, isCache, first_unread, last_message_id, first_message_id, unread_count, last_date, load_type);
|
2014-10-31 20:02:29 +01:00
|
|
|
}
|
|
|
|
});
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void loadDialogs(final int offset, final int serverOffset, final int count, boolean fromCache) {
|
|
|
|
if (loadingDialogs) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
loadingDialogs = true;
|
2014-11-20 15:45:33 +01:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.dialogsNeedReload);
|
2013-10-25 17:19:00 +02:00
|
|
|
|
|
|
|
if (fromCache) {
|
2014-03-22 23:31:55 +01:00
|
|
|
MessagesStorage.getInstance().getDialogs(offset, serverOffset, count);
|
2013-10-25 17:19:00 +02:00
|
|
|
} else {
|
|
|
|
TLRPC.TL_messages_getDialogs req = new TLRPC.TL_messages_getDialogs();
|
|
|
|
req.offset = serverOffset;
|
|
|
|
req.limit = count;
|
2014-03-22 23:31:55 +01:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error == null) {
|
2014-03-22 23:31:55 +01:00
|
|
|
final TLRPC.messages_Dialogs dialogsRes = (TLRPC.messages_Dialogs) response;
|
2013-10-25 17:19:00 +02:00
|
|
|
processLoadedDialogs(dialogsRes, null, offset, serverOffset, count, false, false);
|
|
|
|
}
|
|
|
|
}
|
2014-07-03 00:39:05 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void applyDialogsNotificationsSettings(ArrayList<TLRPC.TL_dialog> dialogs) {
|
|
|
|
SharedPreferences.Editor editor = null;
|
|
|
|
for (TLRPC.TL_dialog dialog : dialogs) {
|
|
|
|
if (dialog.peer != null && dialog.notify_settings instanceof TLRPC.TL_peerNotifySettings) {
|
|
|
|
if (editor == null) {
|
|
|
|
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Activity.MODE_PRIVATE);
|
|
|
|
editor = preferences.edit();
|
|
|
|
}
|
|
|
|
int dialog_id = dialog.peer.user_id;
|
|
|
|
if (dialog_id == 0) {
|
|
|
|
dialog_id = -dialog.peer.chat_id;
|
|
|
|
}
|
|
|
|
if (dialog.notify_settings.mute_until != 0) {
|
2015-02-01 19:51:02 +01:00
|
|
|
if (dialog.notify_settings.mute_until > ConnectionsManager.getInstance().getCurrentTime() + 60 * 60 * 24 * 365) {
|
|
|
|
editor.putInt("notify2_" + dialog_id, 2);
|
|
|
|
dialog.notify_settings.mute_until = Integer.MAX_VALUE;
|
|
|
|
} else {
|
|
|
|
editor.putInt("notify2_" + dialog_id, 3);
|
|
|
|
editor.putInt("notifyuntil_" + dialog_id, dialog.notify_settings.mute_until);
|
|
|
|
}
|
2014-07-03 00:39:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (editor != null) {
|
|
|
|
editor.commit();
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-11 15:54:17 +02:00
|
|
|
public void processDialogsUpdateRead(final HashMap<Long, Integer> dialogsToUpdate) {
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-02-28 23:28:25 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (HashMap.Entry<Long, Integer> entry : dialogsToUpdate.entrySet()) {
|
|
|
|
TLRPC.TL_dialog currentDialog = dialogs_dict.get(entry.getKey());
|
|
|
|
if (currentDialog != null) {
|
|
|
|
currentDialog.unread_count = entry.getValue();
|
|
|
|
}
|
|
|
|
}
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.dialogsNeedReload);
|
2015-02-26 02:32:51 +01:00
|
|
|
NotificationsController.getInstance().processDialogsUpdateRead(dialogsToUpdate);
|
2014-02-28 23:28:25 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-10-25 17:19:00 +02:00
|
|
|
public void processDialogsUpdate(final TLRPC.messages_Dialogs dialogsRes, ArrayList<TLRPC.EncryptedChat> encChats) {
|
|
|
|
Utilities.stageQueue.postRunnable(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2015-01-02 23:15:07 +01:00
|
|
|
final HashMap<Long, TLRPC.TL_dialog> new_dialogs_dict = new HashMap<>();
|
|
|
|
final HashMap<Integer, MessageObject> new_dialogMessage = new HashMap<>();
|
|
|
|
final HashMap<Integer, TLRPC.User> usersLocal = new HashMap<>();
|
|
|
|
final HashMap<Long, Integer> dialogsToUpdate = new HashMap<>();
|
2013-10-25 17:19:00 +02:00
|
|
|
|
|
|
|
for (TLRPC.User u : dialogsRes.users) {
|
|
|
|
usersLocal.put(u.id, u);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (TLRPC.Message m : dialogsRes.messages) {
|
2015-02-01 19:51:02 +01:00
|
|
|
new_dialogMessage.put(m.id, new MessageObject(m, usersLocal, false));
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
for (TLRPC.TL_dialog d : dialogsRes.dialogs) {
|
|
|
|
if (d.last_message_date == 0) {
|
|
|
|
MessageObject mess = new_dialogMessage.get(d.top_message);
|
|
|
|
if (mess != null) {
|
|
|
|
d.last_message_date = mess.messageOwner.date;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (d.id == 0) {
|
|
|
|
if (d.peer instanceof TLRPC.TL_peerUser) {
|
|
|
|
d.id = d.peer.user_id;
|
|
|
|
} else if (d.peer instanceof TLRPC.TL_peerChat) {
|
|
|
|
d.id = -d.peer.chat_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new_dialogs_dict.put(d.id, d);
|
2014-07-11 15:54:17 +02:00
|
|
|
dialogsToUpdate.put(d.id, d.unread_count);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-08-22 16:24:33 +02:00
|
|
|
putUsers(dialogsRes.users, true);
|
|
|
|
putChats(dialogsRes.chats, true);
|
2013-10-25 17:19:00 +02:00
|
|
|
|
|
|
|
for (HashMap.Entry<Long, TLRPC.TL_dialog> pair : new_dialogs_dict.entrySet()) {
|
|
|
|
long key = pair.getKey();
|
|
|
|
TLRPC.TL_dialog value = pair.getValue();
|
|
|
|
TLRPC.TL_dialog currentDialog = dialogs_dict.get(key);
|
|
|
|
if (currentDialog == null) {
|
|
|
|
dialogs_dict.put(key, value);
|
|
|
|
dialogMessage.put(value.top_message, new_dialogMessage.get(value.top_message));
|
|
|
|
} else {
|
|
|
|
currentDialog.unread_count = value.unread_count;
|
|
|
|
MessageObject oldMsg = dialogMessage.get(currentDialog.top_message);
|
|
|
|
if (oldMsg == null || currentDialog.top_message > 0) {
|
|
|
|
if (oldMsg != null && oldMsg.deleted || value.top_message > currentDialog.top_message) {
|
|
|
|
dialogs_dict.put(key, value);
|
|
|
|
if (oldMsg != null) {
|
2015-03-19 00:09:45 +01:00
|
|
|
dialogMessage.remove(oldMsg.getId());
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
dialogMessage.put(value.top_message, new_dialogMessage.get(value.top_message));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MessageObject newMsg = new_dialogMessage.get(value.top_message);
|
|
|
|
if (oldMsg.deleted || newMsg == null || newMsg.messageOwner.date > oldMsg.messageOwner.date) {
|
|
|
|
dialogs_dict.put(key, value);
|
2015-03-19 00:09:45 +01:00
|
|
|
dialogMessage.remove(oldMsg.getId());
|
2013-10-25 17:19:00 +02:00
|
|
|
dialogMessage.put(value.top_message, new_dialogMessage.get(value.top_message));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dialogs.clear();
|
|
|
|
dialogsServerOnly.clear();
|
|
|
|
dialogs.addAll(dialogs_dict.values());
|
|
|
|
Collections.sort(dialogs, new Comparator<TLRPC.TL_dialog>() {
|
|
|
|
@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) {
|
2015-03-19 00:09:45 +01:00
|
|
|
int high_id = (int) (d.id >> 32);
|
|
|
|
if ((int) d.id != 0 && high_id != 1) {
|
2013-10-25 17:19:00 +02:00
|
|
|
dialogsServerOnly.add(d);
|
|
|
|
}
|
|
|
|
}
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.dialogsNeedReload);
|
2015-02-26 02:32:51 +01:00
|
|
|
NotificationsController.getInstance().processDialogsUpdateRead(dialogsToUpdate);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
});
|
2015-03-19 00:09:45 +01:00
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void processLoadedDialogs(final TLRPC.messages_Dialogs dialogsRes, final ArrayList<TLRPC.EncryptedChat> encChats, final int offset, final int serverOffset, final int count, final boolean isCache, final boolean resetEnd) {
|
|
|
|
Utilities.stageQueue.postRunnable(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (isCache && dialogsRes.dialogs.size() == 0) {
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-08-22 16:24:33 +02:00
|
|
|
putUsers(dialogsRes.users, isCache);
|
2013-10-25 17:19:00 +02:00
|
|
|
loadingDialogs = false;
|
|
|
|
if (resetEnd) {
|
|
|
|
dialogsEndReached = false;
|
|
|
|
}
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.dialogsNeedReload);
|
2015-02-26 02:32:51 +01:00
|
|
|
loadDialogs(offset, serverOffset, count, false);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2015-01-02 23:15:07 +01:00
|
|
|
final HashMap<Long, TLRPC.TL_dialog> new_dialogs_dict = new HashMap<>();
|
|
|
|
final HashMap<Integer, MessageObject> new_dialogMessage = new HashMap<>();
|
|
|
|
final HashMap<Integer, TLRPC.User> usersLocal = new HashMap<>();
|
2013-10-25 17:19:00 +02:00
|
|
|
int new_totalDialogsCount;
|
|
|
|
|
|
|
|
if (!isCache) {
|
2015-02-01 19:51:02 +01:00
|
|
|
ImageLoader.saveMessagesThumbs(dialogsRes.messages);
|
2014-03-22 23:31:55 +01:00
|
|
|
MessagesStorage.getInstance().putDialogs(dialogsRes);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dialogsRes instanceof TLRPC.TL_messages_dialogsSlice) {
|
2015-03-19 00:09:45 +01:00
|
|
|
TLRPC.TL_messages_dialogsSlice slice = (TLRPC.TL_messages_dialogsSlice) dialogsRes;
|
2013-10-25 17:19:00 +02:00
|
|
|
new_totalDialogsCount = slice.count;
|
|
|
|
} else {
|
|
|
|
new_totalDialogsCount = dialogsRes.dialogs.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (TLRPC.User u : dialogsRes.users) {
|
|
|
|
usersLocal.put(u.id, u);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (TLRPC.Message m : dialogsRes.messages) {
|
2015-02-01 19:51:02 +01:00
|
|
|
new_dialogMessage.put(m.id, new MessageObject(m, usersLocal, false));
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
for (TLRPC.TL_dialog d : dialogsRes.dialogs) {
|
|
|
|
if (d.last_message_date == 0) {
|
|
|
|
MessageObject mess = new_dialogMessage.get(d.top_message);
|
|
|
|
if (mess != null) {
|
|
|
|
d.last_message_date = mess.messageOwner.date;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (d.id == 0) {
|
|
|
|
if (d.peer instanceof TLRPC.TL_peerUser) {
|
|
|
|
d.id = d.peer.user_id;
|
|
|
|
} else if (d.peer instanceof TLRPC.TL_peerChat) {
|
|
|
|
d.id = -d.peer.chat_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
new_dialogs_dict.put(d.id, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
final int arg1 = new_totalDialogsCount;
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-07-03 00:39:05 +02:00
|
|
|
if (!isCache) {
|
|
|
|
applyDialogsNotificationsSettings(dialogsRes.dialogs);
|
|
|
|
}
|
2014-08-22 16:24:33 +02:00
|
|
|
putUsers(dialogsRes.users, isCache);
|
|
|
|
putChats(dialogsRes.chats, isCache);
|
2013-10-25 17:19:00 +02:00
|
|
|
if (encChats != null) {
|
|
|
|
for (TLRPC.EncryptedChat encryptedChat : encChats) {
|
2014-12-01 18:56:31 +01:00
|
|
|
if (encryptedChat instanceof TLRPC.TL_encryptedChat && AndroidUtilities.getMyLayerVersion(encryptedChat.layer) < SecretChatHelper.CURRENT_SECRET_CHAT_LAYER) {
|
|
|
|
SecretChatHelper.getInstance().sendNotifyLayerMessage(encryptedChat, null);
|
2014-10-22 22:01:07 +02:00
|
|
|
}
|
2014-08-22 16:24:33 +02:00
|
|
|
putEncryptedChat(encryptedChat, true);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
loadingDialogs = false;
|
|
|
|
totalDialogsCount = arg1;
|
|
|
|
|
|
|
|
for (HashMap.Entry<Long, TLRPC.TL_dialog> pair : new_dialogs_dict.entrySet()) {
|
|
|
|
long key = pair.getKey();
|
|
|
|
TLRPC.TL_dialog value = pair.getValue();
|
|
|
|
TLRPC.TL_dialog currentDialog = dialogs_dict.get(key);
|
|
|
|
if (currentDialog == null) {
|
|
|
|
dialogs_dict.put(key, value);
|
|
|
|
dialogMessage.put(value.top_message, new_dialogMessage.get(value.top_message));
|
|
|
|
} else {
|
|
|
|
MessageObject oldMsg = dialogMessage.get(value.top_message);
|
|
|
|
if (oldMsg == null || currentDialog.top_message > 0) {
|
|
|
|
if (oldMsg != null && oldMsg.deleted || value.top_message > currentDialog.top_message) {
|
|
|
|
if (oldMsg != null) {
|
2015-03-19 00:09:45 +01:00
|
|
|
dialogMessage.remove(oldMsg.getId());
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
dialogs_dict.put(key, value);
|
|
|
|
dialogMessage.put(value.top_message, new_dialogMessage.get(value.top_message));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MessageObject newMsg = new_dialogMessage.get(value.top_message);
|
|
|
|
if (oldMsg.deleted || newMsg == null || newMsg.messageOwner.date > oldMsg.messageOwner.date) {
|
2015-03-19 00:09:45 +01:00
|
|
|
dialogMessage.remove(oldMsg.getId());
|
2013-10-25 17:19:00 +02:00
|
|
|
dialogs_dict.put(key, value);
|
|
|
|
dialogMessage.put(value.top_message, new_dialogMessage.get(value.top_message));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dialogs.clear();
|
|
|
|
dialogsServerOnly.clear();
|
|
|
|
dialogs.addAll(dialogs_dict.values());
|
|
|
|
Collections.sort(dialogs, new Comparator<TLRPC.TL_dialog>() {
|
|
|
|
@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) {
|
2015-03-19 00:09:45 +01:00
|
|
|
int high_id = (int) (d.id >> 32);
|
|
|
|
if ((int) d.id != 0 && high_id != 1) {
|
2013-10-25 17:19:00 +02:00
|
|
|
dialogsServerOnly.add(d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dialogsEndReached = (dialogsRes.dialogs.size() == 0 || dialogsRes.dialogs.size() != count) && !isCache;
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.dialogsNeedReload);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-22 22:01:07 +02:00
|
|
|
public void markMessageAsRead(final long dialog_id, final long random_id, int ttl) {
|
2014-10-23 17:30:35 +02:00
|
|
|
if (random_id == 0 || dialog_id == 0 || ttl <= 0) {
|
2014-10-09 17:55:05 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
int lower_part = (int)dialog_id;
|
|
|
|
int high_id = (int)(dialog_id >> 32);
|
|
|
|
if (lower_part != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
TLRPC.EncryptedChat chat = getEncryptedChat(high_id);
|
|
|
|
if (chat == null) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-02 23:15:07 +01:00
|
|
|
ArrayList<Long> random_ids = new ArrayList<>();
|
2014-10-09 17:55:05 +02:00
|
|
|
random_ids.add(random_id);
|
2014-12-01 18:56:31 +01:00
|
|
|
SecretChatHelper.getInstance().sendMessagesReadMessage(chat, random_ids, null);
|
2014-10-23 17:30:35 +02:00
|
|
|
int time = ConnectionsManager.getInstance().getCurrentTime();
|
|
|
|
MessagesStorage.getInstance().createTaskForSecretChat(chat.id, time, time, 0, random_ids);
|
2014-10-09 17:55:05 +02:00
|
|
|
}
|
|
|
|
|
2014-08-08 12:17:06 +02:00
|
|
|
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) {
|
2013-10-25 17:19:00 +02:00
|
|
|
int lower_part = (int)dialog_id;
|
2014-07-27 12:53:00 +02:00
|
|
|
int high_id = (int)(dialog_id >> 32);
|
|
|
|
|
2013-10-25 17:19:00 +02:00
|
|
|
if (lower_part != 0) {
|
2014-10-04 17:56:09 +02:00
|
|
|
if (max_positive_id == 0 && offset == 0 || high_id == 1) {
|
2013-10-25 17:19:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
TLRPC.TL_messages_readHistory req = new TLRPC.TL_messages_readHistory();
|
|
|
|
if (lower_part < 0) {
|
|
|
|
req.peer = new TLRPC.TL_inputPeerChat();
|
|
|
|
req.peer.chat_id = -lower_part;
|
|
|
|
} else {
|
2014-08-22 16:24:33 +02:00
|
|
|
TLRPC.User user = getUser(lower_part);
|
2015-02-26 02:32:51 +01:00
|
|
|
if (user == null) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
if (user instanceof TLRPC.TL_userForeign || user instanceof TLRPC.TL_userRequest) {
|
|
|
|
req.peer = new TLRPC.TL_inputPeerForeign();
|
|
|
|
req.peer.user_id = user.id;
|
|
|
|
req.peer.access_hash = user.access_hash;
|
|
|
|
} else {
|
|
|
|
req.peer = new TLRPC.TL_inputPeerContact();
|
|
|
|
req.peer.user_id = user.id;
|
|
|
|
}
|
|
|
|
}
|
2013-12-20 20:25:49 +01:00
|
|
|
req.max_id = max_positive_id;
|
2013-10-25 17:19:00 +02:00
|
|
|
req.offset = offset;
|
|
|
|
if (offset == 0) {
|
2014-03-22 23:31:55 +01:00
|
|
|
MessagesStorage.getInstance().processPendingRead(dialog_id, max_positive_id, max_date, false);
|
2014-10-31 20:02:29 +01:00
|
|
|
MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
|
2014-07-12 12:23:36 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-07-12 12:23:36 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
TLRPC.TL_dialog dialog = dialogs_dict.get(dialog_id);
|
|
|
|
if (dialog != null) {
|
|
|
|
dialog.unread_count = 0;
|
2014-11-11 23:16:17 +01:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_READ_DIALOG_MESSAGE);
|
2014-08-22 16:24:33 +02:00
|
|
|
}
|
|
|
|
if (!popup) {
|
|
|
|
NotificationsController.getInstance().processReadMessages(null, dialog_id, 0, max_positive_id, false);
|
2015-01-02 23:15:07 +01:00
|
|
|
HashMap<Long, Integer> dialogsToUpdate = new HashMap<>();
|
2014-08-22 16:24:33 +02:00
|
|
|
dialogsToUpdate.put(dialog_id, 0);
|
2014-08-27 00:26:25 +02:00
|
|
|
NotificationsController.getInstance().processDialogsUpdateRead(dialogsToUpdate);
|
2014-08-22 16:24:33 +02:00
|
|
|
} else {
|
|
|
|
NotificationsController.getInstance().processReadMessages(null, dialog_id, 0, max_positive_id, true);
|
2015-01-02 23:15:07 +01:00
|
|
|
HashMap<Long, Integer> dialogsToUpdate = new HashMap<>();
|
2014-08-27 00:26:25 +02:00
|
|
|
dialogsToUpdate.put(dialog_id, -1);
|
|
|
|
NotificationsController.getInstance().processDialogsUpdateRead(dialogsToUpdate);
|
2014-08-22 16:24:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-06-20 02:18:13 +02:00
|
|
|
}
|
2014-08-22 16:24:33 +02:00
|
|
|
});
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-08-22 16:24:33 +02:00
|
|
|
if (req.max_id != Integer.MAX_VALUE) {
|
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error == null) {
|
|
|
|
MessagesStorage.getInstance().processPendingRead(dialog_id, max_positive_id, max_date, true);
|
|
|
|
TLRPC.TL_messages_affectedHistory res = (TLRPC.TL_messages_affectedHistory) response;
|
|
|
|
if (res.offset > 0) {
|
|
|
|
markDialogAsRead(dialog_id, 0, max_positive_id, res.offset, max_date, was, popup);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2015-03-19 00:09:45 +01:00
|
|
|
processNewDifferenceParams(-1, res.pts, -1, res.pts_count);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
2014-08-22 16:24:33 +02:00
|
|
|
});
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
} else {
|
2014-08-22 16:24:33 +02:00
|
|
|
if (max_date == 0) {
|
|
|
|
return;
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-08-22 16:24:33 +02:00
|
|
|
TLRPC.EncryptedChat chat = getEncryptedChat(high_id);
|
|
|
|
if (chat.auth_key != null && chat.auth_key.length > 1 && chat instanceof TLRPC.TL_encryptedChat) {
|
|
|
|
TLRPC.TL_messages_readEncryptedHistory req = new TLRPC.TL_messages_readEncryptedHistory();
|
|
|
|
req.peer = new TLRPC.TL_inputEncryptedChat();
|
|
|
|
req.peer.chat_id = chat.id;
|
|
|
|
req.peer.access_hash = chat.access_hash;
|
|
|
|
req.max_date = max_date;
|
2013-10-25 17:19:00 +02:00
|
|
|
|
2014-08-22 16:24:33 +02:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
//MessagesStorage.getInstance().processPendingRead(dialog_id, max_id, max_date, true);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-08-22 16:24:33 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
MessagesStorage.getInstance().processPendingRead(dialog_id, max_id, max_date, false);
|
|
|
|
|
2014-10-31 20:02:29 +01:00
|
|
|
MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
|
2014-08-22 16:24:33 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationsController.getInstance().processReadMessages(null, dialog_id, max_date, 0, popup);
|
|
|
|
TLRPC.TL_dialog dialog = dialogs_dict.get(dialog_id);
|
|
|
|
if (dialog != null) {
|
|
|
|
dialog.unread_count = 0;
|
2014-11-11 23:16:17 +01:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_READ_DIALOG_MESSAGE);
|
2014-07-27 00:28:33 +02:00
|
|
|
}
|
2015-01-02 23:15:07 +01:00
|
|
|
HashMap<Long, Integer> dialogsToUpdate = new HashMap<>();
|
2014-08-22 16:24:33 +02:00
|
|
|
dialogsToUpdate.put(dialog_id, 0);
|
2014-08-27 00:26:25 +02:00
|
|
|
NotificationsController.getInstance().processDialogsUpdateRead(dialogsToUpdate);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-08-22 16:24:33 +02:00
|
|
|
});
|
2013-10-25 17:19:00 +02:00
|
|
|
|
2014-08-22 16:24:33 +02:00
|
|
|
if (chat.ttl > 0 && was) {
|
|
|
|
int serverTime = Math.max(ConnectionsManager.getInstance().getCurrentTime(), max_date);
|
2014-10-09 17:55:05 +02:00
|
|
|
MessagesStorage.getInstance().createTaskForSecretChat(chat.id, serverTime, serverTime, 0, null);
|
2014-08-22 16:24:33 +02:00
|
|
|
}
|
2014-02-28 23:28:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-27 00:28:33 +02:00
|
|
|
public long createChat(String title, ArrayList<Integer> selectedContacts, final TLRPC.InputFile uploadedAvatar, boolean isBroadcast) {
|
|
|
|
if (isBroadcast) {
|
|
|
|
TLRPC.TL_chat chat = new TLRPC.TL_chat();
|
|
|
|
chat.id = UserConfig.lastBroadcastId;
|
|
|
|
chat.title = title;
|
|
|
|
chat.photo = new TLRPC.TL_chatPhotoEmpty();
|
|
|
|
chat.participants_count = selectedContacts.size();
|
|
|
|
chat.date = (int)(System.currentTimeMillis() / 1000);
|
|
|
|
chat.left = false;
|
|
|
|
chat.version = 1;
|
|
|
|
UserConfig.lastBroadcastId--;
|
2014-08-22 16:24:33 +02:00
|
|
|
putChat(chat, false);
|
2015-01-02 23:15:07 +01:00
|
|
|
ArrayList<TLRPC.Chat> chatsArrays = new ArrayList<>();
|
2014-07-27 00:28:33 +02:00
|
|
|
chatsArrays.add(chat);
|
|
|
|
MessagesStorage.getInstance().putUsersAndChats(null, chatsArrays, true, true);
|
|
|
|
|
|
|
|
TLRPC.TL_chatParticipants participants = new TLRPC.TL_chatParticipants();
|
|
|
|
participants.chat_id = chat.id;
|
|
|
|
participants.admin_id = UserConfig.getClientUserId();
|
|
|
|
participants.version = 1;
|
|
|
|
for (Integer id : selectedContacts) {
|
|
|
|
TLRPC.TL_chatParticipant participant = new TLRPC.TL_chatParticipant();
|
|
|
|
participant.user_id = id;
|
|
|
|
participant.inviter_id = UserConfig.getClientUserId();
|
|
|
|
participant.date = (int)(System.currentTimeMillis() / 1000);
|
|
|
|
participants.participants.add(participant);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-07-27 00:28:33 +02:00
|
|
|
MessagesStorage.getInstance().updateChatInfo(chat.id, participants, false);
|
|
|
|
|
|
|
|
TLRPC.TL_messageService newMsg = new TLRPC.TL_messageService();
|
|
|
|
newMsg.action = new TLRPC.TL_messageActionCreatedBroadcastList();
|
|
|
|
newMsg.local_id = newMsg.id = UserConfig.getNewMessageId();
|
|
|
|
newMsg.from_id = UserConfig.getClientUserId();
|
2014-07-27 12:53:00 +02:00
|
|
|
newMsg.dialog_id = AndroidUtilities.makeBroadcastId(chat.id);
|
2014-07-27 00:28:33 +02:00
|
|
|
newMsg.to_id = new TLRPC.TL_peerChat();
|
|
|
|
newMsg.to_id.chat_id = chat.id;
|
|
|
|
newMsg.date = ConnectionsManager.getInstance().getCurrentTime();
|
|
|
|
newMsg.random_id = 0;
|
|
|
|
UserConfig.saveConfig(false);
|
2015-02-01 19:51:02 +01:00
|
|
|
MessageObject newMsgObj = new MessageObject(newMsg, users, true);
|
2014-08-22 16:24:33 +02:00
|
|
|
newMsgObj.messageOwner.send_state = MessageObject.MESSAGE_SEND_STATE_SENT;
|
2014-07-27 00:28:33 +02:00
|
|
|
|
2015-01-02 23:15:07 +01:00
|
|
|
ArrayList<MessageObject> objArr = new ArrayList<>();
|
2014-07-27 00:28:33 +02:00
|
|
|
objArr.add(newMsgObj);
|
2015-01-02 23:15:07 +01:00
|
|
|
ArrayList<TLRPC.Message> arr = new ArrayList<>();
|
2014-07-27 00:28:33 +02:00
|
|
|
arr.add(newMsg);
|
2014-08-22 16:24:33 +02:00
|
|
|
MessagesStorage.getInstance().putMessages(arr, false, true, false, 0);
|
2014-07-27 00:28:33 +02:00
|
|
|
updateInterfaceWithMessages(newMsg.dialog_id, objArr);
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.dialogsNeedReload);
|
2015-02-26 02:32:51 +01:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.chatDidCreated, chat.id);
|
2014-07-27 00:28:33 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
TLRPC.TL_messages_createChat req = new TLRPC.TL_messages_createChat();
|
|
|
|
req.title = title;
|
|
|
|
for (Integer uid : selectedContacts) {
|
2014-08-22 16:24:33 +02:00
|
|
|
TLRPC.User user = getUser(uid);
|
2014-07-27 00:28:33 +02:00
|
|
|
if (user == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
req.users.add(getInputUser(user));
|
|
|
|
}
|
|
|
|
return ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error != null) {
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-07-27 00:28:33 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.chatDidFailCreate);
|
2014-07-27 00:28:33 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final TLRPC.messages_StatedMessage res = (TLRPC.messages_StatedMessage) response;
|
|
|
|
MessagesStorage.getInstance().putUsersAndChats(res.users, res.chats, true, true);
|
|
|
|
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-08-22 16:24:33 +02:00
|
|
|
putUsers(res.users, false);
|
|
|
|
putChats(res.chats, false);
|
2015-01-02 23:15:07 +01:00
|
|
|
final ArrayList<MessageObject> messagesObj = new ArrayList<>();
|
2015-02-01 19:51:02 +01:00
|
|
|
messagesObj.add(new MessageObject(res.message, users, true));
|
2014-07-27 00:28:33 +02:00
|
|
|
TLRPC.Chat chat = res.chats.get(0);
|
|
|
|
updateInterfaceWithMessages(-chat.id, messagesObj);
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.dialogsNeedReload);
|
2015-02-26 02:32:51 +01:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.chatDidCreated, chat.id);
|
2014-07-27 00:28:33 +02:00
|
|
|
if (uploadedAvatar != null) {
|
|
|
|
changeChatAvatar(chat.id, uploadedAvatar);
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
});
|
2014-02-04 19:36:55 +01:00
|
|
|
|
2015-01-02 23:15:07 +01:00
|
|
|
final ArrayList<TLRPC.Message> messages = new ArrayList<>();
|
2014-07-27 00:28:33 +02:00
|
|
|
messages.add(res.message);
|
2014-08-22 16:24:33 +02:00
|
|
|
MessagesStorage.getInstance().putMessages(messages, true, true, false, 0);
|
2015-03-19 00:09:45 +01:00
|
|
|
|
|
|
|
if (res instanceof TLRPC.TL_messages_statedMessage) {
|
|
|
|
MessagesController.getInstance().processNewDifferenceParams(-1, res.pts, res.message.date, res.pts_count);
|
|
|
|
} else if (res instanceof TLRPC.TL_messages_statedMessageLink) {
|
|
|
|
MessagesController.getInstance().processNewDifferenceParams(res.seq, res.pts, res.message.date, res.pts_count);
|
|
|
|
}
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
2014-07-27 00:28:33 +02:00
|
|
|
});
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
|
2014-07-10 23:15:39 +02:00
|
|
|
public void addUserToChat(int chat_id, final TLRPC.User user, final TLRPC.ChatParticipants info, int count_fwd) {
|
2014-02-04 19:36:55 +01:00
|
|
|
if (user == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-27 00:28:33 +02:00
|
|
|
if (chat_id > 0) {
|
|
|
|
TLRPC.TL_messages_addChatUser req = new TLRPC.TL_messages_addChatUser();
|
|
|
|
req.chat_id = chat_id;
|
|
|
|
req.fwd_limit = count_fwd;
|
|
|
|
req.user_id = getInputUser(user);
|
2014-02-04 19:36:55 +01:00
|
|
|
|
2014-07-27 00:28:33 +02:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error != null) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
|
2014-07-27 00:28:33 +02:00
|
|
|
final TLRPC.messages_StatedMessage res = (TLRPC.messages_StatedMessage) response;
|
|
|
|
MessagesStorage.getInstance().putUsersAndChats(res.users, res.chats, true, true);
|
2013-10-25 17:19:00 +02:00
|
|
|
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-07-27 00:28:33 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-08-22 16:24:33 +02:00
|
|
|
putUsers(res.users, false);
|
|
|
|
putChats(res.chats, false);
|
2015-01-02 23:15:07 +01:00
|
|
|
final ArrayList<MessageObject> messagesObj = new ArrayList<>();
|
2015-02-01 19:51:02 +01:00
|
|
|
messagesObj.add(new MessageObject(res.message, users, true));
|
2014-07-27 00:28:33 +02:00
|
|
|
TLRPC.Chat chat = res.chats.get(0);
|
|
|
|
updateInterfaceWithMessages(-chat.id, messagesObj);
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.dialogsNeedReload);
|
2015-02-26 02:32:51 +01:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_CHAT_MEMBERS);
|
2013-10-25 17:19:00 +02:00
|
|
|
|
2014-07-27 00:28:33 +02:00
|
|
|
if (info != null) {
|
|
|
|
for (TLRPC.TL_chatParticipant p : info.participants) {
|
|
|
|
if (p.user_id == user.id) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-07-27 00:28:33 +02:00
|
|
|
TLRPC.TL_chatParticipant newPart = new TLRPC.TL_chatParticipant();
|
|
|
|
newPart.user_id = user.id;
|
|
|
|
newPart.inviter_id = UserConfig.getClientUserId();
|
|
|
|
newPart.date = ConnectionsManager.getInstance().getCurrentTime();
|
|
|
|
info.participants.add(0, newPart);
|
|
|
|
MessagesStorage.getInstance().updateChatInfo(info.chat_id, info, true);
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.chatInfoDidLoaded, info.chat_id, info);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-27 00:28:33 +02:00
|
|
|
});
|
2014-02-04 19:36:55 +01:00
|
|
|
|
2015-01-02 23:15:07 +01:00
|
|
|
final ArrayList<TLRPC.Message> messages = new ArrayList<>();
|
2014-07-27 00:28:33 +02:00
|
|
|
messages.add(res.message);
|
2014-08-22 16:24:33 +02:00
|
|
|
MessagesStorage.getInstance().putMessages(messages, true, true, false, 0);
|
2015-03-19 00:09:45 +01:00
|
|
|
|
|
|
|
if (res instanceof TLRPC.TL_messages_statedMessage) {
|
|
|
|
MessagesController.getInstance().processNewDifferenceParams(-1, res.pts, res.message.date, res.pts_count);
|
|
|
|
} else if (res instanceof TLRPC.TL_messages_statedMessageLink) {
|
|
|
|
MessagesController.getInstance().processNewDifferenceParams(res.seq, res.pts, res.message.date, res.pts_count);
|
|
|
|
}
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
2014-07-27 00:28:33 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (info != null) {
|
|
|
|
for (TLRPC.TL_chatParticipant p : info.participants) {
|
|
|
|
if (p.user_id == user.id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-22 16:24:33 +02:00
|
|
|
TLRPC.Chat chat = getChat(chat_id);
|
2014-07-27 00:28:33 +02:00
|
|
|
chat.participants_count++;
|
2015-01-02 23:15:07 +01:00
|
|
|
ArrayList<TLRPC.Chat> chatArrayList = new ArrayList<>();
|
2014-07-27 00:28:33 +02:00
|
|
|
chatArrayList.add(chat);
|
|
|
|
MessagesStorage.getInstance().putUsersAndChats(null, chatArrayList, true, true);
|
|
|
|
|
|
|
|
TLRPC.TL_chatParticipant newPart = new TLRPC.TL_chatParticipant();
|
|
|
|
newPart.user_id = user.id;
|
|
|
|
newPart.inviter_id = UserConfig.getClientUserId();
|
|
|
|
newPart.date = ConnectionsManager.getInstance().getCurrentTime();
|
|
|
|
info.participants.add(0, newPart);
|
|
|
|
MessagesStorage.getInstance().updateChatInfo(info.chat_id, info, true);
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.chatInfoDidLoaded, info.chat_id, info);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_CHAT_MEMBERS);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-07-27 00:28:33 +02:00
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
|
2014-10-07 22:14:27 +02:00
|
|
|
public void deleteUserFromChat(final int chat_id, final TLRPC.User user, final TLRPC.ChatParticipants info) {
|
2014-02-04 19:36:55 +01:00
|
|
|
if (user == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-07-27 00:28:33 +02:00
|
|
|
if (chat_id > 0) {
|
|
|
|
TLRPC.TL_messages_deleteChatUser req = new TLRPC.TL_messages_deleteChatUser();
|
|
|
|
req.chat_id = chat_id;
|
|
|
|
req.user_id = getInputUser(user);
|
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final TLRPC.messages_StatedMessage res = (TLRPC.messages_StatedMessage) response;
|
2014-11-11 23:16:17 +01:00
|
|
|
if (user.id == UserConfig.getClientUserId()) {
|
|
|
|
res.chats = null;
|
|
|
|
}
|
2014-07-27 00:28:33 +02:00
|
|
|
MessagesStorage.getInstance().putUsersAndChats(res.users, res.chats, true, true);
|
2013-10-25 17:19:00 +02:00
|
|
|
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-07-27 00:28:33 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-08-22 16:24:33 +02:00
|
|
|
putUsers(res.users, false);
|
|
|
|
putChats(res.chats, false);
|
2014-07-27 00:28:33 +02:00
|
|
|
if (user.id != UserConfig.getClientUserId()) {
|
2015-01-02 23:15:07 +01:00
|
|
|
final ArrayList<MessageObject> messagesObj = new ArrayList<>();
|
2015-02-01 19:51:02 +01:00
|
|
|
messagesObj.add(new MessageObject(res.message, users, true));
|
2014-07-27 00:28:33 +02:00
|
|
|
TLRPC.Chat chat = res.chats.get(0);
|
|
|
|
updateInterfaceWithMessages(-chat.id, messagesObj);
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.dialogsNeedReload);
|
2015-02-26 02:32:51 +01:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_CHAT_MEMBERS);
|
2014-07-27 00:28:33 +02:00
|
|
|
}
|
|
|
|
boolean changed = false;
|
|
|
|
if (info != null) {
|
|
|
|
for (int a = 0; a < info.participants.size(); a++) {
|
|
|
|
TLRPC.TL_chatParticipant p = info.participants.get(a);
|
|
|
|
if (p.user_id == user.id) {
|
|
|
|
info.participants.remove(a);
|
|
|
|
changed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (changed) {
|
2014-10-07 22:14:27 +02:00
|
|
|
MessagesStorage.getInstance().updateChatInfo(chat_id, info, true);
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.chatInfoDidLoaded, info.chat_id, info);
|
2014-10-07 22:14:27 +02:00
|
|
|
} else {
|
|
|
|
MessagesStorage.getInstance().updateChatInfo(chat_id, user.id, true, 0, 0);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-10-07 22:14:27 +02:00
|
|
|
} else {
|
|
|
|
MessagesStorage.getInstance().updateChatInfo(chat_id, user.id, true, 0, 0);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-07-27 00:28:33 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (user.id != UserConfig.getClientUserId()) {
|
2015-01-02 23:15:07 +01:00
|
|
|
final ArrayList<TLRPC.Message> messages = new ArrayList<>();
|
2014-07-27 00:28:33 +02:00
|
|
|
messages.add(res.message);
|
2014-08-22 16:24:33 +02:00
|
|
|
MessagesStorage.getInstance().putMessages(messages, true, true, false, 0);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2015-03-19 00:09:45 +01:00
|
|
|
|
|
|
|
if (res instanceof TLRPC.TL_messages_statedMessage) {
|
|
|
|
MessagesController.getInstance().processNewDifferenceParams(-1, res.pts, res.message.date, res.pts_count);
|
|
|
|
} else if (res instanceof TLRPC.TL_messages_statedMessageLink) {
|
|
|
|
MessagesController.getInstance().processNewDifferenceParams(res.seq, res.pts, res.message.date, res.pts_count);
|
|
|
|
}
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
2014-07-27 00:28:33 +02:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (info != null) {
|
2014-08-22 16:24:33 +02:00
|
|
|
TLRPC.Chat chat = getChat(chat_id);
|
2014-07-27 00:28:33 +02:00
|
|
|
chat.participants_count--;
|
2015-01-02 23:15:07 +01:00
|
|
|
ArrayList<TLRPC.Chat> chatArrayList = new ArrayList<>();
|
2014-07-27 00:28:33 +02:00
|
|
|
chatArrayList.add(chat);
|
|
|
|
MessagesStorage.getInstance().putUsersAndChats(null, chatArrayList, true, true);
|
|
|
|
|
|
|
|
boolean changed = false;
|
|
|
|
if (info != null) {
|
|
|
|
for (int a = 0; a < info.participants.size(); a++) {
|
|
|
|
TLRPC.TL_chatParticipant p = info.participants.get(a);
|
|
|
|
if (p.user_id == user.id) {
|
|
|
|
info.participants.remove(a);
|
|
|
|
changed = true;
|
|
|
|
break;
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
2014-07-27 00:28:33 +02:00
|
|
|
}
|
|
|
|
if (changed) {
|
|
|
|
MessagesStorage.getInstance().updateChatInfo(info.chat_id, info, true);
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.chatInfoDidLoaded, info.chat_id, info);
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
|
|
|
}
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_CHAT_MEMBERS);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-07-27 00:28:33 +02:00
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void changeChatTitle(int chat_id, String title) {
|
2014-07-27 00:28:33 +02:00
|
|
|
if (chat_id > 0) {
|
|
|
|
TLRPC.TL_messages_editChatTitle req = new TLRPC.TL_messages_editChatTitle();
|
|
|
|
req.chat_id = chat_id;
|
|
|
|
req.title = title;
|
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final TLRPC.messages_StatedMessage res = (TLRPC.messages_StatedMessage) response;
|
|
|
|
MessagesStorage.getInstance().putUsersAndChats(res.users, res.chats, true, true);
|
2013-10-25 17:19:00 +02:00
|
|
|
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-07-27 00:28:33 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-08-22 16:24:33 +02:00
|
|
|
putUsers(res.users, false);
|
|
|
|
putChats(res.chats, false);
|
2015-01-02 23:15:07 +01:00
|
|
|
final ArrayList<MessageObject> messagesObj = new ArrayList<>();
|
2015-02-01 19:51:02 +01:00
|
|
|
messagesObj.add(new MessageObject(res.message, users, true));
|
2014-07-27 00:28:33 +02:00
|
|
|
TLRPC.Chat chat = res.chats.get(0);
|
|
|
|
updateInterfaceWithMessages(-chat.id, messagesObj);
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.dialogsNeedReload);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_CHAT_NAME);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-07-27 00:28:33 +02:00
|
|
|
});
|
2014-02-04 19:36:55 +01:00
|
|
|
|
2015-01-02 23:15:07 +01:00
|
|
|
final ArrayList<TLRPC.Message> messages = new ArrayList<>();
|
2014-07-27 00:28:33 +02:00
|
|
|
messages.add(res.message);
|
2014-08-22 16:24:33 +02:00
|
|
|
MessagesStorage.getInstance().putMessages(messages, true, true, false, 0);
|
2015-03-19 00:09:45 +01:00
|
|
|
|
|
|
|
if (res instanceof TLRPC.TL_messages_statedMessage) {
|
|
|
|
MessagesController.getInstance().processNewDifferenceParams(-1, res.pts, res.message.date, res.pts_count);
|
|
|
|
} else if (res instanceof TLRPC.TL_messages_statedMessageLink) {
|
|
|
|
MessagesController.getInstance().processNewDifferenceParams(res.seq, res.pts, res.message.date, res.pts_count);
|
|
|
|
}
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
2014-07-27 00:28:33 +02:00
|
|
|
});
|
|
|
|
} else {
|
2014-08-22 16:24:33 +02:00
|
|
|
TLRPC.Chat chat = getChat(chat_id);
|
2014-07-27 00:28:33 +02:00
|
|
|
chat.title = title;
|
2015-01-02 23:15:07 +01:00
|
|
|
ArrayList<TLRPC.Chat> chatArrayList = new ArrayList<>();
|
2014-07-27 00:28:33 +02:00
|
|
|
chatArrayList.add(chat);
|
|
|
|
MessagesStorage.getInstance().putUsersAndChats(null, chatArrayList, true, true);
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.dialogsNeedReload);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_CHAT_NAME);
|
2014-07-27 00:28:33 +02:00
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
|
2013-12-20 20:25:49 +01:00
|
|
|
public void changeChatAvatar(int chat_id, TLRPC.InputFile uploadedAvatar) {
|
2013-10-25 17:19:00 +02:00
|
|
|
TLRPC.TL_messages_editChatPhoto req2 = new TLRPC.TL_messages_editChatPhoto();
|
|
|
|
req2.chat_id = chat_id;
|
|
|
|
if (uploadedAvatar != null) {
|
|
|
|
req2.photo = new TLRPC.TL_inputChatUploadedPhoto();
|
|
|
|
req2.photo.file = uploadedAvatar;
|
|
|
|
req2.photo.crop = new TLRPC.TL_inputPhotoCropAuto();
|
|
|
|
} else {
|
|
|
|
req2.photo = new TLRPC.TL_inputChatPhotoEmpty();
|
|
|
|
}
|
2014-03-22 23:31:55 +01:00
|
|
|
ConnectionsManager.getInstance().performRpc(req2, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error != null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-22 23:31:55 +01:00
|
|
|
final TLRPC.messages_StatedMessage res = (TLRPC.messages_StatedMessage) response;
|
|
|
|
MessagesStorage.getInstance().putUsersAndChats(res.users, res.chats, true, true);
|
2013-10-25 17:19:00 +02:00
|
|
|
|
2015-02-01 19:51:02 +01:00
|
|
|
final ArrayList<TLRPC.Message> messages = new ArrayList<>();
|
|
|
|
messages.add(res.message);
|
|
|
|
ImageLoader.saveMessagesThumbs(messages);
|
|
|
|
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-08-22 16:24:33 +02:00
|
|
|
putUsers(res.users, false);
|
|
|
|
putChats(res.chats, false);
|
2015-01-02 23:15:07 +01:00
|
|
|
final ArrayList<MessageObject> messagesObj = new ArrayList<>();
|
2015-02-01 19:51:02 +01:00
|
|
|
messagesObj.add(new MessageObject(res.message, users, true));
|
2013-10-25 17:19:00 +02:00
|
|
|
TLRPC.Chat chat = res.chats.get(0);
|
|
|
|
updateInterfaceWithMessages(-chat.id, messagesObj);
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.dialogsNeedReload);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_CHAT_AVATAR);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
});
|
2014-02-04 19:36:55 +01:00
|
|
|
|
2014-08-22 16:24:33 +02:00
|
|
|
MessagesStorage.getInstance().putMessages(messages, true, true, false, 0);
|
2015-03-19 00:09:45 +01:00
|
|
|
|
|
|
|
if (res instanceof TLRPC.TL_messages_statedMessage) {
|
|
|
|
MessagesController.getInstance().processNewDifferenceParams(-1, res.pts, res.message.date, res.pts_count);
|
|
|
|
} else if (res instanceof TLRPC.TL_messages_statedMessageLink) {
|
|
|
|
MessagesController.getInstance().processNewDifferenceParams(res.seq, res.pts, res.message.date, res.pts_count);
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-07-03 00:39:05 +02:00
|
|
|
});
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void unregistedPush() {
|
2014-02-28 23:28:25 +01:00
|
|
|
if (UserConfig.registeredForPush && UserConfig.pushString.length() == 0) {
|
|
|
|
TLRPC.TL_account_unregisterDevice req = new TLRPC.TL_account_unregisterDevice();
|
|
|
|
req.token = UserConfig.pushString;
|
|
|
|
req.token_type = 2;
|
2014-03-22 23:31:55 +01:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2014-02-28 23:28:25 +01:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
2014-02-04 19:36:55 +01:00
|
|
|
|
2014-02-28 23:28:25 +01:00
|
|
|
}
|
2014-07-03 00:39:05 +02:00
|
|
|
});
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-06-13 12:42:21 +02:00
|
|
|
}
|
2014-02-28 23:28:25 +01:00
|
|
|
|
2014-06-13 12:42:21 +02:00
|
|
|
public void logOut() {
|
|
|
|
TLRPC.TL_auth_logOut req = new TLRPC.TL_auth_logOut();
|
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
2014-06-21 23:46:11 +02:00
|
|
|
ConnectionsManager.getInstance().cleanUp();
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-07-03 00:39:05 +02:00
|
|
|
});
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void registerForPush(final String regid) {
|
2014-06-13 12:42:21 +02:00
|
|
|
if (regid == null || regid.length() == 0 || registeringForPush || UserConfig.getClientUserId() == 0) {
|
2013-10-25 17:19:00 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (UserConfig.registeredForPush && regid.equals(UserConfig.pushString)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
registeringForPush = true;
|
|
|
|
TLRPC.TL_account_registerDevice req = new TLRPC.TL_account_registerDevice();
|
2013-12-20 20:25:49 +01:00
|
|
|
req.token_type = 2;
|
|
|
|
req.token = regid;
|
|
|
|
req.app_sandbox = false;
|
2013-10-25 17:19:00 +02:00
|
|
|
try {
|
2014-09-14 01:40:36 +02:00
|
|
|
req.lang_code = LocaleController.getLocaleString(Locale.getDefault());
|
2013-10-25 17:19:00 +02:00
|
|
|
req.device_model = Build.MANUFACTURER + Build.MODEL;
|
|
|
|
if (req.device_model == null) {
|
|
|
|
req.device_model = "Android unknown";
|
|
|
|
}
|
|
|
|
req.system_version = "SDK " + Build.VERSION.SDK_INT;
|
2013-11-04 13:31:01 +01:00
|
|
|
PackageInfo pInfo = ApplicationLoader.applicationContext.getPackageManager().getPackageInfo(ApplicationLoader.applicationContext.getPackageName(), 0);
|
2014-06-22 11:36:52 +02:00
|
|
|
req.app_version = pInfo.versionName + " (" + pInfo.versionCode + ")";
|
2013-10-25 17:19:00 +02:00
|
|
|
if (req.app_version == null) {
|
|
|
|
req.app_version = "App version unknown";
|
|
|
|
}
|
2013-12-20 20:25:49 +01:00
|
|
|
|
2013-10-25 17:19:00 +02:00
|
|
|
} catch (Exception e) {
|
2013-12-20 20:25:49 +01:00
|
|
|
FileLog.e("tmessages", e);
|
|
|
|
req.lang_code = "en";
|
|
|
|
req.device_model = "Android unknown";
|
|
|
|
req.system_version = "SDK " + Build.VERSION.SDK_INT;
|
|
|
|
req.app_version = "App version unknown";
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2013-12-26 17:46:13 +01:00
|
|
|
|
|
|
|
if (req.lang_code == null || req.lang_code.length() == 0) {
|
|
|
|
req.lang_code = "en";
|
|
|
|
}
|
|
|
|
if (req.device_model == null || req.device_model.length() == 0) {
|
|
|
|
req.device_model = "Android unknown";
|
|
|
|
}
|
|
|
|
if (req.app_version == null || req.app_version.length() == 0) {
|
|
|
|
req.app_version = "App version unknown";
|
|
|
|
}
|
|
|
|
if (req.system_version == null || req.system_version.length() == 0) {
|
|
|
|
req.system_version = "SDK Unknown";
|
|
|
|
}
|
|
|
|
|
2013-10-25 17:19:00 +02:00
|
|
|
if (req.app_version != null) {
|
2014-03-22 23:31:55 +01:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error == null) {
|
2013-12-20 20:25:49 +01:00
|
|
|
FileLog.e("tmessages", "registered for push");
|
2013-10-25 17:19:00 +02:00
|
|
|
UserConfig.registeredForPush = true;
|
|
|
|
UserConfig.pushString = regid;
|
2013-11-04 13:31:01 +01:00
|
|
|
UserConfig.saveConfig(false);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
registeringForPush = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-07-03 00:39:05 +02:00
|
|
|
});
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void loadCurrentState() {
|
|
|
|
if (updatingState) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
updatingState = true;
|
|
|
|
TLRPC.TL_updates_getState req = new TLRPC.TL_updates_getState();
|
2014-03-22 23:31:55 +01:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
updatingState = false;
|
|
|
|
if (error == null) {
|
2014-03-22 23:31:55 +01:00
|
|
|
TLRPC.TL_updates_state res = (TLRPC.TL_updates_state) response;
|
2013-11-04 13:31:01 +01:00
|
|
|
MessagesStorage.lastDateValue = res.date;
|
|
|
|
MessagesStorage.lastPtsValue = res.pts;
|
|
|
|
MessagesStorage.lastSeqValue = res.seq;
|
|
|
|
MessagesStorage.lastQtsValue = res.qts;
|
2015-03-19 00:09:45 +01:00
|
|
|
for (int a = 0; a < 3; a++) {
|
|
|
|
processUpdatesQueue(a, 2);
|
|
|
|
}
|
2014-03-22 23:31:55 +01:00
|
|
|
MessagesStorage.getInstance().saveDiffParams(MessagesStorage.lastSeqValue, MessagesStorage.lastPtsValue, MessagesStorage.lastDateValue, MessagesStorage.lastQtsValue);
|
2013-10-25 17:19:00 +02:00
|
|
|
} else {
|
2014-02-04 19:36:55 +01:00
|
|
|
if (error.code != 401) {
|
|
|
|
loadCurrentState();
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-03 00:39:05 +02:00
|
|
|
});
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
|
2014-02-04 19:36:55 +01:00
|
|
|
private int getUpdateSeq(TLRPC.Updates updates) {
|
|
|
|
if (updates instanceof TLRPC.TL_updatesCombined) {
|
|
|
|
return updates.seq_start;
|
|
|
|
} else {
|
|
|
|
return updates.seq;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-19 00:09:45 +01:00
|
|
|
private void setUpdatesStartTime(int type, long time) {
|
|
|
|
if (type == 0) {
|
|
|
|
updatesStartWaitTimeSeq = time;
|
|
|
|
} else if (type == 1) {
|
|
|
|
updatesStartWaitTimePts = time;
|
|
|
|
} else if (type == 2) {
|
|
|
|
updatesStartWaitTimeQts = time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public long getUpdatesStartTime(int type) {
|
|
|
|
if (type == 0) {
|
|
|
|
return updatesStartWaitTimeSeq;
|
|
|
|
} else if (type == 1) {
|
|
|
|
return updatesStartWaitTimePts;
|
|
|
|
} else if (type == 2) {
|
|
|
|
return updatesStartWaitTimeQts;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int isValidUpdate(TLRPC.Updates updates, int type) {
|
|
|
|
if (type == 0) {
|
|
|
|
int seq = getUpdateSeq(updates);
|
|
|
|
if (MessagesStorage.lastSeqValue + 1 == seq || MessagesStorage.lastSeqValue == seq) {
|
|
|
|
return 0;
|
|
|
|
} else if (MessagesStorage.lastSeqValue < seq) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
} else if (type == 1) {
|
|
|
|
if (updates.pts <= MessagesStorage.lastPtsValue) {
|
|
|
|
return 2;
|
|
|
|
} else if (MessagesStorage.lastPtsValue + updates.pts_count == updates.pts) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else if (type == 2) {
|
|
|
|
if (updates.qts <= MessagesStorage.lastQtsValue) {
|
|
|
|
return 2;
|
|
|
|
} else if (MessagesStorage.lastQtsValue + 1 == updates.qts) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean processUpdatesQueue(int type, int state) {
|
|
|
|
ArrayList<TLRPC.Updates> updatesQueue = null;
|
|
|
|
if (type == 0) {
|
|
|
|
updatesQueue = updatesQueueSeq;
|
2014-02-04 19:36:55 +01:00
|
|
|
Collections.sort(updatesQueue, new Comparator<TLRPC.Updates>() {
|
|
|
|
@Override
|
|
|
|
public int compare(TLRPC.Updates updates, TLRPC.Updates updates2) {
|
|
|
|
int seq1 = getUpdateSeq(updates);
|
|
|
|
int seq2 = getUpdateSeq(updates2);
|
|
|
|
if (seq1 == seq2) {
|
|
|
|
return 0;
|
|
|
|
} else if (seq1 > seq2) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
});
|
2015-03-19 00:09:45 +01:00
|
|
|
} else if (type == 1) {
|
|
|
|
updatesQueue = updatesQueuePts;
|
|
|
|
Collections.sort(updatesQueue, new Comparator<TLRPC.Updates>() {
|
|
|
|
@Override
|
|
|
|
public int compare(TLRPC.Updates updates, TLRPC.Updates updates2) {
|
|
|
|
if (updates.pts == updates2.pts) {
|
|
|
|
return 0;
|
|
|
|
} else if (updates.pts > updates2.pts) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else if (type == 2) {
|
|
|
|
updatesQueue = updatesQueueQts;
|
|
|
|
Collections.sort(updatesQueue, new Comparator<TLRPC.Updates>() {
|
|
|
|
@Override
|
|
|
|
public int compare(TLRPC.Updates updates, TLRPC.Updates updates2) {
|
|
|
|
if (updates.qts == updates2.qts) {
|
|
|
|
return 0;
|
|
|
|
} else if (updates.qts > updates2.qts) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (!updatesQueue.isEmpty()) {
|
2014-02-04 19:36:55 +01:00
|
|
|
boolean anyProceed = false;
|
2014-07-23 01:27:00 +02:00
|
|
|
if (state == 2) {
|
|
|
|
TLRPC.Updates updates = updatesQueue.get(0);
|
2015-03-19 00:09:45 +01:00
|
|
|
if (type == 0) {
|
|
|
|
MessagesStorage.lastSeqValue = getUpdateSeq(updates);
|
|
|
|
} else if (type == 1) {
|
|
|
|
MessagesStorage.lastPtsValue = updates.pts;
|
|
|
|
} else if (type == 2) {
|
|
|
|
MessagesStorage.lastQtsValue = updates.qts;
|
|
|
|
}
|
2014-07-23 01:27:00 +02:00
|
|
|
}
|
2014-02-04 19:36:55 +01:00
|
|
|
for (int a = 0; a < updatesQueue.size(); a++) {
|
|
|
|
TLRPC.Updates updates = updatesQueue.get(a);
|
2015-03-19 00:09:45 +01:00
|
|
|
int updateState = isValidUpdate(updates, type);
|
|
|
|
if (updateState == 0) {
|
2014-02-04 19:36:55 +01:00
|
|
|
processUpdates(updates, true);
|
|
|
|
anyProceed = true;
|
|
|
|
updatesQueue.remove(a);
|
|
|
|
a--;
|
2015-03-19 00:09:45 +01:00
|
|
|
} else if (updateState == 1) {
|
|
|
|
if (getUpdatesStartTime(type) != 0 && (anyProceed || getUpdatesStartTime(type) + 1500 > System.currentTimeMillis())) {
|
2014-02-04 19:36:55 +01:00
|
|
|
FileLog.e("tmessages", "HOLE IN UPDATES QUEUE - will wait more time");
|
|
|
|
if (anyProceed) {
|
2015-03-19 00:09:45 +01:00
|
|
|
setUpdatesStartTime(type, System.currentTimeMillis());
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
2015-03-19 00:09:45 +01:00
|
|
|
return false;
|
2014-02-04 19:36:55 +01:00
|
|
|
} else {
|
|
|
|
FileLog.e("tmessages", "HOLE IN UPDATES QUEUE - getDifference");
|
2015-03-19 00:09:45 +01:00
|
|
|
setUpdatesStartTime(type, 0);
|
2014-02-04 19:36:55 +01:00
|
|
|
updatesQueue.clear();
|
|
|
|
getDifference();
|
2015-03-19 00:09:45 +01:00
|
|
|
return false;
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
updatesQueue.remove(a);
|
|
|
|
a--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updatesQueue.clear();
|
|
|
|
FileLog.e("tmessages", "UPDATES QUEUE PROCEED - OK");
|
|
|
|
}
|
2015-03-19 00:09:45 +01:00
|
|
|
setUpdatesStartTime(type, 0);
|
|
|
|
return true;
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
|
|
|
|
2013-10-25 17:19:00 +02:00
|
|
|
public void getDifference() {
|
|
|
|
registerForPush(UserConfig.pushString);
|
2015-03-26 18:34:47 +01:00
|
|
|
if (MessagesStorage.lastPtsValue == 0) {
|
2013-10-25 17:19:00 +02:00
|
|
|
loadCurrentState();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (gettingDifference) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!firstGettingTask) {
|
|
|
|
getNewDeleteTask(null);
|
|
|
|
firstGettingTask = true;
|
|
|
|
}
|
|
|
|
gettingDifference = true;
|
|
|
|
TLRPC.TL_updates_getDifference req = new TLRPC.TL_updates_getDifference();
|
2013-11-04 13:31:01 +01:00
|
|
|
req.pts = MessagesStorage.lastPtsValue;
|
|
|
|
req.date = MessagesStorage.lastDateValue;
|
|
|
|
req.qts = MessagesStorage.lastQtsValue;
|
2014-02-04 19:36:55 +01:00
|
|
|
FileLog.e("tmessages", "start getDifference with date = " + MessagesStorage.lastDateValue + " pts = " + MessagesStorage.lastPtsValue + " seq = " + MessagesStorage.lastSeqValue);
|
2014-07-03 00:39:05 +02:00
|
|
|
if (ConnectionsManager.getInstance().getConnectionState() == 0) {
|
|
|
|
ConnectionsManager.getInstance().setConnectionState(3);
|
|
|
|
final int stateCopy = ConnectionsManager.getInstance().getConnectionState();
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.didUpdatedConnectionState, stateCopy);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-03-22 23:31:55 +01:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
2013-12-20 20:25:49 +01:00
|
|
|
gettingDifferenceAgain = false;
|
2013-10-25 17:19:00 +02:00
|
|
|
if (error == null) {
|
2014-03-22 23:31:55 +01:00
|
|
|
final TLRPC.updates_Difference res = (TLRPC.updates_Difference) response;
|
2013-12-20 20:25:49 +01:00
|
|
|
gettingDifferenceAgain = res instanceof TLRPC.TL_updates_differenceSlice;
|
2013-10-25 17:19:00 +02:00
|
|
|
|
2015-01-02 23:15:07 +01:00
|
|
|
final HashMap<Integer, TLRPC.User> usersDict = new HashMap<>();
|
2013-10-25 17:19:00 +02:00
|
|
|
for (TLRPC.User user : res.users) {
|
|
|
|
usersDict.put(user.id, user);
|
|
|
|
}
|
|
|
|
|
2015-01-02 23:15:07 +01:00
|
|
|
final ArrayList<TLRPC.TL_updateMessageID> msgUpdates = new ArrayList<>();
|
2013-10-25 17:19:00 +02:00
|
|
|
if (!res.other_updates.isEmpty()) {
|
|
|
|
for (int a = 0; a < res.other_updates.size(); a++) {
|
|
|
|
TLRPC.Update upd = res.other_updates.get(a);
|
|
|
|
if (upd instanceof TLRPC.TL_updateMessageID) {
|
2014-03-22 23:31:55 +01:00
|
|
|
msgUpdates.add((TLRPC.TL_updateMessageID) upd);
|
2013-10-25 17:19:00 +02:00
|
|
|
res.other_updates.remove(a);
|
|
|
|
a--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-02-11 15:32:09 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-08-22 16:24:33 +02:00
|
|
|
putUsers(res.users, false);
|
|
|
|
putChats(res.chats, false);
|
2014-02-11 15:32:09 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-10-31 20:02:29 +01:00
|
|
|
MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (!msgUpdates.isEmpty()) {
|
2015-01-02 23:15:07 +01:00
|
|
|
final HashMap<Integer, Integer> corrected = new HashMap<>();
|
2013-10-25 17:19:00 +02:00
|
|
|
for (TLRPC.TL_updateMessageID update : msgUpdates) {
|
2014-03-22 23:31:55 +01:00
|
|
|
Integer oldId = MessagesStorage.getInstance().updateMessageStateAndId(update.random_id, null, update.id, 0, false);
|
2013-10-25 17:19:00 +02:00
|
|
|
if (oldId != null) {
|
|
|
|
corrected.put(oldId, update.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!corrected.isEmpty()) {
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (HashMap.Entry<Integer, Integer> entry : corrected.entrySet()) {
|
|
|
|
Integer oldId = entry.getKey();
|
2014-09-28 15:37:26 +02:00
|
|
|
SendMessagesHelper.getInstance().processSentMessage(oldId);
|
2013-10-25 17:19:00 +02:00
|
|
|
Integer newId = entry.getValue();
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.messageReceivedByServer, oldId, newId, null);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Utilities.stageQueue.postRunnable(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (!res.new_messages.isEmpty() || !res.new_encrypted_messages.isEmpty()) {
|
2015-01-02 23:15:07 +01:00
|
|
|
final HashMap<Long, ArrayList<MessageObject>> messages = new HashMap<>();
|
2013-10-25 17:19:00 +02:00
|
|
|
for (TLRPC.EncryptedMessage encryptedMessage : res.new_encrypted_messages) {
|
2014-12-01 18:56:31 +01:00
|
|
|
ArrayList<TLRPC.Message> decryptedMessages = SecretChatHelper.getInstance().decryptMessage(encryptedMessage);
|
2014-10-28 22:27:44 +01:00
|
|
|
if (decryptedMessages != null && !decryptedMessages.isEmpty()) {
|
|
|
|
for (TLRPC.Message message : decryptedMessages) {
|
|
|
|
res.new_messages.add(message);
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-01 19:51:02 +01:00
|
|
|
ImageLoader.saveMessagesThumbs(res.new_messages);
|
|
|
|
|
2015-01-02 23:15:07 +01:00
|
|
|
final ArrayList<MessageObject> pushMessages = new ArrayList<>();
|
2013-10-25 17:19:00 +02:00
|
|
|
for (TLRPC.Message message : res.new_messages) {
|
2015-02-01 19:51:02 +01:00
|
|
|
MessageObject obj = new MessageObject(message, usersDict, true);
|
2013-12-20 20:25:49 +01:00
|
|
|
|
|
|
|
long dialog_id = obj.messageOwner.dialog_id;
|
|
|
|
if (dialog_id == 0) {
|
|
|
|
if (obj.messageOwner.to_id.chat_id != 0) {
|
|
|
|
dialog_id = -obj.messageOwner.to_id.chat_id;
|
|
|
|
} else {
|
|
|
|
dialog_id = obj.messageOwner.to_id.user_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 18:34:47 +01:00
|
|
|
if (!obj.isOut() && obj.isUnread()) {
|
2014-07-10 02:15:58 +02:00
|
|
|
pushMessages.add(obj);
|
2013-12-20 20:25:49 +01:00
|
|
|
}
|
2014-07-10 02:15:58 +02:00
|
|
|
|
2013-10-25 17:19:00 +02:00
|
|
|
long uid;
|
|
|
|
if (message.dialog_id != 0) {
|
|
|
|
uid = message.dialog_id;
|
|
|
|
} else {
|
|
|
|
if (message.to_id.chat_id != 0) {
|
|
|
|
uid = -message.to_id.chat_id;
|
|
|
|
} else {
|
2014-06-13 12:42:21 +02:00
|
|
|
if (message.to_id.user_id == UserConfig.getClientUserId()) {
|
2013-10-25 17:19:00 +02:00
|
|
|
message.to_id.user_id = message.from_id;
|
|
|
|
}
|
|
|
|
uid = message.to_id.user_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ArrayList<MessageObject> arr = messages.get(uid);
|
|
|
|
if (arr == null) {
|
2015-01-02 23:15:07 +01:00
|
|
|
arr = new ArrayList<>();
|
2013-10-25 17:19:00 +02:00
|
|
|
messages.put(uid, arr);
|
|
|
|
}
|
|
|
|
arr.add(obj);
|
|
|
|
}
|
|
|
|
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (HashMap.Entry<Long, ArrayList<MessageObject>> pair : messages.entrySet()) {
|
|
|
|
Long key = pair.getKey();
|
|
|
|
ArrayList<MessageObject> value = pair.getValue();
|
|
|
|
updateInterfaceWithMessages(key, value);
|
|
|
|
}
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.dialogsNeedReload);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
});
|
2014-10-31 20:02:29 +01:00
|
|
|
MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
|
2014-02-04 19:36:55 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-07-30 09:49:39 +02:00
|
|
|
if (!pushMessages.isEmpty()) {
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-07-30 09:49:39 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
NotificationsController.getInstance().processNewMessages(pushMessages, !(res instanceof TLRPC.TL_updates_differenceSlice));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-03-22 23:31:55 +01:00
|
|
|
MessagesStorage.getInstance().startTransaction(false);
|
2014-08-22 16:24:33 +02:00
|
|
|
MessagesStorage.getInstance().putMessages(res.new_messages, false, false, false, MediaController.getInstance().getAutodownloadMask());
|
2014-03-22 23:31:55 +01:00
|
|
|
MessagesStorage.getInstance().putUsersAndChats(res.users, res.chats, false, false);
|
|
|
|
MessagesStorage.getInstance().commitTransaction(false);
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
|
|
|
});
|
2015-01-02 23:15:07 +01:00
|
|
|
|
|
|
|
SecretChatHelper.getInstance().processPendingEncMessages();
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (res != null && !res.other_updates.isEmpty()) {
|
2013-12-20 20:25:49 +01:00
|
|
|
processUpdateArray(res.other_updates, res.users, res.chats);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
|
2014-02-04 19:36:55 +01:00
|
|
|
gettingDifference = false;
|
2013-10-25 17:19:00 +02:00
|
|
|
if (res instanceof TLRPC.TL_updates_difference) {
|
2013-11-04 13:31:01 +01:00
|
|
|
MessagesStorage.lastSeqValue = res.state.seq;
|
|
|
|
MessagesStorage.lastDateValue = res.state.date;
|
|
|
|
MessagesStorage.lastPtsValue = res.state.pts;
|
|
|
|
MessagesStorage.lastQtsValue = res.state.qts;
|
2014-07-03 00:39:05 +02:00
|
|
|
ConnectionsManager.getInstance().setConnectionState(0);
|
2015-03-19 00:09:45 +01:00
|
|
|
boolean done = true;
|
|
|
|
for (int a = 0; a < 3; a++) {
|
|
|
|
if (!processUpdatesQueue(a, 1)) {
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (done) {
|
|
|
|
final int stateCopy = ConnectionsManager.getInstance().getConnectionState();
|
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.didUpdatedConnectionState, stateCopy);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
} else if (res instanceof TLRPC.TL_updates_differenceSlice) {
|
2013-11-04 13:31:01 +01:00
|
|
|
MessagesStorage.lastDateValue = res.intermediate_state.date;
|
|
|
|
MessagesStorage.lastPtsValue = res.intermediate_state.pts;
|
|
|
|
MessagesStorage.lastQtsValue = res.intermediate_state.qts;
|
2013-12-20 20:25:49 +01:00
|
|
|
gettingDifferenceAgain = true;
|
2013-10-25 17:19:00 +02:00
|
|
|
getDifference();
|
|
|
|
} else if (res instanceof TLRPC.TL_updates_differenceEmpty) {
|
2013-11-04 13:31:01 +01:00
|
|
|
MessagesStorage.lastSeqValue = res.seq;
|
|
|
|
MessagesStorage.lastDateValue = res.date;
|
2014-07-03 00:39:05 +02:00
|
|
|
ConnectionsManager.getInstance().setConnectionState(0);
|
2015-03-19 00:09:45 +01:00
|
|
|
boolean done = true;
|
|
|
|
for (int a = 0; a < 3; a++) {
|
|
|
|
if (!processUpdatesQueue(a, 1)) {
|
|
|
|
done = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (done) {
|
|
|
|
final int stateCopy = ConnectionsManager.getInstance().getConnectionState();
|
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.didUpdatedConnectionState, stateCopy);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-03-22 23:31:55 +01:00
|
|
|
MessagesStorage.getInstance().saveDiffParams(MessagesStorage.lastSeqValue, MessagesStorage.lastPtsValue, MessagesStorage.lastDateValue, MessagesStorage.lastQtsValue);
|
2014-02-04 19:36:55 +01:00
|
|
|
FileLog.e("tmessages", "received difference with date = " + MessagesStorage.lastDateValue + " pts = " + MessagesStorage.lastPtsValue + " seq = " + MessagesStorage.lastSeqValue);
|
|
|
|
FileLog.e("tmessages", "messages = " + res.new_messages.size() + " users = " + res.users.size() + " chats = " + res.chats.size() + " other updates = " + res.other_updates.size());
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2014-02-04 19:36:55 +01:00
|
|
|
gettingDifference = false;
|
2014-07-03 00:39:05 +02:00
|
|
|
ConnectionsManager.getInstance().setConnectionState(0);
|
|
|
|
final int stateCopy = ConnectionsManager.getInstance().getConnectionState();
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-06-07 01:35:21 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.didUpdatedConnectionState, stateCopy);
|
2014-06-07 01:35:21 +02:00
|
|
|
}
|
|
|
|
});
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-03 00:39:05 +02:00
|
|
|
});
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
|
2014-02-04 19:36:55 +01:00
|
|
|
public void processUpdates(final TLRPC.Updates updates, boolean fromQueue) {
|
2013-10-25 17:19:00 +02:00
|
|
|
boolean needGetDiff = false;
|
|
|
|
boolean needReceivedQueue = false;
|
2014-11-21 01:14:44 +01:00
|
|
|
boolean updateStatus = false;
|
2013-10-25 17:19:00 +02:00
|
|
|
if (updates instanceof TLRPC.TL_updateShort) {
|
2015-01-02 23:15:07 +01:00
|
|
|
ArrayList<TLRPC.Update> arr = new ArrayList<>();
|
2013-10-25 17:19:00 +02:00
|
|
|
arr.add(updates.update);
|
|
|
|
processUpdateArray(arr, null, null);
|
2015-03-19 00:09:45 +01:00
|
|
|
} else if (updates instanceof TLRPC.TL_updateShortChatMessage || updates instanceof TLRPC.TL_updateShortMessage) {
|
|
|
|
TLRPC.User user = getUser(updates.user_id);
|
|
|
|
TLRPC.User user2 = null;
|
|
|
|
|
|
|
|
boolean needFwdUser = false;
|
|
|
|
if (updates.fwd_from_id != 0) {
|
|
|
|
user2 = getUser(updates.fwd_from_id);
|
|
|
|
needFwdUser = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean missingData = false;
|
|
|
|
if (updates instanceof TLRPC.TL_updateShortMessage) {
|
|
|
|
missingData = user == null || needFwdUser && user2 == null;
|
|
|
|
} else {
|
|
|
|
missingData = getChat(updates.chat_id) == null || user == null || needFwdUser && user2 == null;
|
|
|
|
}
|
2014-11-21 01:14:44 +01:00
|
|
|
if (user != null && user.status != null && user.status.expires <= 0) {
|
|
|
|
onlinePrivacy.put(user.id, ConnectionsManager.getInstance().getCurrentTime());
|
|
|
|
updateStatus = true;
|
|
|
|
}
|
2015-03-19 00:09:45 +01:00
|
|
|
|
2014-04-03 23:18:54 +02:00
|
|
|
if (missingData) {
|
|
|
|
needGetDiff = true;
|
|
|
|
} else {
|
2015-03-19 00:09:45 +01:00
|
|
|
if (MessagesStorage.lastPtsValue + updates.pts_count == updates.pts) {
|
2014-04-03 23:18:54 +02:00
|
|
|
TLRPC.TL_message message = new TLRPC.TL_message();
|
|
|
|
message.id = updates.id;
|
2015-03-19 00:09:45 +01:00
|
|
|
if (updates instanceof TLRPC.TL_updateShortMessage) {
|
|
|
|
if ((updates.flags & TLRPC.MESSAGE_FLAG_OUT) != 0) {
|
|
|
|
message.from_id = UserConfig.getClientUserId();
|
|
|
|
} else {
|
|
|
|
message.from_id = updates.user_id;
|
|
|
|
}
|
|
|
|
message.to_id = new TLRPC.TL_peerUser();
|
|
|
|
message.to_id.user_id = updates.user_id;
|
|
|
|
message.dialog_id = updates.user_id;
|
|
|
|
} else {
|
|
|
|
message.from_id = updates.user_id;
|
|
|
|
message.to_id = new TLRPC.TL_peerChat();
|
|
|
|
message.to_id.chat_id = updates.chat_id;
|
|
|
|
message.dialog_id = -updates.chat_id;
|
|
|
|
}
|
2014-04-03 23:18:54 +02:00
|
|
|
message.message = updates.message;
|
|
|
|
message.date = updates.date;
|
2015-03-19 00:09:45 +01:00
|
|
|
message.flags = updates.flags;
|
|
|
|
message.fwd_from_id = updates.fwd_from_id;
|
|
|
|
message.fwd_date = updates.fwd_date;
|
|
|
|
message.reply_to_msg_id = updates.reply_to_msg_id;
|
2014-04-03 23:18:54 +02:00
|
|
|
message.media = new TLRPC.TL_messageMediaEmpty();
|
|
|
|
MessagesStorage.lastPtsValue = updates.pts;
|
2015-02-01 19:51:02 +01:00
|
|
|
final MessageObject obj = new MessageObject(message, null, true);
|
2015-01-02 23:15:07 +01:00
|
|
|
final ArrayList<MessageObject> objArr = new ArrayList<>();
|
2014-04-03 23:18:54 +02:00
|
|
|
objArr.add(obj);
|
2015-01-02 23:15:07 +01:00
|
|
|
ArrayList<TLRPC.Message> arr = new ArrayList<>();
|
2014-04-03 23:18:54 +02:00
|
|
|
arr.add(message);
|
2015-03-19 00:09:45 +01:00
|
|
|
if (updates instanceof TLRPC.TL_updateShortMessage) {
|
|
|
|
final boolean printUpdate = (updates.flags & TLRPC.MESSAGE_FLAG_OUT) == 0 && updatePrintingUsersWithNewMessages(updates.user_id, objArr);
|
|
|
|
if (printUpdate) {
|
|
|
|
updatePrintingStrings();
|
|
|
|
}
|
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (printUpdate) {
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_USER_PRINT);
|
|
|
|
}
|
|
|
|
updateInterfaceWithMessages(updates.user_id, objArr);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.dialogsNeedReload);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2015-03-19 00:09:45 +01:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
final boolean printUpdate = updatePrintingUsersWithNewMessages(-updates.chat_id, objArr);
|
|
|
|
if (printUpdate) {
|
|
|
|
updatePrintingStrings();
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2015-03-19 00:09:45 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (printUpdate) {
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_USER_PRINT);
|
|
|
|
}
|
|
|
|
|
|
|
|
updateInterfaceWithMessages(-updates.chat_id, objArr);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.dialogsNeedReload);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-31 20:02:29 +01:00
|
|
|
MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
|
2014-07-30 09:49:39 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-07-30 09:49:39 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2015-03-26 18:34:47 +01:00
|
|
|
if (!obj.isOut()) {
|
2014-07-30 09:49:39 +02:00
|
|
|
NotificationsController.getInstance().processNewMessages(objArr, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2014-08-22 16:24:33 +02:00
|
|
|
MessagesStorage.getInstance().putMessages(arr, false, true, false, 0);
|
2015-03-19 00:09:45 +01:00
|
|
|
} else if (MessagesStorage.lastPtsValue != updates.pts) {
|
|
|
|
FileLog.e("tmessages", "need get diff short message, pts: " + MessagesStorage.lastPtsValue + " " + updates.pts + " count = " + updates.pts_count);
|
|
|
|
if (gettingDifference || updatesStartWaitTimePts == 0 || updatesStartWaitTimePts != 0 && updatesStartWaitTimePts + 1500 > System.currentTimeMillis()) {
|
|
|
|
if (updatesStartWaitTimePts == 0) {
|
|
|
|
updatesStartWaitTimePts = System.currentTimeMillis();
|
2014-04-03 23:18:54 +02:00
|
|
|
}
|
2015-03-19 00:09:45 +01:00
|
|
|
FileLog.e("tmessages", "add short message to queue");
|
|
|
|
updatesQueuePts.add(updates);
|
2014-04-03 23:18:54 +02:00
|
|
|
} else {
|
|
|
|
needGetDiff = true;
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2015-03-19 00:09:45 +01:00
|
|
|
} else if (updates instanceof TLRPC.TL_updatesCombined || updates instanceof TLRPC.TL_updates) {
|
|
|
|
MessagesStorage.getInstance().putUsersAndChats(updates.users, updates.chats, true, true);
|
|
|
|
int lastQtsValue = MessagesStorage.lastQtsValue;
|
|
|
|
for (int a = 0; a < updates.updates.size(); a++) {
|
|
|
|
TLRPC.Update update = updates.updates.get(a);
|
|
|
|
if (update instanceof TLRPC.TL_updateNewMessage || update instanceof TLRPC.TL_updateReadMessages || update instanceof TLRPC.TL_updateReadHistoryInbox ||
|
|
|
|
update instanceof TLRPC.TL_updateReadHistoryOutbox || update instanceof TLRPC.TL_updateDeleteMessages) {
|
|
|
|
TLRPC.TL_updates updatesNew = new TLRPC.TL_updates();
|
|
|
|
updatesNew.updates.add(update);
|
|
|
|
updatesNew.pts = update.pts;
|
|
|
|
updatesNew.pts_count = update.pts_count;
|
|
|
|
if (MessagesStorage.lastPtsValue + update.pts_count == update.pts) {
|
|
|
|
if (!processUpdateArray(updatesNew.updates, updates.users, updates.chats)) {
|
|
|
|
FileLog.e("tmessages", "need get diff inner TL_updates, seq: " + MessagesStorage.lastSeqValue + " " + updates.seq);
|
|
|
|
needGetDiff = true;
|
|
|
|
} else {
|
|
|
|
MessagesStorage.lastPtsValue = update.pts;
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2015-03-19 00:09:45 +01:00
|
|
|
} else if (MessagesStorage.lastPtsValue != update.pts) {
|
|
|
|
FileLog.e("tmessages", update + " need get diff, pts: " + MessagesStorage.lastPtsValue + " " + update.pts + " count = " + update.pts_count);
|
|
|
|
if (gettingDifference || updatesStartWaitTimePts == 0 || updatesStartWaitTimePts != 0 && updatesStartWaitTimePts + 1500 > System.currentTimeMillis()) {
|
|
|
|
if (updatesStartWaitTimePts == 0) {
|
|
|
|
updatesStartWaitTimePts = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
FileLog.e("tmessages", "add short message to queue");
|
|
|
|
updatesQueuePts.add(updatesNew);
|
|
|
|
} else {
|
|
|
|
needGetDiff = true;
|
2014-07-30 09:49:39 +02:00
|
|
|
}
|
2015-03-19 00:09:45 +01:00
|
|
|
}
|
|
|
|
} else if (update instanceof TLRPC.TL_updateNewEncryptedMessage) {
|
|
|
|
TLRPC.TL_updates updatesNew = new TLRPC.TL_updates();
|
|
|
|
updatesNew.updates.add(update);
|
|
|
|
updatesNew.qts = update.qts;
|
|
|
|
if (MessagesStorage.lastQtsValue == 0 || MessagesStorage.lastQtsValue + 1 == update.qts) {
|
|
|
|
processUpdateArray(updatesNew.updates, updates.users, updates.chats);
|
|
|
|
MessagesStorage.lastQtsValue = update.qts;
|
|
|
|
needReceivedQueue = true;
|
|
|
|
} else if (MessagesStorage.lastPtsValue != update.qts) {
|
|
|
|
FileLog.e("tmessages", update + " need get diff, qts: " + MessagesStorage.lastQtsValue + " " + update.qts);
|
|
|
|
if (gettingDifference || updatesStartWaitTimeQts == 0 || updatesStartWaitTimeQts != 0 && updatesStartWaitTimeQts + 1500 > System.currentTimeMillis()) {
|
|
|
|
if (updatesStartWaitTimeQts == 0) {
|
|
|
|
updatesStartWaitTimeQts = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
FileLog.e("tmessages", "add short message to queue");
|
|
|
|
updatesQueueQts.add(updatesNew);
|
|
|
|
} else {
|
|
|
|
needGetDiff = true;
|
2014-04-03 23:18:54 +02:00
|
|
|
}
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
2015-03-19 00:09:45 +01:00
|
|
|
} else {
|
|
|
|
continue;
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
2015-03-19 00:09:45 +01:00
|
|
|
updates.updates.remove(a);
|
|
|
|
a--;
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2015-03-19 00:09:45 +01:00
|
|
|
|
|
|
|
boolean processUpdate = false;
|
|
|
|
if (updates instanceof TLRPC.TL_updatesCombined) {
|
|
|
|
processUpdate = MessagesStorage.lastSeqValue + 1 == updates.seq_start || MessagesStorage.lastSeqValue == updates.seq_start;
|
|
|
|
} else {
|
|
|
|
processUpdate = MessagesStorage.lastSeqValue + 1 == updates.seq || updates.seq == 0 || updates.seq == MessagesStorage.lastSeqValue;
|
|
|
|
}
|
|
|
|
if (processUpdate) {
|
|
|
|
processUpdateArray(updates.updates, updates.users, updates.chats);
|
|
|
|
MessagesStorage.lastDateValue = updates.date;
|
|
|
|
if (updates.seq != 0) {
|
2013-11-04 13:31:01 +01:00
|
|
|
MessagesStorage.lastSeqValue = updates.seq;
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
} else {
|
2015-03-19 00:09:45 +01:00
|
|
|
if (updates instanceof TLRPC.TL_updatesCombined) {
|
|
|
|
FileLog.e("tmessages", "need get diff TL_updatesCombined, seq: " + MessagesStorage.lastSeqValue + " " + updates.seq_start);
|
2014-02-04 19:36:55 +01:00
|
|
|
} else {
|
2015-03-19 00:09:45 +01:00
|
|
|
FileLog.e("tmessages", "need get diff TL_updates, seq: " + MessagesStorage.lastSeqValue + " " + updates.seq);
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
2015-03-19 00:09:45 +01:00
|
|
|
|
|
|
|
if (gettingDifference || updatesStartWaitTimeSeq == 0 || updatesStartWaitTimeSeq != 0 && updatesStartWaitTimeSeq + 1500 > System.currentTimeMillis()) {
|
|
|
|
if (updatesStartWaitTimeSeq == 0) {
|
|
|
|
updatesStartWaitTimeSeq = System.currentTimeMillis();
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2015-03-19 00:09:45 +01:00
|
|
|
FileLog.e("tmessages", "add TL_updates/Combined to queue");
|
|
|
|
updatesQueueSeq.add(updates);
|
2014-02-04 19:36:55 +01:00
|
|
|
} else {
|
|
|
|
needGetDiff = true;
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
} else if (updates instanceof TLRPC.TL_updatesTooLong) {
|
2013-12-20 20:25:49 +01:00
|
|
|
FileLog.e("tmessages", "need get diff TL_updatesTooLong");
|
2013-10-25 17:19:00 +02:00
|
|
|
needGetDiff = true;
|
2015-03-19 00:09:45 +01:00
|
|
|
} else if (updates instanceof UserActionUpdatesSeq) {
|
2014-02-04 19:36:55 +01:00
|
|
|
MessagesStorage.lastSeqValue = updates.seq;
|
2015-03-19 00:09:45 +01:00
|
|
|
} else if (updates instanceof UserActionUpdatesPts) {
|
|
|
|
MessagesStorage.lastPtsValue = updates.pts;
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2015-01-02 23:15:07 +01:00
|
|
|
SecretChatHelper.getInstance().processPendingEncMessages();
|
2015-03-19 00:09:45 +01:00
|
|
|
if (!fromQueue) {
|
|
|
|
if (needGetDiff) {
|
|
|
|
getDifference();
|
|
|
|
} else {
|
|
|
|
for (int a = 0; a < 3; a++) {
|
|
|
|
ArrayList<TLRPC.Updates> updatesQueue = null;
|
|
|
|
if (a == 0) {
|
|
|
|
updatesQueue = updatesQueueSeq;
|
|
|
|
} else if (a == 1) {
|
|
|
|
updatesQueue = updatesQueuePts;
|
|
|
|
} else if (a == 2) {
|
|
|
|
updatesQueue = updatesQueueQts;
|
|
|
|
}
|
|
|
|
if (!updatesQueue.isEmpty()) {
|
|
|
|
processUpdatesQueue(a, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
if (needReceivedQueue) {
|
|
|
|
TLRPC.TL_messages_receivedQueue req = new TLRPC.TL_messages_receivedQueue();
|
2013-11-04 13:31:01 +01:00
|
|
|
req.max_qts = MessagesStorage.lastQtsValue;
|
2014-03-22 23:31:55 +01:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 17:19:00 +02:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
|
|
|
|
}
|
2014-07-03 00:39:05 +02:00
|
|
|
});
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-11-21 01:14:44 +01:00
|
|
|
if (updateStatus) {
|
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, UPDATE_MASK_STATUS);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-03-22 23:31:55 +01:00
|
|
|
MessagesStorage.getInstance().saveDiffParams(MessagesStorage.lastSeqValue, MessagesStorage.lastPtsValue, MessagesStorage.lastDateValue, MessagesStorage.lastQtsValue);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean processUpdateArray(ArrayList<TLRPC.Update> updates, final ArrayList<TLRPC.User> usersArr, final ArrayList<TLRPC.Chat> chatsArr) {
|
|
|
|
if (updates.isEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
long currentTime = System.currentTimeMillis();
|
|
|
|
|
2015-01-02 23:15:07 +01:00
|
|
|
final HashMap<Long, ArrayList<MessageObject>> messages = new HashMap<>();
|
|
|
|
final ArrayList<MessageObject> pushMessages = new ArrayList<>();
|
|
|
|
final ArrayList<TLRPC.Message> messagesArr = new ArrayList<>();
|
2015-03-19 00:09:45 +01:00
|
|
|
final HashMap<Integer, Integer> markAsReadMessagesInbox = new HashMap<>();
|
|
|
|
final HashMap<Integer, Integer> markAsReadMessagesOutbox = new HashMap<>();
|
2015-01-02 23:15:07 +01:00
|
|
|
final HashMap<Integer, Integer> markAsReadEncrypted = new HashMap<>();
|
|
|
|
final ArrayList<Integer> deletedMessages = new ArrayList<>();
|
2014-05-20 22:22:44 +02:00
|
|
|
boolean printChanged = false;
|
2015-01-02 23:15:07 +01:00
|
|
|
final ArrayList<TLRPC.ChatParticipants> chatInfoToUpdate = new ArrayList<>();
|
|
|
|
final ArrayList<TLRPC.Update> updatesOnMainThread = new ArrayList<>();
|
|
|
|
final ArrayList<TLRPC.TL_updateEncryptedMessagesRead> tasks = new ArrayList<>();
|
|
|
|
final ArrayList<Integer> contactsIds = new ArrayList<>();
|
2013-10-25 17:19:00 +02:00
|
|
|
|
|
|
|
boolean checkForUsers = true;
|
|
|
|
ConcurrentHashMap<Integer, TLRPC.User> usersDict;
|
|
|
|
ConcurrentHashMap<Integer, TLRPC.Chat> chatsDict;
|
|
|
|
if (usersArr != null) {
|
2015-01-02 23:15:07 +01:00
|
|
|
usersDict = new ConcurrentHashMap<>();
|
2013-10-25 17:19:00 +02:00
|
|
|
for (TLRPC.User user : usersArr) {
|
|
|
|
usersDict.put(user.id, user);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
checkForUsers = false;
|
|
|
|
usersDict = users;
|
|
|
|
}
|
|
|
|
if (chatsArr != null) {
|
2015-01-02 23:15:07 +01:00
|
|
|
chatsDict = new ConcurrentHashMap<>();
|
2013-10-25 17:19:00 +02:00
|
|
|
for (TLRPC.Chat chat : chatsArr) {
|
|
|
|
chatsDict.put(chat.id, chat);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
checkForUsers = false;
|
|
|
|
chatsDict = chats;
|
|
|
|
}
|
|
|
|
|
2014-02-11 15:32:09 +01:00
|
|
|
if (usersArr != null || chatsArr != null) {
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-02-11 15:32:09 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-08-22 16:24:33 +02:00
|
|
|
putUsers(usersArr, false);
|
|
|
|
putChats(chatsArr, false);
|
2014-02-11 15:32:09 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-02-04 19:36:55 +01:00
|
|
|
int interfaceUpdateMask = 0;
|
2013-10-25 17:19:00 +02:00
|
|
|
|
|
|
|
for (TLRPC.Update update : updates) {
|
|
|
|
if (update instanceof TLRPC.TL_updateNewMessage) {
|
|
|
|
TLRPC.TL_updateNewMessage upd = (TLRPC.TL_updateNewMessage)update;
|
|
|
|
if (checkForUsers) {
|
2014-11-21 01:14:44 +01:00
|
|
|
TLRPC.User user = getUser(upd.message.from_id);
|
|
|
|
if (usersDict.get(upd.message.from_id) == null && user == null || upd.message.to_id.chat_id != 0 && chatsDict.get(upd.message.to_id.chat_id) == null && getChat(upd.message.to_id.chat_id) == null) {
|
2013-10-25 17:19:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-11-21 01:14:44 +01:00
|
|
|
|
|
|
|
if (user != null && user.status != null && user.status.expires <= 0) {
|
|
|
|
onlinePrivacy.put(upd.message.from_id, ConnectionsManager.getInstance().getCurrentTime());
|
|
|
|
interfaceUpdateMask |= UPDATE_MASK_STATUS;
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
messagesArr.add(upd.message);
|
2015-02-01 19:51:02 +01:00
|
|
|
ImageLoader.saveMessageThumbs(upd.message);
|
|
|
|
MessageObject obj = new MessageObject(upd.message, usersDict, true);
|
2013-10-25 17:19:00 +02:00
|
|
|
if (obj.type == 11) {
|
2014-02-04 19:36:55 +01:00
|
|
|
interfaceUpdateMask |= UPDATE_MASK_CHAT_AVATAR;
|
|
|
|
} else if (obj.type == 10) {
|
|
|
|
interfaceUpdateMask |= UPDATE_MASK_CHAT_NAME;
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
long uid;
|
|
|
|
if (upd.message.to_id.chat_id != 0) {
|
|
|
|
uid = -upd.message.to_id.chat_id;
|
|
|
|
} else {
|
2014-06-13 12:42:21 +02:00
|
|
|
if (upd.message.to_id.user_id == UserConfig.getClientUserId()) {
|
2013-10-25 17:19:00 +02:00
|
|
|
upd.message.to_id.user_id = upd.message.from_id;
|
|
|
|
}
|
|
|
|
uid = upd.message.to_id.user_id;
|
|
|
|
}
|
|
|
|
ArrayList<MessageObject> arr = messages.get(uid);
|
|
|
|
if (arr == null) {
|
2015-01-02 23:15:07 +01:00
|
|
|
arr = new ArrayList<>();
|
2013-10-25 17:19:00 +02:00
|
|
|
messages.put(uid, arr);
|
|
|
|
}
|
|
|
|
arr.add(obj);
|
2015-03-26 18:34:47 +01:00
|
|
|
if (!obj.isOut() && obj.isUnread()) {
|
2014-07-10 02:15:58 +02:00
|
|
|
pushMessages.add(obj);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
} else if (update instanceof TLRPC.TL_updateReadMessages) {
|
2015-03-19 00:09:45 +01:00
|
|
|
//markAsReadMessages.addAll(update.messages); disabled for now
|
|
|
|
} else if (update instanceof TLRPC.TL_updateReadHistoryInbox) {
|
|
|
|
TLRPC.Peer peer = ((TLRPC.TL_updateReadHistoryInbox) update).peer;
|
|
|
|
if (peer.chat_id != 0) {
|
|
|
|
markAsReadMessagesInbox.put(-peer.chat_id, update.max_id);
|
|
|
|
} else {
|
|
|
|
markAsReadMessagesInbox.put(peer.user_id, update.max_id);
|
|
|
|
}
|
|
|
|
} else if (update instanceof TLRPC.TL_updateReadHistoryOutbox) {
|
|
|
|
TLRPC.Peer peer = ((TLRPC.TL_updateReadHistoryOutbox) update).peer;
|
|
|
|
if (peer.chat_id != 0) {
|
|
|
|
markAsReadMessagesOutbox.put(-peer.chat_id, update.max_id);
|
|
|
|
} else {
|
|
|
|
markAsReadMessagesOutbox.put(peer.user_id, update.max_id);
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
} else if (update instanceof TLRPC.TL_updateDeleteMessages) {
|
|
|
|
deletedMessages.addAll(update.messages);
|
|
|
|
} else if (update instanceof TLRPC.TL_updateUserTyping || update instanceof TLRPC.TL_updateChatUserTyping) {
|
2014-10-07 22:14:27 +02:00
|
|
|
if (update.action instanceof TLRPC.TL_sendMessageTypingAction && update.user_id != UserConfig.getClientUserId()) {
|
2013-10-25 17:19:00 +02:00
|
|
|
long uid = -update.chat_id;
|
|
|
|
if (uid == 0) {
|
|
|
|
uid = update.user_id;
|
|
|
|
}
|
|
|
|
ArrayList<PrintingUser> arr = printingUsers.get(uid);
|
|
|
|
if (arr == null) {
|
2015-01-02 23:15:07 +01:00
|
|
|
arr = new ArrayList<>();
|
2013-10-25 17:19:00 +02:00
|
|
|
printingUsers.put(uid, arr);
|
|
|
|
}
|
|
|
|
boolean exist = false;
|
|
|
|
for (PrintingUser u : arr) {
|
|
|
|
if (u.userId == update.user_id) {
|
|
|
|
exist = true;
|
|
|
|
u.lastTime = currentTime;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!exist) {
|
|
|
|
PrintingUser newUser = new PrintingUser();
|
|
|
|
newUser.userId = update.user_id;
|
|
|
|
newUser.lastTime = currentTime;
|
|
|
|
arr.add(newUser);
|
2014-05-20 22:22:44 +02:00
|
|
|
printChanged = true;
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-11-21 01:14:44 +01:00
|
|
|
onlinePrivacy.put(update.user_id, ConnectionsManager.getInstance().getCurrentTime());
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
} else if (update instanceof TLRPC.TL_updateChatParticipants) {
|
2014-02-04 19:36:55 +01:00
|
|
|
interfaceUpdateMask |= UPDATE_MASK_CHAT_MEMBERS;
|
2013-10-25 17:19:00 +02:00
|
|
|
chatInfoToUpdate.add(update.participants);
|
|
|
|
} else if (update instanceof TLRPC.TL_updateUserStatus) {
|
2014-02-04 19:36:55 +01:00
|
|
|
interfaceUpdateMask |= UPDATE_MASK_STATUS;
|
2013-10-25 17:19:00 +02:00
|
|
|
updatesOnMainThread.add(update);
|
|
|
|
} else if (update instanceof TLRPC.TL_updateUserName) {
|
2014-02-04 19:36:55 +01:00
|
|
|
interfaceUpdateMask |= UPDATE_MASK_NAME;
|
2013-10-25 17:19:00 +02:00
|
|
|
updatesOnMainThread.add(update);
|
|
|
|
} else if (update instanceof TLRPC.TL_updateUserPhoto) {
|
2014-02-04 19:36:55 +01:00
|
|
|
interfaceUpdateMask |= UPDATE_MASK_AVATAR;
|
2014-03-22 23:31:55 +01:00
|
|
|
MessagesStorage.getInstance().clearUserPhotos(update.user_id);
|
2013-10-25 17:19:00 +02:00
|
|
|
updatesOnMainThread.add(update);
|
2014-12-01 18:56:31 +01:00
|
|
|
} else if (update instanceof TLRPC.TL_updateUserPhone) {
|
|
|
|
interfaceUpdateMask |= UPDATE_MASK_PHONE;
|
|
|
|
updatesOnMainThread.add(update);
|
2013-10-25 17:19:00 +02:00
|
|
|
} else if (update instanceof TLRPC.TL_updateContactRegistered) {
|
2014-02-04 19:36:55 +01:00
|
|
|
if (enableJoined && usersDict.containsKey(update.user_id)) {
|
2013-12-20 20:25:49 +01:00
|
|
|
TLRPC.TL_messageService newMessage = new TLRPC.TL_messageService();
|
|
|
|
newMessage.action = new TLRPC.TL_messageActionUserJoined();
|
|
|
|
newMessage.local_id = newMessage.id = UserConfig.getNewMessageId();
|
|
|
|
UserConfig.saveConfig(false);
|
2014-10-07 22:14:27 +02:00
|
|
|
newMessage.flags = TLRPC.MESSAGE_FLAG_UNREAD;
|
2013-12-20 20:25:49 +01:00
|
|
|
newMessage.date = update.date;
|
|
|
|
newMessage.from_id = update.user_id;
|
|
|
|
newMessage.to_id = new TLRPC.TL_peerUser();
|
2014-06-13 12:42:21 +02:00
|
|
|
newMessage.to_id.user_id = UserConfig.getClientUserId();
|
2013-12-20 20:25:49 +01:00
|
|
|
newMessage.dialog_id = update.user_id;
|
|
|
|
|
|
|
|
messagesArr.add(newMessage);
|
2015-02-01 19:51:02 +01:00
|
|
|
MessageObject obj = new MessageObject(newMessage, usersDict, true);
|
2013-12-20 20:25:49 +01:00
|
|
|
ArrayList<MessageObject> arr = messages.get(newMessage.dialog_id);
|
|
|
|
if (arr == null) {
|
2015-01-02 23:15:07 +01:00
|
|
|
arr = new ArrayList<>();
|
2013-12-20 20:25:49 +01:00
|
|
|
messages.put(newMessage.dialog_id, arr);
|
|
|
|
}
|
|
|
|
arr.add(obj);
|
2014-07-10 02:15:58 +02:00
|
|
|
pushMessages.add(obj);
|
2013-12-20 20:25:49 +01:00
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
} else if (update instanceof TLRPC.TL_updateContactLink) {
|
2015-03-19 00:09:45 +01:00
|
|
|
if (update.my_link instanceof TLRPC.TL_contactLinkContact) {
|
2014-02-04 19:36:55 +01:00
|
|
|
int idx = contactsIds.indexOf(-update.user_id);
|
|
|
|
if (idx != -1) {
|
|
|
|
contactsIds.remove(idx);
|
|
|
|
}
|
|
|
|
if (!contactsIds.contains(update.user_id)) {
|
|
|
|
contactsIds.add(update.user_id);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int idx = contactsIds.indexOf(update.user_id);
|
|
|
|
if (idx != -1) {
|
|
|
|
contactsIds.remove(idx);
|
|
|
|
}
|
|
|
|
if (!contactsIds.contains(update.user_id)) {
|
|
|
|
contactsIds.add(-update.user_id);
|
|
|
|
}
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
} else if (update instanceof TLRPC.TL_updateActivation) {
|
2014-02-04 19:36:55 +01:00
|
|
|
//DEPRECATED
|
2013-10-25 17:19:00 +02:00
|
|
|
} else if (update instanceof TLRPC.TL_updateNewAuthorization) {
|
2013-12-20 20:25:49 +01:00
|
|
|
TLRPC.TL_messageService newMessage = new TLRPC.TL_messageService();
|
|
|
|
newMessage.action = new TLRPC.TL_messageActionLoginUnknownLocation();
|
|
|
|
newMessage.action.title = update.device;
|
|
|
|
newMessage.action.address = update.location;
|
|
|
|
newMessage.local_id = newMessage.id = UserConfig.getNewMessageId();
|
|
|
|
UserConfig.saveConfig(false);
|
2014-10-07 22:14:27 +02:00
|
|
|
newMessage.flags = TLRPC.MESSAGE_FLAG_UNREAD;
|
2013-12-20 20:25:49 +01:00
|
|
|
newMessage.date = update.date;
|
2014-08-29 23:06:04 +02:00
|
|
|
newMessage.from_id = 777000;
|
2013-12-20 20:25:49 +01:00
|
|
|
newMessage.to_id = new TLRPC.TL_peerUser();
|
2014-06-13 12:42:21 +02:00
|
|
|
newMessage.to_id.user_id = UserConfig.getClientUserId();
|
2014-08-29 23:06:04 +02:00
|
|
|
newMessage.dialog_id = 777000;
|
2013-12-20 20:25:49 +01:00
|
|
|
|
|
|
|
messagesArr.add(newMessage);
|
2015-02-01 19:51:02 +01:00
|
|
|
MessageObject obj = new MessageObject(newMessage, usersDict, true);
|
2013-12-20 20:25:49 +01:00
|
|
|
ArrayList<MessageObject> arr = messages.get(newMessage.dialog_id);
|
|
|
|
if (arr == null) {
|
2015-01-02 23:15:07 +01:00
|
|
|
arr = new ArrayList<>();
|
2013-12-20 20:25:49 +01:00
|
|
|
messages.put(newMessage.dialog_id, arr);
|
|
|
|
}
|
|
|
|
arr.add(obj);
|
2014-07-10 02:15:58 +02:00
|
|
|
pushMessages.add(obj);
|
2013-10-25 17:19:00 +02:00
|
|
|
} else if (update instanceof TLRPC.TL_updateNewGeoChatMessage) {
|
2014-02-04 19:36:55 +01:00
|
|
|
//DEPRECATED
|
2013-10-25 17:19:00 +02:00
|
|
|
} else if (update instanceof TLRPC.TL_updateNewEncryptedMessage) {
|
2015-03-19 00:09:45 +01:00
|
|
|
ArrayList<TLRPC.Message> decryptedMessages = SecretChatHelper.getInstance().decryptMessage(((TLRPC.TL_updateNewEncryptedMessage) update).message);
|
2014-10-28 22:27:44 +01:00
|
|
|
if (decryptedMessages != null && !decryptedMessages.isEmpty()) {
|
2013-10-25 17:19:00 +02:00
|
|
|
int cid = ((TLRPC.TL_updateNewEncryptedMessage)update).message.chat_id;
|
2014-10-28 22:27:44 +01:00
|
|
|
long uid = ((long) cid) << 32;
|
2013-10-25 17:19:00 +02:00
|
|
|
ArrayList<MessageObject> arr = messages.get(uid);
|
|
|
|
if (arr == null) {
|
2015-01-02 23:15:07 +01:00
|
|
|
arr = new ArrayList<>();
|
2013-10-25 17:19:00 +02:00
|
|
|
messages.put(uid, arr);
|
|
|
|
}
|
2014-10-28 22:27:44 +01:00
|
|
|
for (TLRPC.Message message : decryptedMessages) {
|
2015-02-01 19:51:02 +01:00
|
|
|
ImageLoader.saveMessageThumbs(message);
|
2014-10-28 22:27:44 +01:00
|
|
|
messagesArr.add(message);
|
2015-02-01 19:51:02 +01:00
|
|
|
MessageObject obj = new MessageObject(message, usersDict, true);
|
2014-10-28 22:27:44 +01:00
|
|
|
arr.add(obj);
|
|
|
|
pushMessages.add(obj);
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
} else if (update instanceof TLRPC.TL_updateEncryptedChatTyping) {
|
2014-08-22 16:24:33 +02:00
|
|
|
TLRPC.EncryptedChat encryptedChat = getEncryptedChatDB(update.chat_id);
|
2014-06-11 01:05:54 +02:00
|
|
|
if (encryptedChat != null) {
|
|
|
|
update.user_id = encryptedChat.user_id;
|
|
|
|
long uid = ((long) update.chat_id) << 32;
|
|
|
|
ArrayList<PrintingUser> arr = printingUsers.get(uid);
|
|
|
|
if (arr == null) {
|
2015-01-02 23:15:07 +01:00
|
|
|
arr = new ArrayList<>();
|
2014-06-11 01:05:54 +02:00
|
|
|
printingUsers.put(uid, arr);
|
|
|
|
}
|
|
|
|
boolean exist = false;
|
|
|
|
for (PrintingUser u : arr) {
|
|
|
|
if (u.userId == update.user_id) {
|
|
|
|
exist = true;
|
|
|
|
u.lastTime = currentTime;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!exist) {
|
|
|
|
PrintingUser newUser = new PrintingUser();
|
|
|
|
newUser.userId = update.user_id;
|
|
|
|
newUser.lastTime = currentTime;
|
|
|
|
arr.add(newUser);
|
|
|
|
printChanged = true;
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-11-21 01:14:44 +01:00
|
|
|
onlinePrivacy.put(update.user_id, ConnectionsManager.getInstance().getCurrentTime());
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
} else if (update instanceof TLRPC.TL_updateEncryptedMessagesRead) {
|
|
|
|
markAsReadEncrypted.put(update.chat_id, Math.max(update.max_date, update.date));
|
2015-03-19 00:09:45 +01:00
|
|
|
tasks.add((TLRPC.TL_updateEncryptedMessagesRead) update);
|
2014-02-04 19:36:55 +01:00
|
|
|
} else if (update instanceof TLRPC.TL_updateChatParticipantAdd) {
|
2014-03-22 23:31:55 +01:00
|
|
|
MessagesStorage.getInstance().updateChatInfo(update.chat_id, update.user_id, false, update.inviter_id, update.version);
|
2014-02-04 19:36:55 +01:00
|
|
|
} else if (update instanceof TLRPC.TL_updateChatParticipantDelete) {
|
2014-03-22 23:31:55 +01:00
|
|
|
MessagesStorage.getInstance().updateChatInfo(update.chat_id, update.user_id, true, 0, update.version);
|
2014-02-04 19:36:55 +01:00
|
|
|
} else if (update instanceof TLRPC.TL_updateDcOptions) {
|
2014-03-22 23:31:55 +01:00
|
|
|
ConnectionsManager.getInstance().updateDcSettings(0);
|
2013-10-25 17:19:00 +02:00
|
|
|
} else if (update instanceof TLRPC.TL_updateEncryption) {
|
2014-12-01 18:56:31 +01:00
|
|
|
SecretChatHelper.getInstance().processUpdateEncryption((TLRPC.TL_updateEncryption) update, usersDict);
|
2014-07-03 00:39:05 +02:00
|
|
|
} else if (update instanceof TLRPC.TL_updateUserBlocked) {
|
2014-08-22 16:24:33 +02:00
|
|
|
final TLRPC.TL_updateUserBlocked finalUpdate = (TLRPC.TL_updateUserBlocked)update;
|
|
|
|
if (finalUpdate.blocked) {
|
2015-01-02 23:15:07 +01:00
|
|
|
ArrayList<Integer> ids = new ArrayList<>();
|
2014-08-22 16:24:33 +02:00
|
|
|
ids.add(finalUpdate.user_id);
|
|
|
|
MessagesStorage.getInstance().putBlockedUsers(ids, false);
|
|
|
|
} else {
|
|
|
|
MessagesStorage.getInstance().deleteBlockedUser(finalUpdate.user_id);
|
|
|
|
}
|
2014-10-31 20:02:29 +01:00
|
|
|
MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
|
2014-08-22 16:24:33 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-08-22 16:24:33 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (finalUpdate.blocked) {
|
|
|
|
if (!blockedUsers.contains(finalUpdate.user_id)) {
|
|
|
|
blockedUsers.add(finalUpdate.user_id);
|
|
|
|
}
|
|
|
|
} else {
|
2014-10-31 20:02:29 +01:00
|
|
|
blockedUsers.remove((Integer) finalUpdate.user_id);
|
2014-08-22 16:24:33 +02:00
|
|
|
}
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.blockedUsersDidLoaded);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2014-07-03 00:39:05 +02:00
|
|
|
} else if (update instanceof TLRPC.TL_updateNotifySettings) {
|
|
|
|
updatesOnMainThread.add(update);
|
2014-10-17 20:29:13 +02:00
|
|
|
} else if (update instanceof TLRPC.TL_updateServiceNotification) {
|
2014-10-31 20:02:29 +01:00
|
|
|
TLRPC.TL_message newMessage = new TLRPC.TL_message();
|
|
|
|
newMessage.local_id = newMessage.id = UserConfig.getNewMessageId();
|
|
|
|
UserConfig.saveConfig(false);
|
|
|
|
newMessage.flags = TLRPC.MESSAGE_FLAG_UNREAD;
|
|
|
|
newMessage.date = update.date;
|
|
|
|
newMessage.from_id = 777000;
|
|
|
|
newMessage.to_id = new TLRPC.TL_peerUser();
|
|
|
|
newMessage.to_id.user_id = UserConfig.getClientUserId();
|
|
|
|
newMessage.dialog_id = 777000;
|
|
|
|
newMessage.media = update.media;
|
|
|
|
newMessage.message = ((TLRPC.TL_updateServiceNotification)update).message;
|
|
|
|
|
|
|
|
messagesArr.add(newMessage);
|
2015-02-01 19:51:02 +01:00
|
|
|
MessageObject obj = new MessageObject(newMessage, usersDict, true);
|
2014-10-31 20:02:29 +01:00
|
|
|
ArrayList<MessageObject> arr = messages.get(newMessage.dialog_id);
|
|
|
|
if (arr == null) {
|
2015-01-02 23:15:07 +01:00
|
|
|
arr = new ArrayList<>();
|
2014-10-31 20:02:29 +01:00
|
|
|
messages.put(newMessage.dialog_id, arr);
|
|
|
|
}
|
|
|
|
arr.add(obj);
|
|
|
|
pushMessages.add(obj);
|
2014-11-17 03:44:57 +01:00
|
|
|
} else if (update instanceof TLRPC.TL_updatePrivacy) {
|
|
|
|
updatesOnMainThread.add(update);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!messages.isEmpty()) {
|
|
|
|
for (HashMap.Entry<Long, ArrayList<MessageObject>> pair : messages.entrySet()) {
|
|
|
|
Long key = pair.getKey();
|
|
|
|
ArrayList<MessageObject> value = pair.getValue();
|
2014-05-20 22:22:44 +02:00
|
|
|
if (updatePrintingUsersWithNewMessages(key, value)) {
|
|
|
|
printChanged = true;
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-20 22:22:44 +02:00
|
|
|
if (printChanged) {
|
2013-10-25 17:19:00 +02:00
|
|
|
updatePrintingStrings();
|
|
|
|
}
|
|
|
|
|
2014-02-04 19:36:55 +01:00
|
|
|
final int interfaceUpdateMaskFinal = interfaceUpdateMask;
|
2014-05-20 22:22:44 +02:00
|
|
|
final boolean printChangedArg = printChanged;
|
2014-02-04 19:36:55 +01:00
|
|
|
|
|
|
|
if (!contactsIds.isEmpty()) {
|
2014-03-22 23:31:55 +01:00
|
|
|
ContactsController.getInstance().processContactsUpdates(contactsIds, usersDict);
|
2014-02-04 19:36:55 +01:00
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
|
2014-10-31 20:02:29 +01:00
|
|
|
MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
|
2014-07-30 09:49:39 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-07-30 09:49:39 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (!pushMessages.isEmpty()) {
|
|
|
|
NotificationsController.getInstance().processNewMessages(pushMessages, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-10-25 17:19:00 +02:00
|
|
|
if (!messagesArr.isEmpty()) {
|
2014-08-22 16:24:33 +02:00
|
|
|
MessagesStorage.getInstance().putMessages(messagesArr, true, true, false, MediaController.getInstance().getAutodownloadMask());
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-07-10 02:15:58 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
int updateMask = interfaceUpdateMaskFinal;
|
|
|
|
|
|
|
|
boolean avatarsUpdate = false;
|
|
|
|
if (!updatesOnMainThread.isEmpty()) {
|
2015-01-02 23:15:07 +01:00
|
|
|
ArrayList<TLRPC.User> dbUsers = new ArrayList<>();
|
|
|
|
ArrayList<TLRPC.User> dbUsersStatus = new ArrayList<>();
|
2014-07-10 02:15:58 +02:00
|
|
|
SharedPreferences.Editor editor = null;
|
|
|
|
for (TLRPC.Update update : updatesOnMainThread) {
|
2014-12-01 18:56:31 +01:00
|
|
|
final TLRPC.User toDbUser = new TLRPC.User();
|
2014-07-10 02:15:58 +02:00
|
|
|
toDbUser.id = update.user_id;
|
2014-12-01 18:56:31 +01:00
|
|
|
final TLRPC.User currentUser = getUser(update.user_id);
|
2014-11-17 03:44:57 +01:00
|
|
|
if (update instanceof TLRPC.TL_updatePrivacy) {
|
|
|
|
if (update.key instanceof TLRPC.TL_privacyKeyStatusTimestamp) {
|
|
|
|
ContactsController.getInstance().setPrivacyRules(update.rules);
|
|
|
|
}
|
|
|
|
} else if (update instanceof TLRPC.TL_updateUserStatus) {
|
|
|
|
if (update.status instanceof TLRPC.TL_userStatusRecently) {
|
|
|
|
update.status.expires = -100;
|
|
|
|
} else if (update.status instanceof TLRPC.TL_userStatusLastWeek) {
|
|
|
|
update.status.expires = -101;
|
|
|
|
} else if (update.status instanceof TLRPC.TL_userStatusLastMonth) {
|
|
|
|
update.status.expires = -102;
|
|
|
|
}
|
2014-07-10 02:15:58 +02:00
|
|
|
if (currentUser != null) {
|
|
|
|
currentUser.id = update.user_id;
|
|
|
|
currentUser.status = update.status;
|
|
|
|
}
|
|
|
|
toDbUser.status = update.status;
|
|
|
|
dbUsersStatus.add(toDbUser);
|
2015-02-01 19:51:02 +01:00
|
|
|
if (update.user_id == UserConfig.getClientUserId()) {
|
|
|
|
NotificationsController.getInstance().setLastOnlineFromOtherDevice(update.status.expires);
|
|
|
|
}
|
2014-07-10 02:15:58 +02:00
|
|
|
} else if (update instanceof TLRPC.TL_updateUserName) {
|
|
|
|
if (currentUser != null) {
|
2015-03-19 00:09:45 +01:00
|
|
|
if (currentUser.username != null && currentUser.username.length() > 0) {
|
|
|
|
usersByUsernames.remove(currentUser.username);
|
|
|
|
}
|
|
|
|
if (update.username != null && update.username.length() > 0) {
|
|
|
|
usersByUsernames.put(update.username, currentUser);
|
|
|
|
}
|
2014-07-10 02:15:58 +02:00
|
|
|
currentUser.first_name = update.first_name;
|
|
|
|
currentUser.last_name = update.last_name;
|
2014-10-17 20:29:13 +02:00
|
|
|
currentUser.username = update.username;
|
2014-07-10 02:15:58 +02:00
|
|
|
}
|
|
|
|
toDbUser.first_name = update.first_name;
|
|
|
|
toDbUser.last_name = update.last_name;
|
2014-10-17 20:29:13 +02:00
|
|
|
toDbUser.username = update.username;
|
2014-07-10 02:15:58 +02:00
|
|
|
dbUsers.add(toDbUser);
|
|
|
|
} else if (update instanceof TLRPC.TL_updateUserPhoto) {
|
|
|
|
if (currentUser != null) {
|
|
|
|
currentUser.photo = update.photo;
|
|
|
|
}
|
|
|
|
avatarsUpdate = true;
|
|
|
|
toDbUser.photo = update.photo;
|
|
|
|
dbUsers.add(toDbUser);
|
2014-12-01 18:56:31 +01:00
|
|
|
} else if (update instanceof TLRPC.TL_updateUserPhone) {
|
|
|
|
if (currentUser != null) {
|
|
|
|
currentUser.phone = update.phone;
|
|
|
|
Utilities.photoBookQueue.postRunnable(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
ContactsController.getInstance().addContactToPhoneBook(currentUser, true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
toDbUser.phone = update.phone;
|
|
|
|
dbUsers.add(toDbUser);
|
2014-07-10 02:15:58 +02:00
|
|
|
} else if (update instanceof TLRPC.TL_updateNotifySettings) {
|
|
|
|
if (update.notify_settings instanceof TLRPC.TL_peerNotifySettings && update.peer instanceof TLRPC.TL_notifyPeer) {
|
|
|
|
if (editor == null) {
|
|
|
|
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Activity.MODE_PRIVATE);
|
|
|
|
editor = preferences.edit();
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2015-02-01 19:51:02 +01:00
|
|
|
long dialog_id = update.peer.peer.user_id;
|
2014-07-10 02:15:58 +02:00
|
|
|
if (dialog_id == 0) {
|
|
|
|
dialog_id = -update.peer.peer.chat_id;
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2015-02-01 19:51:02 +01:00
|
|
|
TLRPC.TL_dialog dialog = dialogs_dict.get(dialog_id);
|
|
|
|
if (dialog != null) {
|
|
|
|
dialog.notify_settings = update.notify_settings;
|
|
|
|
}
|
|
|
|
if (update.notify_settings.mute_until > ConnectionsManager.getInstance().getCurrentTime()) {
|
|
|
|
int until = 0;
|
|
|
|
if (update.notify_settings.mute_until > ConnectionsManager.getInstance().getCurrentTime() + 60 * 60 * 24 * 365) {
|
|
|
|
editor.putInt("notify2_" + dialog_id, 2);
|
|
|
|
if (dialog != null) {
|
|
|
|
dialog.notify_settings.mute_until = Integer.MAX_VALUE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
until = update.notify_settings.mute_until;
|
|
|
|
editor.putInt("notify2_" + dialog_id, 3);
|
|
|
|
editor.putInt("notifyuntil_" + dialog_id, update.notify_settings.mute_until);
|
|
|
|
if (dialog != null) {
|
|
|
|
dialog.notify_settings.mute_until = until;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MessagesStorage.getInstance().setDialogFlags(dialog_id, ((long)until << 32) | 1);
|
2014-07-10 02:15:58 +02:00
|
|
|
} else {
|
2015-02-01 19:51:02 +01:00
|
|
|
if (dialog != null) {
|
|
|
|
dialog.notify_settings.mute_until = 0;
|
|
|
|
}
|
2014-07-10 02:15:58 +02:00
|
|
|
editor.remove("notify2_" + dialog_id);
|
2015-02-01 19:51:02 +01:00
|
|
|
MessagesStorage.getInstance().setDialogFlags(dialog_id, 0);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2015-02-01 19:51:02 +01:00
|
|
|
|
2014-07-27 00:28:33 +02:00
|
|
|
}/* else if (update.peer instanceof TLRPC.TL_notifyChats) { disable global settings sync
|
2014-07-10 02:15:58 +02:00
|
|
|
if (editor == null) {
|
|
|
|
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Activity.MODE_PRIVATE);
|
|
|
|
editor = preferences.edit();
|
2014-07-03 00:39:05 +02:00
|
|
|
}
|
2014-07-10 02:15:58 +02:00
|
|
|
editor.putBoolean("EnableGroup", update.notify_settings.mute_until == 0);
|
|
|
|
editor.putBoolean("EnablePreviewGroup", update.notify_settings.show_previews);
|
|
|
|
} else if (update.peer instanceof TLRPC.TL_notifyUsers) {
|
|
|
|
if (editor == null) {
|
|
|
|
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Activity.MODE_PRIVATE);
|
|
|
|
editor = preferences.edit();
|
|
|
|
}
|
|
|
|
editor.putBoolean("EnableAll", update.notify_settings.mute_until == 0);
|
|
|
|
editor.putBoolean("EnablePreviewAll", update.notify_settings.show_previews);
|
2014-07-27 00:28:33 +02:00
|
|
|
}*/
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-10 02:15:58 +02:00
|
|
|
if (editor != null) {
|
|
|
|
editor.commit();
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.notificationsSettingsUpdated);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-07-10 02:15:58 +02:00
|
|
|
MessagesStorage.getInstance().updateUsers(dbUsersStatus, true, true, true);
|
|
|
|
MessagesStorage.getInstance().updateUsers(dbUsers, false, true, true);
|
|
|
|
}
|
2014-03-22 23:31:55 +01:00
|
|
|
|
2014-07-10 02:15:58 +02:00
|
|
|
if (!messages.isEmpty()) {
|
|
|
|
for (HashMap.Entry<Long, ArrayList<MessageObject>> entry : messages.entrySet()) {
|
|
|
|
Long key = entry.getKey();
|
|
|
|
ArrayList<MessageObject> value = entry.getValue();
|
|
|
|
updateInterfaceWithMessages(key, value);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.dialogsNeedReload);
|
2014-07-10 02:15:58 +02:00
|
|
|
}
|
|
|
|
if (printChangedArg) {
|
|
|
|
updateMask |= UPDATE_MASK_USER_PRINT;
|
|
|
|
}
|
|
|
|
if (!contactsIds.isEmpty()) {
|
|
|
|
updateMask |= UPDATE_MASK_NAME;
|
|
|
|
updateMask |= UPDATE_MASK_USER_PHONE;
|
|
|
|
}
|
|
|
|
if (!chatInfoToUpdate.isEmpty()) {
|
|
|
|
for (TLRPC.ChatParticipants info : chatInfoToUpdate) {
|
|
|
|
MessagesStorage.getInstance().updateChatInfo(info.chat_id, info, true);
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.chatInfoDidLoaded, info.chat_id, info);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
2014-07-10 02:15:58 +02:00
|
|
|
if (updateMask != 0) {
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, updateMask);
|
2014-07-10 02:15:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-10-25 17:19:00 +02:00
|
|
|
|
2014-10-31 20:02:29 +01:00
|
|
|
MessagesStorage.getInstance().getStorageQueue().postRunnable(new Runnable() {
|
2014-07-30 09:49:39 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-11-06 22:34:47 +01:00
|
|
|
AndroidUtilities.runOnUIThread(new Runnable() {
|
2014-07-30 09:49:39 +02:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-08-06 01:17:40 +02:00
|
|
|
int updateMask = 0;
|
2015-03-19 00:09:45 +01:00
|
|
|
if (!markAsReadMessagesInbox.isEmpty() || !markAsReadMessagesOutbox.isEmpty()) {
|
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.messagesRead, markAsReadMessagesInbox, markAsReadMessagesOutbox);
|
|
|
|
NotificationsController.getInstance().processReadMessages(markAsReadMessagesInbox, 0, 0, 0, false);
|
|
|
|
for (HashMap.Entry<Integer, Integer> entry : markAsReadMessagesInbox.entrySet()) {
|
|
|
|
TLRPC.TL_dialog dialog = dialogs_dict.get((long) entry.getKey());
|
|
|
|
if (dialog != null && dialog.top_message <= entry.getValue()) {
|
|
|
|
MessageObject obj = dialogMessage.get(dialog.top_message);
|
|
|
|
if (obj != null) {
|
|
|
|
obj.setIsRead();
|
|
|
|
updateMask |= UPDATE_MASK_READ_DIALOG_MESSAGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (HashMap.Entry<Integer, Integer> entry : markAsReadMessagesOutbox.entrySet()) {
|
|
|
|
TLRPC.TL_dialog dialog = dialogs_dict.get((long) entry.getKey());
|
|
|
|
if (dialog != null && dialog.top_message <= entry.getValue()) {
|
|
|
|
MessageObject obj = dialogMessage.get(dialog.top_message);
|
|
|
|
if (obj != null) {
|
|
|
|
obj.setIsRead();
|
|
|
|
updateMask |= UPDATE_MASK_READ_DIALOG_MESSAGE;
|
|
|
|
}
|
2014-08-06 01:17:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!markAsReadEncrypted.isEmpty()) {
|
|
|
|
for (HashMap.Entry<Integer, Integer> entry : markAsReadEncrypted.entrySet()) {
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.messagesReadedEncrypted, entry.getKey(), entry.getValue());
|
2014-10-31 20:02:29 +01:00
|
|
|
long dialog_id = (long) (entry.getKey()) << 32;
|
2014-08-06 01:17:40 +02:00
|
|
|
TLRPC.TL_dialog dialog = dialogs_dict.get(dialog_id);
|
|
|
|
if (dialog != null) {
|
|
|
|
MessageObject message = dialogMessage.get(dialog.top_message);
|
|
|
|
if (message != null && message.messageOwner.date <= entry.getValue()) {
|
2014-10-09 17:55:05 +02:00
|
|
|
message.setIsRead();
|
2014-08-06 01:17:40 +02:00
|
|
|
updateMask |= UPDATE_MASK_READ_DIALOG_MESSAGE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!deletedMessages.isEmpty()) {
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.messagesDeleted, deletedMessages);
|
2014-08-06 01:17:40 +02:00
|
|
|
for (Integer id : deletedMessages) {
|
|
|
|
MessageObject obj = dialogMessage.get(id);
|
|
|
|
if (obj != null) {
|
|
|
|
obj.deleted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (updateMask != 0) {
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.updateInterfaces, updateMask);
|
2014-03-22 23:31:55 +01:00
|
|
|
}
|
2014-07-30 09:49:39 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2014-03-22 23:31:55 +01:00
|
|
|
|
2015-03-19 00:09:45 +01:00
|
|
|
if (!markAsReadMessagesInbox.isEmpty() || !markAsReadMessagesOutbox.isEmpty() || !markAsReadEncrypted.isEmpty()) {
|
|
|
|
if (!markAsReadMessagesInbox.isEmpty() || !markAsReadMessagesOutbox.isEmpty()) {
|
|
|
|
MessagesStorage.getInstance().updateDialogsWithReadedMessages(markAsReadMessagesInbox, true);
|
2014-02-28 23:28:25 +01:00
|
|
|
}
|
2015-03-19 00:09:45 +01:00
|
|
|
MessagesStorage.getInstance().markMessagesAsRead(markAsReadMessagesInbox, markAsReadMessagesOutbox, markAsReadEncrypted, true);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
if (!deletedMessages.isEmpty()) {
|
2014-03-22 23:31:55 +01:00
|
|
|
MessagesStorage.getInstance().markMessagesAsDeleted(deletedMessages, true);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
if (!deletedMessages.isEmpty()) {
|
2014-03-22 23:31:55 +01:00
|
|
|
MessagesStorage.getInstance().updateDialogsWithDeletedMessages(deletedMessages, true);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
if (!tasks.isEmpty()) {
|
|
|
|
for (TLRPC.TL_updateEncryptedMessagesRead update : tasks) {
|
2014-10-09 17:55:05 +02:00
|
|
|
MessagesStorage.getInstance().createTaskForSecretChat(update.chat_id, update.max_date, update.date, 1, null);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-19 00:09:45 +01:00
|
|
|
private boolean isNotifySettingsMuted(TLRPC.PeerNotifySettings settings) {
|
|
|
|
return settings instanceof TLRPC.TL_peerNotifySettings && settings.mute_until > ConnectionsManager.getInstance().getCurrentTime();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isDialogMuted(long dialog_id) {
|
|
|
|
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Activity.MODE_PRIVATE);
|
|
|
|
int mute_type = preferences.getInt("notify2_" + dialog_id, 0);
|
|
|
|
if (mute_type == 2) {
|
|
|
|
return true;
|
|
|
|
} else if (mute_type == 3) {
|
|
|
|
int mute_until = preferences.getInt("notifyuntil_" + dialog_id, 0);
|
|
|
|
if (mute_until >= ConnectionsManager.getInstance().getCurrentTime()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-25 17:19:00 +02:00
|
|
|
private boolean updatePrintingUsersWithNewMessages(long uid, ArrayList<MessageObject> messages) {
|
|
|
|
if (uid > 0) {
|
|
|
|
ArrayList<PrintingUser> arr = printingUsers.get(uid);
|
|
|
|
if (arr != null) {
|
|
|
|
printingUsers.remove(uid);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (uid < 0) {
|
2015-01-02 23:15:07 +01:00
|
|
|
ArrayList<Integer> messagesUsers = new ArrayList<>();
|
2013-10-25 17:19:00 +02:00
|
|
|
for (MessageObject message : messages) {
|
|
|
|
if (!messagesUsers.contains(message.messageOwner.from_id)) {
|
|
|
|
messagesUsers.add(message.messageOwner.from_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayList<PrintingUser> arr = printingUsers.get(uid);
|
|
|
|
boolean changed = false;
|
|
|
|
if (arr != null) {
|
|
|
|
for (int a = 0; a < arr.size(); a++) {
|
|
|
|
PrintingUser user = arr.get(a);
|
|
|
|
if (messagesUsers.contains(user.userId)) {
|
|
|
|
arr.remove(a);
|
|
|
|
a--;
|
|
|
|
if (arr.isEmpty()) {
|
|
|
|
printingUsers.remove(uid);
|
|
|
|
}
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (changed) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-22 16:24:33 +02:00
|
|
|
protected void updateInterfaceWithMessages(long uid, ArrayList<MessageObject> messages) {
|
2014-07-27 00:28:33 +02:00
|
|
|
updateInterfaceWithMessages(uid, messages, false);
|
|
|
|
}
|
|
|
|
|
2014-08-22 16:24:33 +02:00
|
|
|
protected void updateInterfaceWithMessages(final long uid, final ArrayList<MessageObject> messages, boolean isBroadcast) {
|
2013-10-25 17:19:00 +02:00
|
|
|
MessageObject lastMessage = null;
|
|
|
|
TLRPC.TL_dialog dialog = dialogs_dict.get(uid);
|
|
|
|
|
2014-03-05 17:42:10 +01:00
|
|
|
boolean isEncryptedChat = ((int)uid) == 0;
|
|
|
|
|
2014-08-22 16:24:33 +02:00
|
|
|
NotificationCenter.getInstance().postNotificationName(NotificationCenter.didReceivedNewMessages, uid, messages);
|
2013-10-25 17:19:00 +02:00
|
|
|
|
|
|
|
for (MessageObject message : messages) {
|
2015-03-19 00:09:45 +01:00
|
|
|
if (lastMessage == null || (!isEncryptedChat && message.getId() > lastMessage.getId() || isEncryptedChat && message.getId() < lastMessage.getId()) || message.messageOwner.date > lastMessage.messageOwner.date) {
|
2013-10-25 17:19:00 +02:00
|
|
|
lastMessage = message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-05 17:42:10 +01:00
|
|
|
boolean changed = false;
|
|
|
|
|
2013-10-25 17:19:00 +02:00
|
|
|
if (dialog == null) {
|
2014-07-27 00:28:33 +02:00
|
|
|
if (!isBroadcast) {
|
|
|
|
dialog = new TLRPC.TL_dialog();
|
|
|
|
dialog.id = uid;
|
|
|
|
dialog.unread_count = 0;
|
2015-03-19 00:09:45 +01:00
|
|
|
dialog.top_message = lastMessage.getId();
|
2014-07-27 00:28:33 +02:00
|
|
|
dialog.last_message_date = lastMessage.messageOwner.date;
|
|
|
|
dialogs_dict.put(uid, dialog);
|
|
|
|
dialogs.add(dialog);
|
2015-03-19 00:09:45 +01:00
|
|
|
dialogMessage.put(lastMessage.getId(), lastMessage);
|
2014-07-27 00:28:33 +02:00
|
|
|
changed = true;
|
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
} else {
|
2014-09-24 04:17:27 +02:00
|
|
|
boolean change = false;
|
2015-03-19 00:09:45 +01:00
|
|
|
if (dialog.top_message > 0 && lastMessage.getId() > 0 && lastMessage.getId() > dialog.top_message ||
|
|
|
|
dialog.top_message < 0 && lastMessage.getId() < 0 && lastMessage.getId() < dialog.top_message) {
|
2014-09-24 04:17:27 +02:00
|
|
|
change = true;
|
|
|
|
} else {
|
|
|
|
MessageObject currentDialogMessage = dialogMessage.get(dialog.top_message);
|
|
|
|
if (currentDialogMessage != null) {
|
2014-09-28 15:37:26 +02:00
|
|
|
if (currentDialogMessage.isSending() && lastMessage.isSending()) {
|
2014-09-24 04:17:27 +02:00
|
|
|
change = true;
|
2014-09-28 15:37:26 +02:00
|
|
|
} else if (dialog.last_message_date < lastMessage.messageOwner.date || dialog.last_message_date == lastMessage.messageOwner.date && lastMessage.isSending()) {
|
2014-09-24 04:17:27 +02:00
|
|
|
change = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
change = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (change) {
|
2014-03-05 17:42:10 +01:00
|
|
|
dialogMessage.remove(dialog.top_message);
|
2015-03-19 00:09:45 +01:00
|
|
|
dialog.top_message = lastMessage.getId();
|
2014-07-27 00:28:33 +02:00
|
|
|
if (!isBroadcast) {
|
|
|
|
dialog.last_message_date = lastMessage.messageOwner.date;
|
|
|
|
changed = true;
|
|
|
|
}
|
2015-03-19 00:09:45 +01:00
|
|
|
dialogMessage.put(lastMessage.getId(), lastMessage);
|
2014-03-04 20:29:32 +01:00
|
|
|
}
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
|
2014-03-05 17:42:10 +01:00
|
|
|
if (changed) {
|
|
|
|
dialogsServerOnly.clear();
|
|
|
|
Collections.sort(dialogs, new Comparator<TLRPC.TL_dialog>() {
|
|
|
|
@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) {
|
2014-07-30 09:49:39 +02:00
|
|
|
int high_id = (int)(d.id >> 32);
|
|
|
|
if ((int)d.id != 0 && high_id != 1) {
|
2014-03-05 17:42:10 +01:00
|
|
|
dialogsServerOnly.add(d);
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-27 20:57:58 +01:00
|
|
|
|
|
|
|
|
2013-10-25 17:19:00 +02:00
|
|
|
}
|