2013-10-25 15:19:00 +00:00
|
|
|
/*
|
2013-12-20 19:25:49 +00:00
|
|
|
* This is the source code of Telegram for Android v. 1.3.2.
|
2013-10-25 15:19:00 +00: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.telegram.messenger;
|
|
|
|
|
|
|
|
import android.accounts.Account;
|
|
|
|
import android.accounts.AccountManager;
|
2014-02-04 18:36:55 +00:00
|
|
|
import android.app.Activity;
|
2013-10-25 15:19:00 +00:00
|
|
|
import android.app.AlertDialog;
|
2013-10-26 22:34:39 +00:00
|
|
|
import android.app.Notification;
|
|
|
|
import android.app.NotificationManager;
|
|
|
|
import android.app.PendingIntent;
|
2013-10-25 15:19:00 +00:00
|
|
|
import android.app.ProgressDialog;
|
|
|
|
import android.content.Context;
|
2014-03-22 22:31:55 +00:00
|
|
|
import android.content.DialogInterface;
|
2013-10-26 22:34:39 +00:00
|
|
|
import android.content.Intent;
|
2013-10-25 15:19:00 +00:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.content.pm.PackageInfo;
|
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.media.AudioManager;
|
|
|
|
import android.media.SoundPool;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Build;
|
2014-03-22 22:31:55 +00:00
|
|
|
import android.os.PowerManager;
|
2013-10-25 15:19:00 +00:00
|
|
|
import android.os.Vibrator;
|
2013-10-26 22:34:39 +00:00
|
|
|
import android.provider.Settings;
|
|
|
|
import android.support.v4.app.NotificationCompat;
|
2013-12-20 19:25:49 +00:00
|
|
|
import android.support.v7.app.ActionBarActivity;
|
2013-10-25 15:19:00 +00:00
|
|
|
import android.text.Html;
|
|
|
|
import android.util.SparseArray;
|
|
|
|
|
2014-03-23 12:12:13 +00:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONObject;
|
2013-10-25 15:19:00 +00:00
|
|
|
import org.telegram.objects.MessageObject;
|
2014-02-04 18:36:55 +00:00
|
|
|
import org.telegram.objects.PhotoObject;
|
2014-03-04 20:04:57 +00:00
|
|
|
import org.telegram.ui.LaunchActivity;
|
2013-10-25 15:19:00 +00:00
|
|
|
import org.telegram.ui.ApplicationLoader;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.math.BigInteger;
|
|
|
|
import java.security.SecureRandom;
|
|
|
|
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-02-11 14:32:09 +00:00
|
|
|
public ConcurrentHashMap<Integer, TLRPC.Chat> chats = new ConcurrentHashMap<Integer, TLRPC.Chat>(100, 1.0f, 2);
|
|
|
|
public ConcurrentHashMap<Integer, TLRPC.EncryptedChat> encryptedChats = new ConcurrentHashMap<Integer, TLRPC.EncryptedChat>(10, 1.0f, 2);
|
|
|
|
public ConcurrentHashMap<Integer, TLRPC.User> users = new ConcurrentHashMap<Integer, TLRPC.User>(100, 1.0f, 2);
|
2013-10-25 15:19:00 +00:00
|
|
|
public ArrayList<TLRPC.TL_dialog> dialogs = new ArrayList<TLRPC.TL_dialog>();
|
|
|
|
public ArrayList<TLRPC.TL_dialog> dialogsServerOnly = new ArrayList<TLRPC.TL_dialog>();
|
2014-02-11 14:32:09 +00:00
|
|
|
public ConcurrentHashMap<Long, TLRPC.TL_dialog> dialogs_dict = new ConcurrentHashMap<Long, TLRPC.TL_dialog>(100, 1.0f, 2);
|
2013-10-25 15:19:00 +00:00
|
|
|
public SparseArray<MessageObject> dialogMessage = new SparseArray<MessageObject>();
|
|
|
|
public ConcurrentHashMap<Long, ArrayList<PrintingUser>> printingUsers = new ConcurrentHashMap<Long, ArrayList<PrintingUser>>(100, 1.0f, 2);
|
|
|
|
public HashMap<Long, CharSequence> printingStrings = new HashMap<Long, CharSequence>();
|
|
|
|
|
2014-02-28 22:28:25 +00:00
|
|
|
private HashMap<String, ArrayList<DelayedMessage>> delayedMessages = new HashMap<String, ArrayList<DelayedMessage>>();
|
2013-10-25 15:19:00 +00:00
|
|
|
public SparseArray<MessageObject> sendingMessages = new SparseArray<MessageObject>();
|
|
|
|
public SparseArray<TLRPC.User> hidenAddToContacts = new SparseArray<TLRPC.User>();
|
|
|
|
private SparseArray<TLRPC.EncryptedChat> acceptingChats = new SparseArray<TLRPC.EncryptedChat>();
|
2014-02-04 18:36:55 +00:00
|
|
|
private ArrayList<TLRPC.Updates> updatesQueue = new ArrayList<TLRPC.Updates>();
|
2014-03-22 22:31:55 +00:00
|
|
|
private ArrayList<Long> pendingEncMessagesToDelete = new ArrayList<Long>();
|
2014-02-04 18:36:55 +00:00
|
|
|
private long updatesStartWaitTime = 0;
|
2014-03-22 22:31:55 +00:00
|
|
|
public ArrayList<TLRPC.Update> delayedEncryptedChatUpdates = new ArrayList<TLRPC.Update>();
|
|
|
|
private boolean startingSecretChat = false;
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
private boolean gettingNewDeleteTask = false;
|
|
|
|
private int currentDeletingTaskTime = 0;
|
|
|
|
private Long currentDeletingTask = null;
|
|
|
|
private ArrayList<Integer> currentDeletingTaskMids = null;
|
|
|
|
|
|
|
|
public int totalDialogsCount = 0;
|
|
|
|
public boolean loadingDialogs = false;
|
|
|
|
public boolean dialogsEndReached = false;
|
|
|
|
public boolean gettingDifference = false;
|
2013-12-20 19:25:49 +00:00
|
|
|
public boolean gettingDifferenceAgain = false;
|
2013-10-25 15:19:00 +00:00
|
|
|
public boolean updatingState = false;
|
|
|
|
public boolean firstGettingTask = false;
|
|
|
|
public boolean registeringForPush = false;
|
|
|
|
private long lastSoundPlay = 0;
|
|
|
|
private long lastStatusUpdateTime = 0;
|
2014-02-28 22:28:25 +00:00
|
|
|
private long statusRequest = 0;
|
|
|
|
private int statusSettingState = 0;
|
|
|
|
private boolean offlineSent = false;
|
2013-10-25 15:19:00 +00:00
|
|
|
private String uploadingAvatar = null;
|
|
|
|
private SoundPool soundPool;
|
|
|
|
private int sound;
|
|
|
|
public static SecureRandom random = new SecureRandom();
|
2014-02-04 18:36:55 +00:00
|
|
|
public boolean enableJoined = true;
|
2014-02-28 22:28:25 +00:00
|
|
|
public int fontSize = Utilities.dp(16);
|
2014-02-04 18:36:55 +00:00
|
|
|
public long scheduleContactsReload = 0;
|
|
|
|
|
2014-03-22 22:31:55 +00:00
|
|
|
public static volatile boolean isScreenOn = true;
|
|
|
|
public MessageObject currentPushMessage;
|
|
|
|
|
2014-02-04 18:36:55 +00:00
|
|
|
private class UserActionUpdates extends TLRPC.Updates {
|
|
|
|
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
static {
|
|
|
|
try {
|
|
|
|
File URANDOM_FILE = new File("/dev/urandom");
|
|
|
|
FileInputStream sUrandomIn = new FileInputStream(URANDOM_FILE);
|
|
|
|
byte[] buffer = new byte[1024];
|
|
|
|
sUrandomIn.read(buffer);
|
|
|
|
sUrandomIn.close();
|
|
|
|
random.setSeed(buffer);
|
|
|
|
} catch (Exception e) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", e);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final int MESSAGE_SEND_STATE_SENDING = 1;
|
|
|
|
public static final int MESSAGE_SEND_STATE_SENT = 0;
|
|
|
|
public static final int MESSAGE_SEND_STATE_SEND_ERROR = 2;
|
|
|
|
|
|
|
|
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 18:36:55 +00: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 22:31:55 +00:00
|
|
|
public static final int UPDATE_MASK_READ_DIALOG_MESSAGE = 256;
|
|
|
|
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;
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
public long openned_dialog_id;
|
|
|
|
|
|
|
|
public static class PrintingUser {
|
|
|
|
public long lastTime;
|
|
|
|
public int userId;
|
|
|
|
}
|
|
|
|
|
|
|
|
private class DelayedMessage {
|
|
|
|
public TLRPC.TL_messages_sendMedia sendRequest;
|
|
|
|
public TLRPC.TL_decryptedMessage sendEncryptedRequest;
|
|
|
|
public int type;
|
|
|
|
public TLRPC.FileLocation location;
|
|
|
|
public TLRPC.TL_video videoLocation;
|
2014-02-11 14:32:09 +00:00
|
|
|
public TLRPC.TL_audio audioLocation;
|
2013-12-26 11:43:37 +00:00
|
|
|
public TLRPC.TL_document documentLocation;
|
2013-10-25 15:19:00 +00:00
|
|
|
public MessageObject obj;
|
|
|
|
public TLRPC.EncryptedChat encryptedChat;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final int didReceivedNewMessages = 1;
|
|
|
|
public static final int updateInterfaces = 3;
|
|
|
|
public static final int dialogsNeedReload = 4;
|
|
|
|
public static final int closeChats = 5;
|
|
|
|
public static final int messagesDeleted = 6;
|
|
|
|
public static final int messagesReaded = 7;
|
|
|
|
public static final int messagesDidLoaded = 8;
|
|
|
|
|
|
|
|
public static final int messageReceivedByAck = 9;
|
|
|
|
public static final int messageReceivedByServer = 10;
|
|
|
|
public static final int messageSendError = 11;
|
|
|
|
|
|
|
|
public static final int reloadSearchResults = 12;
|
|
|
|
|
|
|
|
public static final int contactsDidLoaded = 13;
|
|
|
|
|
|
|
|
public static final int chatDidCreated = 15;
|
|
|
|
public static final int chatDidFailCreate = 16;
|
|
|
|
|
|
|
|
public static final int chatInfoDidLoaded = 17;
|
|
|
|
|
|
|
|
public static final int mediaDidLoaded = 18;
|
|
|
|
public static final int mediaCountDidLoaded = 20;
|
|
|
|
|
|
|
|
public static final int encryptedChatUpdated = 21;
|
|
|
|
public static final int messagesReadedEncrypted = 22;
|
|
|
|
public static final int encryptedChatCreated = 23;
|
|
|
|
|
2013-12-20 19:25:49 +00:00
|
|
|
public static final int userPhotosLoaded = 24;
|
|
|
|
|
2014-03-22 22:31:55 +00:00
|
|
|
public static final int removeAllMessagesFromDialog = 25;
|
|
|
|
|
|
|
|
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 15:19:00 +00:00
|
|
|
public MessagesController() {
|
2014-03-22 22:31:55 +00:00
|
|
|
try {
|
|
|
|
PowerManager pm = (PowerManager)ApplicationLoader.applicationContext.getSystemService(Context.POWER_SERVICE);
|
|
|
|
isScreenOn = pm.isScreenOn();
|
|
|
|
FileLog.e("tmessages", "screen state = " + isScreenOn);
|
|
|
|
} catch (Exception e) {
|
|
|
|
FileLog.e("tmessages", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
MessagesStorage storage = MessagesStorage.getInstance();
|
|
|
|
NotificationCenter.getInstance().addObserver(this, FileLoader.FileDidUpload);
|
|
|
|
NotificationCenter.getInstance().addObserver(this, FileLoader.FileDidFailUpload);
|
|
|
|
NotificationCenter.getInstance().addObserver(this, 10);
|
2013-10-25 15:19:00 +00:00
|
|
|
addSupportUser();
|
2014-02-04 18:36:55 +00:00
|
|
|
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Activity.MODE_PRIVATE);
|
|
|
|
enableJoined = preferences.getBoolean("EnableContactJoined", true);
|
2014-02-28 22:28:25 +00:00
|
|
|
preferences = ApplicationLoader.applicationContext.getSharedPreferences("mainconfig", Activity.MODE_PRIVATE);
|
|
|
|
fontSize = preferences.getInt("fons_size", 16);
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
soundPool = new SoundPool(1, AudioManager.STREAM_NOTIFICATION, 0);
|
2013-11-04 12:31:01 +00:00
|
|
|
sound = soundPool.load(ApplicationLoader.applicationContext, R.raw.sound_a, 1);
|
2013-10-25 15:19:00 +00:00
|
|
|
} catch (Exception e) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", e);
|
2013-10-25 15:19:00 +00: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();
|
|
|
|
users.put(user.id, user);
|
|
|
|
}
|
|
|
|
|
2014-02-04 18:36:55 +00:00
|
|
|
public static TLRPC.InputUser getInputUser(TLRPC.User user) {
|
|
|
|
if (user == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
TLRPC.InputUser inputUser = null;
|
|
|
|
if (user.id == UserConfig.clientUserId) {
|
|
|
|
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 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void didReceivedNotification(int id, Object... args) {
|
|
|
|
if (id == FileLoader.FileDidUpload) {
|
2013-12-20 19:25:49 +00:00
|
|
|
fileDidUploaded((String)args[0], (TLRPC.InputFile)args[1], (TLRPC.InputEncryptedFile)args[2]);
|
2013-10-25 15:19:00 +00:00
|
|
|
} else if (id == FileLoader.FileDidFailUpload) {
|
2014-02-28 22:28:25 +00:00
|
|
|
fileDidFailedUpload((String) args[0], (Boolean) args[1]);
|
2013-10-25 15:19:00 +00:00
|
|
|
} else if (id == messageReceivedByServer) {
|
|
|
|
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;
|
|
|
|
obj.messageOwner.send_state = MessagesController.MESSAGE_SEND_STATE_SENT;
|
|
|
|
|
|
|
|
long uid;
|
|
|
|
if (obj.messageOwner.to_id.chat_id != 0) {
|
|
|
|
uid = -obj.messageOwner.to_id.chat_id;
|
|
|
|
} else {
|
|
|
|
if (obj.messageOwner.to_id.user_id == UserConfig.clientUserId) {
|
|
|
|
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-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void finalize() throws Throwable {
|
|
|
|
super.finalize();
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().removeObserver(this, FileLoader.FileDidUpload);
|
|
|
|
NotificationCenter.getInstance().removeObserver(this, FileLoader.FileDidFailUpload);
|
|
|
|
NotificationCenter.getInstance().removeObserver(this, messageReceivedByServer);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void cleanUp() {
|
2014-03-22 22:31:55 +00:00
|
|
|
ContactsController.getInstance().cleanup();
|
|
|
|
MediaController.getInstance().cleanup();
|
2014-02-04 18:36:55 +00:00
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
dialogs_dict.clear();
|
|
|
|
dialogs.clear();
|
|
|
|
dialogsServerOnly.clear();
|
|
|
|
acceptingChats.clear();
|
|
|
|
users.clear();
|
|
|
|
chats.clear();
|
|
|
|
sendingMessages.clear();
|
|
|
|
delayedMessages.clear();
|
|
|
|
dialogMessage.clear();
|
|
|
|
printingUsers.clear();
|
|
|
|
printingStrings.clear();
|
|
|
|
totalDialogsCount = 0;
|
|
|
|
hidenAddToContacts.clear();
|
2014-02-04 18:36:55 +00:00
|
|
|
updatesQueue.clear();
|
2014-03-22 22:31:55 +00:00
|
|
|
pendingEncMessagesToDelete.clear();
|
|
|
|
delayedEncryptedChatUpdates.clear();
|
2013-10-25 15:19:00 +00:00
|
|
|
|
2014-02-04 18:36:55 +00:00
|
|
|
updatesStartWaitTime = 0;
|
2013-10-25 15:19:00 +00:00
|
|
|
currentDeletingTaskTime = 0;
|
2014-02-04 18:36:55 +00:00
|
|
|
scheduleContactsReload = 0;
|
2013-10-25 15:19:00 +00:00
|
|
|
currentDeletingTaskMids = null;
|
|
|
|
gettingNewDeleteTask = false;
|
|
|
|
currentDeletingTask = null;
|
|
|
|
loadingDialogs = false;
|
|
|
|
dialogsEndReached = false;
|
|
|
|
gettingDifference = false;
|
2013-12-20 19:25:49 +00:00
|
|
|
gettingDifferenceAgain = false;
|
2013-10-25 15:19:00 +00:00
|
|
|
firstGettingTask = false;
|
|
|
|
updatingState = false;
|
|
|
|
lastStatusUpdateTime = 0;
|
2014-02-28 22:28:25 +00:00
|
|
|
offlineSent = false;
|
2013-10-25 15:19:00 +00:00
|
|
|
registeringForPush = false;
|
|
|
|
uploadingAvatar = null;
|
2014-03-22 22:31:55 +00:00
|
|
|
startingSecretChat = false;
|
2014-02-28 22:28:25 +00:00
|
|
|
statusRequest = 0;
|
|
|
|
statusSettingState = 0;
|
2013-10-25 15:19:00 +00:00
|
|
|
addSupportUser();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void didAddedNewTask(final int minDate) {
|
|
|
|
Utilities.stageQueue.postRunnable(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (currentDeletingTask == null && !gettingNewDeleteTask || currentDeletingTaskTime != 0 && minDate < currentDeletingTaskTime) {
|
|
|
|
getNewDeleteTask(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void getNewDeleteTask(final Long oldTask) {
|
|
|
|
Utilities.stageQueue.postRunnable(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
gettingNewDeleteTask = true;
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().getNewTask(oldTask);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private void checkDeletingTask() {
|
2014-03-22 22:31:55 +00:00
|
|
|
int currentServerTime = ConnectionsManager.getInstance().getCurrentTime();
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
if (currentDeletingTask != null && currentDeletingTaskTime != 0 && currentDeletingTaskTime <= currentServerTime) {
|
|
|
|
currentDeletingTaskTime = 0;
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-03-22 22:31:55 +00:00
|
|
|
deleteMessages(currentDeletingTaskMids, null, null);
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
Utilities.stageQueue.postRunnable(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
getNewDeleteTask(currentDeletingTask);
|
|
|
|
currentDeletingTaskTime = 0;
|
|
|
|
currentDeletingTask = null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void processLoadedDeleteTask(final Long taskId, final int taskTime, final ArrayList<Integer> messages) {
|
|
|
|
Utilities.stageQueue.postRunnable(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
gettingNewDeleteTask = false;
|
|
|
|
if (taskId != null) {
|
|
|
|
currentDeletingTaskTime = taskTime;
|
|
|
|
currentDeletingTask = taskId;
|
|
|
|
currentDeletingTaskMids = messages;
|
|
|
|
|
|
|
|
checkDeletingTask();
|
|
|
|
} else {
|
|
|
|
currentDeletingTaskTime = 0;
|
|
|
|
currentDeletingTask = null;
|
|
|
|
currentDeletingTaskMids = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-12-20 19:25:49 +00:00
|
|
|
public void deleteAllAppAccounts() {
|
|
|
|
try {
|
|
|
|
AccountManager am = AccountManager.get(ApplicationLoader.applicationContext);
|
|
|
|
Account[] accounts = am.getAccountsByType("org.telegram.messenger.account");
|
|
|
|
for (Account c : accounts) {
|
|
|
|
am.removeAccount(c, null, null);
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().getUserPhotos(uid, offset, count, max_id, classGuid);
|
2013-12-20 19:25:49 +00:00
|
|
|
} else {
|
|
|
|
TLRPC.User user = users.get(uid);
|
|
|
|
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 18:36:55 +00:00
|
|
|
req.user_id = getInputUser(user);
|
2014-03-22 22:31:55 +00:00
|
|
|
long reqId = ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-12-20 19:25:49 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error == null) {
|
2014-03-22 22:31:55 +00:00
|
|
|
TLRPC.photos_Photos res = (TLRPC.photos_Photos) response;
|
2013-12-20 19:25:49 +00:00
|
|
|
processLoadedUserPhotos(res, uid, offset, count, max_id, fromCache, classGuid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().bindRequestToGuid(reqId, classGuid);
|
2013-12-20 19:25:49 +00: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-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().putUserPhotos(uid, res);
|
2013-12-20 19:25:49 +00:00
|
|
|
} else if (res == null || res.photos.isEmpty()) {
|
|
|
|
loadUserPhotos(uid, offset, count, max_id, false, classGuid);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(userPhotosLoaded, uid, offset, count, fromCache, classGuid, res.photos);
|
2013-12-20 19:25:49 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
public void processLoadedMedia(final TLRPC.messages_Messages res, final long uid, int offset, int count, int max_id, final boolean fromCache, final int classGuid) {
|
|
|
|
int lower_part = (int)uid;
|
|
|
|
if (fromCache && res.messages.isEmpty() && lower_part != 0) {
|
|
|
|
loadMedia(uid, offset, count, max_id, false, classGuid);
|
|
|
|
} else {
|
|
|
|
if (!fromCache) {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().putUsersAndChats(res.users, res.chats, true, true);
|
|
|
|
MessagesStorage.getInstance().putMedia(uid, res.messages);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final HashMap<Integer, TLRPC.User> usersLocal = new HashMap<Integer, TLRPC.User>();
|
|
|
|
for (TLRPC.User u : res.users) {
|
|
|
|
usersLocal.put(u.id, u);
|
|
|
|
}
|
|
|
|
final ArrayList<MessageObject> objects = new ArrayList<MessageObject>();
|
|
|
|
for (TLRPC.Message message : res.messages) {
|
|
|
|
objects.add(new MessageObject(message, usersLocal));
|
|
|
|
}
|
|
|
|
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
int totalCount;
|
|
|
|
if (res instanceof TLRPC.TL_messages_messagesSlice) {
|
|
|
|
totalCount = res.count;
|
|
|
|
} else {
|
|
|
|
totalCount = res.messages.size();
|
|
|
|
}
|
|
|
|
for (TLRPC.User user : res.users) {
|
|
|
|
if (fromCache) {
|
|
|
|
users.putIfAbsent(user.id, user);
|
|
|
|
} else {
|
|
|
|
users.put(user.id, user);
|
|
|
|
if (user.id == UserConfig.clientUserId) {
|
|
|
|
UserConfig.currentUser = user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (TLRPC.Chat chat : res.chats) {
|
|
|
|
if (fromCache) {
|
|
|
|
chats.putIfAbsent(chat.id, chat);
|
|
|
|
} else {
|
|
|
|
chats.put(chat.id, chat);
|
|
|
|
}
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(mediaDidLoaded, uid, totalCount, objects, fromCache, classGuid);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void loadMedia(final long uid, final int offset, final int count, final int max_id, final boolean fromCache, final int classGuid) {
|
|
|
|
int lower_part = (int)uid;
|
|
|
|
if (fromCache || lower_part == 0) {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().loadMedia(uid, offset, count, max_id, classGuid);
|
2013-10-25 15:19:00 +00:00
|
|
|
} else {
|
|
|
|
TLRPC.TL_messages_search req = new TLRPC.TL_messages_search();
|
|
|
|
req.offset = offset;
|
|
|
|
req.limit = count;
|
|
|
|
req.max_id = max_id;
|
|
|
|
req.filter = new TLRPC.TL_inputMessagesFilterPhotoVideo();
|
|
|
|
req.q = "";
|
|
|
|
if (uid < 0) {
|
|
|
|
req.peer = new TLRPC.TL_inputPeerChat();
|
|
|
|
req.peer.chat_id = -lower_part;
|
|
|
|
} else {
|
2013-11-04 12:31:01 +00:00
|
|
|
TLRPC.User user = users.get(lower_part);
|
2013-10-25 15:19:00 +00:00
|
|
|
if (user instanceof TLRPC.TL_userForeign || user instanceof TLRPC.TL_userRequest) {
|
|
|
|
req.peer = new TLRPC.TL_inputPeerForeign();
|
|
|
|
req.peer.access_hash = user.access_hash;
|
|
|
|
} else {
|
|
|
|
req.peer = new TLRPC.TL_inputPeerContact();
|
|
|
|
}
|
|
|
|
req.peer.user_id = lower_part;
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
long reqId = ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error == null) {
|
2014-03-22 22:31:55 +00:00
|
|
|
final TLRPC.messages_Messages res = (TLRPC.messages_Messages) response;
|
2013-10-25 15:19:00 +00:00
|
|
|
processLoadedMedia(res, uid, offset, count, max_id, false, classGuid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().bindRequestToGuid(reqId, classGuid);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void processLoadedMediaCount(final int count, final long uid, final int classGuid, final boolean fromCache) {
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
int lower_part = (int)uid;
|
|
|
|
if (fromCache && count == -1 && lower_part != 0) {
|
|
|
|
getMediaCount(uid, classGuid, false);
|
|
|
|
} else {
|
|
|
|
if (!fromCache) {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().putMediaCount(uid, count);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
if (fromCache && count == -1) {
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(mediaCountDidLoaded, uid, 0, fromCache);
|
2013-10-25 15:19:00 +00:00
|
|
|
} else {
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(mediaCountDidLoaded, uid, count, fromCache);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void getMediaCount(final long uid, final int classGuid, boolean fromCache) {
|
|
|
|
int lower_part = (int)uid;
|
|
|
|
if (fromCache || lower_part == 0) {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().getMediaCount(uid, classGuid);
|
2013-10-25 15:19:00 +00:00
|
|
|
} else {
|
|
|
|
TLRPC.TL_messages_search req = new TLRPC.TL_messages_search();
|
|
|
|
req.offset = 0;
|
|
|
|
req.limit = 1;
|
|
|
|
req.max_id = 0;
|
|
|
|
req.filter = new TLRPC.TL_inputMessagesFilterPhotoVideo();
|
|
|
|
req.q = "";
|
|
|
|
if (uid < 0) {
|
|
|
|
req.peer = new TLRPC.TL_inputPeerChat();
|
|
|
|
req.peer.chat_id = -lower_part;
|
|
|
|
} else {
|
2013-11-04 12:31:01 +00:00
|
|
|
TLRPC.User user = users.get(lower_part);
|
2013-10-25 15:19:00 +00:00
|
|
|
if (user instanceof TLRPC.TL_userForeign || user instanceof TLRPC.TL_userRequest) {
|
|
|
|
req.peer = new TLRPC.TL_inputPeerForeign();
|
|
|
|
req.peer.access_hash = user.access_hash;
|
|
|
|
} else {
|
|
|
|
req.peer = new TLRPC.TL_inputPeerContact();
|
|
|
|
}
|
|
|
|
req.peer.user_id = lower_part;
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
long reqId = ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error == null) {
|
2014-03-22 22:31:55 +00:00
|
|
|
final TLRPC.messages_Messages res = (TLRPC.messages_Messages) response;
|
2013-10-25 15:19:00 +00:00
|
|
|
if (res instanceof TLRPC.TL_messages_messagesSlice) {
|
|
|
|
processLoadedMediaCount(res.count, uid, classGuid, false);
|
|
|
|
} else {
|
|
|
|
processLoadedMediaCount(res.messages.size(), uid, classGuid, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().bindRequestToGuid(reqId, classGuid);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void uploadAndApplyUserAvatar(TLRPC.PhotoSize bigPhoto) {
|
|
|
|
if (bigPhoto != null) {
|
|
|
|
uploadingAvatar = Utilities.getCacheDir() + "/" + bigPhoto.location.volume_id + "_" + bigPhoto.location.local_id + ".jpg";
|
2014-03-22 22:31:55 +00:00
|
|
|
FileLoader.getInstance().uploadFile(uploadingAvatar, null, null);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-22 22:31:55 +00:00
|
|
|
public void deleteMessages(ArrayList<Integer> messages, ArrayList<Long> randoms, TLRPC.EncryptedChat encryptedChat) {
|
2013-10-25 15:19:00 +00:00
|
|
|
for (Integer id : messages) {
|
|
|
|
MessageObject obj = dialogMessage.get(id);
|
|
|
|
if (obj != null) {
|
|
|
|
obj.deleted = true;
|
|
|
|
}
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().markMessagesAsDeleted(messages, true);
|
|
|
|
MessagesStorage.getInstance().updateDialogsWithDeletedMessages(messages, true);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(messagesDeleted, messages);
|
|
|
|
|
|
|
|
if (randoms != null && encryptedChat != null && !randoms.isEmpty()) {
|
|
|
|
sendMessagesDeleteMessage(randoms, encryptedChat);
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
ArrayList<Integer> toSend = new ArrayList<Integer>();
|
|
|
|
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 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void deleteDialog(final long did, int offset, final boolean onlyHistory) {
|
|
|
|
TLRPC.TL_dialog dialog = dialogs_dict.get(did);
|
|
|
|
if (dialog != null) {
|
|
|
|
int lower_part = (int)did;
|
|
|
|
|
|
|
|
if (offset == 0) {
|
|
|
|
if (!onlyHistory) {
|
|
|
|
dialogs.remove(dialog);
|
|
|
|
dialogsServerOnly.remove(dialog);
|
|
|
|
dialogs_dict.remove(did);
|
|
|
|
totalDialogsCount--;
|
|
|
|
}
|
|
|
|
dialogMessage.remove(dialog.top_message);
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().deleteDialog(did, onlyHistory);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(removeAllMessagesFromDialog, did);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2013-11-04 12:31:01 +00:00
|
|
|
TLRPC.User user = users.get(lower_part);
|
2013-10-25 15:19:00 +00:00
|
|
|
if (user instanceof TLRPC.TL_userForeign || user instanceof TLRPC.TL_userRequest) {
|
|
|
|
req.peer = new TLRPC.TL_inputPeerForeign();
|
|
|
|
req.peer.access_hash = user.access_hash;
|
|
|
|
} else {
|
|
|
|
req.peer = new TLRPC.TL_inputPeerContact();
|
|
|
|
}
|
|
|
|
req.peer.user_id = lower_part;
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error == null) {
|
2014-03-22 22:31:55 +00:00
|
|
|
TLRPC.TL_messages_affectedHistory res = (TLRPC.TL_messages_affectedHistory) response;
|
2013-10-25 15:19:00 +00:00
|
|
|
if (res.offset > 0) {
|
|
|
|
deleteDialog(did, res.offset, onlyHistory);
|
|
|
|
}
|
2013-11-04 12:31:01 +00:00
|
|
|
if (MessagesStorage.lastSeqValue + 1 == res.seq) {
|
|
|
|
MessagesStorage.lastSeqValue = res.seq;
|
|
|
|
MessagesStorage.lastPtsValue = res.pts;
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().saveDiffParams(MessagesStorage.lastSeqValue, MessagesStorage.lastPtsValue, MessagesStorage.lastDateValue, MessagesStorage.lastQtsValue);
|
2013-11-04 12:31:01 +00:00
|
|
|
} else if (MessagesStorage.lastSeqValue != res.seq) {
|
2014-02-04 18:36:55 +00:00
|
|
|
FileLog.e("tmessages", "need get diff TL_messages_deleteHistory, seq: " + MessagesStorage.lastSeqValue + " " + res.seq);
|
|
|
|
if (gettingDifference || updatesStartWaitTime == 0 || updatesStartWaitTime != 0 && updatesStartWaitTime + 1500 > System.currentTimeMillis()) {
|
|
|
|
if (updatesStartWaitTime == 0) {
|
|
|
|
updatesStartWaitTime = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
FileLog.e("tmessages", "add TL_messages_deleteHistory to queue");
|
|
|
|
UserActionUpdates updates = new UserActionUpdates();
|
|
|
|
updates.seq = res.seq;
|
|
|
|
updatesQueue.add(updates);
|
|
|
|
} else {
|
|
|
|
getDifference();
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
|
|
|
} else {
|
|
|
|
int encId = (int)(did >> 32);
|
2014-03-22 22:31:55 +00:00
|
|
|
if (onlyHistory) {
|
|
|
|
TLRPC.EncryptedChat encryptedChat = encryptedChats.get(encId);
|
|
|
|
sendClearHistoryMessage(encryptedChat);
|
|
|
|
} else {
|
|
|
|
declineSecretChat(encId);
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void loadChatInfo(final int chat_id) {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().loadChatInfo(chat_id);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void processChatInfo(final int chat_id, final TLRPC.ChatParticipants info, final ArrayList<TLRPC.User> usersArr, final boolean fromCache) {
|
|
|
|
if (info == null && fromCache) {
|
|
|
|
TLRPC.TL_messages_getFullChat req = new TLRPC.TL_messages_getFullChat();
|
|
|
|
req.chat_id = chat_id;
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error != null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
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);
|
2013-10-25 15:19:00 +00:00
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (TLRPC.User user : res.users) {
|
|
|
|
users.put(user.id, user);
|
|
|
|
if (user.id == UserConfig.clientUserId) {
|
|
|
|
UserConfig.currentUser = user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (TLRPC.Chat chat : res.chats) {
|
|
|
|
chats.put(chat.id, chat);
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(chatInfoDidLoaded, chat_id, res.full_chat.participants);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
|
|
|
} else {
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (TLRPC.User user : usersArr) {
|
|
|
|
if (fromCache) {
|
|
|
|
users.putIfAbsent(user.id, user);
|
|
|
|
} else {
|
|
|
|
users.put(user.id, user);
|
|
|
|
if (user.id == UserConfig.clientUserId) {
|
|
|
|
UserConfig.currentUser = user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(chatInfoDidLoaded, chat_id, info);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateTimerProc() {
|
|
|
|
long currentTime = System.currentTimeMillis();
|
|
|
|
|
|
|
|
checkDeletingTask();
|
|
|
|
|
|
|
|
if (UserConfig.clientUserId != 0) {
|
2014-02-04 18:36:55 +00:00
|
|
|
if (scheduleContactsReload != 0 && currentTime > scheduleContactsReload) {
|
2014-03-22 22:31:55 +00:00
|
|
|
ContactsController.getInstance().performSyncPhoneBook(ContactsController.getInstance().getContactsCopy(ContactsController.getInstance().contactsBook), true, false, true);
|
2014-02-04 18:36:55 +00:00
|
|
|
scheduleContactsReload = 0;
|
|
|
|
}
|
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
if (ApplicationLoader.lastPauseTime == 0) {
|
2014-02-28 22:28:25 +00:00
|
|
|
if (statusSettingState != 1 && (lastStatusUpdateTime == 0 || lastStatusUpdateTime <= System.currentTimeMillis() - 55000 || offlineSent)) {
|
|
|
|
statusSettingState = 1;
|
|
|
|
|
|
|
|
if (statusRequest != 0) {
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().cancelRpc(statusRequest, true);
|
2014-02-28 22:28:25 +00:00
|
|
|
}
|
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
TLRPC.TL_account_updateStatus req = new TLRPC.TL_account_updateStatus();
|
|
|
|
req.offline = false;
|
2014-03-22 22:31:55 +00:00
|
|
|
statusRequest = ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
2014-02-28 22:28:25 +00:00
|
|
|
if (error == null) {
|
|
|
|
lastStatusUpdateTime = System.currentTimeMillis();
|
|
|
|
offlineSent = false;
|
|
|
|
statusSettingState = 0;
|
|
|
|
} else {
|
|
|
|
if (lastStatusUpdateTime != 0) {
|
|
|
|
lastStatusUpdateTime += 5000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
statusRequest = 0;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
|
|
|
}
|
2014-02-28 22:28:25 +00:00
|
|
|
} else if (statusSettingState != 2 && !offlineSent && ApplicationLoader.lastPauseTime <= System.currentTimeMillis() - 2000) {
|
|
|
|
statusSettingState = 2;
|
|
|
|
if (statusRequest != 0) {
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().cancelRpc(statusRequest, true);
|
2014-02-28 22:28:25 +00:00
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
TLRPC.TL_account_updateStatus req = new TLRPC.TL_account_updateStatus();
|
|
|
|
req.offline = true;
|
2014-03-22 22:31:55 +00:00
|
|
|
statusRequest = ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
2014-02-28 22:28:25 +00:00
|
|
|
if (error == null) {
|
|
|
|
offlineSent = true;
|
|
|
|
} else {
|
|
|
|
if (lastStatusUpdateTime != 0) {
|
|
|
|
lastStatusUpdateTime += 5000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
statusRequest = 0;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
|
|
|
}
|
2014-02-04 18:36:55 +00:00
|
|
|
|
|
|
|
if (updatesStartWaitTime != 0 && updatesStartWaitTime + 1500 < currentTime) {
|
|
|
|
FileLog.e("tmessages", "UPDATES WAIT TIMEOUT - CHECK QUEUE");
|
|
|
|
processUpdatesQueue(false);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
scheduleContactsReload = 0;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
if (!printingUsers.isEmpty()) {
|
2014-02-04 18:36:55 +00:00
|
|
|
boolean updated = false;
|
2013-10-25 15:19:00 +00:00
|
|
|
ArrayList<Long> keys = new ArrayList<Long>(printingUsers.keySet());
|
|
|
|
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 18:36:55 +00:00
|
|
|
updated = true;
|
2013-10-25 15:19:00 +00:00
|
|
|
arr.remove(user);
|
|
|
|
a--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (arr.isEmpty()) {
|
|
|
|
printingUsers.remove(key);
|
|
|
|
keys.remove(b);
|
|
|
|
b--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
updatePrintingStrings();
|
|
|
|
|
2014-02-04 18:36:55 +00:00
|
|
|
if (updated) {
|
2013-10-25 15:19:00 +00:00
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(updateInterfaces, UPDATE_MASK_USER_PRINT);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updatePrintingStrings() {
|
|
|
|
final HashMap<Long, CharSequence> newPrintingStrings = new HashMap<Long, CharSequence>();
|
|
|
|
|
|
|
|
ArrayList<Long> keys = new ArrayList<Long>(printingUsers.keySet());
|
|
|
|
for (Long key : keys) {
|
|
|
|
if (key > 0) {
|
2014-03-22 22:31:55 +00:00
|
|
|
newPrintingStrings.put(key, LocaleController.getString("Typing", R.string.Typing));
|
2013-10-25 15:19:00 +00:00
|
|
|
} else {
|
|
|
|
ArrayList<PrintingUser> arr = printingUsers.get(key);
|
|
|
|
int count = 0;
|
|
|
|
String label = "";
|
|
|
|
for (PrintingUser pu : arr) {
|
2013-11-04 12:31:01 +00:00
|
|
|
TLRPC.User user = users.get(pu.userId);
|
2013-10-25 15:19:00 +00:00
|
|
|
if (user != null) {
|
|
|
|
if (label.length() != 0) {
|
|
|
|
label += ", ";
|
|
|
|
}
|
|
|
|
label += Utilities.formatName(user.first_name, user.last_name);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
if (count == 2) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (label.length() != 0) {
|
|
|
|
if (count > 1) {
|
|
|
|
if (arr.size() > 2) {
|
2014-03-22 22:31:55 +00:00
|
|
|
newPrintingStrings.put(key, Html.fromHtml(String.format("%s %s %s", label, String.format(LocaleController.getString("AndMoreTyping", R.string.AndMoreTyping), arr.size() - 2), LocaleController.getString("AreTyping", R.string.AreTyping))));
|
2013-10-25 15:19:00 +00:00
|
|
|
} else {
|
2014-03-22 22:31:55 +00:00
|
|
|
newPrintingStrings.put(key, Html.fromHtml(String.format("%s %s", label, LocaleController.getString("AreTyping", R.string.AreTyping))));
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
2014-03-22 22:31:55 +00:00
|
|
|
newPrintingStrings.put(key, Html.fromHtml(String.format("%s %s", label, LocaleController.getString("IsTyping", R.string.IsTyping))));
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
printingStrings = newPrintingStrings;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sendTyping(long dialog_id, int classGuid) {
|
|
|
|
if (dialog_id == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int lower_part = (int)dialog_id;
|
|
|
|
if (lower_part != 0) {
|
|
|
|
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 {
|
|
|
|
TLRPC.User user = users.get(lower_part);
|
2013-12-20 19:25:49 +00: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 15:19:00 +00:00
|
|
|
} else {
|
2013-12-20 19:25:49 +00:00
|
|
|
return;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
req.typing = true;
|
2014-03-22 22:31:55 +00:00
|
|
|
long reqId = ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().bindRequestToGuid(reqId, classGuid);
|
2013-10-25 15:19:00 +00:00
|
|
|
} else {
|
|
|
|
int encId = (int)(dialog_id >> 32);
|
|
|
|
TLRPC.EncryptedChat chat = encryptedChats.get(encId);
|
|
|
|
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-03-22 22:31:55 +00:00
|
|
|
long reqId = ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().bindRequestToGuid(reqId, classGuid);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 19:25:49 +00:00
|
|
|
public void loadMessages(final long dialog_id, final int offset, final int count, final int max_id, boolean fromCache, int midDate, final int classGuid, boolean from_unread, boolean forward) {
|
2013-10-25 15:19:00 +00:00
|
|
|
int lower_part = (int)dialog_id;
|
|
|
|
if (fromCache || lower_part == 0) {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().getMessages(dialog_id, offset, count, max_id, midDate, classGuid, from_unread, forward);
|
2013-10-25 15:19:00 +00: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 {
|
|
|
|
TLRPC.User user = users.get(lower_part);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
req.offset = offset;
|
|
|
|
req.limit = count;
|
|
|
|
req.max_id = max_id;
|
2014-03-22 22:31:55 +00:00
|
|
|
long reqId = ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error == null) {
|
2014-03-22 22:31:55 +00:00
|
|
|
final TLRPC.messages_Messages res = (TLRPC.messages_Messages) response;
|
2013-12-20 19:25:49 +00:00
|
|
|
processLoadedMessages(res, dialog_id, offset, count, max_id, false, classGuid, 0, 0, 0, 0, false);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().bindRequestToGuid(reqId, classGuid);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 19:25:49 +00:00
|
|
|
public void processLoadedMessages(final TLRPC.messages_Messages messagesRes, final long dialog_id, final int offset, final int count, final int max_id, final boolean isCache, final int classGuid, final int first_unread, final int last_unread, final int unread_count, final int last_date, final boolean isForward) {
|
2013-10-25 15:19:00 +00:00
|
|
|
Utilities.stageQueue.postRunnable(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
int lower_id = (int)dialog_id;
|
|
|
|
if (!isCache) {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().putMessages(messagesRes, dialog_id);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2013-12-20 19:25:49 +00:00
|
|
|
if (lower_id != 0 && isCache && messagesRes.messages.size() == 0 && !isForward) {
|
2013-10-25 15:19:00 +00:00
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2013-12-20 19:25:49 +00:00
|
|
|
loadMessages(dialog_id, offset, count, max_id, false, 0, classGuid, false, false);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final HashMap<Integer, TLRPC.User> usersLocal = new HashMap<Integer, TLRPC.User>();
|
|
|
|
for (TLRPC.User u : messagesRes.users) {
|
|
|
|
usersLocal.put(u.id, u);
|
|
|
|
}
|
|
|
|
final ArrayList<MessageObject> objects = new ArrayList<MessageObject>();
|
|
|
|
for (TLRPC.Message message : messagesRes.messages) {
|
|
|
|
message.dialog_id = dialog_id;
|
|
|
|
objects.add(new MessageObject(message, usersLocal));
|
|
|
|
}
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (TLRPC.User u : messagesRes.users) {
|
|
|
|
if (isCache) {
|
2014-02-28 22:28:25 +00:00
|
|
|
if (u.id == UserConfig.clientUserId || u.id / 1000 == 333) {
|
2013-11-04 12:31:01 +00:00
|
|
|
users.put(u.id, u);
|
|
|
|
} else {
|
|
|
|
users.putIfAbsent(u.id, u);
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
} else {
|
|
|
|
users.put(u.id, u);
|
|
|
|
if (u.id == UserConfig.clientUserId) {
|
|
|
|
UserConfig.currentUser = u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (TLRPC.Chat c : messagesRes.chats) {
|
|
|
|
if (isCache) {
|
|
|
|
chats.putIfAbsent(c.id, c);
|
|
|
|
} else {
|
|
|
|
chats.put(c.id, c);
|
|
|
|
}
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(messagesDidLoaded, dialog_id, offset, count, objects, isCache, first_unread, last_unread, unread_count, last_date, isForward);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public void loadDialogs(final int offset, final int serverOffset, final int count, boolean fromCache) {
|
|
|
|
if (loadingDialogs) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
loadingDialogs = true;
|
|
|
|
|
|
|
|
if (fromCache) {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().getDialogs(offset, serverOffset, count);
|
2013-10-25 15:19:00 +00:00
|
|
|
} else {
|
|
|
|
TLRPC.TL_messages_getDialogs req = new TLRPC.TL_messages_getDialogs();
|
|
|
|
req.offset = serverOffset;
|
|
|
|
req.limit = count;
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error == null) {
|
2014-03-22 22:31:55 +00:00
|
|
|
final TLRPC.messages_Dialogs dialogsRes = (TLRPC.messages_Dialogs) response;
|
2013-10-25 15:19:00 +00:00
|
|
|
processLoadedDialogs(dialogsRes, null, offset, serverOffset, count, false, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-28 22:28:25 +00:00
|
|
|
public void processDialogsUpdateRead(final HashMap<Long, Integer>dialogsToUpdate) {
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@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-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
|
2014-02-28 22:28:25 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
public void processDialogsUpdate(final TLRPC.messages_Dialogs dialogsRes, ArrayList<TLRPC.EncryptedChat> encChats) {
|
|
|
|
Utilities.stageQueue.postRunnable(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
final HashMap<Long, TLRPC.TL_dialog> new_dialogs_dict = new HashMap<Long, TLRPC.TL_dialog>();
|
|
|
|
final HashMap<Integer, MessageObject> new_dialogMessage = new HashMap<Integer, MessageObject>();
|
|
|
|
final HashMap<Integer, TLRPC.User> usersLocal = new HashMap<Integer, TLRPC.User>();
|
|
|
|
|
|
|
|
for (TLRPC.User u : dialogsRes.users) {
|
|
|
|
usersLocal.put(u.id, u);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (TLRPC.Message m : dialogsRes.messages) {
|
|
|
|
new_dialogMessage.put(m.id, new MessageObject(m, usersLocal));
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (TLRPC.User u : dialogsRes.users) {
|
|
|
|
users.putIfAbsent(u.id, u);
|
|
|
|
}
|
|
|
|
for (TLRPC.Chat c : dialogsRes.chats) {
|
|
|
|
chats.putIfAbsent(c.id, c);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
dialogMessage.remove(oldMsg.messageOwner.id);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
dialogMessage.remove(oldMsg.messageOwner.id);
|
|
|
|
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) {
|
|
|
|
if ((int)d.id != 0) {
|
|
|
|
dialogsServerOnly.add(d);
|
|
|
|
}
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
|
2013-10-25 15:19:00 +00: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) {
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (TLRPC.User u : dialogsRes.users) {
|
|
|
|
if (isCache) {
|
2014-02-28 22:28:25 +00:00
|
|
|
if (u.id == UserConfig.clientUserId || u.id / 1000 == 333) {
|
2013-11-04 12:31:01 +00:00
|
|
|
users.put(u.id, u);
|
|
|
|
} else {
|
|
|
|
users.putIfAbsent(u.id, u);
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
} else {
|
|
|
|
users.put(u.id, u);
|
|
|
|
if (u.id == UserConfig.clientUserId) {
|
|
|
|
UserConfig.currentUser = u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loadingDialogs = false;
|
|
|
|
if (resetEnd) {
|
|
|
|
dialogsEndReached = false;
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
loadDialogs(offset, serverOffset, count, false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
final HashMap<Long, TLRPC.TL_dialog> new_dialogs_dict = new HashMap<Long, TLRPC.TL_dialog>();
|
|
|
|
final HashMap<Integer, MessageObject> new_dialogMessage = new HashMap<Integer, MessageObject>();
|
|
|
|
final HashMap<Integer, TLRPC.User> usersLocal = new HashMap<Integer, TLRPC.User>();
|
|
|
|
int new_totalDialogsCount;
|
|
|
|
|
|
|
|
if (!isCache) {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().putDialogs(dialogsRes);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dialogsRes instanceof TLRPC.TL_messages_dialogsSlice) {
|
|
|
|
TLRPC.TL_messages_dialogsSlice slice = (TLRPC.TL_messages_dialogsSlice)dialogsRes;
|
|
|
|
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) {
|
|
|
|
new_dialogMessage.put(m.id, new MessageObject(m, usersLocal));
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (TLRPC.User u : dialogsRes.users) {
|
|
|
|
if (isCache) {
|
2014-02-28 22:28:25 +00:00
|
|
|
if (u.id == UserConfig.clientUserId || u.id / 1000 == 333) {
|
2013-11-04 12:31:01 +00:00
|
|
|
users.put(u.id, u);
|
|
|
|
} else {
|
|
|
|
users.putIfAbsent(u.id, u);
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
} else {
|
|
|
|
users.put(u.id, u);
|
|
|
|
if (u.id == UserConfig.clientUserId) {
|
|
|
|
UserConfig.currentUser = u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (TLRPC.Chat c : dialogsRes.chats) {
|
|
|
|
if (isCache) {
|
|
|
|
chats.putIfAbsent(c.id, c);
|
|
|
|
} else {
|
|
|
|
chats.put(c.id, c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (encChats != null) {
|
|
|
|
for (TLRPC.EncryptedChat encryptedChat : encChats) {
|
|
|
|
encryptedChats.put(encryptedChat.id, encryptedChat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
dialogMessage.remove(oldMsg.messageOwner.id);
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
dialogMessage.remove(oldMsg.messageOwner.id);
|
|
|
|
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) {
|
|
|
|
if ((int)d.id != 0) {
|
|
|
|
dialogsServerOnly.add(d);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dialogsEndReached = (dialogsRes.dialogs.size() == 0 || dialogsRes.dialogs.size() != count) && !isCache;
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-03-04 19:29:32 +00:00
|
|
|
public TLRPC.TL_photo generatePhotoSizes(String path, Uri imageUri) {
|
2013-10-25 15:19:00 +00:00
|
|
|
long time = System.currentTimeMillis();
|
2014-03-04 19:29:32 +00:00
|
|
|
Bitmap bitmap = FileLoader.loadBitmap(path, imageUri, 800, 800);
|
2013-10-25 15:19:00 +00:00
|
|
|
ArrayList<TLRPC.PhotoSize> sizes = new ArrayList<TLRPC.PhotoSize>();
|
|
|
|
TLRPC.PhotoSize size = FileLoader.scaleAndSaveImage(bitmap, 90, 90, 55, true);
|
|
|
|
if (size != null) {
|
|
|
|
size.type = "s";
|
|
|
|
sizes.add(size);
|
|
|
|
}
|
|
|
|
size = FileLoader.scaleAndSaveImage(bitmap, 320, 320, 87, false);
|
|
|
|
if (size != null) {
|
|
|
|
size.type = "m";
|
|
|
|
sizes.add(size);
|
|
|
|
}
|
|
|
|
size = FileLoader.scaleAndSaveImage(bitmap, 800, 800, 87, false);
|
|
|
|
if (size != null) {
|
|
|
|
size.type = "x";
|
|
|
|
sizes.add(size);
|
|
|
|
}
|
2013-12-20 19:25:49 +00:00
|
|
|
if (Build.VERSION.SDK_INT < 11) {
|
|
|
|
if (bitmap != null) {
|
|
|
|
bitmap.recycle();
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
if (sizes.isEmpty()) {
|
|
|
|
return null;
|
|
|
|
} else {
|
2013-11-04 12:31:01 +00:00
|
|
|
UserConfig.saveConfig(false);
|
2013-10-25 15:19:00 +00:00
|
|
|
TLRPC.TL_photo photo = new TLRPC.TL_photo();
|
|
|
|
photo.user_id = UserConfig.clientUserId;
|
2014-03-22 22:31:55 +00:00
|
|
|
photo.date = ConnectionsManager.getInstance().getCurrentTime();
|
2013-10-25 15:19:00 +00:00
|
|
|
photo.sizes = sizes;
|
|
|
|
photo.caption = "";
|
|
|
|
photo.geo = new TLRPC.TL_geoPointEmpty();
|
|
|
|
return photo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 19:25:49 +00: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) {
|
2013-10-25 15:19:00 +00:00
|
|
|
int lower_part = (int)dialog_id;
|
|
|
|
if (lower_part != 0) {
|
|
|
|
if (max_id == 0 && offset == 0) {
|
|
|
|
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 {
|
|
|
|
TLRPC.User user = users.get(lower_part);
|
|
|
|
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 19:25:49 +00:00
|
|
|
req.max_id = max_positive_id;
|
2013-10-25 15:19:00 +00:00
|
|
|
req.offset = offset;
|
|
|
|
if (offset == 0) {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().processPendingRead(dialog_id, max_positive_id, max_date, false);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2013-12-20 19:25:49 +00:00
|
|
|
if (req.max_id != Integer.MAX_VALUE) {
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-12-20 19:25:49 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error == null) {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().processPendingRead(dialog_id, max_positive_id, max_date, true);
|
|
|
|
TLRPC.TL_messages_affectedHistory res = (TLRPC.TL_messages_affectedHistory) response;
|
2013-12-20 19:25:49 +00:00
|
|
|
if (res.offset > 0) {
|
|
|
|
markDialogAsRead(dialog_id, 0, max_positive_id, res.offset, max_date, was);
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
|
2013-12-20 19:25:49 +00:00
|
|
|
if (MessagesStorage.lastSeqValue + 1 == res.seq) {
|
|
|
|
MessagesStorage.lastSeqValue = res.seq;
|
|
|
|
MessagesStorage.lastPtsValue = res.pts;
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().saveDiffParams(MessagesStorage.lastSeqValue, MessagesStorage.lastPtsValue, MessagesStorage.lastDateValue, MessagesStorage.lastQtsValue);
|
2013-12-20 19:25:49 +00:00
|
|
|
} else if (MessagesStorage.lastSeqValue != res.seq) {
|
2014-02-04 18:36:55 +00:00
|
|
|
FileLog.e("tmessages", "need get diff TL_messages_readHistory, seq: " + MessagesStorage.lastSeqValue + " " + res.seq);
|
|
|
|
if (gettingDifference || updatesStartWaitTime == 0 || updatesStartWaitTime != 0 && updatesStartWaitTime + 1500 > System.currentTimeMillis()) {
|
|
|
|
if (updatesStartWaitTime == 0) {
|
|
|
|
updatesStartWaitTime = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
FileLog.e("tmessages", "add TL_messages_readHistory to queue");
|
|
|
|
UserActionUpdates updates = new UserActionUpdates();
|
|
|
|
updates.seq = res.seq;
|
|
|
|
updatesQueue.add(updates);
|
|
|
|
} else {
|
|
|
|
getDifference();
|
|
|
|
}
|
2013-12-20 19:25:49 +00:00
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-20 19:25:49 +00:00
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().storageQueue.postRunnable(new Runnable() {
|
2014-02-04 18:36:55 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (offset == 0) {
|
|
|
|
TLRPC.TL_dialog dialog = dialogs_dict.get(dialog_id);
|
|
|
|
if (dialog != null) {
|
|
|
|
dialog.unread_count = 0;
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
|
2014-02-04 18:36:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2014-02-04 18:36:55 +00:00
|
|
|
});
|
|
|
|
if (offset == 0) {
|
2013-10-25 15:19:00 +00:00
|
|
|
TLRPC.TL_messages_receivedMessages req2 = new TLRPC.TL_messages_receivedMessages();
|
2013-12-20 19:25:49 +00:00
|
|
|
req2.max_id = max_positive_id;
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req2, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric | RPCRequest.RPCRequestClassFailOnServerErrors);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (max_date == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
int encId = (int)(dialog_id >> 32);
|
|
|
|
TLRPC.EncryptedChat chat = encryptedChats.get(encId);
|
|
|
|
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-12-20 19:25:49 +00:00
|
|
|
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
2014-03-22 22:31:55 +00:00
|
|
|
//MessagesStorage.getInstance().processPendingRead(dialog_id, max_id, max_date, true);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
2013-12-20 19:25:49 +00:00
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().processPendingRead(dialog_id, max_id, max_date, false);
|
2014-02-04 18:36:55 +00:00
|
|
|
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().storageQueue.postRunnable(new Runnable() {
|
2014-02-04 18:36:55 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
TLRPC.TL_dialog dialog = dialogs_dict.get(dialog_id);
|
|
|
|
if (dialog != null) {
|
|
|
|
dialog.unread_count = 0;
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
|
2014-02-04 18:36:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2013-10-25 15:19:00 +00:00
|
|
|
|
2013-12-20 19:25:49 +00:00
|
|
|
if (chat.ttl > 0 && was) {
|
2014-03-22 22:31:55 +00:00
|
|
|
int serverTime = Math.max(ConnectionsManager.getInstance().getCurrentTime(), max_date);
|
|
|
|
MessagesStorage.getInstance().createTaskForDate(chat.id, serverTime, serverTime, 0);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cancelSendingMessage(MessageObject object) {
|
|
|
|
String keyToRemvoe = null;
|
2014-02-28 22:28:25 +00:00
|
|
|
boolean enc = false;
|
|
|
|
for (HashMap.Entry<String, ArrayList<DelayedMessage>> entry : delayedMessages.entrySet()) {
|
|
|
|
ArrayList<DelayedMessage> messages = entry.getValue();
|
|
|
|
for (int a = 0; a < messages.size(); a++) {
|
|
|
|
DelayedMessage message = messages.get(a);
|
|
|
|
if (message.obj.messageOwner.id == object.messageOwner.id) {
|
|
|
|
messages.remove(a);
|
|
|
|
if (messages.size() == 0) {
|
|
|
|
keyToRemvoe = entry.getKey();
|
|
|
|
if (message.sendEncryptedRequest != null) {
|
|
|
|
enc = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (keyToRemvoe != null) {
|
2014-03-22 22:31:55 +00:00
|
|
|
FileLoader.getInstance().cancelUploadFile(keyToRemvoe, enc);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2014-02-28 22:28:25 +00:00
|
|
|
ArrayList<Integer> messages = new ArrayList<Integer>();
|
|
|
|
messages.add(object.messageOwner.id);
|
2014-03-22 22:31:55 +00:00
|
|
|
deleteMessages(messages, null, null);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private long getNextRandomId() {
|
|
|
|
long val = 0;
|
|
|
|
while (val == 0) {
|
|
|
|
val = random.nextLong();
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sendMessage(TLRPC.User user, long peer) {
|
2014-02-11 14:32:09 +00:00
|
|
|
sendMessage(null, 0, 0, null, null, null, null, user, null, null, peer);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void sendMessage(MessageObject message, long peer) {
|
2014-02-11 14:32:09 +00:00
|
|
|
sendMessage(null, 0, 0, null, null, message, null, null, null, null, peer);
|
2013-12-26 11:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void sendMessage(TLRPC.TL_document document, long peer) {
|
2014-02-11 14:32:09 +00:00
|
|
|
sendMessage(null, 0, 0, null, null, null, null, null, document, null, peer);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void sendMessage(String message, long peer) {
|
2014-02-11 14:32:09 +00:00
|
|
|
sendMessage(message, 0, 0, null, null, null, null, null, null, null, peer);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void sendMessage(TLRPC.FileLocation location, long peer) {
|
2014-02-11 14:32:09 +00:00
|
|
|
sendMessage(null, 0, 0, null, null, null, location, null, null, null, peer);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void sendMessage(double lat, double lon, long peer) {
|
2014-02-11 14:32:09 +00:00
|
|
|
sendMessage(null, lat, lon, null, null, null, null, null, null, null, peer);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void sendMessage(TLRPC.TL_photo photo, long peer) {
|
2014-02-11 14:32:09 +00:00
|
|
|
sendMessage(null, 0, 0, photo, null, null, null, null, null, null, peer);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void sendMessage(TLRPC.TL_video video, long peer) {
|
2014-02-11 14:32:09 +00:00
|
|
|
sendMessage(null, 0, 0, null, video, null, null, null, null, null, peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sendMessage(TLRPC.TL_audio audio, long peer) {
|
|
|
|
sendMessage(null, 0, 0, null, null, null, null, null, null, audio, peer);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
2014-03-22 22:31:55 +00:00
|
|
|
private void processPendingEncMessages() {
|
|
|
|
if (pendingEncMessagesToDelete.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ArrayList<Long> arr = new ArrayList<Long>(pendingEncMessagesToDelete);
|
|
|
|
MessagesStorage.getInstance().markMessagesAsDeletedByRandoms(arr);
|
|
|
|
pendingEncMessagesToDelete.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendMessagesDeleteMessage(ArrayList<Long> random_ids, TLRPC.EncryptedChat encryptedChat) {
|
|
|
|
TLRPC.TL_decryptedMessageService reqSend = new TLRPC.TL_decryptedMessageService();
|
|
|
|
reqSend.random_id = getNextRandomId();
|
|
|
|
reqSend.random_bytes = new byte[Math.max(1, (int)Math.ceil(random.nextDouble() * 16))];
|
|
|
|
random.nextBytes(reqSend.random_bytes);
|
|
|
|
reqSend.action = new TLRPC.TL_decryptedMessageActionDeleteMessages();
|
|
|
|
reqSend.action.random_ids = random_ids;
|
|
|
|
performSendEncryptedRequest(reqSend, null, encryptedChat, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sendClearHistoryMessage(TLRPC.EncryptedChat encryptedChat) {
|
|
|
|
TLRPC.TL_decryptedMessageService reqSend = new TLRPC.TL_decryptedMessageService();
|
|
|
|
reqSend.random_id = getNextRandomId();
|
|
|
|
reqSend.random_bytes = new byte[Math.max(1, (int)Math.ceil(random.nextDouble() * 16))];
|
|
|
|
random.nextBytes(reqSend.random_bytes);
|
|
|
|
reqSend.action = new TLRPC.TL_decryptedMessageActionFlushHistory();
|
|
|
|
performSendEncryptedRequest(reqSend, null, encryptedChat, null);
|
|
|
|
}
|
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
public void sendTTLMessage(TLRPC.EncryptedChat encryptedChat) {
|
|
|
|
TLRPC.TL_messageService newMsg = new TLRPC.TL_messageService();
|
|
|
|
|
|
|
|
newMsg.action = new TLRPC.TL_messageActionTTLChange();
|
|
|
|
newMsg.action.ttl = encryptedChat.ttl;
|
|
|
|
newMsg.local_id = newMsg.id = UserConfig.getNewMessageId();
|
|
|
|
newMsg.from_id = UserConfig.clientUserId;
|
|
|
|
newMsg.unread = true;
|
|
|
|
newMsg.dialog_id = ((long)encryptedChat.id) << 32;
|
|
|
|
newMsg.to_id = new TLRPC.TL_peerUser();
|
|
|
|
if (encryptedChat.participant_id == UserConfig.clientUserId) {
|
|
|
|
newMsg.to_id.user_id = encryptedChat.admin_id;
|
|
|
|
} else {
|
|
|
|
newMsg.to_id.user_id = encryptedChat.participant_id;
|
|
|
|
}
|
|
|
|
newMsg.out = true;
|
2014-03-22 22:31:55 +00:00
|
|
|
newMsg.date = ConnectionsManager.getInstance().getCurrentTime();
|
2013-10-25 15:19:00 +00:00
|
|
|
newMsg.random_id = getNextRandomId();
|
2013-11-04 12:31:01 +00:00
|
|
|
UserConfig.saveConfig(false);
|
2013-10-25 15:19:00 +00:00
|
|
|
final MessageObject newMsgObj = new MessageObject(newMsg, users);
|
|
|
|
newMsgObj.messageOwner.send_state = MESSAGE_SEND_STATE_SENDING;
|
|
|
|
|
|
|
|
final ArrayList<MessageObject> objArr = new ArrayList<MessageObject>();
|
|
|
|
objArr.add(newMsgObj);
|
|
|
|
ArrayList<TLRPC.Message> arr = new ArrayList<TLRPC.Message>();
|
|
|
|
arr.add(newMsg);
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().putMessages(arr, false, true);
|
2013-10-25 15:19:00 +00:00
|
|
|
updateInterfaceWithMessages(newMsg.dialog_id, objArr);
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
sendingMessages.put(newMsg.id, newMsgObj);
|
|
|
|
|
|
|
|
TLRPC.TL_decryptedMessageService reqSend = new TLRPC.TL_decryptedMessageService();
|
|
|
|
reqSend.random_id = newMsg.random_id;
|
|
|
|
reqSend.random_bytes = new byte[Math.max(1, (int)Math.ceil(random.nextDouble() * 16))];
|
|
|
|
random.nextBytes(reqSend.random_bytes);
|
|
|
|
reqSend.action = new TLRPC.TL_decryptedMessageActionSetMessageTTL();
|
|
|
|
reqSend.action.ttl_seconds = encryptedChat.ttl;
|
|
|
|
performSendEncryptedRequest(reqSend, newMsgObj, encryptedChat, null);
|
|
|
|
}
|
|
|
|
|
2014-02-11 14:32:09 +00:00
|
|
|
private void sendMessage(String message, double lat, double lon, TLRPC.TL_photo photo, TLRPC.TL_video video, MessageObject msgObj, TLRPC.FileLocation location, TLRPC.User user, TLRPC.TL_document document, TLRPC.TL_audio audio, long peer) {
|
2013-10-25 15:19:00 +00:00
|
|
|
TLRPC.Message newMsg = null;
|
|
|
|
int type = -1;
|
|
|
|
if (message != null) {
|
|
|
|
newMsg = new TLRPC.TL_message();
|
|
|
|
newMsg.media = new TLRPC.TL_messageMediaEmpty();
|
|
|
|
type = 0;
|
|
|
|
newMsg.message = message;
|
|
|
|
} else if (lat != 0 && lon != 0) {
|
|
|
|
newMsg = new TLRPC.TL_message();
|
|
|
|
newMsg.media = new TLRPC.TL_messageMediaGeo();
|
|
|
|
newMsg.media.geo = new TLRPC.TL_geoPoint();
|
|
|
|
newMsg.media.geo.lat = lat;
|
|
|
|
newMsg.media.geo._long = lon;
|
|
|
|
newMsg.message = "";
|
|
|
|
type = 1;
|
|
|
|
} else if (photo != null) {
|
|
|
|
newMsg = new TLRPC.TL_message();
|
|
|
|
newMsg.media = new TLRPC.TL_messageMediaPhoto();
|
|
|
|
newMsg.media.photo = photo;
|
|
|
|
type = 2;
|
|
|
|
newMsg.message = "-1";
|
|
|
|
TLRPC.FileLocation location1 = photo.sizes.get(photo.sizes.size() - 1).location;
|
|
|
|
newMsg.attachPath = Utilities.getCacheDir() + "/" + location1.volume_id + "_" + location1.local_id + ".jpg";
|
|
|
|
} else if (video != null) {
|
|
|
|
newMsg = new TLRPC.TL_message();
|
|
|
|
newMsg.media = new TLRPC.TL_messageMediaVideo();
|
|
|
|
newMsg.media.video = video;
|
|
|
|
type = 3;
|
|
|
|
newMsg.message = "-1";
|
|
|
|
newMsg.attachPath = video.path;
|
|
|
|
} else if (msgObj != null) {
|
|
|
|
newMsg = new TLRPC.TL_messageForwarded();
|
|
|
|
if (msgObj.messageOwner instanceof TLRPC.TL_messageForwarded) {
|
|
|
|
newMsg.fwd_from_id = msgObj.messageOwner.fwd_from_id;
|
|
|
|
newMsg.fwd_date = msgObj.messageOwner.fwd_date;
|
|
|
|
newMsg.media = msgObj.messageOwner.media;
|
|
|
|
newMsg.message = msgObj.messageOwner.message;
|
|
|
|
newMsg.fwd_msg_id = msgObj.messageOwner.id;
|
2014-02-28 22:28:25 +00:00
|
|
|
newMsg.attachPath = msgObj.messageOwner.attachPath;
|
2013-10-25 15:19:00 +00:00
|
|
|
type = 4;
|
|
|
|
} else if (msgObj.type == 11) {
|
|
|
|
newMsg.fwd_from_id = msgObj.messageOwner.from_id;
|
|
|
|
newMsg.fwd_date = msgObj.messageOwner.date;
|
|
|
|
newMsg.media = new TLRPC.TL_messageMediaPhoto();
|
|
|
|
newMsg.media.photo = msgObj.messageOwner.action.photo;
|
|
|
|
newMsg.message = "";
|
|
|
|
newMsg.fwd_msg_id = msgObj.messageOwner.id;
|
|
|
|
type = 5;
|
|
|
|
} else {
|
|
|
|
newMsg.fwd_from_id = msgObj.messageOwner.from_id;
|
|
|
|
newMsg.fwd_date = msgObj.messageOwner.date;
|
|
|
|
newMsg.media = msgObj.messageOwner.media;
|
|
|
|
newMsg.message = msgObj.messageOwner.message;
|
|
|
|
newMsg.fwd_msg_id = msgObj.messageOwner.id;
|
2014-02-28 22:28:25 +00:00
|
|
|
newMsg.attachPath = msgObj.messageOwner.attachPath;
|
2013-10-25 15:19:00 +00:00
|
|
|
type = 4;
|
|
|
|
}
|
|
|
|
} else if (location != null) {
|
|
|
|
|
|
|
|
} else if (user != null) {
|
|
|
|
newMsg = new TLRPC.TL_message();
|
|
|
|
newMsg.media = new TLRPC.TL_messageMediaContact();
|
|
|
|
newMsg.media.phone_number = user.phone;
|
|
|
|
newMsg.media.first_name = user.first_name;
|
|
|
|
newMsg.media.last_name = user.last_name;
|
|
|
|
newMsg.media.user_id = user.id;
|
|
|
|
newMsg.message = "";
|
|
|
|
type = 6;
|
2013-12-26 11:43:37 +00:00
|
|
|
} else if (document != null) {
|
|
|
|
newMsg = new TLRPC.TL_message();
|
|
|
|
newMsg.media = new TLRPC.TL_messageMediaDocument();
|
|
|
|
newMsg.media.document = document;
|
|
|
|
type = 7;
|
|
|
|
newMsg.message = "-1";
|
|
|
|
newMsg.attachPath = document.path;
|
2014-02-11 14:32:09 +00:00
|
|
|
} else if (audio != null) {
|
|
|
|
newMsg = new TLRPC.TL_message();
|
|
|
|
newMsg.media = new TLRPC.TL_messageMediaAudio();
|
|
|
|
newMsg.media.audio = audio;
|
|
|
|
type = 8;
|
|
|
|
newMsg.message = "-1";
|
|
|
|
newMsg.attachPath = audio.path;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
if (newMsg == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
newMsg.local_id = newMsg.id = UserConfig.getNewMessageId();
|
|
|
|
newMsg.from_id = UserConfig.clientUserId;
|
|
|
|
newMsg.unread = true;
|
|
|
|
newMsg.dialog_id = peer;
|
|
|
|
int lower_id = (int)peer;
|
|
|
|
TLRPC.EncryptedChat encryptedChat = null;
|
|
|
|
TLRPC.InputPeer sendToPeer = null;
|
|
|
|
if (lower_id != 0) {
|
|
|
|
if (lower_id < 0) {
|
|
|
|
newMsg.to_id = new TLRPC.TL_peerChat();
|
|
|
|
newMsg.to_id.chat_id = -lower_id;
|
|
|
|
sendToPeer = new TLRPC.TL_inputPeerChat();
|
|
|
|
sendToPeer.chat_id = -lower_id;
|
|
|
|
} else {
|
|
|
|
newMsg.to_id = new TLRPC.TL_peerUser();
|
|
|
|
newMsg.to_id.user_id = lower_id;
|
|
|
|
|
|
|
|
TLRPC.User sendToUser = users.get(lower_id);
|
2013-12-20 19:25:49 +00:00
|
|
|
if (sendToUser == null) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
if (sendToUser instanceof TLRPC.TL_userForeign || sendToUser instanceof TLRPC.TL_userRequest) {
|
|
|
|
sendToPeer = new TLRPC.TL_inputPeerForeign();
|
|
|
|
sendToPeer.user_id = sendToUser.id;
|
|
|
|
sendToPeer.access_hash = sendToUser.access_hash;
|
|
|
|
} else {
|
|
|
|
sendToPeer = new TLRPC.TL_inputPeerContact();
|
|
|
|
sendToPeer.user_id = sendToUser.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
encryptedChat = encryptedChats.get((int)(peer >> 32));
|
|
|
|
newMsg.to_id = new TLRPC.TL_peerUser();
|
|
|
|
if (encryptedChat.participant_id == UserConfig.clientUserId) {
|
|
|
|
newMsg.to_id.user_id = encryptedChat.admin_id;
|
|
|
|
} else {
|
|
|
|
newMsg.to_id.user_id = encryptedChat.participant_id;
|
|
|
|
}
|
|
|
|
newMsg.ttl = encryptedChat.ttl;
|
|
|
|
}
|
|
|
|
newMsg.out = true;
|
2014-03-22 22:31:55 +00:00
|
|
|
newMsg.date = ConnectionsManager.getInstance().getCurrentTime();
|
2013-10-25 15:19:00 +00:00
|
|
|
newMsg.random_id = getNextRandomId();
|
2013-11-04 12:31:01 +00:00
|
|
|
UserConfig.saveConfig(false);
|
2013-10-25 15:19:00 +00:00
|
|
|
final MessageObject newMsgObj = new MessageObject(newMsg, null);
|
|
|
|
newMsgObj.messageOwner.send_state = MESSAGE_SEND_STATE_SENDING;
|
|
|
|
|
|
|
|
final ArrayList<MessageObject> objArr = new ArrayList<MessageObject>();
|
|
|
|
objArr.add(newMsgObj);
|
|
|
|
ArrayList<TLRPC.Message> arr = new ArrayList<TLRPC.Message>();
|
|
|
|
arr.add(newMsg);
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().putMessages(arr, false, true);
|
2013-10-25 15:19:00 +00:00
|
|
|
updateInterfaceWithMessages(peer, objArr);
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
sendingMessages.put(newMsg.id, newMsgObj);
|
|
|
|
|
|
|
|
if (type == 0) {
|
|
|
|
if (encryptedChat == null) {
|
|
|
|
TLRPC.TL_messages_sendMessage reqSend = new TLRPC.TL_messages_sendMessage();
|
|
|
|
reqSend.message = message;
|
|
|
|
reqSend.peer = sendToPeer;
|
|
|
|
reqSend.random_id = newMsg.random_id;
|
|
|
|
performSendMessageRequest(reqSend, newMsgObj);
|
|
|
|
} else {
|
|
|
|
TLRPC.TL_decryptedMessage reqSend = new TLRPC.TL_decryptedMessage();
|
|
|
|
reqSend.random_id = newMsg.random_id;
|
|
|
|
reqSend.random_bytes = new byte[Math.max(1, (int)Math.ceil(random.nextDouble() * 16))];
|
|
|
|
random.nextBytes(reqSend.random_bytes);
|
|
|
|
reqSend.message = message;
|
|
|
|
reqSend.media = new TLRPC.TL_decryptedMessageMediaEmpty();
|
|
|
|
performSendEncryptedRequest(reqSend, newMsgObj, encryptedChat, null);
|
|
|
|
}
|
2014-02-11 14:32:09 +00:00
|
|
|
} else if (type >= 1 && type <= 3 || type >= 5 && type <= 8) {
|
2013-10-25 15:19:00 +00:00
|
|
|
if (encryptedChat == null) {
|
|
|
|
TLRPC.TL_messages_sendMedia reqSend = new TLRPC.TL_messages_sendMedia();
|
|
|
|
reqSend.peer = sendToPeer;
|
|
|
|
reqSend.random_id = newMsg.random_id;
|
|
|
|
if (type == 1) {
|
|
|
|
reqSend.media = new TLRPC.TL_inputMediaGeoPoint();
|
|
|
|
reqSend.media.geo_point = new TLRPC.TL_inputGeoPoint();
|
|
|
|
reqSend.media.geo_point.lat = lat;
|
|
|
|
reqSend.media.geo_point._long = lon;
|
|
|
|
performSendMessageRequest(reqSend, newMsgObj);
|
|
|
|
} else if (type == 2) {
|
|
|
|
reqSend.media = new TLRPC.TL_inputMediaUploadedPhoto();
|
|
|
|
DelayedMessage delayedMessage = new DelayedMessage();
|
|
|
|
delayedMessage.sendRequest = reqSend;
|
|
|
|
delayedMessage.type = 0;
|
|
|
|
delayedMessage.obj = newMsgObj;
|
|
|
|
delayedMessage.location = photo.sizes.get(photo.sizes.size() - 1).location;
|
|
|
|
performSendDelayedMessage(delayedMessage);
|
|
|
|
} else if (type == 3) {
|
|
|
|
reqSend.media = new TLRPC.TL_inputMediaUploadedThumbVideo();
|
|
|
|
reqSend.media.duration = video.duration;
|
|
|
|
reqSend.media.w = video.w;
|
|
|
|
reqSend.media.h = video.h;
|
|
|
|
DelayedMessage delayedMessage = new DelayedMessage();
|
|
|
|
delayedMessage.sendRequest = reqSend;
|
|
|
|
delayedMessage.type = 1;
|
|
|
|
delayedMessage.obj = newMsgObj;
|
|
|
|
delayedMessage.location = video.thumb.location;
|
|
|
|
delayedMessage.videoLocation = video;
|
|
|
|
performSendDelayedMessage(delayedMessage);
|
|
|
|
} else if (type == 5) {
|
|
|
|
reqSend.media = new TLRPC.TL_inputMediaPhoto();
|
|
|
|
TLRPC.TL_inputPhoto ph = new TLRPC.TL_inputPhoto();
|
|
|
|
ph.id = msgObj.messageOwner.action.photo.id;
|
|
|
|
ph.access_hash = msgObj.messageOwner.action.photo.access_hash;
|
|
|
|
((TLRPC.TL_inputMediaPhoto)reqSend.media).id = ph;
|
|
|
|
performSendMessageRequest(reqSend, newMsgObj);
|
|
|
|
} else if (type == 6) {
|
|
|
|
reqSend.media = new TLRPC.TL_inputMediaContact();
|
|
|
|
reqSend.media.phone_number = user.phone;
|
|
|
|
reqSend.media.first_name = user.first_name;
|
|
|
|
reqSend.media.last_name = user.last_name;
|
|
|
|
performSendMessageRequest(reqSend, newMsgObj);
|
2013-12-26 11:43:37 +00:00
|
|
|
} else if (type == 7) {
|
|
|
|
reqSend.media = new TLRPC.TL_inputMediaUploadedDocument();
|
|
|
|
reqSend.media.mime_type = document.mime_type;
|
|
|
|
reqSend.media.file_name = document.file_name;
|
|
|
|
DelayedMessage delayedMessage = new DelayedMessage();
|
|
|
|
delayedMessage.sendRequest = reqSend;
|
|
|
|
delayedMessage.type = 2;
|
|
|
|
delayedMessage.obj = newMsgObj;
|
|
|
|
delayedMessage.documentLocation = document;
|
|
|
|
performSendDelayedMessage(delayedMessage);
|
2014-02-11 14:32:09 +00:00
|
|
|
} else if (type == 8) {
|
|
|
|
reqSend.media = new TLRPC.TL_inputMediaUploadedAudio();
|
|
|
|
reqSend.media.duration = audio.duration;
|
|
|
|
DelayedMessage delayedMessage = new DelayedMessage();
|
|
|
|
delayedMessage.sendRequest = reqSend;
|
|
|
|
delayedMessage.type = 3;
|
|
|
|
delayedMessage.obj = newMsgObj;
|
|
|
|
delayedMessage.audioLocation = audio;
|
|
|
|
performSendDelayedMessage(delayedMessage);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
TLRPC.TL_decryptedMessage reqSend = new TLRPC.TL_decryptedMessage();
|
|
|
|
reqSend.random_id = newMsg.random_id;
|
|
|
|
reqSend.random_bytes = new byte[Math.max(1, (int)Math.ceil(random.nextDouble() * 16))];
|
|
|
|
random.nextBytes(reqSend.random_bytes);
|
|
|
|
reqSend.message = "";
|
|
|
|
if (type == 1) {
|
|
|
|
reqSend.media = new TLRPC.TL_decryptedMessageMediaGeoPoint();
|
|
|
|
reqSend.media.lat = lat;
|
|
|
|
reqSend.media._long = lon;
|
|
|
|
performSendEncryptedRequest(reqSend, newMsgObj, encryptedChat, null);
|
|
|
|
} else if (type == 2) {
|
|
|
|
reqSend.media = new TLRPC.TL_decryptedMessageMediaPhoto();
|
|
|
|
reqSend.media.iv = new byte[32];
|
|
|
|
reqSend.media.key = new byte[32];
|
|
|
|
random.nextBytes(reqSend.media.iv);
|
|
|
|
random.nextBytes(reqSend.media.key);
|
|
|
|
TLRPC.PhotoSize small = photo.sizes.get(0);
|
|
|
|
TLRPC.PhotoSize big = photo.sizes.get(photo.sizes.size() - 1);
|
|
|
|
reqSend.media.thumb = small.bytes;
|
|
|
|
reqSend.media.thumb_h = small.h;
|
|
|
|
reqSend.media.thumb_w = small.w;
|
|
|
|
reqSend.media.w = big.w;
|
|
|
|
reqSend.media.h = big.h;
|
|
|
|
reqSend.media.size = big.size;
|
|
|
|
|
|
|
|
DelayedMessage delayedMessage = new DelayedMessage();
|
|
|
|
delayedMessage.sendEncryptedRequest = reqSend;
|
|
|
|
delayedMessage.type = 0;
|
|
|
|
delayedMessage.obj = newMsgObj;
|
|
|
|
delayedMessage.encryptedChat = encryptedChat;
|
|
|
|
delayedMessage.location = photo.sizes.get(photo.sizes.size() - 1).location;
|
|
|
|
performSendDelayedMessage(delayedMessage);
|
|
|
|
} else if (type == 3) {
|
|
|
|
reqSend.media = new TLRPC.TL_decryptedMessageMediaVideo();
|
|
|
|
reqSend.media.iv = new byte[32];
|
|
|
|
reqSend.media.key = new byte[32];
|
|
|
|
random.nextBytes(reqSend.media.iv);
|
|
|
|
random.nextBytes(reqSend.media.key);
|
|
|
|
reqSend.media.duration = video.duration;
|
|
|
|
reqSend.media.size = video.size;
|
|
|
|
reqSend.media.w = video.w;
|
|
|
|
reqSend.media.h = video.h;
|
|
|
|
reqSend.media.thumb = video.thumb.bytes;
|
|
|
|
reqSend.media.thumb_h = video.thumb.h;
|
|
|
|
reqSend.media.thumb_w = video.thumb.w;
|
|
|
|
|
|
|
|
DelayedMessage delayedMessage = new DelayedMessage();
|
|
|
|
delayedMessage.sendEncryptedRequest = reqSend;
|
|
|
|
delayedMessage.type = 1;
|
|
|
|
delayedMessage.obj = newMsgObj;
|
|
|
|
delayedMessage.encryptedChat = encryptedChat;
|
|
|
|
delayedMessage.videoLocation = video;
|
|
|
|
performSendDelayedMessage(delayedMessage);
|
|
|
|
} else if (type == 5) {
|
|
|
|
|
|
|
|
} else if (type == 6) {
|
|
|
|
reqSend.media = new TLRPC.TL_decryptedMessageMediaContact();
|
|
|
|
reqSend.media.phone_number = user.phone;
|
|
|
|
reqSend.media.first_name = user.first_name;
|
|
|
|
reqSend.media.last_name = user.last_name;
|
|
|
|
reqSend.media.user_id = user.id;
|
|
|
|
performSendEncryptedRequest(reqSend, newMsgObj, encryptedChat, null);
|
2013-12-26 11:43:37 +00:00
|
|
|
} else if (type == 7) {
|
|
|
|
reqSend.media = new TLRPC.TL_decryptedMessageMediaDocument();
|
|
|
|
reqSend.media.iv = new byte[32];
|
|
|
|
reqSend.media.key = new byte[32];
|
|
|
|
random.nextBytes(reqSend.media.iv);
|
|
|
|
random.nextBytes(reqSend.media.key);
|
|
|
|
reqSend.media.size = document.size;
|
|
|
|
reqSend.media.thumb = new byte[0];
|
|
|
|
reqSend.media.thumb_h = 0;
|
|
|
|
reqSend.media.thumb_w = 0;
|
|
|
|
reqSend.media.file_name = document.file_name;
|
|
|
|
reqSend.media.mime_type = document.mime_type;
|
|
|
|
|
|
|
|
DelayedMessage delayedMessage = new DelayedMessage();
|
|
|
|
delayedMessage.sendEncryptedRequest = reqSend;
|
|
|
|
delayedMessage.type = 2;
|
|
|
|
delayedMessage.obj = newMsgObj;
|
|
|
|
delayedMessage.encryptedChat = encryptedChat;
|
|
|
|
delayedMessage.documentLocation = document;
|
|
|
|
performSendDelayedMessage(delayedMessage);
|
2014-02-11 14:32:09 +00:00
|
|
|
} else if (type == 8) {
|
|
|
|
reqSend.media = new TLRPC.TL_decryptedMessageMediaAudio();
|
|
|
|
reqSend.media.iv = new byte[32];
|
|
|
|
reqSend.media.key = new byte[32];
|
|
|
|
random.nextBytes(reqSend.media.iv);
|
|
|
|
random.nextBytes(reqSend.media.key);
|
|
|
|
reqSend.media.duration = audio.duration;
|
|
|
|
reqSend.media.size = audio.size;
|
|
|
|
|
|
|
|
DelayedMessage delayedMessage = new DelayedMessage();
|
|
|
|
delayedMessage.sendEncryptedRequest = reqSend;
|
|
|
|
delayedMessage.type = 3;
|
|
|
|
delayedMessage.obj = newMsgObj;
|
|
|
|
delayedMessage.encryptedChat = encryptedChat;
|
|
|
|
delayedMessage.audioLocation = audio;
|
|
|
|
performSendDelayedMessage(delayedMessage);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (type == 4) {
|
|
|
|
TLRPC.TL_messages_forwardMessage reqSend = new TLRPC.TL_messages_forwardMessage();
|
|
|
|
reqSend.peer = sendToPeer;
|
|
|
|
reqSend.random_id = newMsg.random_id;
|
|
|
|
if (msgObj.messageOwner.id >= 0) {
|
|
|
|
reqSend.id = msgObj.messageOwner.id;
|
|
|
|
} else {
|
|
|
|
reqSend.id = msgObj.messageOwner.fwd_msg_id;
|
|
|
|
}
|
|
|
|
performSendMessageRequest(reqSend, newMsgObj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-28 22:28:25 +00:00
|
|
|
private void processSentMessage(TLRPC.Message newMsg, TLRPC.Message sentMessage, TLRPC.EncryptedFile file, TLRPC.DecryptedMessage decryptedMessage) {
|
2014-02-11 14:32:09 +00:00
|
|
|
if (sentMessage != null) {
|
|
|
|
if (sentMessage.media instanceof TLRPC.TL_messageMediaPhoto && sentMessage.media.photo != null && newMsg.media instanceof TLRPC.TL_messageMediaPhoto && newMsg.media.photo != null) {
|
|
|
|
for (TLRPC.PhotoSize size : sentMessage.media.photo.sizes) {
|
2013-12-20 19:25:49 +00:00
|
|
|
if (size instanceof TLRPC.TL_photoSizeEmpty) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
for (TLRPC.PhotoSize size2 : newMsg.media.photo.sizes) {
|
|
|
|
if (size.type.equals(size2.type)) {
|
|
|
|
String fileName = size2.location.volume_id + "_" + size2.location.local_id;
|
|
|
|
String fileName2 = size.location.volume_id + "_" + size.location.local_id;
|
|
|
|
if (fileName.equals(fileName2)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
File cacheFile = new File(Utilities.getCacheDir(), fileName + ".jpg");
|
|
|
|
File cacheFile2 = new File(Utilities.getCacheDir(), fileName2 + ".jpg");
|
|
|
|
cacheFile.renameTo(cacheFile2);
|
2014-03-22 22:31:55 +00:00
|
|
|
FileLoader.getInstance().replaceImageInCache(fileName, fileName2);
|
2013-10-25 15:19:00 +00:00
|
|
|
size2.location = size.location;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-11 14:32:09 +00:00
|
|
|
sentMessage.message = newMsg.message;
|
|
|
|
sentMessage.attachPath = newMsg.attachPath;
|
|
|
|
} else if (sentMessage.media instanceof TLRPC.TL_messageMediaVideo && sentMessage.media.video != null && newMsg.media instanceof TLRPC.TL_messageMediaVideo && newMsg.media.video != null) {
|
2013-10-25 15:19:00 +00:00
|
|
|
TLRPC.PhotoSize size2 = newMsg.media.video.thumb;
|
2014-02-11 14:32:09 +00:00
|
|
|
TLRPC.PhotoSize size = sentMessage.media.video.thumb;
|
2013-12-20 19:25:49 +00:00
|
|
|
if (size2.location != null && size.location != null && !(size instanceof TLRPC.TL_photoSizeEmpty) && !(size2 instanceof TLRPC.TL_photoSizeEmpty)) {
|
|
|
|
String fileName = size2.location.volume_id + "_" + size2.location.local_id;
|
|
|
|
String fileName2 = size.location.volume_id + "_" + size.location.local_id;
|
|
|
|
if (fileName.equals(fileName2)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
File cacheFile = new File(Utilities.getCacheDir(), fileName + ".jpg");
|
|
|
|
File cacheFile2 = new File(Utilities.getCacheDir(), fileName2 + ".jpg");
|
|
|
|
boolean result = cacheFile.renameTo(cacheFile2);
|
2014-03-22 22:31:55 +00:00
|
|
|
FileLoader.getInstance().replaceImageInCache(fileName, fileName2);
|
2013-12-20 19:25:49 +00:00
|
|
|
size2.location = size.location;
|
2014-02-11 14:32:09 +00:00
|
|
|
sentMessage.message = newMsg.message;
|
|
|
|
sentMessage.attachPath = newMsg.attachPath;
|
|
|
|
}
|
|
|
|
} else if (sentMessage.media instanceof TLRPC.TL_messageMediaDocument && sentMessage.media.document != null && newMsg.media instanceof TLRPC.TL_messageMediaDocument && newMsg.media.document != null) {
|
|
|
|
sentMessage.message = newMsg.message;
|
|
|
|
sentMessage.attachPath = newMsg.attachPath;
|
|
|
|
} else if (sentMessage.media instanceof TLRPC.TL_messageMediaAudio && sentMessage.media.audio != null && newMsg.media instanceof TLRPC.TL_messageMediaAudio && newMsg.media.audio != null) {
|
|
|
|
sentMessage.message = newMsg.message;
|
|
|
|
sentMessage.attachPath = newMsg.attachPath;
|
|
|
|
|
|
|
|
String fileName = newMsg.media.audio.dc_id + "_" + newMsg.media.audio.id + ".m4a";
|
|
|
|
String fileName2 = sentMessage.media.audio.dc_id + "_" + sentMessage.media.audio.id + ".m4a";
|
|
|
|
if (fileName.equals(fileName2)) {
|
|
|
|
return;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2014-02-11 14:32:09 +00:00
|
|
|
File cacheFile = new File(Utilities.getCacheDir(), fileName);
|
|
|
|
File cacheFile2 = new File(Utilities.getCacheDir(), fileName2);
|
|
|
|
cacheFile.renameTo(cacheFile2);
|
2014-02-28 22:28:25 +00:00
|
|
|
newMsg.media.audio.dc_id = sentMessage.media.audio.dc_id;
|
|
|
|
newMsg.media.audio.id = sentMessage.media.audio.id;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
} else if (file != null) {
|
|
|
|
if (newMsg.media instanceof TLRPC.TL_messageMediaPhoto && newMsg.media.photo != null) {
|
|
|
|
TLRPC.PhotoSize size = newMsg.media.photo.sizes.get(newMsg.media.photo.sizes.size() - 1);
|
|
|
|
String fileName = size.location.volume_id + "_" + size.location.local_id;
|
|
|
|
size.location = new TLRPC.TL_fileEncryptedLocation();
|
|
|
|
size.location.key = decryptedMessage.media.key;
|
|
|
|
size.location.iv = decryptedMessage.media.iv;
|
|
|
|
size.location.dc_id = file.dc_id;
|
|
|
|
size.location.volume_id = file.id;
|
|
|
|
size.location.secret = file.access_hash;
|
|
|
|
size.location.local_id = file.key_fingerprint;
|
|
|
|
String fileName2 = size.location.volume_id + "_" + size.location.local_id;
|
|
|
|
File cacheFile = new File(Utilities.getCacheDir(), fileName + ".jpg");
|
|
|
|
File cacheFile2 = new File(Utilities.getCacheDir(), fileName2 + ".jpg");
|
|
|
|
boolean result = cacheFile.renameTo(cacheFile2);
|
2014-03-22 22:31:55 +00:00
|
|
|
FileLoader.getInstance().replaceImageInCache(fileName, fileName2);
|
2013-10-25 15:19:00 +00:00
|
|
|
ArrayList<TLRPC.Message> arr = new ArrayList<TLRPC.Message>();
|
|
|
|
arr.add(newMsg);
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().putMessages(arr, false, true);
|
2013-10-25 15:19:00 +00:00
|
|
|
} else if (newMsg.media instanceof TLRPC.TL_messageMediaVideo && newMsg.media.video != null) {
|
|
|
|
TLRPC.Video video = newMsg.media.video;
|
|
|
|
newMsg.media.video = new TLRPC.TL_videoEncrypted();
|
|
|
|
newMsg.media.video.duration = video.duration;
|
|
|
|
newMsg.media.video.thumb = video.thumb;
|
|
|
|
newMsg.media.video.id = video.id;
|
|
|
|
newMsg.media.video.dc_id = file.dc_id;
|
|
|
|
newMsg.media.video.w = video.w;
|
|
|
|
newMsg.media.video.h = video.h;
|
|
|
|
newMsg.media.video.date = video.date;
|
|
|
|
newMsg.media.video.caption = "";
|
|
|
|
newMsg.media.video.user_id = video.user_id;
|
|
|
|
newMsg.media.video.size = file.size;
|
|
|
|
newMsg.media.video.id = file.id;
|
|
|
|
newMsg.media.video.access_hash = file.access_hash;
|
|
|
|
newMsg.media.video.key = decryptedMessage.media.key;
|
|
|
|
newMsg.media.video.iv = decryptedMessage.media.iv;
|
|
|
|
newMsg.media.video.path = video.path;
|
|
|
|
ArrayList<TLRPC.Message> arr = new ArrayList<TLRPC.Message>();
|
|
|
|
arr.add(newMsg);
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().putMessages(arr, false, true);
|
2013-12-26 11:43:37 +00:00
|
|
|
} else if (newMsg.media instanceof TLRPC.TL_messageMediaDocument && newMsg.media.document != null) {
|
|
|
|
TLRPC.Document document = newMsg.media.document;
|
|
|
|
newMsg.media.document = new TLRPC.TL_documentEncrypted();
|
|
|
|
newMsg.media.document.id = file.id;
|
|
|
|
newMsg.media.document.access_hash = file.access_hash;
|
|
|
|
newMsg.media.document.user_id = document.user_id;
|
|
|
|
newMsg.media.document.date = document.date;
|
|
|
|
newMsg.media.document.file_name = document.file_name;
|
|
|
|
newMsg.media.document.mime_type = document.mime_type;
|
|
|
|
newMsg.media.document.size = file.size;
|
|
|
|
newMsg.media.document.key = decryptedMessage.media.key;
|
|
|
|
newMsg.media.document.iv = decryptedMessage.media.iv;
|
|
|
|
newMsg.media.document.path = document.path;
|
|
|
|
newMsg.media.document.thumb = document.thumb;
|
|
|
|
newMsg.media.document.dc_id = file.dc_id;
|
2014-02-11 14:32:09 +00:00
|
|
|
ArrayList<TLRPC.Message> arr = new ArrayList<TLRPC.Message>();
|
|
|
|
arr.add(newMsg);
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().putMessages(arr, false, true);
|
2014-02-11 14:32:09 +00:00
|
|
|
} else if (newMsg.media instanceof TLRPC.TL_messageMediaAudio && newMsg.media.audio != null) {
|
|
|
|
TLRPC.Audio audio = newMsg.media.audio;
|
|
|
|
newMsg.media.audio = new TLRPC.TL_audioEncrypted();
|
|
|
|
newMsg.media.audio.id = file.id;
|
|
|
|
newMsg.media.audio.access_hash = file.access_hash;
|
|
|
|
newMsg.media.audio.user_id = audio.user_id;
|
|
|
|
newMsg.media.audio.date = audio.date;
|
|
|
|
newMsg.media.audio.duration = audio.duration;
|
|
|
|
newMsg.media.audio.size = file.size;
|
|
|
|
newMsg.media.audio.dc_id = file.dc_id;
|
|
|
|
newMsg.media.audio.key = decryptedMessage.media.key;
|
|
|
|
newMsg.media.audio.iv = decryptedMessage.media.iv;
|
|
|
|
newMsg.media.audio.path = audio.path;
|
|
|
|
|
|
|
|
String fileName = audio.dc_id + "_" + audio.id + ".m4a";
|
|
|
|
String fileName2 = newMsg.media.audio.dc_id + "_" + newMsg.media.audio.id + ".m4a";
|
|
|
|
if (fileName.equals(fileName2)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
File cacheFile = new File(Utilities.getCacheDir(), fileName);
|
|
|
|
File cacheFile2 = new File(Utilities.getCacheDir(), fileName2);
|
|
|
|
cacheFile.renameTo(cacheFile2);
|
|
|
|
|
2013-12-26 11:43:37 +00:00
|
|
|
ArrayList<TLRPC.Message> arr = new ArrayList<TLRPC.Message>();
|
|
|
|
arr.add(newMsg);
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().putMessages(arr, false, true);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void performSendEncryptedRequest(final TLRPC.DecryptedMessage req, final MessageObject newMsgObj, final TLRPC.EncryptedChat chat, final TLRPC.InputEncryptedFile encryptedFile) {
|
2013-11-04 12:31:01 +00:00
|
|
|
if (req == null) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
//TLRPC.decryptedMessageLayer messageLayer = new TLRPC.decryptedMessageLayer();
|
|
|
|
//messageLayer.layer = 8;
|
|
|
|
//messageLayer.message = req;
|
|
|
|
SerializedData data = new SerializedData();
|
|
|
|
req.serializeToStream(data);
|
|
|
|
|
|
|
|
SerializedData toEncrypt = new SerializedData();
|
|
|
|
toEncrypt.writeInt32(data.length());
|
|
|
|
toEncrypt.writeRaw(data.toByteArray());
|
|
|
|
|
|
|
|
byte[] innerData = toEncrypt.toByteArray();
|
|
|
|
|
|
|
|
byte[] messageKeyFull = Utilities.computeSHA1(innerData);
|
|
|
|
byte[] messageKey = new byte[16];
|
|
|
|
System.arraycopy(messageKeyFull, messageKeyFull.length - 16, messageKey, 0, 16);
|
|
|
|
|
|
|
|
MessageKeyData keyData = Utilities.generateMessageKeyData(chat.auth_key, messageKey, false);
|
|
|
|
|
|
|
|
SerializedData dataForEncryption = new SerializedData();
|
|
|
|
dataForEncryption.writeRaw(innerData);
|
2013-12-20 19:25:49 +00:00
|
|
|
byte[] b = new byte[1];
|
2013-10-25 15:19:00 +00:00
|
|
|
while (dataForEncryption.length() % 16 != 0) {
|
2013-12-20 19:25:49 +00:00
|
|
|
MessagesController.random.nextBytes(b);
|
|
|
|
dataForEncryption.writeByte(b[0]);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 22:28:25 +00:00
|
|
|
byte[] encryptedData = Utilities.aesIgeEncryption(dataForEncryption.toByteArray(), keyData.aesKey, keyData.aesIv, true, false, 0);
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
data = new SerializedData();
|
|
|
|
data.writeInt64(chat.key_fingerprint);
|
|
|
|
data.writeRaw(messageKey);
|
|
|
|
data.writeRaw(encryptedData);
|
|
|
|
|
|
|
|
TLObject reqToSend;
|
|
|
|
|
|
|
|
if (encryptedFile == null) {
|
|
|
|
TLRPC.TL_messages_sendEncrypted req2 = new TLRPC.TL_messages_sendEncrypted();
|
|
|
|
req2.data = data.toByteArray();
|
|
|
|
req2.random_id = req.random_id;
|
|
|
|
req2.peer = new TLRPC.TL_inputEncryptedChat();
|
|
|
|
req2.peer.chat_id = chat.id;
|
|
|
|
req2.peer.access_hash = chat.access_hash;
|
|
|
|
reqToSend = req2;
|
|
|
|
} else {
|
|
|
|
TLRPC.TL_messages_sendEncryptedFile req2 = new TLRPC.TL_messages_sendEncryptedFile();
|
|
|
|
req2.data = data.toByteArray();
|
|
|
|
req2.random_id = req.random_id;
|
|
|
|
req2.peer = new TLRPC.TL_inputEncryptedChat();
|
|
|
|
req2.peer.chat_id = chat.id;
|
|
|
|
req2.peer.access_hash = chat.access_hash;
|
|
|
|
req2.file = encryptedFile;
|
|
|
|
reqToSend = req2;
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(reqToSend, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
2014-03-22 22:31:55 +00:00
|
|
|
if (newMsgObj != null) {
|
|
|
|
if (error == null) {
|
|
|
|
TLRPC.messages_SentEncryptedMessage res = (TLRPC.messages_SentEncryptedMessage) response;
|
|
|
|
newMsgObj.messageOwner.date = res.date;
|
|
|
|
if (res.file instanceof TLRPC.TL_encryptedFile) {
|
|
|
|
processSentMessage(newMsgObj.messageOwner, null, res.file, req);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().updateMessageStateAndId(newMsgObj.messageOwner.random_id, newMsgObj.messageOwner.id, newMsgObj.messageOwner.id, res.date, true);
|
|
|
|
MessagesStorage.getInstance().storageQueue.postRunnable(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
newMsgObj.messageOwner.send_state = MESSAGE_SEND_STATE_SENT;
|
|
|
|
NotificationCenter.getInstance().postNotificationName(messageReceivedByServer, newMsgObj.messageOwner.id, newMsgObj.messageOwner.id, newMsgObj);
|
|
|
|
sendingMessages.remove(newMsgObj.messageOwner.id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
sendingMessages.remove(newMsgObj.messageOwner.id);
|
|
|
|
newMsgObj.messageOwner.send_state = MESSAGE_SEND_STATE_SEND_ERROR;
|
|
|
|
NotificationCenter.getInstance().postNotificationName(messageSendError, newMsgObj.messageOwner.id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-04 18:36:55 +00:00
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric | RPCRequest.RPCRequestClassCanCompress);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void performSendMessageRequest(TLObject req, final MessageObject newMsgObj) {
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error == null) {
|
|
|
|
final int oldId = newMsgObj.messageOwner.id;
|
2014-02-28 22:28:25 +00:00
|
|
|
ArrayList<TLRPC.Message> sentMessages = new ArrayList<TLRPC.Message>();
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
if (response instanceof TLRPC.TL_messages_sentMessage) {
|
2014-03-22 22:31:55 +00:00
|
|
|
TLRPC.TL_messages_sentMessage res = (TLRPC.TL_messages_sentMessage) response;
|
2013-10-25 15:19:00 +00:00
|
|
|
newMsgObj.messageOwner.id = res.id;
|
2014-03-22 22:31:55 +00:00
|
|
|
if (MessagesStorage.lastSeqValue + 1 == res.seq) {
|
2013-11-04 12:31:01 +00:00
|
|
|
MessagesStorage.lastSeqValue = res.seq;
|
|
|
|
MessagesStorage.lastDateValue = res.date;
|
|
|
|
MessagesStorage.lastPtsValue = res.pts;
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().saveDiffParams(MessagesStorage.lastSeqValue, MessagesStorage.lastPtsValue, MessagesStorage.lastDateValue, MessagesStorage.lastQtsValue);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (MessagesStorage.lastSeqValue != res.seq) {
|
|
|
|
FileLog.e("tmessages", "need get diff TL_messages_sentMessage, seq: " + MessagesStorage.lastSeqValue + " " + res.seq);
|
|
|
|
if (gettingDifference || updatesStartWaitTime == 0 || updatesStartWaitTime != 0 && updatesStartWaitTime + 1500 > System.currentTimeMillis()) {
|
|
|
|
if (updatesStartWaitTime == 0) {
|
|
|
|
updatesStartWaitTime = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
FileLog.e("tmessages", "add TL_messages_sentMessage to queue");
|
|
|
|
UserActionUpdates updates = new UserActionUpdates();
|
|
|
|
updates.seq = res.seq;
|
|
|
|
updatesQueue.add(updates);
|
|
|
|
} else {
|
|
|
|
getDifference();
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
} else if (response instanceof TLRPC.messages_StatedMessage) {
|
2014-03-22 22:31:55 +00:00
|
|
|
TLRPC.messages_StatedMessage res = (TLRPC.messages_StatedMessage) response;
|
2014-02-28 22:28:25 +00:00
|
|
|
sentMessages.add(res.message);
|
2013-10-25 15:19:00 +00:00
|
|
|
newMsgObj.messageOwner.id = res.message.id;
|
2014-02-28 22:28:25 +00:00
|
|
|
processSentMessage(newMsgObj.messageOwner, res.message, null, null);
|
2014-03-22 22:31:55 +00:00
|
|
|
if (MessagesStorage.lastSeqValue + 1 == res.seq) {
|
2013-11-04 12:31:01 +00:00
|
|
|
MessagesStorage.lastSeqValue = res.seq;
|
|
|
|
MessagesStorage.lastPtsValue = res.pts;
|
|
|
|
MessagesStorage.lastDateValue = res.message.date;
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().saveDiffParams(MessagesStorage.lastSeqValue, MessagesStorage.lastPtsValue, MessagesStorage.lastDateValue, MessagesStorage.lastQtsValue);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (MessagesStorage.lastSeqValue != res.seq) {
|
|
|
|
FileLog.e("tmessages", "need get diff messages_StatedMessage, seq: " + MessagesStorage.lastSeqValue + " " + res.seq);
|
|
|
|
if (gettingDifference || updatesStartWaitTime == 0 || updatesStartWaitTime != 0 && updatesStartWaitTime + 1500 > System.currentTimeMillis()) {
|
|
|
|
if (updatesStartWaitTime == 0) {
|
|
|
|
updatesStartWaitTime = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
FileLog.e("tmessages", "add messages_StatedMessage to queue");
|
|
|
|
UserActionUpdates updates = new UserActionUpdates();
|
|
|
|
updates.seq = res.seq;
|
|
|
|
updatesQueue.add(updates);
|
|
|
|
} else {
|
|
|
|
getDifference();
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
} else if (response instanceof TLRPC.messages_StatedMessages) {
|
2014-03-22 22:31:55 +00:00
|
|
|
TLRPC.messages_StatedMessages res = (TLRPC.messages_StatedMessages) response;
|
2013-10-25 15:19:00 +00:00
|
|
|
if (!res.messages.isEmpty()) {
|
|
|
|
TLRPC.Message message = res.messages.get(0);
|
|
|
|
newMsgObj.messageOwner.id = message.id;
|
2014-02-28 22:28:25 +00:00
|
|
|
sentMessages.add(message);
|
|
|
|
processSentMessage(newMsgObj.messageOwner, message, null, null);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2014-02-04 18:36:55 +00:00
|
|
|
if (MessagesStorage.lastSeqValue + 1 == res.seq) {
|
2013-11-04 12:31:01 +00:00
|
|
|
MessagesStorage.lastSeqValue = res.seq;
|
|
|
|
MessagesStorage.lastPtsValue = res.pts;
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().saveDiffParams(MessagesStorage.lastSeqValue, MessagesStorage.lastPtsValue, MessagesStorage.lastDateValue, MessagesStorage.lastQtsValue);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (MessagesStorage.lastSeqValue != res.seq) {
|
|
|
|
FileLog.e("tmessages", "need get diff messages_StatedMessages, seq: " + MessagesStorage.lastSeqValue + " " + res.seq);
|
|
|
|
if (gettingDifference || updatesStartWaitTime == 0 || updatesStartWaitTime != 0 && updatesStartWaitTime + 1500 > System.currentTimeMillis()) {
|
|
|
|
if (updatesStartWaitTime == 0) {
|
|
|
|
updatesStartWaitTime = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
FileLog.e("tmessages", "add messages_StatedMessages to queue");
|
|
|
|
UserActionUpdates updates = new UserActionUpdates();
|
|
|
|
updates.seq = res.seq;
|
|
|
|
updatesQueue.add(updates);
|
|
|
|
} else {
|
|
|
|
getDifference();
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().updateMessageStateAndId(newMsgObj.messageOwner.random_id, oldId, newMsgObj.messageOwner.id, 0, true);
|
2014-02-28 22:28:25 +00:00
|
|
|
if (!sentMessages.isEmpty()) {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().putMessages(sentMessages, true, true);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().storageQueue.postRunnable(new Runnable() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-03-22 22:31:55 +00:00
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
newMsgObj.messageOwner.send_state = MESSAGE_SEND_STATE_SENT;
|
|
|
|
NotificationCenter.getInstance().postNotificationName(messageReceivedByServer, oldId, newMsgObj.messageOwner.id, newMsgObj);
|
|
|
|
sendingMessages.remove(oldId);
|
|
|
|
}
|
|
|
|
});
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
sendingMessages.remove(newMsgObj.messageOwner.id);
|
|
|
|
newMsgObj.messageOwner.send_state = MESSAGE_SEND_STATE_SEND_ERROR;
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(messageSendError, newMsgObj.messageOwner.id);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, null, (req instanceof TLRPC.TL_messages_forwardMessages ? null : new RPCRequest.RPCQuickAckDelegate() {
|
|
|
|
@Override
|
|
|
|
public void quickAck() {
|
|
|
|
final int msg_id = newMsgObj.messageOwner.id;
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
newMsgObj.messageOwner.send_state = MESSAGE_SEND_STATE_SENT;
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(messageReceivedByAck, msg_id);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-02-04 18:36:55 +00:00
|
|
|
}), true, RPCRequest.RPCRequestClassGeneric | RPCRequest.RPCRequestClassFailOnServerErrors | RPCRequest.RPCRequestClassCanCompress, ConnectionsManager.DEFAULT_DATACENTER_ID);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 22:28:25 +00:00
|
|
|
private void putToDelayedMessages(String location, DelayedMessage message) {
|
|
|
|
ArrayList<DelayedMessage> arrayList = delayedMessages.get(location);
|
|
|
|
if (arrayList == null) {
|
|
|
|
arrayList = new ArrayList<DelayedMessage>();
|
|
|
|
delayedMessages.put(location, arrayList);
|
|
|
|
}
|
|
|
|
arrayList.add(message);
|
|
|
|
}
|
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
private void performSendDelayedMessage(final DelayedMessage message) {
|
2014-02-28 22:28:25 +00:00
|
|
|
if (message.type == 0) {
|
|
|
|
String location = Utilities.getCacheDir() + "/" + message.location.volume_id + "_" + message.location.local_id + ".jpg";
|
|
|
|
putToDelayedMessages(location, message);
|
|
|
|
if (message.sendRequest != null) {
|
2014-03-22 22:31:55 +00:00
|
|
|
FileLoader.getInstance().uploadFile(location, null, null);
|
2014-02-28 22:28:25 +00:00
|
|
|
} else {
|
2014-03-22 22:31:55 +00:00
|
|
|
FileLoader.getInstance().uploadFile(location, message.sendEncryptedRequest.media.key, message.sendEncryptedRequest.media.iv);
|
2014-02-28 22:28:25 +00:00
|
|
|
}
|
|
|
|
} else if (message.type == 1) {
|
|
|
|
if (message.sendRequest != null) {
|
|
|
|
if (message.sendRequest.media.thumb == null) {
|
2013-10-25 15:19:00 +00:00
|
|
|
String location = Utilities.getCacheDir() + "/" + message.location.volume_id + "_" + message.location.local_id + ".jpg";
|
2014-02-28 22:28:25 +00:00
|
|
|
putToDelayedMessages(location, message);
|
2014-03-22 22:31:55 +00:00
|
|
|
FileLoader.getInstance().uploadFile(location, null, null);
|
2014-02-28 22:28:25 +00:00
|
|
|
} else {
|
|
|
|
String location = message.videoLocation.path;
|
|
|
|
if (location == null) {
|
|
|
|
location = Utilities.getCacheDir() + "/" + message.videoLocation.id + ".mp4";
|
2014-02-11 14:32:09 +00:00
|
|
|
}
|
2014-02-28 22:28:25 +00:00
|
|
|
putToDelayedMessages(location, message);
|
2014-03-22 22:31:55 +00:00
|
|
|
FileLoader.getInstance().uploadFile(location, null, null);
|
2014-02-28 22:28:25 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
String location = message.videoLocation.path;
|
|
|
|
if (location == null) {
|
|
|
|
location = Utilities.getCacheDir() + "/" + message.videoLocation.id + ".mp4";
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2014-02-28 22:28:25 +00:00
|
|
|
putToDelayedMessages(location, message);
|
2014-03-22 22:31:55 +00:00
|
|
|
FileLoader.getInstance().uploadFile(location, message.sendEncryptedRequest.media.key, message.sendEncryptedRequest.media.iv);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2014-02-28 22:28:25 +00:00
|
|
|
} else if (message.type == 2) {
|
|
|
|
String location = message.documentLocation.path;
|
|
|
|
putToDelayedMessages(location, message);
|
|
|
|
if (message.sendRequest != null) {
|
2014-03-22 22:31:55 +00:00
|
|
|
FileLoader.getInstance().uploadFile(location, null, null);
|
2014-02-28 22:28:25 +00:00
|
|
|
} else {
|
2014-03-22 22:31:55 +00:00
|
|
|
FileLoader.getInstance().uploadFile(location, message.sendEncryptedRequest.media.key, message.sendEncryptedRequest.media.iv);
|
2014-02-28 22:28:25 +00:00
|
|
|
}
|
|
|
|
} else if (message.type == 3) {
|
|
|
|
String location = message.audioLocation.path;
|
|
|
|
putToDelayedMessages(location, message);
|
|
|
|
if (message.sendRequest != null) {
|
2014-03-22 22:31:55 +00:00
|
|
|
FileLoader.getInstance().uploadFile(location, null, null);
|
2014-02-28 22:28:25 +00:00
|
|
|
} else {
|
2014-03-22 22:31:55 +00:00
|
|
|
FileLoader.getInstance().uploadFile(location, message.sendEncryptedRequest.media.key, message.sendEncryptedRequest.media.iv);
|
2014-02-28 22:28:25 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
2014-02-28 22:28:25 +00:00
|
|
|
public void fileDidFailedUpload(final String location, final boolean enc) {
|
2013-10-25 15:19:00 +00:00
|
|
|
if (uploadingAvatar != null && uploadingAvatar.equals(location)) {
|
|
|
|
uploadingAvatar = null;
|
|
|
|
} else {
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-02-28 22:28:25 +00:00
|
|
|
ArrayList<DelayedMessage> arr = delayedMessages.get(location);
|
|
|
|
if (arr != null) {
|
|
|
|
for (int a = 0; a < arr.size(); a++) {
|
|
|
|
DelayedMessage obj = arr.get(a);
|
|
|
|
if (enc && obj.sendEncryptedRequest != null || !enc && obj.sendRequest != null) {
|
|
|
|
obj.obj.messageOwner.send_state = MESSAGE_SEND_STATE_SEND_ERROR;
|
|
|
|
sendingMessages.remove(obj.obj.messageOwner.id);
|
|
|
|
arr.remove(a);
|
|
|
|
a--;
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(messageSendError, obj.obj.messageOwner.id);
|
2014-02-28 22:28:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (arr.isEmpty()) {
|
|
|
|
delayedMessages.remove(location);
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 19:25:49 +00:00
|
|
|
public void fileDidUploaded(final String location, final TLRPC.InputFile file, final TLRPC.InputEncryptedFile encryptedFile) {
|
2013-10-25 15:19:00 +00:00
|
|
|
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();
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
2014-02-04 18:36:55 +00:00
|
|
|
if (error == null) {
|
|
|
|
TLRPC.User user = users.get(UserConfig.clientUserId);
|
|
|
|
if (user == null) {
|
|
|
|
user = UserConfig.currentUser;
|
|
|
|
users.put(user.id, user);
|
|
|
|
} else {
|
|
|
|
UserConfig.currentUser = user;
|
|
|
|
}
|
|
|
|
if (user == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
TLRPC.TL_photos_photo photo = (TLRPC.TL_photos_photo) response;
|
2014-02-04 18:36:55 +00:00
|
|
|
ArrayList<TLRPC.PhotoSize> sizes = photo.photo.sizes;
|
|
|
|
TLRPC.PhotoSize smallSize = PhotoObject.getClosestPhotoSizeWithSize(sizes, 100, 100);
|
|
|
|
TLRPC.PhotoSize bigSize = PhotoObject.getClosestPhotoSizeWithSize(sizes, 1000, 1000);
|
|
|
|
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;
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().clearUserPhotos(user.id);
|
2014-02-04 18:36:55 +00:00
|
|
|
ArrayList<TLRPC.User> users = new ArrayList<TLRPC.User>();
|
|
|
|
users.add(user);
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().putUsersAndChats(users, null, false, true);
|
2014-02-04 18:36:55 +00:00
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(updateInterfaces, UPDATE_MASK_AVATAR);
|
2014-02-04 18:36:55 +00:00
|
|
|
UserConfig.saveConfig(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
|
|
|
} else {
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-02-28 22:28:25 +00:00
|
|
|
ArrayList<DelayedMessage> arr = delayedMessages.get(location);
|
|
|
|
if (arr != null) {
|
|
|
|
for (int a = 0; a < arr.size(); a++) {
|
|
|
|
DelayedMessage message = arr.get(a);
|
|
|
|
if (file != null && message.sendRequest != null) {
|
|
|
|
if (message.type == 0) {
|
|
|
|
message.sendRequest.media.file = file;
|
|
|
|
performSendMessageRequest(message.sendRequest, message.obj);
|
|
|
|
} else if (message.type == 1) {
|
|
|
|
if (message.sendRequest.media.thumb == null) {
|
|
|
|
message.sendRequest.media.thumb = file;
|
|
|
|
performSendDelayedMessage(message);
|
|
|
|
} else {
|
|
|
|
message.sendRequest.media.file = file;
|
|
|
|
performSendMessageRequest(message.sendRequest, message.obj);
|
|
|
|
}
|
|
|
|
} else if (message.type == 2) {
|
|
|
|
message.sendRequest.media.file = file;
|
|
|
|
performSendMessageRequest(message.sendRequest, message.obj);
|
|
|
|
} else if (message.type == 3) {
|
2013-10-25 15:19:00 +00:00
|
|
|
message.sendRequest.media.file = file;
|
|
|
|
performSendMessageRequest(message.sendRequest, message.obj);
|
|
|
|
}
|
2014-02-28 22:28:25 +00:00
|
|
|
arr.remove(a);
|
|
|
|
a--;
|
|
|
|
} else if (encryptedFile != null && message.sendEncryptedRequest != null) {
|
|
|
|
performSendEncryptedRequest(message.sendEncryptedRequest, message.obj, message.encryptedChat, encryptedFile);
|
|
|
|
arr.remove(a);
|
|
|
|
a--;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-28 22:28:25 +00:00
|
|
|
if (arr.isEmpty()) {
|
|
|
|
delayedMessages.remove(location);
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-22 22:31:55 +00:00
|
|
|
public long createChat(String title, ArrayList<Integer> selectedContacts, final TLRPC.InputFile uploadedAvatar) {
|
2013-10-25 15:19:00 +00:00
|
|
|
TLRPC.TL_messages_createChat req = new TLRPC.TL_messages_createChat();
|
|
|
|
req.title = title;
|
|
|
|
for (Integer uid : selectedContacts) {
|
2013-11-04 12:31:01 +00:00
|
|
|
TLRPC.User user = users.get(uid);
|
2014-02-04 18:36:55 +00:00
|
|
|
if (user == null) {
|
|
|
|
continue;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2014-02-04 18:36:55 +00:00
|
|
|
req.users.add(getInputUser(user));
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
return ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error != null) {
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(chatDidFailCreate);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
final TLRPC.messages_StatedMessage res = (TLRPC.messages_StatedMessage) response;
|
|
|
|
MessagesStorage.getInstance().putUsersAndChats(res.users, res.chats, true, true);
|
2014-02-04 18:36:55 +00:00
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (TLRPC.User user : res.users) {
|
|
|
|
users.put(user.id, user);
|
|
|
|
if (user.id == UserConfig.clientUserId) {
|
|
|
|
UserConfig.currentUser = user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (TLRPC.Chat chat : res.chats) {
|
|
|
|
chats.put(chat.id, chat);
|
|
|
|
}
|
|
|
|
final ArrayList<MessageObject> messagesObj = new ArrayList<MessageObject>();
|
|
|
|
messagesObj.add(new MessageObject(res.message, users));
|
|
|
|
TLRPC.Chat chat = res.chats.get(0);
|
|
|
|
updateInterfaceWithMessages(-chat.id, messagesObj);
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(chatDidCreated, chat.id);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
|
2013-10-25 15:19:00 +00:00
|
|
|
if (uploadedAvatar != null) {
|
|
|
|
changeChatAvatar(chat.id, uploadedAvatar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-02-04 18:36:55 +00:00
|
|
|
final ArrayList<TLRPC.Message> messages = new ArrayList<TLRPC.Message>();
|
|
|
|
messages.add(res.message);
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().putMessages(messages, true, true);
|
2014-02-04 18:36:55 +00:00
|
|
|
if (MessagesStorage.lastSeqValue + 1 == res.seq) {
|
|
|
|
MessagesStorage.lastSeqValue = res.seq;
|
|
|
|
MessagesStorage.lastPtsValue = res.pts;
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().saveDiffParams(MessagesStorage.lastSeqValue, MessagesStorage.lastPtsValue, MessagesStorage.lastDateValue, MessagesStorage.lastQtsValue);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (MessagesStorage.lastSeqValue != res.seq) {
|
|
|
|
FileLog.e("tmessages", "need get diff TL_messages_createChat, seq: " + MessagesStorage.lastSeqValue + " " + res.seq);
|
|
|
|
if (gettingDifference || updatesStartWaitTime == 0 || updatesStartWaitTime != 0 && updatesStartWaitTime + 1500 > System.currentTimeMillis()) {
|
|
|
|
if (updatesStartWaitTime == 0) {
|
|
|
|
updatesStartWaitTime = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
FileLog.e("tmessages", "add TL_messages_createChat to queue");
|
|
|
|
UserActionUpdates updates = new UserActionUpdates();
|
|
|
|
updates.seq = res.seq;
|
|
|
|
updatesQueue.add(updates);
|
|
|
|
} else {
|
|
|
|
getDifference();
|
|
|
|
}
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
|
|
|
}
|
|
|
|
|
2014-02-04 18:36:55 +00:00
|
|
|
public void addUserToChat(int chat_id, final TLRPC.User user, final TLRPC.ChatParticipants info) {
|
|
|
|
if (user == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
TLRPC.TL_messages_addChatUser req = new TLRPC.TL_messages_addChatUser();
|
|
|
|
req.chat_id = chat_id;
|
|
|
|
req.fwd_limit = 50;
|
2014-02-04 18:36:55 +00:00
|
|
|
req.user_id = getInputUser(user);
|
|
|
|
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-22 22:31:55 +00:00
|
|
|
final TLRPC.messages_StatedMessage res = (TLRPC.messages_StatedMessage) response;
|
|
|
|
MessagesStorage.getInstance().putUsersAndChats(res.users, res.chats, true, true);
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (TLRPC.User user : res.users) {
|
|
|
|
users.put(user.id, user);
|
|
|
|
if (user.id == UserConfig.clientUserId) {
|
|
|
|
UserConfig.currentUser = user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (TLRPC.Chat chat : res.chats) {
|
|
|
|
chats.put(chat.id, chat);
|
|
|
|
}
|
|
|
|
final ArrayList<MessageObject> messagesObj = new ArrayList<MessageObject>();
|
|
|
|
messagesObj.add(new MessageObject(res.message, users));
|
|
|
|
TLRPC.Chat chat = res.chats.get(0);
|
|
|
|
chats.put(chat.id, chat);
|
|
|
|
updateInterfaceWithMessages(-chat.id, messagesObj);
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(updateInterfaces, UPDATE_MASK_CHAT_MEMBERS);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
if (info != null) {
|
|
|
|
for (TLRPC.TL_chatParticipant p : info.participants) {
|
2014-02-04 18:36:55 +00:00
|
|
|
if (p.user_id == user.id) {
|
2013-10-25 15:19:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TLRPC.TL_chatParticipant newPart = new TLRPC.TL_chatParticipant();
|
2014-02-04 18:36:55 +00:00
|
|
|
newPart.user_id = user.id;
|
2013-10-25 15:19:00 +00:00
|
|
|
newPart.inviter_id = UserConfig.clientUserId;
|
2014-03-22 22:31:55 +00:00
|
|
|
newPart.date = ConnectionsManager.getInstance().getCurrentTime();
|
2013-10-25 15:19:00 +00:00
|
|
|
info.participants.add(0, newPart);
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().updateChatInfo(info.chat_id, info, true);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(chatInfoDidLoaded, info.chat_id, info);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-02-04 18:36:55 +00:00
|
|
|
|
|
|
|
final ArrayList<TLRPC.Message> messages = new ArrayList<TLRPC.Message>();
|
|
|
|
messages.add(res.message);
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().putMessages(messages, true, true);
|
2014-02-04 18:36:55 +00:00
|
|
|
if (MessagesStorage.lastSeqValue + 1 == res.seq) {
|
|
|
|
MessagesStorage.lastSeqValue = res.seq;
|
|
|
|
MessagesStorage.lastPtsValue = res.pts;
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().saveDiffParams(MessagesStorage.lastSeqValue, MessagesStorage.lastPtsValue, MessagesStorage.lastDateValue, MessagesStorage.lastQtsValue);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (MessagesStorage.lastSeqValue != res.seq) {
|
|
|
|
FileLog.e("tmessages", "need get diff TL_messages_addChatUser, seq: " + MessagesStorage.lastSeqValue + " " + res.seq);
|
|
|
|
if (gettingDifference || updatesStartWaitTime == 0 || updatesStartWaitTime != 0 && updatesStartWaitTime + 1500 > System.currentTimeMillis()) {
|
|
|
|
if (updatesStartWaitTime == 0) {
|
|
|
|
updatesStartWaitTime = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
FileLog.e("tmessages", "add TL_messages_addChatUser to queue");
|
|
|
|
UserActionUpdates updates = new UserActionUpdates();
|
|
|
|
updates.seq = res.seq;
|
|
|
|
updatesQueue.add(updates);
|
|
|
|
} else {
|
|
|
|
getDifference();
|
|
|
|
}
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
|
|
|
}
|
|
|
|
|
2014-02-04 18:36:55 +00:00
|
|
|
public void deleteUserFromChat(int chat_id, final TLRPC.User user, final TLRPC.ChatParticipants info) {
|
|
|
|
if (user == null) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
TLRPC.TL_messages_deleteChatUser req = new TLRPC.TL_messages_deleteChatUser();
|
|
|
|
req.chat_id = chat_id;
|
2014-02-04 18:36:55 +00:00
|
|
|
req.user_id = getInputUser(user);
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error != null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
final TLRPC.messages_StatedMessage res = (TLRPC.messages_StatedMessage) response;
|
|
|
|
MessagesStorage.getInstance().putUsersAndChats(res.users, res.chats, true, true);
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (TLRPC.User user : res.users) {
|
|
|
|
users.put(user.id, user);
|
|
|
|
if (user.id == UserConfig.clientUserId) {
|
|
|
|
UserConfig.currentUser = user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (TLRPC.Chat chat : res.chats) {
|
|
|
|
chats.put(chat.id, chat);
|
|
|
|
}
|
2014-02-04 18:36:55 +00:00
|
|
|
if (user.id != UserConfig.clientUserId) {
|
2013-10-25 15:19:00 +00:00
|
|
|
final ArrayList<MessageObject> messagesObj = new ArrayList<MessageObject>();
|
|
|
|
messagesObj.add(new MessageObject(res.message, users));
|
|
|
|
TLRPC.Chat chat = res.chats.get(0);
|
|
|
|
chats.put(chat.id, chat);
|
|
|
|
updateInterfaceWithMessages(-chat.id, messagesObj);
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(updateInterfaces, UPDATE_MASK_CHAT_MEMBERS);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
boolean changed = false;
|
|
|
|
if (info != null) {
|
|
|
|
for (int a = 0; a < info.participants.size(); a++) {
|
|
|
|
TLRPC.TL_chatParticipant p = info.participants.get(a);
|
2014-02-04 18:36:55 +00:00
|
|
|
if (p.user_id == user.id) {
|
2013-10-25 15:19:00 +00:00
|
|
|
info.participants.remove(a);
|
|
|
|
changed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (changed) {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().updateChatInfo(info.chat_id, info, true);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(chatInfoDidLoaded, info.chat_id, info);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-02-04 18:36:55 +00:00
|
|
|
|
|
|
|
if (user.id != UserConfig.clientUserId) {
|
|
|
|
final ArrayList<TLRPC.Message> messages = new ArrayList<TLRPC.Message>();
|
|
|
|
messages.add(res.message);
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().putMessages(messages, true, true);
|
2014-02-04 18:36:55 +00:00
|
|
|
}
|
|
|
|
if (MessagesStorage.lastSeqValue + 1 == res.seq) {
|
|
|
|
MessagesStorage.lastSeqValue = res.seq;
|
|
|
|
MessagesStorage.lastPtsValue = res.pts;
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().saveDiffParams(MessagesStorage.lastSeqValue, MessagesStorage.lastPtsValue, MessagesStorage.lastDateValue, MessagesStorage.lastQtsValue);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (MessagesStorage.lastSeqValue != res.seq) {
|
|
|
|
FileLog.e("tmessages", "need get diff TL_messages_deleteChatUser, seq: " + MessagesStorage.lastSeqValue + " " + res.seq);
|
|
|
|
if (gettingDifference || updatesStartWaitTime == 0 || updatesStartWaitTime != 0 && updatesStartWaitTime + 1500 > System.currentTimeMillis()) {
|
|
|
|
if (updatesStartWaitTime == 0) {
|
|
|
|
updatesStartWaitTime = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
FileLog.e("tmessages", "add TL_messages_deleteChatUser to queue");
|
|
|
|
UserActionUpdates updates = new UserActionUpdates();
|
|
|
|
updates.seq = res.seq;
|
|
|
|
updatesQueue.add(updates);
|
|
|
|
} else {
|
|
|
|
getDifference();
|
|
|
|
}
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void changeChatTitle(int chat_id, String title) {
|
|
|
|
TLRPC.TL_messages_editChatTitle req = new TLRPC.TL_messages_editChatTitle();
|
|
|
|
req.chat_id = chat_id;
|
|
|
|
req.title = title;
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error != null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
final TLRPC.messages_StatedMessage res = (TLRPC.messages_StatedMessage) response;
|
|
|
|
MessagesStorage.getInstance().putUsersAndChats(res.users, res.chats, true, true);
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (TLRPC.User user : res.users) {
|
|
|
|
users.put(user.id, user);
|
|
|
|
if (user.id == UserConfig.clientUserId) {
|
|
|
|
UserConfig.currentUser = user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (TLRPC.Chat chat : res.chats) {
|
|
|
|
chats.put(chat.id, chat);
|
|
|
|
}
|
|
|
|
final ArrayList<MessageObject> messagesObj = new ArrayList<MessageObject>();
|
|
|
|
messagesObj.add(new MessageObject(res.message, users));
|
|
|
|
TLRPC.Chat chat = res.chats.get(0);
|
|
|
|
chats.put(chat.id, chat);
|
|
|
|
updateInterfaceWithMessages(-chat.id, messagesObj);
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(updateInterfaces, UPDATE_MASK_CHAT_NAME);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
2014-02-04 18:36:55 +00:00
|
|
|
|
|
|
|
final ArrayList<TLRPC.Message> messages = new ArrayList<TLRPC.Message>();
|
|
|
|
messages.add(res.message);
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().putMessages(messages, true, true);
|
2014-02-04 18:36:55 +00:00
|
|
|
if (MessagesStorage.lastSeqValue + 1 == res.seq) {
|
|
|
|
MessagesStorage.lastSeqValue = res.seq;
|
|
|
|
MessagesStorage.lastPtsValue = res.pts;
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().saveDiffParams(MessagesStorage.lastSeqValue, MessagesStorage.lastPtsValue, MessagesStorage.lastDateValue, MessagesStorage.lastQtsValue);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (MessagesStorage.lastSeqValue != res.seq) {
|
|
|
|
FileLog.e("tmessages", "need get diff TL_messages_editChatTitle, seq: " + MessagesStorage.lastSeqValue + " " + res.seq);
|
|
|
|
if (gettingDifference || updatesStartWaitTime == 0 || updatesStartWaitTime != 0 && updatesStartWaitTime + 1500 > System.currentTimeMillis()) {
|
|
|
|
if (updatesStartWaitTime == 0) {
|
|
|
|
updatesStartWaitTime = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
FileLog.e("tmessages", "add TL_messages_editChatTitle to queue");
|
|
|
|
UserActionUpdates updates = new UserActionUpdates();
|
|
|
|
updates.seq = res.seq;
|
|
|
|
updatesQueue.add(updates);
|
|
|
|
} else {
|
|
|
|
getDifference();
|
|
|
|
}
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
|
|
|
}
|
|
|
|
|
2013-12-20 19:25:49 +00:00
|
|
|
public void changeChatAvatar(int chat_id, TLRPC.InputFile uploadedAvatar) {
|
2013-10-25 15:19:00 +00: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 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req2, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error != null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
final TLRPC.messages_StatedMessage res = (TLRPC.messages_StatedMessage) response;
|
|
|
|
MessagesStorage.getInstance().putUsersAndChats(res.users, res.chats, true, true);
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (TLRPC.User user : res.users) {
|
|
|
|
users.put(user.id, user);
|
|
|
|
if (user.id == UserConfig.clientUserId) {
|
|
|
|
UserConfig.currentUser = user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (TLRPC.Chat chat : res.chats) {
|
|
|
|
chats.put(chat.id, chat);
|
|
|
|
}
|
|
|
|
final ArrayList<MessageObject> messagesObj = new ArrayList<MessageObject>();
|
|
|
|
messagesObj.add(new MessageObject(res.message, users));
|
|
|
|
TLRPC.Chat chat = res.chats.get(0);
|
|
|
|
chats.put(chat.id, chat);
|
|
|
|
updateInterfaceWithMessages(-chat.id, messagesObj);
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(updateInterfaces, UPDATE_MASK_CHAT_AVATAR);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
2014-02-04 18:36:55 +00:00
|
|
|
|
|
|
|
final ArrayList<TLRPC.Message> messages = new ArrayList<TLRPC.Message>();
|
|
|
|
messages.add(res.message);
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().putMessages(messages, true, true);
|
2014-02-04 18:36:55 +00:00
|
|
|
if (MessagesStorage.lastSeqValue + 1 == res.seq) {
|
|
|
|
MessagesStorage.lastSeqValue = res.seq;
|
|
|
|
MessagesStorage.lastPtsValue = res.pts;
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().saveDiffParams(MessagesStorage.lastSeqValue, MessagesStorage.lastPtsValue, MessagesStorage.lastDateValue, MessagesStorage.lastQtsValue);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (MessagesStorage.lastSeqValue != res.seq) {
|
|
|
|
FileLog.e("tmessages", "need get diff TL_messages_editChatPhoto, seq: " + MessagesStorage.lastSeqValue + " " + res.seq);
|
|
|
|
if (gettingDifference || updatesStartWaitTime == 0 || updatesStartWaitTime != 0 && updatesStartWaitTime + 1500 > System.currentTimeMillis()) {
|
|
|
|
if (updatesStartWaitTime == 0) {
|
|
|
|
updatesStartWaitTime = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
FileLog.e("tmessages", "add TL_messages_editChatPhoto to queue");
|
|
|
|
UserActionUpdates updates = new UserActionUpdates();
|
|
|
|
updates.seq = res.seq;
|
|
|
|
updatesQueue.add(updates);
|
|
|
|
} else {
|
|
|
|
getDifference();
|
|
|
|
}
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void unregistedPush() {
|
2014-02-28 22:28:25 +00: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 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2014-02-28 22:28:25 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
2014-02-04 18:36:55 +00:00
|
|
|
|
2014-02-28 22:28:25 +00:00
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2014-02-28 22:28:25 +00:00
|
|
|
|
|
|
|
TLRPC.TL_auth_logOut req2 = new TLRPC.TL_auth_logOut();
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req2, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void registerForPush(final String regid) {
|
|
|
|
if (regid == null || regid.length() == 0 || registeringForPush || UserConfig.clientUserId == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (UserConfig.registeredForPush && regid.equals(UserConfig.pushString)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
registeringForPush = true;
|
|
|
|
TLRPC.TL_account_registerDevice req = new TLRPC.TL_account_registerDevice();
|
2013-12-20 19:25:49 +00:00
|
|
|
req.token_type = 2;
|
|
|
|
req.token = regid;
|
|
|
|
req.app_sandbox = false;
|
2013-10-25 15:19:00 +00:00
|
|
|
try {
|
|
|
|
req.lang_code = Locale.getDefault().getCountry();
|
|
|
|
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 12:31:01 +00:00
|
|
|
PackageInfo pInfo = ApplicationLoader.applicationContext.getPackageManager().getPackageInfo(ApplicationLoader.applicationContext.getPackageName(), 0);
|
2013-10-25 15:19:00 +00:00
|
|
|
req.app_version = pInfo.versionName;
|
|
|
|
if (req.app_version == null) {
|
|
|
|
req.app_version = "App version unknown";
|
|
|
|
}
|
2013-12-20 19:25:49 +00:00
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
} catch (Exception e) {
|
2013-12-20 19:25:49 +00: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 15:19:00 +00:00
|
|
|
}
|
2013-12-26 16:46:13 +00: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 15:19:00 +00:00
|
|
|
if (req.app_version != null) {
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error == null) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", "registered for push");
|
2013-10-25 15:19:00 +00:00
|
|
|
UserConfig.registeredForPush = true;
|
|
|
|
UserConfig.pushString = regid;
|
2013-11-04 12:31:01 +00:00
|
|
|
UserConfig.saveConfig(false);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
registeringForPush = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void loadCurrentState() {
|
|
|
|
if (updatingState) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
updatingState = true;
|
|
|
|
TLRPC.TL_updates_getState req = new TLRPC.TL_updates_getState();
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
updatingState = false;
|
|
|
|
if (error == null) {
|
2014-03-22 22:31:55 +00:00
|
|
|
TLRPC.TL_updates_state res = (TLRPC.TL_updates_state) response;
|
2013-11-04 12:31:01 +00:00
|
|
|
MessagesStorage.lastDateValue = res.date;
|
|
|
|
MessagesStorage.lastPtsValue = res.pts;
|
|
|
|
MessagesStorage.lastSeqValue = res.seq;
|
|
|
|
MessagesStorage.lastQtsValue = res.qts;
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().saveDiffParams(MessagesStorage.lastSeqValue, MessagesStorage.lastPtsValue, MessagesStorage.lastDateValue, MessagesStorage.lastQtsValue);
|
2013-10-25 15:19:00 +00:00
|
|
|
} else {
|
2014-02-04 18:36:55 +00:00
|
|
|
if (error.code != 401) {
|
|
|
|
loadCurrentState();
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
|
|
|
}
|
|
|
|
|
2014-02-04 18:36:55 +00:00
|
|
|
private int getUpdateSeq(TLRPC.Updates updates) {
|
|
|
|
if (updates instanceof TLRPC.TL_updatesCombined) {
|
|
|
|
return updates.seq_start;
|
|
|
|
} else {
|
|
|
|
return updates.seq;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void processUpdatesQueue(boolean getDifference) {
|
|
|
|
if (!updatesQueue.isEmpty()) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
boolean anyProceed = false;
|
|
|
|
for (int a = 0; a < updatesQueue.size(); a++) {
|
|
|
|
TLRPC.Updates updates = updatesQueue.get(a);
|
|
|
|
int seq = getUpdateSeq(updates);
|
|
|
|
if (MessagesStorage.lastSeqValue + 1 == seq || MessagesStorage.lastSeqValue == seq) {
|
|
|
|
processUpdates(updates, true);
|
|
|
|
anyProceed = true;
|
|
|
|
updatesQueue.remove(a);
|
|
|
|
a--;
|
|
|
|
} else if (MessagesStorage.lastSeqValue < seq) {
|
|
|
|
if (updatesStartWaitTime != 0 && (anyProceed || updatesStartWaitTime + 1500 > System.currentTimeMillis())) {
|
|
|
|
FileLog.e("tmessages", "HOLE IN UPDATES QUEUE - will wait more time");
|
|
|
|
if (anyProceed) {
|
|
|
|
updatesStartWaitTime = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
FileLog.e("tmessages", "HOLE IN UPDATES QUEUE - getDifference");
|
|
|
|
updatesStartWaitTime = 0;
|
|
|
|
updatesQueue.clear();
|
|
|
|
getDifference();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
updatesQueue.remove(a);
|
|
|
|
a--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updatesQueue.clear();
|
|
|
|
FileLog.e("tmessages", "UPDATES QUEUE PROCEED - OK");
|
|
|
|
updatesStartWaitTime = 0;
|
|
|
|
if (getDifference) {
|
2014-03-22 22:31:55 +00:00
|
|
|
final int stateCopy = ConnectionsManager.getInstance().connectionState;
|
2014-02-04 18:36:55 +00:00
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(703, stateCopy);
|
2014-02-04 18:36:55 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (getDifference) {
|
2014-03-22 22:31:55 +00:00
|
|
|
final int stateCopy = ConnectionsManager.getInstance().connectionState;
|
2014-02-04 18:36:55 +00:00
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(703, stateCopy);
|
2014-02-04 18:36:55 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
updatesStartWaitTime = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
public void getDifference() {
|
|
|
|
registerForPush(UserConfig.pushString);
|
2013-11-04 12:31:01 +00:00
|
|
|
if (MessagesStorage.lastDateValue == 0) {
|
2013-10-25 15:19:00 +00: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 12:31:01 +00:00
|
|
|
req.pts = MessagesStorage.lastPtsValue;
|
|
|
|
req.date = MessagesStorage.lastDateValue;
|
|
|
|
req.qts = MessagesStorage.lastQtsValue;
|
2014-02-04 18:36:55 +00:00
|
|
|
FileLog.e("tmessages", "start getDifference with date = " + MessagesStorage.lastDateValue + " pts = " + MessagesStorage.lastPtsValue + " seq = " + MessagesStorage.lastSeqValue);
|
2014-03-22 22:31:55 +00:00
|
|
|
if (ConnectionsManager.getInstance().connectionState == 0) {
|
|
|
|
ConnectionsManager.getInstance().connectionState = 3;
|
|
|
|
final int stateCopy = ConnectionsManager.getInstance().connectionState;
|
2013-10-25 15:19:00 +00:00
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(703, stateCopy);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
2013-12-20 19:25:49 +00:00
|
|
|
gettingDifferenceAgain = false;
|
2013-10-25 15:19:00 +00:00
|
|
|
if (error == null) {
|
2014-03-22 22:31:55 +00:00
|
|
|
final TLRPC.updates_Difference res = (TLRPC.updates_Difference) response;
|
2013-12-20 19:25:49 +00:00
|
|
|
gettingDifferenceAgain = res instanceof TLRPC.TL_updates_differenceSlice;
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
final HashMap<Integer, TLRPC.User> usersDict = new HashMap<Integer, TLRPC.User>();
|
|
|
|
for (TLRPC.User user : res.users) {
|
|
|
|
usersDict.put(user.id, user);
|
|
|
|
}
|
|
|
|
|
2014-03-22 22:31:55 +00:00
|
|
|
final ArrayList<Integer> readMessages = new ArrayList<Integer>();
|
2013-10-25 15:19:00 +00:00
|
|
|
final ArrayList<TLRPC.TL_updateMessageID> msgUpdates = new ArrayList<TLRPC.TL_updateMessageID>();
|
|
|
|
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 22:31:55 +00:00
|
|
|
msgUpdates.add((TLRPC.TL_updateMessageID) upd);
|
2013-10-25 15:19:00 +00:00
|
|
|
res.other_updates.remove(a);
|
|
|
|
a--;
|
2014-03-22 22:31:55 +00:00
|
|
|
} else if (upd instanceof TLRPC.TL_updateReadMessages) {
|
|
|
|
readMessages.addAll(upd.messages);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-11 14:32:09 +00:00
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (TLRPC.User user : res.users) {
|
|
|
|
users.put(user.id, user);
|
|
|
|
if (user.id == UserConfig.clientUserId) {
|
|
|
|
UserConfig.currentUser = user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (TLRPC.Chat chat : res.chats) {
|
|
|
|
chats.put(chat.id, chat);
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
|
|
|
|
if (currentPushMessage != null && readMessages.contains(currentPushMessage.messageOwner.id)) {
|
|
|
|
NotificationManager mNotificationManager = (NotificationManager) ApplicationLoader.applicationContext.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
mNotificationManager.cancel(1);
|
|
|
|
currentPushMessage = null;
|
|
|
|
}
|
2014-02-11 14:32:09 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().storageQueue.postRunnable(new Runnable() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (!msgUpdates.isEmpty()) {
|
|
|
|
final HashMap<Integer, Integer> corrected = new HashMap<Integer, Integer>();
|
|
|
|
for (TLRPC.TL_updateMessageID update : msgUpdates) {
|
2014-03-22 22:31:55 +00:00
|
|
|
Integer oldId = MessagesStorage.getInstance().updateMessageStateAndId(update.random_id, null, update.id, 0, false);
|
2013-10-25 15:19:00 +00:00
|
|
|
if (oldId != null) {
|
|
|
|
corrected.put(oldId, update.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!corrected.isEmpty()) {
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (HashMap.Entry<Integer, Integer> entry : corrected.entrySet()) {
|
|
|
|
Integer oldId = entry.getKey();
|
|
|
|
sendingMessages.remove(oldId);
|
|
|
|
Integer newId = entry.getValue();
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(messageReceivedByServer, oldId, newId, null);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Utilities.stageQueue.postRunnable(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (!res.new_messages.isEmpty() || !res.new_encrypted_messages.isEmpty()) {
|
|
|
|
final HashMap<Long, ArrayList<MessageObject>> messages = new HashMap<Long, ArrayList<MessageObject>>();
|
|
|
|
for (TLRPC.EncryptedMessage encryptedMessage : res.new_encrypted_messages) {
|
|
|
|
TLRPC.Message message = decryptMessage(encryptedMessage);
|
|
|
|
if (message != null) {
|
|
|
|
res.new_messages.add(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-20 19:25:49 +00:00
|
|
|
MessageObject lastMessage = null;
|
2013-10-25 15:19:00 +00:00
|
|
|
for (TLRPC.Message message : res.new_messages) {
|
|
|
|
MessageObject obj = new MessageObject(message, usersDict);
|
2013-12-20 19:25:49 +00: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-04 18:36:55 +00:00
|
|
|
if (!(res instanceof TLRPC.TL_updates_differenceSlice)) {
|
|
|
|
if ((dialog_id != openned_dialog_id || ApplicationLoader.lastPauseTime != 0) && !obj.messageOwner.out && obj.messageOwner.unread && (lastMessage == null || lastMessage.messageOwner.date < obj.messageOwner.date)) {
|
2014-03-22 22:31:55 +00:00
|
|
|
if (!readMessages.contains(obj.messageOwner.id)) {
|
|
|
|
lastMessage = obj;
|
|
|
|
}
|
2014-02-04 18:36:55 +00:00
|
|
|
}
|
2013-12-20 19:25:49 +00:00
|
|
|
}
|
2013-10-25 15:19:00 +00: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 {
|
|
|
|
if (message.to_id.user_id == UserConfig.clientUserId) {
|
|
|
|
message.to_id.user_id = message.from_id;
|
|
|
|
}
|
|
|
|
uid = message.to_id.user_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ArrayList<MessageObject> arr = messages.get(uid);
|
|
|
|
if (arr == null) {
|
|
|
|
arr = new ArrayList<MessageObject>();
|
|
|
|
messages.put(uid, arr);
|
|
|
|
}
|
|
|
|
arr.add(obj);
|
|
|
|
}
|
|
|
|
|
2014-03-22 22:31:55 +00:00
|
|
|
processPendingEncMessages();
|
|
|
|
|
2013-12-20 19:25:49 +00:00
|
|
|
final MessageObject object = lastMessage;
|
2013-10-25 15:19:00 +00:00
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@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-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
|
2013-12-20 19:25:49 +00:00
|
|
|
if (object != null) {
|
|
|
|
showInAppNotification(object);
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().storageQueue.postRunnable(new Runnable() {
|
2014-02-04 18:36:55 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().startTransaction(false);
|
|
|
|
MessagesStorage.getInstance().putMessages(res.new_messages, false, false);
|
|
|
|
MessagesStorage.getInstance().putUsersAndChats(res.users, res.chats, false, false);
|
|
|
|
MessagesStorage.getInstance().commitTransaction(false);
|
2014-02-04 18:36:55 +00:00
|
|
|
}
|
|
|
|
});
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (res != null && !res.other_updates.isEmpty()) {
|
2013-12-20 19:25:49 +00:00
|
|
|
processUpdateArray(res.other_updates, res.users, res.chats);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
2014-02-04 18:36:55 +00:00
|
|
|
gettingDifference = false;
|
2013-10-25 15:19:00 +00:00
|
|
|
if (res instanceof TLRPC.TL_updates_difference) {
|
2013-11-04 12:31:01 +00:00
|
|
|
MessagesStorage.lastSeqValue = res.state.seq;
|
|
|
|
MessagesStorage.lastDateValue = res.state.date;
|
|
|
|
MessagesStorage.lastPtsValue = res.state.pts;
|
|
|
|
MessagesStorage.lastQtsValue = res.state.qts;
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().connectionState = 0;
|
2014-02-04 18:36:55 +00:00
|
|
|
processUpdatesQueue(true);
|
2013-10-25 15:19:00 +00:00
|
|
|
} else if (res instanceof TLRPC.TL_updates_differenceSlice) {
|
2013-11-04 12:31:01 +00:00
|
|
|
MessagesStorage.lastSeqValue = res.intermediate_state.seq;
|
|
|
|
MessagesStorage.lastDateValue = res.intermediate_state.date;
|
|
|
|
MessagesStorage.lastPtsValue = res.intermediate_state.pts;
|
|
|
|
MessagesStorage.lastQtsValue = res.intermediate_state.qts;
|
2013-12-20 19:25:49 +00:00
|
|
|
gettingDifferenceAgain = true;
|
2013-10-25 15:19:00 +00:00
|
|
|
getDifference();
|
|
|
|
} else if (res instanceof TLRPC.TL_updates_differenceEmpty) {
|
2013-11-04 12:31:01 +00:00
|
|
|
MessagesStorage.lastSeqValue = res.seq;
|
|
|
|
MessagesStorage.lastDateValue = res.date;
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().connectionState = 0;
|
2014-02-04 18:36:55 +00:00
|
|
|
processUpdatesQueue(true);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().saveDiffParams(MessagesStorage.lastSeqValue, MessagesStorage.lastPtsValue, MessagesStorage.lastDateValue, MessagesStorage.lastQtsValue);
|
2014-02-04 18:36:55 +00: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 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2014-02-04 18:36:55 +00:00
|
|
|
gettingDifference = false;
|
2013-10-25 15:19:00 +00:00
|
|
|
loadCurrentState();
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", "get difference error, don't know what to do :(");
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
|
|
|
}
|
|
|
|
|
2014-02-04 18:36:55 +00:00
|
|
|
public void processUpdates(final TLRPC.Updates updates, boolean fromQueue) {
|
2013-10-25 15:19:00 +00:00
|
|
|
boolean needGetDiff = false;
|
|
|
|
boolean needReceivedQueue = false;
|
2014-02-04 18:36:55 +00:00
|
|
|
boolean addedToQueue = false;
|
2013-10-25 15:19:00 +00:00
|
|
|
if (updates instanceof TLRPC.TL_updateShort) {
|
|
|
|
ArrayList<TLRPC.Update> arr = new ArrayList<TLRPC.Update>();
|
|
|
|
arr.add(updates.update);
|
|
|
|
processUpdateArray(arr, null, null);
|
|
|
|
} else if (updates instanceof TLRPC.TL_updateShortChatMessage) {
|
2014-02-04 18:36:55 +00:00
|
|
|
boolean missingData = chats.get(updates.chat_id) == null || users.get(updates.from_id) == null;
|
|
|
|
if (MessagesStorage.lastSeqValue + 1 == updates.seq && !missingData) {
|
2013-10-25 15:19:00 +00:00
|
|
|
TLRPC.TL_message message = new TLRPC.TL_message();
|
|
|
|
message.from_id = updates.from_id;
|
|
|
|
message.id = updates.id;
|
|
|
|
message.to_id = new TLRPC.TL_peerChat();
|
|
|
|
message.to_id.chat_id = updates.chat_id;
|
|
|
|
message.message = updates.message;
|
|
|
|
message.date = updates.date;
|
|
|
|
message.unread = true;
|
|
|
|
message.media = new TLRPC.TL_messageMediaEmpty();
|
2013-11-04 12:31:01 +00:00
|
|
|
MessagesStorage.lastSeqValue = updates.seq;
|
|
|
|
MessagesStorage.lastPtsValue = updates.pts;
|
2013-10-25 15:19:00 +00:00
|
|
|
final MessageObject obj = new MessageObject(message, null);
|
|
|
|
final ArrayList<MessageObject> objArr = new ArrayList<MessageObject>();
|
|
|
|
objArr.add(obj);
|
|
|
|
ArrayList<TLRPC.Message> arr = new ArrayList<TLRPC.Message>();
|
|
|
|
arr.add(message);
|
|
|
|
final boolean printUpdate = updatePrintingUsersWithNewMessages(-updates.chat_id, objArr);
|
|
|
|
if (printUpdate) {
|
|
|
|
updatePrintingStrings();
|
|
|
|
}
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (printUpdate) {
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(updateInterfaces, UPDATE_MASK_USER_PRINT);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
if (obj.messageOwner.from_id != UserConfig.clientUserId) {
|
|
|
|
long dialog_id;
|
|
|
|
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;
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
if (dialog_id != openned_dialog_id || ApplicationLoader.lastPauseTime != 0 || !isScreenOn) {
|
2013-10-25 15:19:00 +00:00
|
|
|
showInAppNotification(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updateInterfaceWithMessages(-updates.chat_id, objArr);
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().putMessages(arr, false, true);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (!missingData && MessagesStorage.lastSeqValue != updates.seq) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", "need get diff TL_updateShortChatMessage, seq: " + MessagesStorage.lastSeqValue + " " + updates.seq);
|
2014-02-04 18:36:55 +00:00
|
|
|
if (gettingDifference || updatesStartWaitTime == 0 || updatesStartWaitTime != 0 && updatesStartWaitTime + 1500 > System.currentTimeMillis()) {
|
|
|
|
if (updatesStartWaitTime == 0) {
|
|
|
|
updatesStartWaitTime = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
FileLog.e("tmessages", "add TL_updateShortChatMessage to queue");
|
|
|
|
updatesQueue.add(updates);
|
|
|
|
addedToQueue = true;
|
|
|
|
} else {
|
|
|
|
needGetDiff = true;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
} else if (updates instanceof TLRPC.TL_updateShortMessage) {
|
2014-02-04 18:36:55 +00:00
|
|
|
boolean missingData = users.get(updates.from_id) == null;
|
|
|
|
if (MessagesStorage.lastSeqValue + 1 == updates.seq && !missingData) {
|
2013-10-25 15:19:00 +00:00
|
|
|
TLRPC.TL_message message = new TLRPC.TL_message();
|
|
|
|
message.from_id = updates.from_id;
|
|
|
|
message.id = updates.id;
|
|
|
|
message.to_id = new TLRPC.TL_peerUser();
|
|
|
|
message.to_id.user_id = updates.from_id;
|
|
|
|
message.message = updates.message;
|
|
|
|
message.date = updates.date;
|
|
|
|
message.unread = true;
|
|
|
|
message.media = new TLRPC.TL_messageMediaEmpty();
|
2013-11-04 12:31:01 +00:00
|
|
|
MessagesStorage.lastSeqValue = updates.seq;
|
|
|
|
MessagesStorage.lastPtsValue = updates.pts;
|
|
|
|
MessagesStorage.lastDateValue = updates.date;
|
2013-10-25 15:19:00 +00:00
|
|
|
final MessageObject obj = new MessageObject(message, null);
|
|
|
|
final ArrayList<MessageObject> objArr = new ArrayList<MessageObject>();
|
|
|
|
objArr.add(obj);
|
|
|
|
ArrayList<TLRPC.Message> arr = new ArrayList<TLRPC.Message>();
|
|
|
|
arr.add(message);
|
|
|
|
final boolean printUpdate = updatePrintingUsersWithNewMessages(updates.from_id, objArr);
|
|
|
|
if (printUpdate) {
|
|
|
|
updatePrintingStrings();
|
|
|
|
}
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (printUpdate) {
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(updateInterfaces, UPDATE_MASK_USER_PRINT);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
if (obj.messageOwner.from_id != UserConfig.clientUserId) {
|
|
|
|
long dialog_id;
|
|
|
|
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;
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
if (dialog_id != openned_dialog_id || ApplicationLoader.lastPauseTime != 0 || !isScreenOn) {
|
2013-10-25 15:19:00 +00:00
|
|
|
showInAppNotification(obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
updateInterfaceWithMessages(updates.from_id, objArr);
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().putMessages(arr, false, true);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (!missingData && MessagesStorage.lastSeqValue != updates.seq) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", "need get diff TL_updateShortMessage, seq: " + MessagesStorage.lastSeqValue + " " + updates.seq);
|
2014-02-04 18:36:55 +00:00
|
|
|
if (gettingDifference || updatesStartWaitTime == 0 || updatesStartWaitTime != 0 && updatesStartWaitTime + 1500 > System.currentTimeMillis()) {
|
|
|
|
if (updatesStartWaitTime == 0) {
|
|
|
|
updatesStartWaitTime = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
FileLog.e("tmessages", "add TL_updateShortMessage to queue");
|
|
|
|
updatesQueue.add(updates);
|
|
|
|
addedToQueue = true;
|
|
|
|
} else {
|
|
|
|
needGetDiff = true;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
} else if (updates instanceof TLRPC.TL_updatesCombined) {
|
2014-02-04 18:36:55 +00:00
|
|
|
if (MessagesStorage.lastSeqValue + 1 == updates.seq_start || MessagesStorage.lastSeqValue == updates.seq_start) {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().putUsersAndChats(updates.users, updates.chats, true, true);
|
2013-11-04 12:31:01 +00:00
|
|
|
int lastPtsValue = MessagesStorage.lastPtsValue;
|
|
|
|
int lastQtsValue = MessagesStorage.lastQtsValue;
|
2013-10-25 15:19:00 +00:00
|
|
|
if (!processUpdateArray(updates.updates, updates.users, updates.chats)) {
|
2013-11-04 12:31:01 +00:00
|
|
|
MessagesStorage.lastPtsValue = lastPtsValue;
|
|
|
|
MessagesStorage.lastQtsValue = lastQtsValue;
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", "need get diff inner TL_updatesCombined, seq: " + MessagesStorage.lastSeqValue + " " + updates.seq);
|
2013-10-25 15:19:00 +00:00
|
|
|
needGetDiff = true;
|
|
|
|
} else {
|
2013-11-04 12:31:01 +00:00
|
|
|
MessagesStorage.lastDateValue = updates.date;
|
|
|
|
MessagesStorage.lastSeqValue = updates.seq;
|
|
|
|
if (MessagesStorage.lastQtsValue != lastQtsValue) {
|
2013-10-25 15:19:00 +00:00
|
|
|
needReceivedQueue = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", "need get diff TL_updatesCombined, seq: " + MessagesStorage.lastSeqValue + " " + updates.seq_start);
|
2014-02-04 18:36:55 +00:00
|
|
|
if (gettingDifference || updatesStartWaitTime == 0 || updatesStartWaitTime != 0 && updatesStartWaitTime + 1500 > System.currentTimeMillis()) {
|
|
|
|
if (updatesStartWaitTime == 0) {
|
|
|
|
updatesStartWaitTime = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
FileLog.e("tmessages", "add TL_updatesCombined to queue");
|
|
|
|
updatesQueue.add(updates);
|
|
|
|
addedToQueue = true;
|
|
|
|
} else {
|
|
|
|
needGetDiff = true;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
} else if (updates instanceof TLRPC.TL_updates) {
|
2013-11-04 12:31:01 +00:00
|
|
|
if (MessagesStorage.lastSeqValue + 1 == updates.seq || updates.seq == 0 || updates.seq == MessagesStorage.lastSeqValue) {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().putUsersAndChats(updates.users, updates.chats, true, true);
|
2013-11-04 12:31:01 +00:00
|
|
|
int lastPtsValue = MessagesStorage.lastPtsValue;
|
|
|
|
int lastQtsValue = MessagesStorage.lastQtsValue;
|
2013-10-25 15:19:00 +00:00
|
|
|
if (!processUpdateArray(updates.updates, updates.users, updates.chats)) {
|
|
|
|
needGetDiff = true;
|
2013-11-04 12:31:01 +00:00
|
|
|
MessagesStorage.lastPtsValue = lastPtsValue;
|
|
|
|
MessagesStorage.lastQtsValue = lastQtsValue;
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", "need get diff inner TL_updates, seq: " + MessagesStorage.lastSeqValue + " " + updates.seq);
|
2013-10-25 15:19:00 +00:00
|
|
|
} else {
|
2013-11-04 12:31:01 +00:00
|
|
|
MessagesStorage.lastDateValue = updates.date;
|
2014-02-04 18:36:55 +00:00
|
|
|
if (updates.seq != 0) {
|
|
|
|
MessagesStorage.lastSeqValue = updates.seq;
|
|
|
|
}
|
2013-11-04 12:31:01 +00:00
|
|
|
if (MessagesStorage.lastQtsValue != lastQtsValue) {
|
2013-10-25 15:19:00 +00:00
|
|
|
needReceivedQueue = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", "need get diff TL_updates, seq: " + MessagesStorage.lastSeqValue + " " + updates.seq);
|
2014-02-04 18:36:55 +00:00
|
|
|
if (gettingDifference || updatesStartWaitTime == 0 || updatesStartWaitTime != 0 && updatesStartWaitTime + 1500 > System.currentTimeMillis()) {
|
|
|
|
if (updatesStartWaitTime == 0) {
|
|
|
|
updatesStartWaitTime = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
FileLog.e("tmessages", "add TL_updates to queue");
|
|
|
|
updatesQueue.add(updates);
|
|
|
|
addedToQueue = true;
|
|
|
|
} else {
|
|
|
|
needGetDiff = true;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
} else if (updates instanceof TLRPC.TL_updatesTooLong) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", "need get diff TL_updatesTooLong");
|
2013-10-25 15:19:00 +00:00
|
|
|
needGetDiff = true;
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (updates instanceof UserActionUpdates) {
|
|
|
|
MessagesStorage.lastSeqValue = updates.seq;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2014-02-04 18:36:55 +00:00
|
|
|
if (needGetDiff && !fromQueue) {
|
2013-10-25 15:19:00 +00:00
|
|
|
getDifference();
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (!fromQueue && !updatesQueue.isEmpty()) {
|
|
|
|
processUpdatesQueue(false);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
if (needReceivedQueue) {
|
|
|
|
TLRPC.TL_messages_receivedQueue req = new TLRPC.TL_messages_receivedQueue();
|
2013-11-04 12:31:01 +00:00
|
|
|
req.max_qts = MessagesStorage.lastQtsValue;
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().saveDiffParams(MessagesStorage.lastSeqValue, MessagesStorage.lastPtsValue, MessagesStorage.lastDateValue, MessagesStorage.lastQtsValue);
|
2013-10-25 15:19:00 +00: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();
|
|
|
|
|
2014-02-04 18:36:55 +00:00
|
|
|
final HashMap<Long, ArrayList<MessageObject>> messages = new HashMap<Long, ArrayList<MessageObject>>();
|
|
|
|
final ArrayList<TLRPC.Message> messagesArr = new ArrayList<TLRPC.Message>();
|
|
|
|
final ArrayList<Integer> markAsReadMessages = new ArrayList<Integer>();
|
|
|
|
final HashMap<Integer, Integer> markAsReadEncrypted = new HashMap<Integer, Integer>();
|
|
|
|
final ArrayList<Integer> deletedMessages = new ArrayList<Integer>();
|
|
|
|
final ArrayList<Long> printChanges = new ArrayList<Long>();
|
|
|
|
final ArrayList<TLRPC.ChatParticipants> chatInfoToUpdate = new ArrayList<TLRPC.ChatParticipants>();
|
|
|
|
final ArrayList<TLRPC.Update> updatesOnMainThread = new ArrayList<TLRPC.Update>();
|
2013-10-25 15:19:00 +00:00
|
|
|
final ArrayList<TLRPC.TL_updateEncryptedMessagesRead> tasks = new ArrayList<TLRPC.TL_updateEncryptedMessagesRead>();
|
2014-02-04 18:36:55 +00:00
|
|
|
final ArrayList<Integer> contactsIds = new ArrayList<Integer>();
|
2013-10-25 15:19:00 +00:00
|
|
|
MessageObject lastMessage = null;
|
|
|
|
|
|
|
|
boolean checkForUsers = true;
|
|
|
|
ConcurrentHashMap<Integer, TLRPC.User> usersDict;
|
|
|
|
ConcurrentHashMap<Integer, TLRPC.Chat> chatsDict;
|
|
|
|
if (usersArr != null) {
|
|
|
|
usersDict = new ConcurrentHashMap<Integer, TLRPC.User>();
|
|
|
|
for (TLRPC.User user : usersArr) {
|
|
|
|
usersDict.put(user.id, user);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
checkForUsers = false;
|
|
|
|
usersDict = users;
|
|
|
|
}
|
|
|
|
if (chatsArr != null) {
|
|
|
|
chatsDict = new ConcurrentHashMap<Integer, TLRPC.Chat>();
|
|
|
|
for (TLRPC.Chat chat : chatsArr) {
|
|
|
|
chatsDict.put(chat.id, chat);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
checkForUsers = false;
|
|
|
|
chatsDict = chats;
|
|
|
|
}
|
|
|
|
|
2014-02-11 14:32:09 +00:00
|
|
|
if (usersArr != null || chatsArr != null) {
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (usersArr != null) {
|
|
|
|
for (TLRPC.User user : usersArr) {
|
|
|
|
users.put(user.id, user);
|
|
|
|
if (user.id == UserConfig.clientUserId) {
|
|
|
|
UserConfig.currentUser = user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (chatsArr != null) {
|
|
|
|
for (TLRPC.Chat chat : chatsArr) {
|
|
|
|
chats.put(chat.id, chat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-02-04 18:36:55 +00:00
|
|
|
int interfaceUpdateMask = 0;
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
for (TLRPC.Update update : updates) {
|
|
|
|
if (update instanceof TLRPC.TL_updateNewMessage) {
|
|
|
|
TLRPC.TL_updateNewMessage upd = (TLRPC.TL_updateNewMessage)update;
|
|
|
|
if (checkForUsers) {
|
|
|
|
if (usersDict.get(upd.message.from_id) == null && users.get(upd.message.from_id) == null || upd.message.to_id.chat_id != 0 && chatsDict.get(upd.message.to_id.chat_id) == null && chats.get(upd.message.to_id.chat_id) == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
messagesArr.add(upd.message);
|
|
|
|
MessageObject obj = new MessageObject(upd.message, usersDict);
|
|
|
|
if (obj.type == 11) {
|
2014-02-04 18:36:55 +00:00
|
|
|
interfaceUpdateMask |= UPDATE_MASK_CHAT_AVATAR;
|
|
|
|
} else if (obj.type == 10) {
|
|
|
|
interfaceUpdateMask |= UPDATE_MASK_CHAT_NAME;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
long uid;
|
|
|
|
if (upd.message.to_id.chat_id != 0) {
|
|
|
|
uid = -upd.message.to_id.chat_id;
|
|
|
|
} else {
|
|
|
|
if (upd.message.to_id.user_id == UserConfig.clientUserId) {
|
|
|
|
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) {
|
|
|
|
arr = new ArrayList<MessageObject>();
|
|
|
|
messages.put(uid, arr);
|
|
|
|
}
|
|
|
|
arr.add(obj);
|
2013-11-04 12:31:01 +00:00
|
|
|
MessagesStorage.lastPtsValue = update.pts;
|
2013-10-25 15:19:00 +00:00
|
|
|
if (upd.message.from_id != UserConfig.clientUserId && upd.message.to_id != null) {
|
2013-12-20 19:25:49 +00:00
|
|
|
if (uid != openned_dialog_id || ApplicationLoader.lastPauseTime != 0) {
|
2013-10-25 15:19:00 +00:00
|
|
|
lastMessage = obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (update instanceof TLRPC.TL_updateMessageID) {
|
|
|
|
//can't be here
|
|
|
|
} else if (update instanceof TLRPC.TL_updateReadMessages) {
|
|
|
|
markAsReadMessages.addAll(update.messages);
|
2013-11-04 12:31:01 +00:00
|
|
|
MessagesStorage.lastPtsValue = update.pts;
|
2013-10-25 15:19:00 +00:00
|
|
|
} else if (update instanceof TLRPC.TL_updateDeleteMessages) {
|
|
|
|
deletedMessages.addAll(update.messages);
|
2013-11-04 12:31:01 +00:00
|
|
|
MessagesStorage.lastPtsValue = update.pts;
|
2013-10-25 15:19:00 +00:00
|
|
|
} else if (update instanceof TLRPC.TL_updateRestoreMessages) {
|
2013-11-04 12:31:01 +00:00
|
|
|
MessagesStorage.lastPtsValue = update.pts;
|
2013-10-25 15:19:00 +00:00
|
|
|
} else if (update instanceof TLRPC.TL_updateUserTyping || update instanceof TLRPC.TL_updateChatUserTyping) {
|
|
|
|
if (update.user_id != UserConfig.clientUserId) {
|
|
|
|
long uid = -update.chat_id;
|
|
|
|
if (uid == 0) {
|
|
|
|
uid = update.user_id;
|
|
|
|
}
|
|
|
|
ArrayList<PrintingUser> arr = printingUsers.get(uid);
|
|
|
|
if (arr == null) {
|
|
|
|
arr = new ArrayList<PrintingUser>();
|
|
|
|
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);
|
|
|
|
if (!printChanges.contains(uid)) {
|
|
|
|
printChanges.add(uid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (update instanceof TLRPC.TL_updateChatParticipants) {
|
2014-02-04 18:36:55 +00:00
|
|
|
interfaceUpdateMask |= UPDATE_MASK_CHAT_MEMBERS;
|
2013-10-25 15:19:00 +00:00
|
|
|
chatInfoToUpdate.add(update.participants);
|
|
|
|
} else if (update instanceof TLRPC.TL_updateUserStatus) {
|
2014-02-04 18:36:55 +00:00
|
|
|
interfaceUpdateMask |= UPDATE_MASK_STATUS;
|
2013-10-25 15:19:00 +00:00
|
|
|
updatesOnMainThread.add(update);
|
|
|
|
} else if (update instanceof TLRPC.TL_updateUserName) {
|
2014-02-04 18:36:55 +00:00
|
|
|
interfaceUpdateMask |= UPDATE_MASK_NAME;
|
2013-10-25 15:19:00 +00:00
|
|
|
updatesOnMainThread.add(update);
|
|
|
|
} else if (update instanceof TLRPC.TL_updateUserPhoto) {
|
2014-02-04 18:36:55 +00:00
|
|
|
interfaceUpdateMask |= UPDATE_MASK_AVATAR;
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().clearUserPhotos(update.user_id);
|
2013-12-20 19:25:49 +00:00
|
|
|
/*if (!(update.photo instanceof TLRPC.TL_userProfilePhotoEmpty)) { DEPRECATED
|
|
|
|
if (usersDict.containsKey(update.user_id)) {
|
|
|
|
TLRPC.TL_messageService newMessage = new TLRPC.TL_messageService();
|
|
|
|
newMessage.action = new TLRPC.TL_messageActionUserUpdatedPhoto();
|
|
|
|
newMessage.action.newUserPhoto = update.photo;
|
|
|
|
newMessage.local_id = newMessage.id = UserConfig.getNewMessageId();
|
|
|
|
UserConfig.saveConfig(false);
|
|
|
|
newMessage.unread = true;
|
|
|
|
newMessage.date = update.date;
|
|
|
|
newMessage.from_id = update.user_id;
|
|
|
|
newMessage.to_id = new TLRPC.TL_peerUser();
|
|
|
|
newMessage.to_id.user_id = UserConfig.clientUserId;
|
|
|
|
newMessage.out = false;
|
|
|
|
newMessage.dialog_id = update.user_id;
|
|
|
|
|
|
|
|
messagesArr.add(newMessage);
|
|
|
|
MessageObject obj = new MessageObject(newMessage, usersDict);
|
|
|
|
ArrayList<MessageObject> arr = messages.get(newMessage.dialog_id);
|
|
|
|
if (arr == null) {
|
|
|
|
arr = new ArrayList<MessageObject>();
|
|
|
|
messages.put(newMessage.dialog_id, arr);
|
|
|
|
}
|
|
|
|
arr.add(obj);
|
|
|
|
if (newMessage.from_id != UserConfig.clientUserId && newMessage.to_id != null) {
|
|
|
|
if (newMessage.dialog_id != openned_dialog_id || ApplicationLoader.lastPauseTime != 0) {
|
|
|
|
lastMessage = obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
updatesOnMainThread.add(update);
|
|
|
|
} else if (update instanceof TLRPC.TL_updateContactRegistered) {
|
2014-02-04 18:36:55 +00:00
|
|
|
if (enableJoined && usersDict.containsKey(update.user_id)) {
|
2013-12-20 19:25:49 +00: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);
|
|
|
|
newMessage.unread = true;
|
|
|
|
newMessage.date = update.date;
|
|
|
|
newMessage.from_id = update.user_id;
|
|
|
|
newMessage.to_id = new TLRPC.TL_peerUser();
|
|
|
|
newMessage.to_id.user_id = UserConfig.clientUserId;
|
|
|
|
newMessage.out = false;
|
|
|
|
newMessage.dialog_id = update.user_id;
|
|
|
|
|
|
|
|
messagesArr.add(newMessage);
|
|
|
|
MessageObject obj = new MessageObject(newMessage, usersDict);
|
|
|
|
ArrayList<MessageObject> arr = messages.get(newMessage.dialog_id);
|
|
|
|
if (arr == null) {
|
|
|
|
arr = new ArrayList<MessageObject>();
|
|
|
|
messages.put(newMessage.dialog_id, arr);
|
|
|
|
}
|
|
|
|
arr.add(obj);
|
|
|
|
if (newMessage.from_id != UserConfig.clientUserId && newMessage.to_id != null) {
|
|
|
|
if (newMessage.dialog_id != openned_dialog_id || ApplicationLoader.lastPauseTime != 0) {
|
|
|
|
lastMessage = obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-04 18:36:55 +00:00
|
|
|
// if (!contactsIds.contains(update.user_id)) {
|
|
|
|
// contactsIds.add(update.user_id);
|
|
|
|
// }
|
2013-10-25 15:19:00 +00:00
|
|
|
} else if (update instanceof TLRPC.TL_updateContactLink) {
|
2014-02-04 18:36:55 +00:00
|
|
|
if (update.my_link instanceof TLRPC.TL_contacts_myLinkContact || update.my_link instanceof TLRPC.TL_contacts_myLinkRequested && update.my_link.contact) {
|
|
|
|
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 15:19:00 +00:00
|
|
|
} else if (update instanceof TLRPC.TL_updateActivation) {
|
2014-02-04 18:36:55 +00:00
|
|
|
//DEPRECATED
|
2013-10-25 15:19:00 +00:00
|
|
|
} else if (update instanceof TLRPC.TL_updateNewAuthorization) {
|
2013-12-20 19:25:49 +00: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);
|
|
|
|
newMessage.unread = true;
|
|
|
|
newMessage.date = update.date;
|
|
|
|
newMessage.from_id = 333000;
|
|
|
|
newMessage.to_id = new TLRPC.TL_peerUser();
|
|
|
|
newMessage.to_id.user_id = UserConfig.clientUserId;
|
|
|
|
newMessage.out = false;
|
|
|
|
newMessage.dialog_id = 333000;
|
|
|
|
|
|
|
|
messagesArr.add(newMessage);
|
|
|
|
MessageObject obj = new MessageObject(newMessage, usersDict);
|
|
|
|
ArrayList<MessageObject> arr = messages.get(newMessage.dialog_id);
|
|
|
|
if (arr == null) {
|
|
|
|
arr = new ArrayList<MessageObject>();
|
|
|
|
messages.put(newMessage.dialog_id, arr);
|
|
|
|
}
|
|
|
|
arr.add(obj);
|
|
|
|
if (newMessage.from_id != UserConfig.clientUserId && newMessage.to_id != null) {
|
|
|
|
if (newMessage.dialog_id != openned_dialog_id || ApplicationLoader.lastPauseTime != 0) {
|
|
|
|
lastMessage = obj;
|
|
|
|
}
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
} else if (update instanceof TLRPC.TL_updateNewGeoChatMessage) {
|
2014-02-04 18:36:55 +00:00
|
|
|
//DEPRECATED
|
2013-10-25 15:19:00 +00:00
|
|
|
} else if (update instanceof TLRPC.TL_updateNewEncryptedMessage) {
|
2013-11-04 12:31:01 +00:00
|
|
|
MessagesStorage.lastQtsValue = update.qts;
|
2013-10-25 15:19:00 +00:00
|
|
|
TLRPC.Message message = decryptMessage(((TLRPC.TL_updateNewEncryptedMessage)update).message);
|
|
|
|
if (message != null) {
|
|
|
|
int cid = ((TLRPC.TL_updateNewEncryptedMessage)update).message.chat_id;
|
|
|
|
messagesArr.add(message);
|
|
|
|
MessageObject obj = new MessageObject(message, usersDict);
|
|
|
|
long uid = ((long)cid) << 32;
|
|
|
|
ArrayList<MessageObject> arr = messages.get(uid);
|
|
|
|
if (arr == null) {
|
|
|
|
arr = new ArrayList<MessageObject>();
|
|
|
|
messages.put(uid, arr);
|
|
|
|
}
|
|
|
|
arr.add(obj);
|
|
|
|
if (message.from_id != UserConfig.clientUserId && message.to_id != null) {
|
2013-12-20 19:25:49 +00:00
|
|
|
if (uid != openned_dialog_id || ApplicationLoader.lastPauseTime != 0) {
|
2013-10-25 15:19:00 +00:00
|
|
|
lastMessage = obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (update instanceof TLRPC.TL_updateEncryptedChatTyping) {
|
|
|
|
long uid = ((long)update.chat_id) << 32;
|
|
|
|
ArrayList<PrintingUser> arr = printingUsers.get(uid);
|
|
|
|
if (arr == null) {
|
|
|
|
arr = new ArrayList<PrintingUser>();
|
|
|
|
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);
|
|
|
|
if (!printChanges.contains(uid)) {
|
|
|
|
printChanges.add(uid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (update instanceof TLRPC.TL_updateEncryptedMessagesRead) {
|
|
|
|
markAsReadEncrypted.put(update.chat_id, Math.max(update.max_date, update.date));
|
|
|
|
tasks.add((TLRPC.TL_updateEncryptedMessagesRead)update);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (update instanceof TLRPC.TL_updateChatParticipantAdd) {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().updateChatInfo(update.chat_id, update.user_id, false, update.inviter_id, update.version);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (update instanceof TLRPC.TL_updateChatParticipantDelete) {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().updateChatInfo(update.chat_id, update.user_id, true, 0, update.version);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (update instanceof TLRPC.TL_updateDcOptions) {
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().updateDcSettings(0);
|
2013-10-25 15:19:00 +00:00
|
|
|
} else if (update instanceof TLRPC.TL_updateEncryption) {
|
|
|
|
final TLRPC.EncryptedChat newChat = update.chat;
|
|
|
|
long dialog_id = ((long)newChat.id) << 32;
|
|
|
|
TLRPC.EncryptedChat existingChat = encryptedChats.get(newChat.id);
|
|
|
|
if (existingChat == null) {
|
|
|
|
Semaphore semaphore = new Semaphore(0);
|
|
|
|
ArrayList<TLObject> result = new ArrayList<TLObject>();
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().getEncryptedChat(newChat.id, semaphore, result);
|
2013-10-25 15:19:00 +00:00
|
|
|
try {
|
|
|
|
semaphore.acquire();
|
|
|
|
} catch (Exception e) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", e);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
if (result.size() == 2) {
|
|
|
|
existingChat = (TLRPC.EncryptedChat)result.get(0);
|
|
|
|
TLRPC.User user = (TLRPC.User)result.get(1);
|
|
|
|
users.putIfAbsent(user.id, user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newChat instanceof TLRPC.TL_encryptedChatRequested && existingChat == null) {
|
|
|
|
int user_id = newChat.participant_id;
|
|
|
|
if (user_id == UserConfig.clientUserId) {
|
|
|
|
user_id = newChat.admin_id;
|
|
|
|
}
|
|
|
|
TLRPC.User user = users.get(user_id);
|
2013-12-20 19:25:49 +00:00
|
|
|
if (user == null) {
|
|
|
|
user = usersDict.get(user_id);
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
newChat.user_id = user_id;
|
|
|
|
final TLRPC.TL_dialog dialog = new TLRPC.TL_dialog();
|
|
|
|
dialog.id = dialog_id;
|
|
|
|
dialog.unread_count = 0;
|
|
|
|
dialog.top_message = 0;
|
|
|
|
dialog.last_message_date = update.date;
|
2014-02-11 14:32:09 +00:00
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
dialogs_dict.put(dialog.id, dialog);
|
|
|
|
dialogs.add(dialog);
|
|
|
|
dialogsServerOnly.clear();
|
|
|
|
encryptedChats.put(newChat.id, newChat);
|
|
|
|
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) {
|
|
|
|
if ((int)d.id != 0) {
|
|
|
|
dialogsServerOnly.add(d);
|
|
|
|
}
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().putEncryptedChat(newChat, user, dialog);
|
2013-11-04 12:31:01 +00:00
|
|
|
acceptSecretChat(newChat);
|
2013-10-25 15:19:00 +00:00
|
|
|
} else if (newChat instanceof TLRPC.TL_encryptedChat) {
|
|
|
|
if (existingChat != null && existingChat instanceof TLRPC.TL_encryptedChatWaiting && (existingChat.auth_key == null || existingChat.auth_key.length == 1)) {
|
|
|
|
newChat.a_or_b = existingChat.a_or_b;
|
|
|
|
newChat.user_id = existingChat.user_id;
|
|
|
|
processAcceptedSecretChat(newChat);
|
2014-03-22 22:31:55 +00:00
|
|
|
} else if (existingChat == null && startingSecretChat) {
|
|
|
|
delayedEncryptedChatUpdates.add(update);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
final TLRPC.EncryptedChat exist = existingChat;
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (exist != null) {
|
|
|
|
newChat.user_id = exist.user_id;
|
|
|
|
newChat.auth_key = exist.auth_key;
|
2014-03-22 22:31:55 +00:00
|
|
|
newChat.ttl = exist.ttl;
|
2013-10-25 15:19:00 +00:00
|
|
|
encryptedChats.put(newChat.id, newChat);
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().updateEncryptedChat(newChat);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(encryptedChatUpdated, newChat);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!messages.isEmpty()) {
|
|
|
|
for (HashMap.Entry<Long, ArrayList<MessageObject>> pair : messages.entrySet()) {
|
|
|
|
Long key = pair.getKey();
|
|
|
|
ArrayList<MessageObject> value = pair.getValue();
|
|
|
|
boolean printChanged = updatePrintingUsersWithNewMessages(key, value);
|
|
|
|
if (printChanged && !printChanges.contains(key)) {
|
|
|
|
printChanges.add(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!printChanges.isEmpty()) {
|
|
|
|
updatePrintingStrings();
|
|
|
|
}
|
|
|
|
|
|
|
|
final MessageObject lastMessageArg = lastMessage;
|
2014-02-04 18:36:55 +00:00
|
|
|
final int interfaceUpdateMaskFinal = interfaceUpdateMask;
|
|
|
|
|
2014-03-22 22:31:55 +00:00
|
|
|
processPendingEncMessages();
|
|
|
|
|
2014-02-04 18:36:55 +00:00
|
|
|
if (!contactsIds.isEmpty()) {
|
2014-03-22 22:31:55 +00:00
|
|
|
ContactsController.getInstance().processContactsUpdates(contactsIds, usersDict);
|
2014-02-04 18:36:55 +00:00
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
if (!messagesArr.isEmpty()) {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().putMessages(messagesArr, true, true);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
2014-02-04 18:36:55 +00:00
|
|
|
if (!messages.isEmpty() || !markAsReadMessages.isEmpty() || !deletedMessages.isEmpty() || !printChanges.isEmpty() || !chatInfoToUpdate.isEmpty() || !updatesOnMainThread.isEmpty() || !markAsReadEncrypted.isEmpty() || !contactsIds.isEmpty()) {
|
2013-10-25 15:19:00 +00:00
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-02-04 18:36:55 +00:00
|
|
|
int updateMask = interfaceUpdateMaskFinal;
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
boolean avatarsUpdate = false;
|
|
|
|
if (!updatesOnMainThread.isEmpty()) {
|
|
|
|
ArrayList<TLRPC.User> dbUsers = new ArrayList<TLRPC.User>();
|
|
|
|
ArrayList<TLRPC.User> dbUsersStatus = new ArrayList<TLRPC.User>();
|
|
|
|
for (TLRPC.Update update : updatesOnMainThread) {
|
|
|
|
TLRPC.User toDbUser = new TLRPC.User();
|
|
|
|
toDbUser.id = update.user_id;
|
|
|
|
TLRPC.User currentUser = users.get(update.user_id);
|
|
|
|
if (update instanceof TLRPC.TL_updateUserStatus) {
|
2014-02-28 22:28:25 +00:00
|
|
|
if (currentUser != null) {
|
|
|
|
currentUser.id = update.user_id;
|
|
|
|
currentUser.status = update.status;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2014-02-28 22:28:25 +00:00
|
|
|
toDbUser.status = update.status;
|
|
|
|
dbUsersStatus.add(toDbUser);
|
2013-10-25 15:19:00 +00:00
|
|
|
} else if (update instanceof TLRPC.TL_updateUserName) {
|
|
|
|
if (currentUser != null) {
|
|
|
|
currentUser.first_name = update.first_name;
|
|
|
|
currentUser.last_name = update.last_name;
|
|
|
|
}
|
|
|
|
toDbUser.first_name = update.first_name;
|
|
|
|
toDbUser.last_name = update.last_name;
|
|
|
|
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-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().updateUsers(dbUsersStatus, true, true, true);
|
|
|
|
MessagesStorage.getInstance().updateUsers(dbUsers, false, true, true);
|
2013-10-25 15:19:00 +00: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);
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
if (!markAsReadMessages.isEmpty()) {
|
|
|
|
for (Integer id : markAsReadMessages) {
|
|
|
|
MessageObject obj = dialogMessage.get(id);
|
|
|
|
if (obj != null) {
|
|
|
|
obj.messageOwner.unread = false;
|
2014-03-22 22:31:55 +00:00
|
|
|
updateMask |= UPDATE_MASK_READ_DIALOG_MESSAGE;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
|
|
|
|
if (currentPushMessage != null && markAsReadMessages.contains(currentPushMessage.messageOwner.id)) {
|
|
|
|
NotificationManager mNotificationManager = (NotificationManager)ApplicationLoader.applicationContext.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
|
mNotificationManager.cancel(1);
|
|
|
|
currentPushMessage = null;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
if (!markAsReadEncrypted.isEmpty()) {
|
|
|
|
for (HashMap.Entry<Integer, Integer> entry : markAsReadEncrypted.entrySet()) {
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(messagesReadedEncrypted, entry.getKey(), entry.getValue());
|
2013-10-25 15:19:00 +00:00
|
|
|
long dialog_id = (long)(entry.getKey()) << 32;
|
|
|
|
TLRPC.TL_dialog dialog = dialogs_dict.get(dialog_id);
|
|
|
|
if (dialog != null) {
|
|
|
|
MessageObject message = dialogMessage.get(dialog.top_message);
|
2013-12-20 19:25:49 +00:00
|
|
|
if (message != null && message.messageOwner.date <= entry.getValue()) {
|
2013-10-25 15:19:00 +00:00
|
|
|
message.messageOwner.unread = false;
|
2014-03-22 22:31:55 +00:00
|
|
|
updateMask |= UPDATE_MASK_READ_DIALOG_MESSAGE;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!deletedMessages.isEmpty()) {
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(messagesDeleted, deletedMessages);
|
2013-10-25 15:19:00 +00:00
|
|
|
for (Integer id : deletedMessages) {
|
|
|
|
MessageObject obj = dialogMessage.get(id);
|
|
|
|
if (obj != null) {
|
|
|
|
obj.deleted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!printChanges.isEmpty()) {
|
2014-02-04 18:36:55 +00:00
|
|
|
updateMask |= UPDATE_MASK_USER_PRINT;
|
|
|
|
}
|
|
|
|
if (!contactsIds.isEmpty()) {
|
|
|
|
updateMask |= UPDATE_MASK_NAME;
|
|
|
|
updateMask |= UPDATE_MASK_USER_PHONE;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
if (!chatInfoToUpdate.isEmpty()) {
|
|
|
|
for (TLRPC.ChatParticipants info : chatInfoToUpdate) {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().updateChatInfo(info.chat_id, info, true);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(chatInfoDidLoaded, info.chat_id, info);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-04 18:36:55 +00:00
|
|
|
if (updateMask != 0) {
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(updateInterfaces, updateMask);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
if (lastMessageArg != null) {
|
|
|
|
showInAppNotification(lastMessageArg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-03-22 22:31:55 +00:00
|
|
|
if (!markAsReadMessages.isEmpty() || !markAsReadEncrypted.isEmpty()) {
|
|
|
|
MessagesStorage.getInstance().storageQueue.postRunnable(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (!markAsReadMessages.isEmpty()) {
|
|
|
|
NotificationCenter.getInstance().postNotificationName(messagesReaded, markAsReadMessages);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
if (!markAsReadMessages.isEmpty() || !markAsReadEncrypted.isEmpty()) {
|
2014-02-28 22:28:25 +00:00
|
|
|
if (!markAsReadMessages.isEmpty()) {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().updateDialogsWithReadedMessages(markAsReadMessages, true);
|
2014-02-28 22:28:25 +00:00
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().markMessagesAsRead(markAsReadMessages, markAsReadEncrypted, true);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
if (!deletedMessages.isEmpty()) {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().markMessagesAsDeleted(deletedMessages, true);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
if (!deletedMessages.isEmpty()) {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().updateDialogsWithDeletedMessages(deletedMessages, true);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
if (!tasks.isEmpty()) {
|
|
|
|
for (TLRPC.TL_updateEncryptedMessagesRead update : tasks) {
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().createTaskForDate(update.chat_id, update.max_date, update.date, 1);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
ArrayList<Integer> messagesUsers = new ArrayList<Integer>();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void playNotificationSound() {
|
|
|
|
if (lastSoundPlay > System.currentTimeMillis() - 1800) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
lastSoundPlay = System.currentTimeMillis();
|
|
|
|
soundPool.play(sound, 1, 1, 1, 0, 1);
|
|
|
|
} catch (Exception e) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", e);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void showInAppNotification(MessageObject messageObject) {
|
2014-02-04 18:36:55 +00:00
|
|
|
if (!UserConfig.clientActivated) {
|
|
|
|
return;
|
|
|
|
}
|
2013-12-20 19:25:49 +00:00
|
|
|
if (ApplicationLoader.lastPauseTime != 0) {
|
|
|
|
ApplicationLoader.lastPauseTime = System.currentTimeMillis();
|
|
|
|
FileLog.e("tmessages", "reset sleep timeout by recieved message");
|
|
|
|
}
|
|
|
|
if (messageObject == null) {
|
|
|
|
return;
|
|
|
|
}
|
2013-11-04 12:31:01 +00:00
|
|
|
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Context.MODE_PRIVATE);
|
2013-10-25 15:19:00 +00:00
|
|
|
boolean globalEnabled = preferences.getBoolean("EnableAll", true);
|
|
|
|
if (!globalEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-22 22:31:55 +00:00
|
|
|
if (ApplicationLoader.lastPauseTime == 0 && isScreenOn) {
|
2013-10-26 22:34:39 +00:00
|
|
|
boolean inAppSounds = preferences.getBoolean("EnableInAppSounds", true);
|
|
|
|
boolean inAppVibrate = preferences.getBoolean("EnableInAppVibrate", true);
|
|
|
|
boolean inAppPreview = preferences.getBoolean("EnableInAppPreview", true);
|
|
|
|
|
|
|
|
if (inAppSounds || inAppVibrate || inAppPreview) {
|
|
|
|
long dialog_id = messageObject.messageOwner.dialog_id;
|
|
|
|
int user_id = messageObject.messageOwner.from_id;
|
|
|
|
int chat_id = 0;
|
|
|
|
if (dialog_id == 0) {
|
|
|
|
if (messageObject.messageOwner.to_id.chat_id != 0) {
|
|
|
|
dialog_id = -messageObject.messageOwner.to_id.chat_id;
|
|
|
|
chat_id = messageObject.messageOwner.to_id.chat_id;
|
|
|
|
} else if (messageObject.messageOwner.to_id.user_id != 0) {
|
|
|
|
if (messageObject.messageOwner.to_id.user_id == UserConfig.clientUserId) {
|
|
|
|
dialog_id = messageObject.messageOwner.from_id;
|
|
|
|
} else {
|
|
|
|
dialog_id = messageObject.messageOwner.to_id.user_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
TLRPC.EncryptedChat chat = encryptedChats.get((int)(dialog_id >> 32));
|
|
|
|
if (chat == null) {
|
|
|
|
return;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-26 22:34:39 +00:00
|
|
|
if (dialog_id == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
TLRPC.User user = users.get(user_id);
|
|
|
|
if (user == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
TLRPC.Chat chat;
|
|
|
|
if (chat_id != 0) {
|
|
|
|
chat = chats.get(chat_id);
|
|
|
|
if (chat == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
String key = "notify_" + dialog_id;
|
|
|
|
boolean value = preferences.getBoolean(key, true);
|
|
|
|
if (!value) {
|
2013-10-25 15:19:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-10-26 22:34:39 +00:00
|
|
|
|
|
|
|
if (inAppPreview) {
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(701, messageObject);
|
2013-10-26 22:34:39 +00:00
|
|
|
}
|
|
|
|
if (inAppVibrate) {
|
2013-11-04 12:31:01 +00:00
|
|
|
Vibrator v = (Vibrator)ApplicationLoader.applicationContext.getSystemService(Context.VIBRATOR_SERVICE);
|
2013-10-26 22:34:39 +00:00
|
|
|
v.vibrate(100);
|
|
|
|
}
|
|
|
|
if (inAppSounds) {
|
|
|
|
playNotificationSound();
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2013-10-26 22:34:39 +00:00
|
|
|
} else {
|
|
|
|
long dialog_id = messageObject.messageOwner.dialog_id;
|
|
|
|
int chat_id = messageObject.messageOwner.to_id.chat_id;
|
|
|
|
int user_id = messageObject.messageOwner.to_id.user_id;
|
2013-12-20 19:25:49 +00:00
|
|
|
if (user_id != 0 && user_id == UserConfig.clientUserId) {
|
|
|
|
user_id = messageObject.messageOwner.from_id;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
if (dialog_id == 0) {
|
2013-10-26 22:34:39 +00:00
|
|
|
if (chat_id != 0) {
|
|
|
|
dialog_id = -chat_id;
|
|
|
|
} else if (user_id != 0) {
|
|
|
|
dialog_id = user_id;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2013-10-26 22:34:39 +00:00
|
|
|
|
|
|
|
if (dialog_id != 0) {
|
|
|
|
String key = "notify_" + dialog_id;
|
|
|
|
boolean value = preferences.getBoolean(key, true);
|
|
|
|
if (!value) {
|
2013-10-25 15:19:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-10-26 22:34:39 +00:00
|
|
|
|
|
|
|
boolean groupEnabled = preferences.getBoolean("EnableGroup", true);
|
|
|
|
if (chat_id != 0 && !globalEnabled) {
|
2013-10-25 15:19:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-02-04 18:36:55 +00:00
|
|
|
TLRPC.FileLocation photoPath = null;
|
2013-10-25 15:19:00 +00:00
|
|
|
|
2013-10-26 22:34:39 +00:00
|
|
|
boolean globalVibrate = preferences.getBoolean("EnableVibrateAll", true);
|
|
|
|
boolean groupVibrate = preferences.getBoolean("EnableVibrateGroup", true);
|
2014-02-04 18:36:55 +00:00
|
|
|
boolean groupPreview = preferences.getBoolean("EnablePreviewGroup", true);
|
|
|
|
boolean userPreview = preferences.getBoolean("EnablePreviewAll", true);
|
2013-10-26 22:34:39 +00:00
|
|
|
|
|
|
|
String defaultPath = null;
|
|
|
|
Uri defaultUri = Settings.System.DEFAULT_NOTIFICATION_URI;
|
|
|
|
if (defaultUri != null) {
|
|
|
|
defaultPath = defaultUri.getPath();
|
|
|
|
}
|
|
|
|
|
|
|
|
String globalSound = preferences.getString("GlobalSoundPath", defaultPath);
|
|
|
|
String chatSound = preferences.getString("GroupSoundPath", defaultPath);
|
|
|
|
String userSoundPath = null;
|
|
|
|
String chatSoundPath = null;
|
|
|
|
|
2013-11-04 12:31:01 +00:00
|
|
|
NotificationManager mNotificationManager = (NotificationManager)ApplicationLoader.applicationContext.getSystemService(Context.NOTIFICATION_SERVICE);
|
2014-03-04 20:04:57 +00:00
|
|
|
Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class);
|
2013-12-20 19:25:49 +00:00
|
|
|
String msg = null;
|
2013-10-26 22:34:39 +00:00
|
|
|
|
|
|
|
if ((int)dialog_id != 0) {
|
|
|
|
if (chat_id != 0) {
|
|
|
|
intent.putExtra("chatId", chat_id);
|
|
|
|
}
|
|
|
|
if (user_id != 0) {
|
|
|
|
intent.putExtra("userId", user_id);
|
|
|
|
}
|
2013-12-20 19:25:49 +00:00
|
|
|
|
|
|
|
if (chat_id == 0 && user_id != 0) {
|
2014-02-04 18:36:55 +00:00
|
|
|
|
2013-12-20 19:25:49 +00:00
|
|
|
TLRPC.User u = users.get(user_id);
|
|
|
|
if (u == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-04 18:36:55 +00:00
|
|
|
|
|
|
|
if (u.photo != null && u.photo.photo_small != null && u.photo.photo_small.volume_id != 0 && u.photo.photo_small.local_id != 0) {
|
|
|
|
photoPath = u.photo.photo_small;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (userPreview) {
|
|
|
|
if (messageObject.messageOwner instanceof TLRPC.TL_messageService) {
|
|
|
|
if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionUserJoined) {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationContactJoined", R.string.NotificationContactJoined, Utilities.formatName(u.first_name, u.last_name));
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionUserUpdatedPhoto) {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationContactNewPhoto", R.string.NotificationContactNewPhoto, Utilities.formatName(u.first_name, u.last_name));
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionLoginUnknownLocation) {
|
2014-03-25 00:25:32 +00:00
|
|
|
String date = String.format("%s %s %s", LocaleController.formatterYear.format(((long)messageObject.messageOwner.date) * 1000), LocaleController.getString("OtherAt", R.string.OtherAt), LocaleController.formatterDay.format(((long)messageObject.messageOwner.date) * 1000));
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationUnrecognizedDevice", R.string.NotificationUnrecognizedDevice, UserConfig.currentUser.first_name, date, messageObject.messageOwner.action.title, messageObject.messageOwner.action.address);
|
2014-02-04 18:36:55 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaEmpty) {
|
|
|
|
if (messageObject.messageOwner.message != null && messageObject.messageOwner.message.length() != 0) {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationMessageText", R.string.NotificationMessageText, Utilities.formatName(u.first_name, u.last_name), messageObject.messageOwner.message);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationMessageNoText", R.string.NotificationMessageNoText, Utilities.formatName(u.first_name, u.last_name));
|
2014-02-04 18:36:55 +00:00
|
|
|
}
|
|
|
|
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaPhoto) {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationMessagePhoto", R.string.NotificationMessagePhoto, Utilities.formatName(u.first_name, u.last_name));
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaVideo) {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationMessageVideo", R.string.NotificationMessageVideo, Utilities.formatName(u.first_name, u.last_name));
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaContact) {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationMessageContact", R.string.NotificationMessageContact, Utilities.formatName(u.first_name, u.last_name));
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaGeo) {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationMessageMap", R.string.NotificationMessageMap, Utilities.formatName(u.first_name, u.last_name));
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaDocument) {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationMessageDocument", R.string.NotificationMessageDocument, Utilities.formatName(u.first_name, u.last_name));
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaAudio) {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationMessageAudio", R.string.NotificationMessageAudio, Utilities.formatName(u.first_name, u.last_name));
|
2013-12-20 19:25:49 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-04 18:36:55 +00:00
|
|
|
} else {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationMessageNoText", R.string.NotificationMessageNoText, Utilities.formatName(u.first_name, u.last_name));
|
2013-12-20 19:25:49 +00:00
|
|
|
}
|
|
|
|
} else if (chat_id != 0 && user_id == 0) {
|
|
|
|
TLRPC.Chat chat = chats.get(chat_id);
|
|
|
|
if (chat == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
TLRPC.User u = users.get(messageObject.messageOwner.from_id);
|
|
|
|
if (u == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-04 18:36:55 +00:00
|
|
|
|
|
|
|
if (u.photo != null && u.photo.photo_small != null && u.photo.photo_small.volume_id != 0 && u.photo.photo_small.local_id != 0) {
|
|
|
|
photoPath = u.photo.photo_small;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (groupPreview) {
|
|
|
|
if (messageObject.messageOwner instanceof TLRPC.TL_messageService) {
|
|
|
|
if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChatAddUser) {
|
|
|
|
if (messageObject.messageOwner.action.user_id == UserConfig.clientUserId) {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationInvitedToGroup", R.string.NotificationInvitedToGroup, Utilities.formatName(u.first_name, u.last_name), chat.title);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else {
|
|
|
|
TLRPC.User u2 = users.get(messageObject.messageOwner.action.user_id);
|
|
|
|
if (u2 == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationGroupAddMember", R.string.NotificationGroupAddMember, Utilities.formatName(u.first_name, u.last_name), chat.title, Utilities.formatName(u2.first_name, u2.last_name));
|
2014-02-04 18:36:55 +00:00
|
|
|
}
|
|
|
|
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChatEditTitle) {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationEditedGroupName", R.string.NotificationEditedGroupName, Utilities.formatName(u.first_name, u.last_name), messageObject.messageOwner.action.title);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChatEditPhoto || messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChatDeletePhoto) {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationEditedGroupPhoto", R.string.NotificationEditedGroupPhoto, Utilities.formatName(u.first_name, u.last_name), chat.title);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChatDeleteUser) {
|
|
|
|
if (messageObject.messageOwner.action.user_id == UserConfig.clientUserId) {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationGroupKickYou", R.string.NotificationGroupKickYou, Utilities.formatName(u.first_name, u.last_name), chat.title);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (messageObject.messageOwner.action.user_id == u.id) {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationGroupLeftMember", R.string.NotificationGroupLeftMember, Utilities.formatName(u.first_name, u.last_name), chat.title);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else {
|
|
|
|
TLRPC.User u2 = users.get(messageObject.messageOwner.action.user_id);
|
|
|
|
if (u2 == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationGroupKickMember", R.string.NotificationGroupKickMember, Utilities.formatName(u.first_name, u.last_name), chat.title, Utilities.formatName(u2.first_name, u2.last_name));
|
2013-12-20 19:25:49 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-04 18:36:55 +00:00
|
|
|
} else {
|
|
|
|
if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaEmpty) {
|
|
|
|
if (messageObject.messageOwner.message != null && messageObject.messageOwner.message.length() != 0) {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationMessageGroupText", R.string.NotificationMessageGroupText, Utilities.formatName(u.first_name, u.last_name), chat.title, messageObject.messageOwner.message);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationMessageGroupNoText", R.string.NotificationMessageGroupNoText, Utilities.formatName(u.first_name, u.last_name), chat.title);
|
2013-12-20 19:25:49 +00:00
|
|
|
}
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaPhoto) {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationMessageGroupPhoto", R.string.NotificationMessageGroupPhoto, Utilities.formatName(u.first_name, u.last_name), chat.title);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaVideo) {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationMessageGroupVideo", R.string.NotificationMessageGroupVideo, Utilities.formatName(u.first_name, u.last_name), chat.title);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaContact) {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationMessageGroupContact", R.string.NotificationMessageGroupContact, Utilities.formatName(u.first_name, u.last_name), chat.title);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaGeo) {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationMessageGroupMap", R.string.NotificationMessageGroupMap, Utilities.formatName(u.first_name, u.last_name), chat.title);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaDocument) {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationMessageGroupDocument", R.string.NotificationMessageGroupDocument, Utilities.formatName(u.first_name, u.last_name), chat.title);
|
2014-02-04 18:36:55 +00:00
|
|
|
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaAudio) {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationMessageGroupAudio", R.string.NotificationMessageGroupAudio, Utilities.formatName(u.first_name, u.last_name), chat.title);
|
2013-12-20 19:25:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.formatString("NotificationMessageGroupNoText", R.string.NotificationMessageGroupNoText, Utilities.formatName(u.first_name, u.last_name), chat.title);
|
2013-12-20 19:25:49 +00:00
|
|
|
}
|
|
|
|
}
|
2013-10-26 22:34:39 +00:00
|
|
|
} else {
|
2014-03-22 22:31:55 +00:00
|
|
|
msg = LocaleController.getString("YouHaveNewMessage", R.string.YouHaveNewMessage);
|
2014-02-04 18:36:55 +00:00
|
|
|
int enc_id = (int)(dialog_id >> 32);
|
|
|
|
intent.putExtra("encId", enc_id);
|
2013-10-26 22:34:39 +00:00
|
|
|
}
|
2013-12-20 19:25:49 +00:00
|
|
|
if (msg == null) {
|
|
|
|
return;
|
|
|
|
}
|
2013-10-26 22:34:39 +00:00
|
|
|
|
|
|
|
boolean needVibrate = false;
|
|
|
|
|
|
|
|
if (user_id != 0) {
|
|
|
|
userSoundPath = preferences.getString("sound_path_" + user_id, null);
|
|
|
|
needVibrate = globalVibrate;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2013-10-26 22:34:39 +00:00
|
|
|
if (chat_id != 0) {
|
|
|
|
chatSoundPath = preferences.getString("sound_chat_path_" + chat_id, null);
|
|
|
|
needVibrate = groupVibrate;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2013-10-26 22:34:39 +00:00
|
|
|
|
|
|
|
String choosenSoundPath = null;
|
|
|
|
|
|
|
|
if (user_id != 0) {
|
|
|
|
if (userSoundPath != null) {
|
|
|
|
choosenSoundPath = userSoundPath;
|
|
|
|
} else if (globalSound != null) {
|
|
|
|
choosenSoundPath = globalSound;
|
|
|
|
}
|
|
|
|
} else if (chat_id != 0) {
|
|
|
|
if (chatSoundPath != null) {
|
|
|
|
choosenSoundPath = chatSoundPath;
|
|
|
|
} else if (chatSound != null) {
|
|
|
|
choosenSoundPath = chatSound;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
choosenSoundPath = globalSound;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2013-10-26 22:34:39 +00:00
|
|
|
|
|
|
|
intent.setAction("com.tmessages.openchat" + Math.random() + Integer.MAX_VALUE);
|
2013-12-20 19:25:49 +00:00
|
|
|
intent.setFlags(32768);
|
2013-11-04 12:31:01 +00:00
|
|
|
PendingIntent contentIntent = PendingIntent.getActivity(ApplicationLoader.applicationContext, 0, intent, PendingIntent.FLAG_ONE_SHOT);
|
2013-10-26 22:34:39 +00:00
|
|
|
|
2013-11-04 12:31:01 +00:00
|
|
|
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ApplicationLoader.applicationContext)
|
2014-03-22 22:31:55 +00:00
|
|
|
.setContentTitle(LocaleController.getString("AppName", R.string.AppName))
|
2013-10-26 22:34:39 +00:00
|
|
|
.setSmallIcon(R.drawable.notification)
|
|
|
|
.setStyle(new NotificationCompat.BigTextStyle()
|
|
|
|
.bigText(msg))
|
|
|
|
.setContentText(msg)
|
|
|
|
.setAutoCancel(true)
|
|
|
|
.setTicker(msg);
|
|
|
|
|
2014-02-04 18:36:55 +00:00
|
|
|
if (photoPath != null) {
|
2014-03-22 22:31:55 +00:00
|
|
|
Bitmap img = FileLoader.getInstance().getImageFromMemory(photoPath, null, null, "50_50", false);
|
2014-02-04 18:36:55 +00:00
|
|
|
if (img != null) {
|
|
|
|
mBuilder.setLargeIcon(img);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-26 22:34:39 +00:00
|
|
|
if (needVibrate) {
|
|
|
|
mBuilder.setVibrate(new long[]{0, 100, 0, 100});
|
|
|
|
}
|
|
|
|
if (choosenSoundPath != null && !choosenSoundPath.equals("NoSound")) {
|
|
|
|
if (choosenSoundPath.equals(defaultPath)) {
|
|
|
|
mBuilder.setSound(defaultUri);
|
|
|
|
} else {
|
|
|
|
mBuilder.setSound(Uri.parse(choosenSoundPath));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-22 22:31:55 +00:00
|
|
|
currentPushMessage = null;
|
2013-10-26 22:34:39 +00:00
|
|
|
mBuilder.setContentIntent(contentIntent);
|
|
|
|
mNotificationManager.cancel(1);
|
|
|
|
Notification notification = mBuilder.build();
|
|
|
|
notification.ledARGB = 0xff00ff00;
|
|
|
|
notification.ledOnMS = 1000;
|
|
|
|
notification.ledOffMS = 1000;
|
|
|
|
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
|
2013-12-20 19:25:49 +00:00
|
|
|
try {
|
|
|
|
mNotificationManager.notify(1, notification);
|
2014-03-23 12:12:13 +00:00
|
|
|
if (preferences.getBoolean("EnablePebbleNotifications", false)) {
|
|
|
|
sendAlertToPebble(msg);
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
currentPushMessage = messageObject;
|
2013-12-20 19:25:49 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
FileLog.e("tmessages", e);
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-23 12:12:13 +00:00
|
|
|
public void sendAlertToPebble(String message) {
|
|
|
|
try {
|
|
|
|
final Intent i = new Intent("com.getpebble.action.SEND_NOTIFICATION");
|
|
|
|
|
|
|
|
final HashMap<String, String> data = new HashMap<String, String>();
|
|
|
|
data.put("title", LocaleController.getString("AppName", R.string.AppName));
|
|
|
|
data.put("body", message);
|
|
|
|
final JSONObject jsonData = new JSONObject(data);
|
|
|
|
final String notificationData = new JSONArray().put(jsonData).toString();
|
|
|
|
|
|
|
|
i.putExtra("messageType", "PEBBLE_ALERT");
|
2014-03-23 12:22:04 +00:00
|
|
|
i.putExtra("sender", LocaleController.formatString("AppName", R.string.AppName));
|
2014-03-23 12:12:13 +00:00
|
|
|
i.putExtra("notificationData", notificationData);
|
|
|
|
|
|
|
|
ApplicationLoader.applicationContext.sendBroadcast(i);
|
|
|
|
} catch (Exception e) {
|
|
|
|
FileLog.e("tmessages", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-04 18:36:55 +00:00
|
|
|
public void dialogsUnreadCountIncr(final HashMap<Long, Integer> values) {
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
for (HashMap.Entry<Long, Integer> entry : values.entrySet()) {
|
|
|
|
TLRPC.TL_dialog dialog = dialogs_dict.get(entry.getKey());
|
|
|
|
if (dialog != null) {
|
|
|
|
dialog.unread_count += entry.getValue();
|
|
|
|
}
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
|
2014-02-04 18:36:55 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
private void updateInterfaceWithMessages(long uid, ArrayList<MessageObject> messages) {
|
|
|
|
MessageObject lastMessage = null;
|
|
|
|
TLRPC.TL_dialog dialog = dialogs_dict.get(uid);
|
|
|
|
|
2014-03-05 16:42:10 +00:00
|
|
|
boolean isEncryptedChat = ((int)uid) == 0;
|
|
|
|
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(didReceivedNewMessages, uid, messages);
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
for (MessageObject message : messages) {
|
2014-03-05 16:42:10 +00:00
|
|
|
if (lastMessage == null || (!isEncryptedChat && message.messageOwner.id > lastMessage.messageOwner.id || isEncryptedChat && message.messageOwner.id < lastMessage.messageOwner.id) || message.messageOwner.date > lastMessage.messageOwner.date) {
|
2013-10-25 15:19:00 +00:00
|
|
|
lastMessage = message;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-05 16:42:10 +00:00
|
|
|
boolean changed = false;
|
|
|
|
|
2013-10-25 15:19:00 +00:00
|
|
|
if (dialog == null) {
|
|
|
|
dialog = new TLRPC.TL_dialog();
|
|
|
|
dialog.id = uid;
|
2014-02-04 18:36:55 +00:00
|
|
|
dialog.unread_count = 0;
|
2013-10-25 15:19:00 +00:00
|
|
|
dialog.top_message = lastMessage.messageOwner.id;
|
|
|
|
dialog.last_message_date = lastMessage.messageOwner.date;
|
|
|
|
dialogs_dict.put(uid, dialog);
|
|
|
|
dialogs.add(dialog);
|
|
|
|
dialogMessage.put(lastMessage.messageOwner.id, lastMessage);
|
2014-03-05 16:42:10 +00:00
|
|
|
changed = true;
|
2013-10-25 15:19:00 +00:00
|
|
|
} else {
|
2014-03-04 19:29:32 +00:00
|
|
|
if (dialog.top_message > 0 && lastMessage.messageOwner.id > 0 && lastMessage.messageOwner.id > dialog.top_message ||
|
|
|
|
dialog.top_message < 0 && lastMessage.messageOwner.id < 0 && lastMessage.messageOwner.id < dialog.top_message ||
|
|
|
|
dialog.last_message_date < lastMessage.messageOwner.date) {
|
2014-03-05 16:42:10 +00:00
|
|
|
dialogMessage.remove(dialog.top_message);
|
2014-03-04 19:29:32 +00:00
|
|
|
dialog.top_message = lastMessage.messageOwner.id;
|
|
|
|
dialog.last_message_date = lastMessage.messageOwner.date;
|
|
|
|
dialogMessage.put(lastMessage.messageOwner.id, lastMessage);
|
2014-03-05 16:42:10 +00:00
|
|
|
changed = true;
|
2014-03-04 19:29:32 +00:00
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
2014-03-05 16:42:10 +00: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) {
|
|
|
|
if ((int)d.id != 0) {
|
|
|
|
dialogsServerOnly.add(d);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public TLRPC.Message decryptMessage(TLRPC.EncryptedMessage message) {
|
|
|
|
TLRPC.EncryptedChat chat = encryptedChats.get(message.chat_id);
|
|
|
|
if (chat == null) {
|
|
|
|
Semaphore semaphore = new Semaphore(0);
|
|
|
|
ArrayList<TLObject> result = new ArrayList<TLObject>();
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().getEncryptedChat(message.chat_id, semaphore, result);
|
2013-10-25 15:19:00 +00:00
|
|
|
try {
|
|
|
|
semaphore.acquire();
|
|
|
|
} catch (Exception e) {
|
2013-12-20 19:25:49 +00:00
|
|
|
FileLog.e("tmessages", e);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
if (result.size() == 2) {
|
|
|
|
chat = (TLRPC.EncryptedChat)result.get(0);
|
|
|
|
TLRPC.User user = (TLRPC.User)result.get(1);
|
|
|
|
encryptedChats.put(chat.id, chat);
|
|
|
|
users.putIfAbsent(user.id, user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (chat == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
SerializedData is = new SerializedData(message.bytes);
|
|
|
|
long fingerprint = is.readInt64();
|
|
|
|
if (chat.key_fingerprint == fingerprint) {
|
|
|
|
byte[] messageKey = is.readData(16);
|
|
|
|
MessageKeyData keyData = Utilities.generateMessageKeyData(chat.auth_key, messageKey, false);
|
|
|
|
|
|
|
|
byte[] messageData = is.readData(message.bytes.length - 24);
|
2014-02-28 22:28:25 +00:00
|
|
|
messageData = Utilities.aesIgeEncryption(messageData, keyData.aesKey, keyData.aesIv, false, false, 0);
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
is = new SerializedData(messageData);
|
|
|
|
int len = is.readInt32();
|
|
|
|
TLObject object = TLClassStore.Instance().TLdeserialize(is, is.readInt32());
|
|
|
|
if (object != null) {
|
|
|
|
|
|
|
|
int from_id = chat.admin_id;
|
|
|
|
if (from_id == UserConfig.clientUserId) {
|
|
|
|
from_id = chat.participant_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object instanceof TLRPC.TL_decryptedMessage) {
|
|
|
|
TLRPC.TL_decryptedMessage decryptedMessage = (TLRPC.TL_decryptedMessage)object;
|
|
|
|
TLRPC.TL_message newMessage = new TLRPC.TL_message();
|
|
|
|
newMessage.message = decryptedMessage.message;
|
|
|
|
newMessage.date = message.date;
|
2013-12-20 19:25:49 +00:00
|
|
|
newMessage.local_id = newMessage.id = UserConfig.getNewMessageId();
|
|
|
|
UserConfig.saveConfig(false);
|
2013-10-25 15:19:00 +00:00
|
|
|
newMessage.from_id = from_id;
|
|
|
|
newMessage.to_id = new TLRPC.TL_peerUser();
|
2014-03-22 22:31:55 +00:00
|
|
|
newMessage.random_id = message.random_id;
|
2013-10-25 15:19:00 +00:00
|
|
|
newMessage.to_id.user_id = UserConfig.clientUserId;
|
|
|
|
newMessage.out = false;
|
|
|
|
newMessage.unread = true;
|
|
|
|
newMessage.dialog_id = ((long)chat.id) << 32;
|
|
|
|
newMessage.ttl = chat.ttl;
|
|
|
|
if (decryptedMessage.media instanceof TLRPC.TL_decryptedMessageMediaEmpty) {
|
|
|
|
newMessage.media = new TLRPC.TL_messageMediaEmpty();
|
|
|
|
} else if (decryptedMessage.media instanceof TLRPC.TL_decryptedMessageMediaContact) {
|
|
|
|
newMessage.media = new TLRPC.TL_messageMediaContact();
|
|
|
|
newMessage.media.last_name = decryptedMessage.media.last_name;
|
|
|
|
newMessage.media.first_name = decryptedMessage.media.first_name;
|
|
|
|
newMessage.media.phone_number = decryptedMessage.media.phone_number;
|
|
|
|
newMessage.media.user_id = decryptedMessage.media.user_id;
|
|
|
|
} else if (decryptedMessage.media instanceof TLRPC.TL_decryptedMessageMediaGeoPoint) {
|
|
|
|
newMessage.media = new TLRPC.TL_messageMediaGeo();
|
|
|
|
newMessage.media.geo = new TLRPC.TL_geoPoint();
|
|
|
|
newMessage.media.geo.lat = decryptedMessage.media.lat;
|
|
|
|
newMessage.media.geo._long = decryptedMessage.media._long;
|
|
|
|
} else if (decryptedMessage.media instanceof TLRPC.TL_decryptedMessageMediaPhoto) {
|
2013-12-20 19:25:49 +00:00
|
|
|
if (decryptedMessage.media.key.length != 32 || decryptedMessage.media.iv.length != 32) {
|
|
|
|
return null;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
newMessage.media = new TLRPC.TL_messageMediaPhoto();
|
|
|
|
newMessage.media.photo = new TLRPC.TL_photo();
|
|
|
|
newMessage.media.photo.user_id = newMessage.from_id;
|
|
|
|
newMessage.media.photo.date = newMessage.date;
|
|
|
|
newMessage.media.photo.caption = "";
|
|
|
|
newMessage.media.photo.geo = new TLRPC.TL_geoPointEmpty();
|
2013-12-26 11:43:37 +00:00
|
|
|
if (decryptedMessage.media.thumb.length != 0 && decryptedMessage.media.thumb.length <= 5000 && decryptedMessage.media.thumb_w < 100 && decryptedMessage.media.thumb_h < 100) {
|
|
|
|
TLRPC.TL_photoCachedSize small = new TLRPC.TL_photoCachedSize();
|
|
|
|
small.w = decryptedMessage.media.thumb_w;
|
|
|
|
small.h = decryptedMessage.media.thumb_h;
|
|
|
|
small.bytes = decryptedMessage.media.thumb;
|
|
|
|
small.type = "s";
|
|
|
|
small.location = new TLRPC.TL_fileLocationUnavailable();
|
|
|
|
newMessage.media.photo.sizes.add(small);
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
TLRPC.TL_photoSize big = new TLRPC.TL_photoSize();
|
|
|
|
big.w = decryptedMessage.media.w;
|
|
|
|
big.h = decryptedMessage.media.h;
|
|
|
|
big.type = "x";
|
|
|
|
big.size = message.file.size;
|
|
|
|
big.location = new TLRPC.TL_fileEncryptedLocation();
|
|
|
|
big.location.key = decryptedMessage.media.key;
|
|
|
|
big.location.iv = decryptedMessage.media.iv;
|
|
|
|
big.location.dc_id = message.file.dc_id;
|
|
|
|
big.location.volume_id = message.file.id;
|
|
|
|
big.location.secret = message.file.access_hash;
|
|
|
|
big.location.local_id = message.file.key_fingerprint;
|
|
|
|
newMessage.media.photo.sizes.add(big);
|
|
|
|
} else if (decryptedMessage.media instanceof TLRPC.TL_decryptedMessageMediaVideo) {
|
2013-12-20 19:25:49 +00:00
|
|
|
if (decryptedMessage.media.key.length != 32 || decryptedMessage.media.iv.length != 32) {
|
|
|
|
return null;
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
newMessage.media = new TLRPC.TL_messageMediaVideo();
|
|
|
|
newMessage.media.video = new TLRPC.TL_videoEncrypted();
|
2013-12-26 11:43:37 +00:00
|
|
|
if (decryptedMessage.media.thumb.length != 0 && decryptedMessage.media.thumb.length <= 5000 && decryptedMessage.media.thumb_w < 100 && decryptedMessage.media.thumb_h < 100) {
|
|
|
|
newMessage.media.video.thumb = new TLRPC.TL_photoCachedSize();
|
|
|
|
newMessage.media.video.thumb.bytes = decryptedMessage.media.thumb;
|
|
|
|
newMessage.media.video.thumb.w = decryptedMessage.media.thumb_w;
|
|
|
|
newMessage.media.video.thumb.h = decryptedMessage.media.thumb_h;
|
|
|
|
newMessage.media.video.thumb.type = "s";
|
|
|
|
newMessage.media.video.thumb.location = new TLRPC.TL_fileLocationUnavailable();
|
|
|
|
} else {
|
|
|
|
newMessage.media.video.thumb = new TLRPC.TL_photoSizeEmpty();
|
|
|
|
newMessage.media.video.thumb.type = "s";
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
newMessage.media.video.duration = decryptedMessage.media.duration;
|
|
|
|
newMessage.media.video.dc_id = message.file.dc_id;
|
|
|
|
newMessage.media.video.w = decryptedMessage.media.w;
|
|
|
|
newMessage.media.video.h = decryptedMessage.media.h;
|
|
|
|
newMessage.media.video.date = message.date;
|
|
|
|
newMessage.media.video.caption = "";
|
|
|
|
newMessage.media.video.user_id = from_id;
|
|
|
|
newMessage.media.video.size = message.file.size;
|
|
|
|
newMessage.media.video.id = message.file.id;
|
|
|
|
newMessage.media.video.access_hash = message.file.access_hash;
|
|
|
|
newMessage.media.video.key = decryptedMessage.media.key;
|
|
|
|
newMessage.media.video.iv = decryptedMessage.media.iv;
|
2013-12-26 11:43:37 +00:00
|
|
|
} else if (decryptedMessage.media instanceof TLRPC.TL_decryptedMessageMediaDocument) {
|
|
|
|
if (decryptedMessage.media.key.length != 32 || decryptedMessage.media.iv.length != 32) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
newMessage.media = new TLRPC.TL_messageMediaDocument();
|
|
|
|
newMessage.media.document = new TLRPC.TL_documentEncrypted();
|
|
|
|
newMessage.media.document.id = message.file.id;
|
|
|
|
newMessage.media.document.access_hash = message.file.access_hash;
|
|
|
|
newMessage.media.document.user_id = decryptedMessage.media.user_id;
|
|
|
|
newMessage.media.document.date = message.date;
|
|
|
|
newMessage.media.document.file_name = decryptedMessage.media.file_name;
|
|
|
|
newMessage.media.document.mime_type = decryptedMessage.media.mime_type;
|
|
|
|
newMessage.media.document.size = message.file.size;
|
|
|
|
newMessage.media.document.key = decryptedMessage.media.key;
|
|
|
|
newMessage.media.document.iv = decryptedMessage.media.iv;
|
|
|
|
if (decryptedMessage.media.thumb.length != 0 && decryptedMessage.media.thumb.length <= 5000 && decryptedMessage.media.thumb_w < 100 && decryptedMessage.media.thumb_h < 100) {
|
|
|
|
newMessage.media.document.thumb = new TLRPC.TL_photoCachedSize();
|
|
|
|
newMessage.media.document.thumb.bytes = decryptedMessage.media.thumb;
|
|
|
|
newMessage.media.document.thumb.w = decryptedMessage.media.thumb_w;
|
|
|
|
newMessage.media.document.thumb.h = decryptedMessage.media.thumb_h;
|
|
|
|
newMessage.media.document.thumb.type = "s";
|
|
|
|
newMessage.media.document.thumb.location = new TLRPC.TL_fileLocationUnavailable();
|
|
|
|
} else {
|
|
|
|
newMessage.media.document.thumb = new TLRPC.TL_photoSizeEmpty();
|
|
|
|
newMessage.media.document.thumb.type = "s";
|
|
|
|
}
|
|
|
|
newMessage.media.document.dc_id = message.file.dc_id;
|
2014-03-04 19:29:32 +00:00
|
|
|
} else if (decryptedMessage.media instanceof TLRPC.TL_decryptedMessageMediaAudio) {
|
|
|
|
if (decryptedMessage.media.key.length != 32 || decryptedMessage.media.iv.length != 32) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
newMessage.media = new TLRPC.TL_messageMediaAudio();
|
|
|
|
newMessage.media.audio = new TLRPC.TL_audioEncrypted();
|
|
|
|
newMessage.media.audio.id = message.file.id;
|
|
|
|
newMessage.media.audio.access_hash = message.file.access_hash;
|
|
|
|
newMessage.media.audio.user_id = decryptedMessage.media.user_id;
|
|
|
|
newMessage.media.audio.date = message.date;
|
|
|
|
newMessage.media.audio.size = message.file.size;
|
|
|
|
newMessage.media.audio.key = decryptedMessage.media.key;
|
|
|
|
newMessage.media.audio.iv = decryptedMessage.media.iv;
|
|
|
|
newMessage.media.audio.dc_id = message.file.dc_id;
|
|
|
|
newMessage.media.audio.duration = decryptedMessage.media.duration;
|
2013-10-25 15:19:00 +00:00
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return newMessage;
|
|
|
|
} else if (object instanceof TLRPC.TL_decryptedMessageService) {
|
|
|
|
TLRPC.TL_decryptedMessageService serviceMessage = (TLRPC.TL_decryptedMessageService)object;
|
|
|
|
if (serviceMessage.action instanceof TLRPC.TL_decryptedMessageActionSetMessageTTL) {
|
|
|
|
TLRPC.TL_messageService newMessage = new TLRPC.TL_messageService();
|
|
|
|
newMessage.action = new TLRPC.TL_messageActionTTLChange();
|
|
|
|
newMessage.action.ttl = chat.ttl = serviceMessage.action.ttl_seconds;
|
|
|
|
newMessage.local_id = newMessage.id = UserConfig.getNewMessageId();
|
2013-12-20 19:25:49 +00:00
|
|
|
UserConfig.saveConfig(false);
|
2013-10-25 15:19:00 +00:00
|
|
|
newMessage.unread = true;
|
|
|
|
newMessage.date = message.date;
|
|
|
|
newMessage.from_id = from_id;
|
|
|
|
newMessage.to_id = new TLRPC.TL_peerUser();
|
|
|
|
newMessage.to_id.user_id = UserConfig.clientUserId;
|
|
|
|
newMessage.out = false;
|
|
|
|
newMessage.dialog_id = ((long)chat.id) << 32;
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().updateEncryptedChatTTL(chat);
|
2013-10-25 15:19:00 +00:00
|
|
|
return newMessage;
|
2014-03-22 22:31:55 +00:00
|
|
|
} else if (serviceMessage.action instanceof TLRPC.TL_decryptedMessageActionFlushHistory) {
|
|
|
|
final long did = ((long)chat.id) << 32;
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
TLRPC.TL_dialog dialog = dialogs_dict.get(did);
|
|
|
|
if (dialog != null) {
|
|
|
|
dialogMessage.remove(dialog.top_message);
|
|
|
|
}
|
|
|
|
MessagesStorage.getInstance().deleteDialog(did, true);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(removeAllMessagesFromDialog, did);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return null;
|
|
|
|
} else if (serviceMessage.action instanceof TLRPC.TL_decryptedMessageActionDeleteMessages) {
|
|
|
|
if (!serviceMessage.action.random_ids.isEmpty()) {
|
|
|
|
pendingEncMessagesToDelete.addAll(serviceMessage.action.random_ids);
|
|
|
|
}
|
|
|
|
return null;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2014-02-04 18:36:55 +00:00
|
|
|
} else {
|
|
|
|
FileLog.e("tmessages", "unkown message " + object);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2014-02-04 18:36:55 +00:00
|
|
|
} else {
|
|
|
|
FileLog.e("tmessages", "unkown TLObject");
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2014-02-04 18:36:55 +00:00
|
|
|
} else {
|
|
|
|
FileLog.e("tmessages", "fingerprint mismatch");
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void processAcceptedSecretChat(final TLRPC.EncryptedChat encryptedChat) {
|
2013-12-23 11:05:57 +00:00
|
|
|
BigInteger p = new BigInteger(1, MessagesStorage.secretPBytes);
|
2013-10-25 15:19:00 +00:00
|
|
|
BigInteger i_authKey = new BigInteger(1, encryptedChat.g_a_or_b);
|
2013-12-23 11:05:57 +00:00
|
|
|
|
2013-12-23 11:54:31 +00:00
|
|
|
if (!Utilities.isGoodGaAndGb(i_authKey, p)) {
|
2013-12-23 11:05:57 +00:00
|
|
|
declineSecretChat(encryptedChat.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
i_authKey = i_authKey.modPow(new BigInteger(1, encryptedChat.a_or_b), p);
|
2013-10-25 15:19:00 +00:00
|
|
|
|
|
|
|
byte[] authKey = i_authKey.toByteArray();
|
|
|
|
if (authKey.length > 256) {
|
|
|
|
byte[] correctedAuth = new byte[256];
|
|
|
|
System.arraycopy(authKey, authKey.length - 256, correctedAuth, 0, 256);
|
|
|
|
authKey = correctedAuth;
|
|
|
|
} else if (authKey.length < 256) {
|
|
|
|
byte[] correctedAuth = new byte[256];
|
|
|
|
System.arraycopy(authKey, 0, correctedAuth, 256 - authKey.length, authKey.length);
|
|
|
|
for (int a = 0; a < 256 - authKey.length; a++) {
|
|
|
|
authKey[a] = 0;
|
|
|
|
}
|
|
|
|
authKey = correctedAuth;
|
|
|
|
}
|
|
|
|
byte[] authKeyHash = Utilities.computeSHA1(authKey);
|
|
|
|
byte[] authKeyId = new byte[8];
|
|
|
|
System.arraycopy(authKeyHash, authKeyHash.length - 8, authKeyId, 0, 8);
|
|
|
|
long fingerprint = Utilities.bytesToLong(authKeyId);
|
|
|
|
if (encryptedChat.key_fingerprint == fingerprint) {
|
|
|
|
encryptedChat.auth_key = authKey;
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().updateEncryptedChat(encryptedChat);
|
2013-10-25 15:19:00 +00:00
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
encryptedChats.put(encryptedChat.id, encryptedChat);
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(encryptedChatUpdated, encryptedChat);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
final TLRPC.TL_encryptedChatDiscarded newChat = new TLRPC.TL_encryptedChatDiscarded();
|
|
|
|
newChat.id = encryptedChat.id;
|
|
|
|
newChat.user_id = encryptedChat.user_id;
|
|
|
|
newChat.auth_key = encryptedChat.auth_key;
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().updateEncryptedChat(newChat);
|
2013-10-25 15:19:00 +00:00
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
encryptedChats.put(newChat.id, newChat);
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(encryptedChatUpdated, newChat);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
declineSecretChat(encryptedChat.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void declineSecretChat(int chat_id) {
|
|
|
|
TLRPC.TL_messages_discardEncryption req = new TLRPC.TL_messages_discardEncryption();
|
|
|
|
req.chat_id = chat_id;
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void acceptSecretChat(final TLRPC.EncryptedChat encryptedChat) {
|
|
|
|
if (acceptingChats.get(encryptedChat.id) != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
acceptingChats.put(encryptedChat.id, encryptedChat);
|
|
|
|
TLRPC.TL_messages_getDhConfig req = new TLRPC.TL_messages_getDhConfig();
|
|
|
|
req.random_length = 256;
|
2013-11-04 12:31:01 +00:00
|
|
|
req.version = MessagesStorage.lastSecretVersion;
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error == null) {
|
2014-03-22 22:31:55 +00:00
|
|
|
TLRPC.messages_DhConfig res = (TLRPC.messages_DhConfig) response;
|
2013-10-25 15:19:00 +00:00
|
|
|
if (response instanceof TLRPC.TL_messages_dhConfig) {
|
2013-12-23 11:05:57 +00:00
|
|
|
if (!Utilities.isGoodPrime(res.p, res.g)) {
|
2013-12-22 23:47:35 +00:00
|
|
|
acceptingChats.remove(encryptedChat.id);
|
|
|
|
declineSecretChat(encryptedChat.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-04 12:31:01 +00:00
|
|
|
MessagesStorage.secretPBytes = res.p;
|
|
|
|
MessagesStorage.secretG = res.g;
|
|
|
|
MessagesStorage.lastSecretVersion = res.version;
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().saveSecretParams(MessagesStorage.lastSecretVersion, MessagesStorage.secretG, MessagesStorage.secretPBytes);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
byte[] salt = new byte[256];
|
|
|
|
for (int a = 0; a < 256; a++) {
|
2014-03-22 22:31:55 +00:00
|
|
|
salt[a] = (byte) ((byte) (random.nextDouble() * 256) ^ res.random[a]);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
encryptedChat.a_or_b = salt;
|
2013-12-23 11:05:57 +00:00
|
|
|
BigInteger p = new BigInteger(1, MessagesStorage.secretPBytes);
|
|
|
|
BigInteger g_b = BigInteger.valueOf(MessagesStorage.secretG);
|
|
|
|
g_b = g_b.modPow(new BigInteger(1, salt), p);
|
|
|
|
BigInteger g_a = new BigInteger(1, encryptedChat.g_a);
|
|
|
|
|
2013-12-23 11:54:31 +00:00
|
|
|
if (!Utilities.isGoodGaAndGb(g_a, p)) {
|
2013-12-23 11:05:57 +00:00
|
|
|
acceptingChats.remove(encryptedChat.id);
|
|
|
|
declineSecretChat(encryptedChat.id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
byte[] g_b_bytes = g_b.toByteArray();
|
|
|
|
if (g_b_bytes.length > 256) {
|
2013-10-25 15:19:00 +00:00
|
|
|
byte[] correctedAuth = new byte[256];
|
2013-12-23 11:05:57 +00:00
|
|
|
System.arraycopy(g_b_bytes, 1, correctedAuth, 0, 256);
|
|
|
|
g_b_bytes = correctedAuth;
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
|
2013-12-23 11:05:57 +00:00
|
|
|
g_a = g_a.modPow(new BigInteger(1, salt), p);
|
2013-10-25 15:19:00 +00:00
|
|
|
|
2013-12-23 11:05:57 +00:00
|
|
|
byte[] authKey = g_a.toByteArray();
|
2013-10-25 15:19:00 +00:00
|
|
|
if (authKey.length > 256) {
|
|
|
|
byte[] correctedAuth = new byte[256];
|
|
|
|
System.arraycopy(authKey, authKey.length - 256, correctedAuth, 0, 256);
|
|
|
|
authKey = correctedAuth;
|
|
|
|
} else if (authKey.length < 256) {
|
|
|
|
byte[] correctedAuth = new byte[256];
|
|
|
|
System.arraycopy(authKey, 0, correctedAuth, 256 - authKey.length, authKey.length);
|
|
|
|
for (int a = 0; a < 256 - authKey.length; a++) {
|
|
|
|
authKey[a] = 0;
|
|
|
|
}
|
|
|
|
authKey = correctedAuth;
|
|
|
|
}
|
|
|
|
byte[] authKeyHash = Utilities.computeSHA1(authKey);
|
|
|
|
byte[] authKeyId = new byte[8];
|
|
|
|
System.arraycopy(authKeyHash, authKeyHash.length - 8, authKeyId, 0, 8);
|
|
|
|
encryptedChat.auth_key = authKey;
|
|
|
|
|
|
|
|
TLRPC.TL_messages_acceptEncryption req2 = new TLRPC.TL_messages_acceptEncryption();
|
2013-12-23 11:05:57 +00:00
|
|
|
req2.g_b = g_b_bytes;
|
2013-10-25 15:19:00 +00:00
|
|
|
req2.peer = new TLRPC.TL_inputEncryptedChat();
|
|
|
|
req2.peer.chat_id = encryptedChat.id;
|
|
|
|
req2.peer.access_hash = encryptedChat.access_hash;
|
|
|
|
req2.key_fingerprint = Utilities.bytesToLong(authKeyId);
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req2, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
acceptingChats.remove(encryptedChat.id);
|
|
|
|
if (error == null) {
|
2014-03-22 22:31:55 +00:00
|
|
|
final TLRPC.EncryptedChat newChat = (TLRPC.EncryptedChat) response;
|
2013-10-25 15:19:00 +00:00
|
|
|
newChat.auth_key = encryptedChat.auth_key;
|
|
|
|
newChat.user_id = encryptedChat.user_id;
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().updateEncryptedChat(newChat);
|
2013-10-25 15:19:00 +00:00
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
encryptedChats.put(newChat.id, newChat);
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(encryptedChatUpdated, newChat);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
|
|
|
} else {
|
|
|
|
acceptingChats.remove(encryptedChat.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric);
|
|
|
|
}
|
|
|
|
|
2014-02-04 18:36:55 +00:00
|
|
|
public void startSecretChat(final Context context, final TLRPC.User user) {
|
|
|
|
if (user == null) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
startingSecretChat = true;
|
2013-10-25 15:19:00 +00:00
|
|
|
final ProgressDialog progressDialog = new ProgressDialog(context);
|
2014-03-22 22:31:55 +00:00
|
|
|
progressDialog.setMessage(LocaleController.getString("Loading", R.string.Loading));
|
2013-10-25 15:19:00 +00:00
|
|
|
progressDialog.setCanceledOnTouchOutside(false);
|
|
|
|
progressDialog.setCancelable(false);
|
|
|
|
TLRPC.TL_messages_getDhConfig req = new TLRPC.TL_messages_getDhConfig();
|
|
|
|
req.random_length = 256;
|
2013-11-04 12:31:01 +00:00
|
|
|
req.version = MessagesStorage.lastSecretVersion;
|
2014-03-22 22:31:55 +00:00
|
|
|
final long reqId = ConnectionsManager.getInstance().performRpc(req, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error == null) {
|
2014-03-22 22:31:55 +00:00
|
|
|
TLRPC.messages_DhConfig res = (TLRPC.messages_DhConfig) response;
|
2013-10-25 15:19:00 +00:00
|
|
|
if (response instanceof TLRPC.TL_messages_dhConfig) {
|
2013-12-23 11:05:57 +00:00
|
|
|
if (!Utilities.isGoodPrime(res.p, res.g)) {
|
2013-12-22 23:47:35 +00:00
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-02-04 18:36:55 +00:00
|
|
|
try {
|
2014-03-22 22:31:55 +00:00
|
|
|
if (!((ActionBarActivity) context).isFinishing()) {
|
2014-02-04 18:36:55 +00:00
|
|
|
progressDialog.dismiss();
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
FileLog.e("tmessages", e);
|
2013-12-22 23:47:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2013-11-04 12:31:01 +00:00
|
|
|
MessagesStorage.secretPBytes = res.p;
|
|
|
|
MessagesStorage.secretG = res.g;
|
|
|
|
MessagesStorage.lastSecretVersion = res.version;
|
2014-03-22 22:31:55 +00:00
|
|
|
MessagesStorage.getInstance().saveSecretParams(MessagesStorage.lastSecretVersion, MessagesStorage.secretG, MessagesStorage.secretPBytes);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
final byte[] salt = new byte[256];
|
|
|
|
for (int a = 0; a < 256; a++) {
|
2014-03-22 22:31:55 +00:00
|
|
|
salt[a] = (byte) ((byte) (random.nextDouble() * 256) ^ res.random[a]);
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2013-12-23 11:05:57 +00:00
|
|
|
|
2013-11-04 12:31:01 +00:00
|
|
|
BigInteger i_g_a = BigInteger.valueOf(MessagesStorage.secretG);
|
|
|
|
i_g_a = i_g_a.modPow(new BigInteger(1, salt), new BigInteger(1, MessagesStorage.secretPBytes));
|
2013-10-25 15:19:00 +00:00
|
|
|
byte[] g_a = i_g_a.toByteArray();
|
|
|
|
if (g_a.length > 256) {
|
|
|
|
byte[] correctedAuth = new byte[256];
|
|
|
|
System.arraycopy(g_a, 1, correctedAuth, 0, 256);
|
|
|
|
g_a = correctedAuth;
|
|
|
|
}
|
|
|
|
|
|
|
|
TLRPC.TL_messages_requestEncryption req2 = new TLRPC.TL_messages_requestEncryption();
|
|
|
|
req2.g_a = g_a;
|
2014-02-04 18:36:55 +00:00
|
|
|
req2.user_id = getInputUser(user);
|
2014-03-04 19:29:32 +00:00
|
|
|
req2.random_id = random.nextInt();
|
2014-03-22 22:31:55 +00:00
|
|
|
ConnectionsManager.getInstance().performRpc(req2, new RPCRequest.RPCRequestDelegate() {
|
2013-10-25 15:19:00 +00:00
|
|
|
@Override
|
|
|
|
public void run(final TLObject response, TLRPC.TL_error error) {
|
|
|
|
if (error == null) {
|
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-03-22 22:31:55 +00:00
|
|
|
startingSecretChat = false;
|
|
|
|
if (!((ActionBarActivity) context).isFinishing()) {
|
2014-02-04 18:36:55 +00:00
|
|
|
try {
|
|
|
|
progressDialog.dismiss();
|
|
|
|
} catch (Exception e) {
|
|
|
|
FileLog.e("tmessages", e);
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
TLRPC.EncryptedChat chat = (TLRPC.EncryptedChat) response;
|
2013-10-25 15:19:00 +00:00
|
|
|
chat.user_id = chat.participant_id;
|
|
|
|
encryptedChats.put(chat.id, chat);
|
|
|
|
chat.a_or_b = salt;
|
|
|
|
TLRPC.TL_dialog dialog = new TLRPC.TL_dialog();
|
2014-03-22 22:31:55 +00:00
|
|
|
dialog.id = ((long) chat.id) << 32;
|
2013-10-25 15:19:00 +00:00
|
|
|
dialog.unread_count = 0;
|
|
|
|
dialog.top_message = 0;
|
2014-03-22 22:31:55 +00:00
|
|
|
dialog.last_message_date = ConnectionsManager.getInstance().getCurrentTime();
|
2013-10-25 15:19:00 +00:00
|
|
|
dialogs_dict.put(dialog.id, dialog);
|
|
|
|
dialogs.add(dialog);
|
|
|
|
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-03-22 22:31:55 +00:00
|
|
|
if ((int) d.id != 0) {
|
2013-10-25 15:19:00 +00:00
|
|
|
dialogsServerOnly.add(d);
|
|
|
|
}
|
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
|
|
|
|
MessagesStorage.getInstance().putEncryptedChat(chat, user, dialog);
|
|
|
|
NotificationCenter.getInstance().postNotificationName(encryptedChatCreated, chat);
|
|
|
|
Utilities.stageQueue.postRunnable(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
if (!delayedEncryptedChatUpdates.isEmpty()) {
|
|
|
|
processUpdateArray(delayedEncryptedChatUpdates, null, null);
|
|
|
|
delayedEncryptedChatUpdates.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2014-03-22 22:31:55 +00:00
|
|
|
delayedEncryptedChatUpdates.clear();
|
2013-10-25 15:19:00 +00:00
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-03-22 22:31:55 +00:00
|
|
|
if (!((ActionBarActivity) context).isFinishing()) {
|
|
|
|
startingSecretChat = false;
|
2014-02-04 18:36:55 +00:00
|
|
|
try {
|
|
|
|
progressDialog.dismiss();
|
|
|
|
} catch (Exception e) {
|
|
|
|
FileLog.e("tmessages", e);
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
2014-03-22 22:31:55 +00:00
|
|
|
builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
|
|
|
|
builder.setMessage(LocaleController.formatString("CreateEncryptedChatOutdatedError", R.string.CreateEncryptedChatOutdatedError, user.first_name, user.first_name));
|
|
|
|
builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), null);
|
2013-10-25 15:19:00 +00:00
|
|
|
builder.show().setCanceledOnTouchOutside(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric | RPCRequest.RPCRequestClassFailOnServerErrors);
|
|
|
|
} else {
|
2014-03-22 22:31:55 +00:00
|
|
|
delayedEncryptedChatUpdates.clear();
|
2013-10-25 15:19:00 +00:00
|
|
|
Utilities.RunOnUIThread(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2014-03-22 22:31:55 +00:00
|
|
|
startingSecretChat = false;
|
|
|
|
if (!((ActionBarActivity) context).isFinishing()) {
|
2014-02-04 18:36:55 +00:00
|
|
|
try {
|
|
|
|
progressDialog.dismiss();
|
|
|
|
} catch (Exception e) {
|
|
|
|
FileLog.e("tmessages", e);
|
|
|
|
}
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, null, true, RPCRequest.RPCRequestClassGeneric | RPCRequest.RPCRequestClassFailOnServerErrors);
|
2014-03-22 22:31:55 +00:00
|
|
|
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, LocaleController.getString("Cancel", R.string.Cancel), new DialogInterface.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
ConnectionsManager.getInstance().cancelRpc(reqId, true);
|
|
|
|
try {
|
|
|
|
dialog.dismiss();
|
|
|
|
} catch (Exception e) {
|
|
|
|
FileLog.e("tmessages", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
progressDialog.show();
|
2013-10-25 15:19:00 +00:00
|
|
|
}
|
|
|
|
}
|