NekoX/TMessagesProj/src/main/java/org/telegram/messenger/NotificationsController.java

4259 lines
241 KiB
Java
Raw Normal View History

2014-07-10 02:15:58 +02:00
/*
2019-01-23 18:03:33 +01:00
* This is the source code of Telegram for Android v. 5.x.x.
2014-07-10 02:15:58 +02:00
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
2019-01-23 18:03:33 +01:00
* Copyright Nikolai Kudashov, 2013-2018.
2014-07-10 02:15:58 +02:00
*/
2015-09-24 22:52:02 +02:00
package org.telegram.messenger;
2014-07-10 02:15:58 +02:00
2015-05-21 23:27:27 +02:00
import android.annotation.SuppressLint;
2018-07-30 04:07:02 +02:00
import android.annotation.TargetApi;
2018-08-27 10:33:11 +02:00
import android.app.Activity;
2019-01-23 18:03:33 +01:00
import android.app.ActivityManager;
import android.app.AlarmManager;
2018-07-30 04:07:02 +02:00
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
2014-07-10 02:15:58 +02:00
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
2018-08-27 10:33:11 +02:00
import android.graphics.Color;
import android.graphics.ImageDecoder;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.Point;
2018-08-27 10:33:11 +02:00
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.drawable.BitmapDrawable;
2018-07-30 04:07:02 +02:00
import android.media.AudioAttributes;
2014-07-10 02:15:58 +02:00
import android.media.AudioManager;
import android.media.SoundPool;
2014-07-10 02:15:58 +02:00
import android.net.Uri;
2014-10-04 17:56:09 +02:00
import android.os.Build;
2015-06-29 19:12:11 +02:00
import android.os.PowerManager;
import android.os.SystemClock;
2014-07-10 02:15:58 +02:00
import android.provider.Settings;
2019-05-14 14:08:05 +02:00
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.app.Person;
import androidx.core.app.RemoteInput;
import androidx.core.content.FileProvider;
2020-07-26 10:03:38 +02:00
import androidx.core.content.pm.ShortcutInfoCompat;
import androidx.core.content.pm.ShortcutManagerCompat;
2019-05-14 14:08:05 +02:00
import androidx.core.graphics.drawable.IconCompat;
2016-10-11 13:57:01 +02:00
import android.text.TextUtils;
2018-07-30 04:07:02 +02:00
import android.util.LongSparseArray;
2015-09-24 22:52:02 +02:00
import android.util.SparseArray;
2018-07-30 04:07:02 +02:00
import android.util.SparseIntArray;
2014-07-10 02:15:58 +02:00
2018-07-30 04:07:02 +02:00
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.telegram.messenger.support.SparseLongArray;
2015-09-24 22:52:02 +02:00
import org.telegram.tgnet.ConnectionsManager;
import org.telegram.tgnet.TLRPC;
2020-07-26 10:03:38 +02:00
import org.telegram.ui.BubbleActivity;
2014-07-10 02:15:58 +02:00
import org.telegram.ui.LaunchActivity;
import org.telegram.ui.PopupNotificationActivity;
2017-07-23 19:29:17 +02:00
import java.io.File;
2014-07-10 02:15:58 +02:00
import java.util.ArrayList;
import java.util.Calendar;
2020-07-26 10:03:38 +02:00
import java.util.HashSet;
2018-07-30 04:07:02 +02:00
import java.util.List;
2019-06-04 12:14:50 +02:00
import java.util.Map;
2019-01-23 18:03:33 +01:00
import java.util.concurrent.CountDownLatch;
2014-07-10 02:15:58 +02:00
2019-07-18 15:01:39 +02:00
public class NotificationsController extends BaseController {
2014-07-10 02:15:58 +02:00
public static final String EXTRA_VOICE_REPLY = "extra_voice_reply";
2018-08-27 10:33:11 +02:00
public static String OTHER_NOTIFICATIONS_CHANNEL = null;
2018-07-30 04:07:02 +02:00
private static DispatchQueue notificationsQueue = new DispatchQueue("notificationsQueue");
2015-01-02 23:15:07 +01:00
private ArrayList<MessageObject> pushMessages = new ArrayList<>();
private ArrayList<MessageObject> delayedPushMessages = new ArrayList<>();
2018-07-30 04:07:02 +02:00
private LongSparseArray<MessageObject> pushMessagesDict = new LongSparseArray<>();
private LongSparseArray<MessageObject> fcmRandomMessagesDict = new LongSparseArray<>();
private LongSparseArray<Point> smartNotificationsDialogs = new LongSparseArray<>();
private static NotificationManagerCompat notificationManager = null;
private static NotificationManager systemNotificationManager = null;
private LongSparseArray<Integer> pushDialogs = new LongSparseArray<>();
private LongSparseArray<Integer> wearNotificationsIds = new LongSparseArray<>();
private LongSparseArray<Integer> lastWearNotifiedMessageId = new LongSparseArray<>();
private LongSparseArray<Integer> pushDialogsOverrideMention = new LongSparseArray<>();
2015-01-02 23:15:07 +01:00
public ArrayList<MessageObject> popupMessages = new ArrayList<>();
2016-10-11 13:57:01 +02:00
public ArrayList<MessageObject> popupReplyMessages = new ArrayList<>();
2020-07-26 10:03:38 +02:00
private HashSet<Long> openedInBubbleDialogs = new HashSet<>();
2016-03-16 13:26:32 +01:00
private long opened_dialog_id = 0;
2018-07-30 04:07:02 +02:00
private int lastButtonId = 5000;
2014-07-11 15:54:17 +02:00
private int total_unread_count = 0;
private int personal_count = 0;
2014-07-10 02:15:58 +02:00
private boolean notifyCheck = false;
2015-02-01 19:51:02 +01:00
private int lastOnlineFromOtherDevice = 0;
2018-07-30 04:07:02 +02:00
private boolean inChatSoundEnabled;
2017-03-31 01:58:05 +02:00
private int lastBadgeCount = -1;
2015-05-21 23:27:27 +02:00
private String launcherClassName;
2015-03-26 18:34:47 +01:00
2018-07-30 04:07:02 +02:00
public static long globalSecretChatId = -(1L << 32);
public boolean showBadgeNumber;
2019-01-23 18:03:33 +01:00
public boolean showBadgeMuted;
public boolean showBadgeMessages;
2018-07-30 04:07:02 +02:00
2015-06-29 19:12:11 +02:00
private Runnable notificationDelayRunnable;
private PowerManager.WakeLock notificationDelayWakelock;
2015-03-26 18:34:47 +01:00
private long lastSoundPlay;
2015-05-21 23:27:27 +02:00
private long lastSoundOutPlay;
private SoundPool soundPool;
private int soundIn;
private int soundOut;
2016-03-06 02:49:31 +01:00
private int soundRecord;
2015-06-29 19:12:11 +02:00
private boolean soundInLoaded;
private boolean soundOutLoaded;
2016-03-06 02:49:31 +01:00
private boolean soundRecordLoaded;
2018-07-30 04:07:02 +02:00
protected static AudioManager audioManager;
2015-06-29 19:12:11 +02:00
private AlarmManager alarmManager;
2014-07-10 02:15:58 +02:00
2018-07-30 04:07:02 +02:00
private int notificationId;
private String notificationGroup;
static {
if (Build.VERSION.SDK_INT >= 26 && ApplicationLoader.applicationContext != null) {
notificationManager = NotificationManagerCompat.from(ApplicationLoader.applicationContext);
systemNotificationManager = (NotificationManager) ApplicationLoader.applicationContext.getSystemService(Context.NOTIFICATION_SERVICE);
2018-08-27 10:33:11 +02:00
checkOtherNotificationsChannel();
2018-07-30 04:07:02 +02:00
}
audioManager = (AudioManager) ApplicationLoader.applicationContext.getSystemService(Context.AUDIO_SERVICE);
}
2019-07-18 15:01:39 +02:00
2019-05-14 14:08:05 +02:00
private static volatile NotificationsController[] Instance = new NotificationsController[UserConfig.MAX_ACCOUNT_COUNT];
2018-07-30 04:07:02 +02:00
public static NotificationsController getInstance(int num) {
NotificationsController localInstance = Instance[num];
2014-07-10 02:15:58 +02:00
if (localInstance == null) {
2017-03-31 01:58:05 +02:00
synchronized (NotificationsController.class) {
2018-07-30 04:07:02 +02:00
localInstance = Instance[num];
2014-07-10 02:15:58 +02:00
if (localInstance == null) {
2018-07-30 04:07:02 +02:00
Instance[num] = localInstance = new NotificationsController(num);
2014-07-10 02:15:58 +02:00
}
}
}
return localInstance;
}
2018-07-30 04:07:02 +02:00
public NotificationsController(int instance) {
2019-07-18 15:01:39 +02:00
super(instance);
2018-07-30 04:07:02 +02:00
notificationId = currentAccount + 1;
notificationGroup = "messages" + (currentAccount == 0 ? "" : currentAccount);
2019-07-18 15:01:39 +02:00
SharedPreferences preferences = getAccountInstance().getNotificationsSettings();
2015-03-26 18:34:47 +01:00
inChatSoundEnabled = preferences.getBoolean("EnableInChatSound", true);
2018-07-30 04:07:02 +02:00
showBadgeNumber = preferences.getBoolean("badgeNumber", true);
2019-01-23 18:03:33 +01:00
showBadgeMuted = preferences.getBoolean("badgeNumberMuted", false);
showBadgeMessages = preferences.getBoolean("badgeNumberMessages", true);
2018-07-30 04:07:02 +02:00
notificationManager = NotificationManagerCompat.from(ApplicationLoader.applicationContext);
systemNotificationManager = (NotificationManager) ApplicationLoader.applicationContext.getSystemService(Context.NOTIFICATION_SERVICE);
2015-03-26 18:34:47 +01:00
try {
2015-03-27 11:32:33 +01:00
audioManager = (AudioManager) ApplicationLoader.applicationContext.getSystemService(Context.AUDIO_SERVICE);
2015-03-26 18:34:47 +01:00
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2015-03-26 18:34:47 +01:00
}
2015-06-29 19:12:11 +02:00
try {
alarmManager = (AlarmManager) ApplicationLoader.applicationContext.getSystemService(Context.ALARM_SERVICE);
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2015-06-29 19:12:11 +02:00
}
try {
PowerManager pm = (PowerManager) ApplicationLoader.applicationContext.getSystemService(Context.POWER_SERVICE);
2019-07-18 15:01:39 +02:00
notificationDelayWakelock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "telegram:notification_delay_lock");
2015-06-29 19:12:11 +02:00
notificationDelayWakelock.setReferenceCounted(false);
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2015-06-29 19:12:11 +02:00
}
2018-08-27 10:33:11 +02:00
notificationDelayRunnable = () -> {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("delay reached");
}
if (!delayedPushMessages.isEmpty()) {
showOrUpdateNotification(true);
delayedPushMessages.clear();
}
try {
if (notificationDelayWakelock.isHeld()) {
notificationDelayWakelock.release();
2015-06-29 19:12:11 +02:00
}
2018-08-27 10:33:11 +02:00
} catch (Exception e) {
FileLog.e(e);
2015-06-29 19:12:11 +02:00
}
};
2014-07-10 02:15:58 +02:00
}
2018-08-27 10:33:11 +02:00
public static void checkOtherNotificationsChannel() {
if (Build.VERSION.SDK_INT < 26) {
return;
}
SharedPreferences preferences = null;
if (OTHER_NOTIFICATIONS_CHANNEL == null) {
preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Activity.MODE_PRIVATE);
OTHER_NOTIFICATIONS_CHANNEL = preferences.getString("OtherKey", "Other3");
}
NotificationChannel notificationChannel = systemNotificationManager.getNotificationChannel(OTHER_NOTIFICATIONS_CHANNEL);
if (notificationChannel != null && notificationChannel.getImportance() == NotificationManager.IMPORTANCE_NONE) {
systemNotificationManager.deleteNotificationChannel(OTHER_NOTIFICATIONS_CHANNEL);
OTHER_NOTIFICATIONS_CHANNEL = null;
notificationChannel = null;
}
if (OTHER_NOTIFICATIONS_CHANNEL == null) {
if (preferences == null) {
preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Activity.MODE_PRIVATE);
}
OTHER_NOTIFICATIONS_CHANNEL = "Other" + Utilities.random.nextLong();
preferences.edit().putString("OtherKey", OTHER_NOTIFICATIONS_CHANNEL).commit();
}
if (notificationChannel == null) {
notificationChannel = new NotificationChannel(OTHER_NOTIFICATIONS_CHANNEL, "Other", NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.enableLights(false);
notificationChannel.enableVibration(false);
notificationChannel.setSound(null, null);
2020-07-26 23:11:01 +02:00
try {
systemNotificationManager.createNotificationChannel(notificationChannel);
} catch (Exception e) {
FileLog.e(e);
}
2018-08-27 10:33:11 +02:00
}
}
2014-07-10 02:15:58 +02:00
public void cleanup() {
popupMessages.clear();
2016-10-11 13:57:01 +02:00
popupReplyMessages.clear();
2018-08-27 10:33:11 +02:00
notificationsQueue.postRunnable(() -> {
opened_dialog_id = 0;
total_unread_count = 0;
personal_count = 0;
pushMessages.clear();
pushMessagesDict.clear();
fcmRandomMessagesDict.clear();
pushDialogs.clear();
wearNotificationsIds.clear();
lastWearNotifiedMessageId.clear();
2020-07-26 10:03:38 +02:00
openedInBubbleDialogs.clear();
2018-08-27 10:33:11 +02:00
delayedPushMessages.clear();
notifyCheck = false;
lastBadgeCount = 0;
try {
if (notificationDelayWakelock.isHeld()) {
notificationDelayWakelock.release();
2015-10-29 18:10:07 +01:00
}
2018-08-27 10:33:11 +02:00
} catch (Exception e) {
FileLog.e(e);
}
2019-01-23 18:03:33 +01:00
dismissNotification();
2018-08-27 10:33:11 +02:00
setBadge(getTotalAllUnreadCount());
2019-07-18 15:01:39 +02:00
SharedPreferences preferences = getAccountInstance().getNotificationsSettings();
2018-08-27 10:33:11 +02:00
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();
2018-07-30 04:07:02 +02:00
2018-08-27 10:33:11 +02:00
if (Build.VERSION.SDK_INT >= 26) {
try {
String keyStart = currentAccount + "channel";
List<NotificationChannel> list = systemNotificationManager.getNotificationChannels();
int count = list.size();
for (int a = 0; a < count; a++) {
NotificationChannel channel = list.get(a);
String id = channel.getId();
if (id.startsWith(keyStart)) {
systemNotificationManager.deleteNotificationChannel(id);
2018-07-30 04:07:02 +02:00
}
}
2018-08-27 10:33:11 +02:00
} catch (Throwable e) {
FileLog.e(e);
2018-07-30 04:07:02 +02:00
}
2015-06-29 19:12:11 +02:00
}
2015-10-29 18:10:07 +01:00
});
2014-07-10 02:15:58 +02:00
}
2015-03-26 18:34:47 +01:00
public void setInChatSoundEnabled(boolean value) {
inChatSoundEnabled = value;
}
2016-03-16 13:26:32 +01:00
public void setOpenedDialogId(final long dialog_id) {
2018-08-27 10:33:11 +02:00
notificationsQueue.postRunnable(() -> opened_dialog_id = dialog_id);
2015-10-29 18:10:07 +01:00
}
2020-07-26 10:03:38 +02:00
public void setOpenedInBubble(final long dialog_id, boolean opened) {
notificationsQueue.postRunnable(() -> {
if (opened) {
openedInBubbleDialogs.add(dialog_id);
} else {
openedInBubbleDialogs.remove(dialog_id);
}
});
}
2015-10-29 18:10:07 +01:00
public void setLastOnlineFromOtherDevice(final int time) {
2018-08-27 10:33:11 +02:00
notificationsQueue.postRunnable(() -> {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("set last online from other device = " + time);
2015-10-29 18:10:07 +01:00
}
2018-08-27 10:33:11 +02:00
lastOnlineFromOtherDevice = time;
2015-10-29 18:10:07 +01:00
});
}
2015-11-26 22:04:02 +01:00
public void removeNotificationsForDialog(long did) {
2019-07-18 15:01:39 +02:00
processReadMessages(null, did, 0, Integer.MAX_VALUE, false);
2018-07-30 04:07:02 +02:00
LongSparseArray<Integer> dialogsToUpdate = new LongSparseArray<>();
2015-11-26 22:04:02 +01:00
dialogsToUpdate.put(did, 0);
2019-07-18 15:01:39 +02:00
processDialogsUpdateRead(dialogsToUpdate);
2015-11-26 22:04:02 +01:00
}
2016-10-11 13:57:01 +02:00
public boolean hasMessagesToReply() {
for (int a = 0; a < pushMessages.size(); a++) {
MessageObject messageObject = pushMessages.get(a);
long dialog_id = messageObject.getDialogId();
if (messageObject.messageOwner.mentioned && messageObject.messageOwner.action instanceof TLRPC.TL_messageActionPinMessage ||
2020-09-30 15:48:47 +02:00
(int) dialog_id == 0 || messageObject.messageOwner.peer_id.channel_id != 0 && !messageObject.isMegagroup()) {
2016-10-11 13:57:01 +02:00
continue;
}
return true;
}
return false;
}
protected void forceShowPopupForReply() {
2018-08-27 10:33:11 +02:00
notificationsQueue.postRunnable(() -> {
final ArrayList<MessageObject> popupArray = new ArrayList<>();
for (int a = 0; a < pushMessages.size(); a++) {
MessageObject messageObject = pushMessages.get(a);
long dialog_id = messageObject.getDialogId();
if (messageObject.messageOwner.mentioned && messageObject.messageOwner.action instanceof TLRPC.TL_messageActionPinMessage ||
2020-09-30 15:48:47 +02:00
(int) dialog_id == 0 || messageObject.messageOwner.peer_id.channel_id != 0 && !messageObject.isMegagroup()) {
2018-08-27 10:33:11 +02:00
continue;
2016-10-11 13:57:01 +02:00
}
2018-08-27 10:33:11 +02:00
popupArray.add(0, messageObject);
}
2020-01-23 07:15:40 +01:00
if (!popupArray.isEmpty() && !AndroidUtilities.needShowPasscode() && !SharedConfig.isWaitingForPasscodeEnter) {
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> {
popupReplyMessages = popupArray;
Intent popupIntent = new Intent(ApplicationLoader.applicationContext, PopupNotificationActivity.class);
popupIntent.putExtra("force", true);
popupIntent.putExtra("currentAccount", currentAccount);
popupIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_FROM_BACKGROUND);
ApplicationLoader.applicationContext.startActivity(popupIntent);
Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
ApplicationLoader.applicationContext.sendBroadcast(it);
});
2016-10-11 13:57:01 +02:00
}
});
}
2015-11-26 22:04:02 +01:00
public void removeDeletedMessagesFromNotifications(final SparseArray<ArrayList<Integer>> deletedMessages) {
2018-07-30 04:07:02 +02:00
final ArrayList<MessageObject> popupArrayRemove = new ArrayList<>(0);
2018-08-27 10:33:11 +02:00
notificationsQueue.postRunnable(() -> {
int old_unread_count = total_unread_count;
2019-07-18 15:01:39 +02:00
SharedPreferences preferences = getAccountInstance().getNotificationsSettings();
2018-08-27 10:33:11 +02:00
for (int a = 0; a < deletedMessages.size(); a++) {
int key = deletedMessages.keyAt(a);
ArrayList<Integer> mids = deletedMessages.get(key);
for (int b = 0; b < mids.size(); b++) {
long mid = mids.get(b);
2019-12-31 14:08:08 +01:00
if (key != 0) {
mid |= ((long) key) << 32;
}
2018-08-27 10:33:11 +02:00
MessageObject messageObject = pushMessagesDict.get(mid);
if (messageObject != null) {
2019-12-31 14:08:08 +01:00
long dialogId = messageObject.getDialogId();
Integer currentCount = pushDialogs.get(dialogId);
if (currentCount == null) {
currentCount = 0;
}
Integer newCount = currentCount - 1;
if (newCount <= 0) {
newCount = 0;
smartNotificationsDialogs.remove(dialogId);
}
if (!newCount.equals(currentCount)) {
total_unread_count -= currentCount;
total_unread_count += newCount;
pushDialogs.put(dialogId, newCount);
}
if (newCount == 0) {
pushDialogs.remove(dialogId);
pushDialogsOverrideMention.remove(dialogId);
}
2018-08-27 10:33:11 +02:00
pushMessagesDict.remove(mid);
delayedPushMessages.remove(messageObject);
pushMessages.remove(messageObject);
if (isPersonalMessage(messageObject)) {
personal_count--;
2015-11-26 22:04:02 +01:00
}
2018-08-27 10:33:11 +02:00
popupArrayRemove.add(messageObject);
2015-11-26 22:04:02 +01:00
}
}
}
2018-08-27 10:33:11 +02:00
if (!popupArrayRemove.isEmpty()) {
AndroidUtilities.runOnUIThread(() -> {
for (int a = 0, size = popupArrayRemove.size(); a < size; a++) {
popupMessages.remove(popupArrayRemove.get(a));
}
2020-07-26 10:03:38 +02:00
NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.pushMessagesUpdated);
2018-08-27 10:33:11 +02:00
});
}
if (old_unread_count != total_unread_count) {
if (!notifyCheck) {
delayedPushMessages.clear();
showOrUpdateNotification(notifyCheck);
} else {
2019-07-18 15:01:39 +02:00
scheduleNotificationDelay(lastOnlineFromOtherDevice > getConnectionsManager().getCurrentTime());
2018-08-27 10:33:11 +02:00
}
final int pushDialogsCount = pushDialogs.size();
AndroidUtilities.runOnUIThread(() -> {
NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.notificationsCountUpdated, currentAccount);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsUnreadCounterChanged, pushDialogsCount);
2018-08-27 10:33:11 +02:00
});
}
notifyCheck = false;
if (showBadgeNumber) {
setBadge(getTotalAllUnreadCount());
}
2015-11-26 22:04:02 +01:00
});
}
2018-07-30 04:07:02 +02:00
public void removeDeletedHisoryFromNotifications(final SparseIntArray deletedMessages) {
final ArrayList<MessageObject> popupArrayRemove = new ArrayList<>(0);
2018-08-27 10:33:11 +02:00
notificationsQueue.postRunnable(() -> {
int old_unread_count = total_unread_count;
2019-07-18 15:01:39 +02:00
SharedPreferences preferences = getAccountInstance().getNotificationsSettings();
2017-12-08 18:35:59 +01:00
2018-08-27 10:33:11 +02:00
for (int a = 0; a < deletedMessages.size(); a++) {
int key = deletedMessages.keyAt(a);
long dialog_id = -key;
int id = deletedMessages.get(key);
Integer currentCount = pushDialogs.get(dialog_id);
if (currentCount == null) {
currentCount = 0;
}
Integer newCount = currentCount;
for (int c = 0; c < pushMessages.size(); c++) {
MessageObject messageObject = pushMessages.get(c);
if (messageObject.getDialogId() == dialog_id && messageObject.getId() <= id) {
pushMessagesDict.remove(messageObject.getIdWithChannel());
delayedPushMessages.remove(messageObject);
pushMessages.remove(messageObject);
c--;
if (isPersonalMessage(messageObject)) {
personal_count--;
2017-12-08 18:35:59 +01:00
}
2018-08-27 10:33:11 +02:00
popupArrayRemove.add(messageObject);
newCount--;
2017-12-08 18:35:59 +01:00
}
2018-08-27 10:33:11 +02:00
}
2017-12-08 18:35:59 +01:00
2018-08-27 10:33:11 +02:00
if (newCount <= 0) {
newCount = 0;
smartNotificationsDialogs.remove(dialog_id);
2017-12-08 18:35:59 +01:00
}
2018-08-27 10:33:11 +02:00
if (!newCount.equals(currentCount)) {
total_unread_count -= currentCount;
total_unread_count += newCount;
pushDialogs.put(dialog_id, newCount);
2017-12-08 18:35:59 +01:00
}
2018-08-27 10:33:11 +02:00
if (newCount == 0) {
pushDialogs.remove(dialog_id);
pushDialogsOverrideMention.remove(dialog_id);
2017-12-08 18:35:59 +01:00
}
2018-08-27 10:33:11 +02:00
}
if (popupArrayRemove.isEmpty()) {
AndroidUtilities.runOnUIThread(() -> {
for (int a = 0, size = popupArrayRemove.size(); a < size; a++) {
popupMessages.remove(popupArrayRemove.get(a));
}
2020-07-26 10:03:38 +02:00
NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.pushMessagesUpdated);
2018-08-27 10:33:11 +02:00
});
}
if (old_unread_count != total_unread_count) {
if (!notifyCheck) {
delayedPushMessages.clear();
showOrUpdateNotification(notifyCheck);
} else {
2019-07-18 15:01:39 +02:00
scheduleNotificationDelay(lastOnlineFromOtherDevice > getConnectionsManager().getCurrentTime());
2017-12-08 18:35:59 +01:00
}
2018-08-27 10:33:11 +02:00
final int pushDialogsCount = pushDialogs.size();
AndroidUtilities.runOnUIThread(() -> {
NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.notificationsCountUpdated, currentAccount);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsUnreadCounterChanged, pushDialogsCount);
2018-08-27 10:33:11 +02:00
});
}
notifyCheck = false;
if (showBadgeNumber) {
setBadge(getTotalAllUnreadCount());
2017-12-08 18:35:59 +01:00
}
});
}
2018-07-30 04:07:02 +02:00
public void processReadMessages(final SparseLongArray inbox, final long dialog_id, final int max_date, final int max_id, final boolean isPopup) {
final ArrayList<MessageObject> popupArrayRemove = new ArrayList<>(0);
2018-08-27 10:33:11 +02:00
notificationsQueue.postRunnable(() -> {
if (inbox != null) {
for (int b = 0; b < inbox.size(); b++) {
int key = inbox.keyAt(b);
long messageId = inbox.get(key);
for (int a = 0; a < pushMessages.size(); a++) {
MessageObject messageObject = pushMessages.get(a);
2019-09-10 12:56:11 +02:00
if (!messageObject.messageOwner.from_scheduled && messageObject.getDialogId() == key && messageObject.getId() <= (int) messageId) {
2018-08-27 10:33:11 +02:00
if (isPersonalMessage(messageObject)) {
personal_count--;
}
popupArrayRemove.add(messageObject);
long mid = messageObject.getId();
2020-09-30 15:48:47 +02:00
if (messageObject.messageOwner.peer_id.channel_id != 0) {
mid |= ((long) messageObject.messageOwner.peer_id.channel_id) << 32;
2015-10-29 18:10:07 +01:00
}
2018-08-27 10:33:11 +02:00
pushMessagesDict.remove(mid);
delayedPushMessages.remove(messageObject);
pushMessages.remove(a);
a--;
2015-10-29 18:10:07 +01:00
}
}
}
2018-08-27 10:33:11 +02:00
}
if (dialog_id != 0 && (max_id != 0 || max_date != 0)) {
for (int a = 0; a < pushMessages.size(); a++) {
MessageObject messageObject = pushMessages.get(a);
if (messageObject.getDialogId() == dialog_id) {
boolean remove = false;
if (max_date != 0) {
if (messageObject.messageOwner.date <= max_date) {
remove = true;
}
} else {
if (!isPopup) {
if (messageObject.getId() <= max_id || max_id < 0) {
2015-10-29 18:10:07 +01:00
remove = true;
}
} else {
2018-08-27 10:33:11 +02:00
if (messageObject.getId() == max_id || max_id < 0) {
remove = true;
2015-10-29 18:10:07 +01:00
}
}
}
2018-08-27 10:33:11 +02:00
if (remove) {
if (isPersonalMessage(messageObject)) {
personal_count--;
}
pushMessages.remove(a);
delayedPushMessages.remove(messageObject);
popupArrayRemove.add(messageObject);
long mid = messageObject.getId();
2020-09-30 15:48:47 +02:00
if (messageObject.messageOwner.peer_id.channel_id != 0) {
mid |= ((long) messageObject.messageOwner.peer_id.channel_id) << 32;
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
pushMessagesDict.remove(mid);
a--;
2015-10-29 18:10:07 +01:00
}
2018-08-27 10:33:11 +02:00
}
2015-10-29 18:10:07 +01:00
}
}
2018-08-27 10:33:11 +02:00
if (!popupArrayRemove.isEmpty()) {
AndroidUtilities.runOnUIThread(() -> {
for (int a = 0, size = popupArrayRemove.size(); a < size; a++) {
popupMessages.remove(popupArrayRemove.get(a));
}
NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.pushMessagesUpdated);
});
}
2015-10-29 18:10:07 +01:00
});
}
2019-01-23 18:03:33 +01:00
private int addToPopupMessages(final ArrayList<MessageObject> popupArrayAdd, MessageObject messageObject, int lower_id, long dialog_id, boolean isChannel, SharedPreferences preferences) {
int popup = 0;
if (lower_id != 0) {
if (preferences.getBoolean("custom_" + dialog_id, false)) {
popup = preferences.getInt("popup_" + dialog_id, 0);
} else {
popup = 0;
}
if (popup == 0) {
if (isChannel) {
popup = preferences.getInt("popupChannel", 0);
} else {
popup = preferences.getInt((int) dialog_id < 0 ? "popupGroup" : "popupAll", 0);
}
} else if (popup == 1) {
popup = 3;
} else if (popup == 2) {
popup = 0;
}
}
2020-09-30 15:48:47 +02:00
if (popup != 0 && messageObject.messageOwner.peer_id.channel_id != 0 && !messageObject.isMegagroup()) {
2019-01-23 18:03:33 +01:00
popup = 0;
}
if (popup != 0) {
popupArrayAdd.add(0, messageObject);
}
return popup;
}
2020-09-30 15:48:47 +02:00
public void processEditedMessages(final LongSparseArray<ArrayList<MessageObject>> editedMessages) {
/*if (editedMessages.size() == 0) {
return;
}
final ArrayList<MessageObject> popupArrayAdd = new ArrayList<>(0);
notificationsQueue.postRunnable(() -> {
boolean added = false;
boolean edited = false;
LongSparseArray<Boolean> settingsCache = new LongSparseArray<>();
SharedPreferences preferences = getAccountInstance().getNotificationsSettings();
boolean allowPinned = preferences.getBoolean("PinnedMessages", true);
int popup = 0;
boolean hasScheduled = false;
for (int a = 0; a < messageObjects.size(); a++) {
MessageObject messageObject = messageObjects.get(a);
if (messageObject.messageOwner != null && messageObject.messageOwner.silent && (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionContactSignUp || messageObject.messageOwner.action instanceof TLRPC.TL_messageActionUserJoined)) {
continue;
}
long mid = messageObject.getId();
long random_id = messageObject.isFcmMessage() ? messageObject.messageOwner.random_id : 0;
long dialog_id = messageObject.getDialogId();
int lower_id = (int) dialog_id;
boolean isChannel;
if (messageObject.isFcmMessage()) {
isChannel = messageObject.localChannel;
} else if (lower_id < 0) {
TLRPC.Chat chat = getMessagesController().getChat(-lower_id);
isChannel = ChatObject.isChannel(chat) && !chat.megagroup;
} else {
isChannel = false;
}
if (messageObject.messageOwner.to_id.channel_id != 0) {
mid |= ((long) messageObject.messageOwner.to_id.channel_id) << 32;
}
MessageObject oldMessageObject = pushMessagesDict.get(mid);
if (oldMessageObject == null && messageObject.messageOwner.random_id != 0) {
oldMessageObject = fcmRandomMessagesDict.get(messageObject.messageOwner.random_id);
if (oldMessageObject != null) {
fcmRandomMessagesDict.remove(messageObject.messageOwner.random_id);
}
}
if (oldMessageObject != null) {
if (oldMessageObject.isFcmMessage()) {
pushMessagesDict.put(mid, messageObject);
int idxOld = pushMessages.indexOf(oldMessageObject);
if (idxOld >= 0) {
pushMessages.set(idxOld, messageObject);
popup = addToPopupMessages(popupArrayAdd, messageObject, lower_id, dialog_id, isChannel, preferences);
}
if (isFcm && (edited = messageObject.localEdit)) {
getMessagesStorage().putPushMessage(messageObject);
}
}
continue;
}
if (edited) {
continue;
}
if (isFcm) {
getMessagesStorage().putPushMessage(messageObject);
}
long original_dialog_id = dialog_id;
if (dialog_id == opened_dialog_id && ApplicationLoader.isScreenOn) {
if (!isFcm) {
playInChatSound();
}
continue;
}
if (messageObject.messageOwner.mentioned) {
if (!allowPinned && messageObject.messageOwner.action instanceof TLRPC.TL_messageActionPinMessage) {
continue;
}
dialog_id = messageObject.messageOwner.from_id;
}
if (isPersonalMessage(messageObject)) {
personal_count++;
}
added = true;
boolean isChat = lower_id < 0;
int index = settingsCache.indexOfKey(dialog_id);
boolean value;
if (index >= 0) {
value = settingsCache.valueAt(index);
} else {
int notifyOverride = getNotifyOverride(preferences, dialog_id);
if (notifyOverride == -1) {
value = isGlobalNotificationsEnabled(dialog_id, isChannel);
} else {
value = notifyOverride != 2;
}
settingsCache.put(dialog_id, value);
}
if (value) {
if (!isFcm) {
popup = addToPopupMessages(popupArrayAdd, messageObject, lower_id, dialog_id, isChannel, preferences);
}
if (!hasScheduled) {
hasScheduled = messageObject.messageOwner.from_scheduled;
}
delayedPushMessages.add(messageObject);
pushMessages.add(0, messageObject);
if (mid != 0) {
pushMessagesDict.put(mid, messageObject);
} else if (random_id != 0) {
fcmRandomMessagesDict.put(random_id, messageObject);
}
if (original_dialog_id != dialog_id) {
Integer current = pushDialogsOverrideMention.get(original_dialog_id);
pushDialogsOverrideMention.put(original_dialog_id, current == null ? 1 : current + 1);
}
}
}
if (added) {
notifyCheck = isLast;
}
if (!popupArrayAdd.isEmpty() && !AndroidUtilities.needShowPasscode() && !SharedConfig.isWaitingForPasscodeEnter) {
final int popupFinal = popup;
AndroidUtilities.runOnUIThread(() -> {
popupMessages.addAll(0, popupArrayAdd);
if (ApplicationLoader.mainInterfacePaused || !ApplicationLoader.isScreenOn) {
if (popupFinal == 3 || popupFinal == 1 && ApplicationLoader.isScreenOn || popupFinal == 2 && !ApplicationLoader.isScreenOn) {
Intent popupIntent = new Intent(ApplicationLoader.applicationContext, PopupNotificationActivity.class);
popupIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_FROM_BACKGROUND);
try {
ApplicationLoader.applicationContext.startActivity(popupIntent);
} catch (Throwable ignore) {
}
}
}
});
}
if (isFcm || hasScheduled) {
if (edited) {
delayedPushMessages.clear();
showOrUpdateNotification(notifyCheck);
} else if (added) {
MessageObject messageObject = messageObjects.get(0);
long dialog_id = messageObject.getDialogId();
Boolean isChannel;
if (messageObject.isFcmMessage()) {
isChannel = messageObject.localChannel;
} else {
isChannel = null;
}
int old_unread_count = total_unread_count;
int notifyOverride = getNotifyOverride(preferences, dialog_id);
boolean canAddValue;
if (notifyOverride == -1) {
canAddValue = isGlobalNotificationsEnabled(dialog_id, isChannel);
} else {
canAddValue = notifyOverride != 2;
}
Integer currentCount = pushDialogs.get(dialog_id);
int newCount = currentCount != null ? currentCount + 1 : 1;
if (notifyCheck && !canAddValue) {
Integer override = pushDialogsOverrideMention.get(dialog_id);
if (override != null && override != 0) {
canAddValue = true;
newCount = override;
}
}
if (canAddValue) {
if (currentCount != null) {
total_unread_count -= currentCount;
}
total_unread_count += newCount;
pushDialogs.put(dialog_id, newCount);
}
if (old_unread_count != total_unread_count) {
delayedPushMessages.clear();
showOrUpdateNotification(notifyCheck);
final int pushDialogsCount = pushDialogs.size();
AndroidUtilities.runOnUIThread(() -> {
NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.notificationsCountUpdated, currentAccount);
getNotificationCenter().postNotificationName(NotificationCenter.dialogsUnreadCounterChanged, pushDialogsCount);
});
}
notifyCheck = false;
if (showBadgeNumber) {
setBadge(getTotalAllUnreadCount());
}
}
}
if (countDownLatch != null) {
countDownLatch.countDown();
}
});*/
}
2019-01-23 18:03:33 +01:00
public void processNewMessages(final ArrayList<MessageObject> messageObjects, final boolean isLast, final boolean isFcm, CountDownLatch countDownLatch) {
2015-10-29 18:10:07 +01:00
if (messageObjects.isEmpty()) {
2019-01-23 18:03:33 +01:00
if (countDownLatch != null) {
countDownLatch.countDown();
}
2015-10-29 18:10:07 +01:00
return;
}
2018-07-30 04:07:02 +02:00
final ArrayList<MessageObject> popupArrayAdd = new ArrayList<>(0);
2018-08-27 10:33:11 +02:00
notificationsQueue.postRunnable(() -> {
boolean added = false;
2019-05-14 14:08:05 +02:00
boolean edited = false;
2015-10-29 18:10:07 +01:00
2018-08-27 10:33:11 +02:00
LongSparseArray<Boolean> settingsCache = new LongSparseArray<>();
2019-07-18 15:01:39 +02:00
SharedPreferences preferences = getAccountInstance().getNotificationsSettings();
2018-08-27 10:33:11 +02:00
boolean allowPinned = preferences.getBoolean("PinnedMessages", true);
int popup = 0;
2019-09-10 12:56:11 +02:00
boolean hasScheduled = false;
2018-08-27 10:33:11 +02:00
for (int a = 0; a < messageObjects.size(); a++) {
MessageObject messageObject = messageObjects.get(a);
2019-05-14 14:08:05 +02:00
if (messageObject.messageOwner != null && messageObject.messageOwner.silent && (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionContactSignUp || messageObject.messageOwner.action instanceof TLRPC.TL_messageActionUserJoined)) {
continue;
}
2018-08-27 10:33:11 +02:00
long mid = messageObject.getId();
long random_id = messageObject.isFcmMessage() ? messageObject.messageOwner.random_id : 0;
2019-01-23 18:03:33 +01:00
long dialog_id = messageObject.getDialogId();
int lower_id = (int) dialog_id;
boolean isChannel;
2020-04-24 11:21:58 +02:00
if (messageObject.isFcmMessage()) {
isChannel = messageObject.localChannel;
} else if (lower_id < 0) {
TLRPC.Chat chat = getMessagesController().getChat(-lower_id);
isChannel = ChatObject.isChannel(chat) && !chat.megagroup;
2019-01-23 18:03:33 +01:00
} else {
isChannel = false;
2018-08-27 10:33:11 +02:00
}
2020-09-30 15:48:47 +02:00
if (messageObject.messageOwner.peer_id.channel_id != 0) {
mid |= ((long) messageObject.messageOwner.peer_id.channel_id) << 32;
2020-04-24 11:21:58 +02:00
}
2019-01-23 18:03:33 +01:00
2018-08-27 10:33:11 +02:00
MessageObject oldMessageObject = pushMessagesDict.get(mid);
if (oldMessageObject == null && messageObject.messageOwner.random_id != 0) {
oldMessageObject = fcmRandomMessagesDict.get(messageObject.messageOwner.random_id);
2018-07-30 04:07:02 +02:00
if (oldMessageObject != null) {
2018-08-27 10:33:11 +02:00
fcmRandomMessagesDict.remove(messageObject.messageOwner.random_id);
2015-10-29 18:10:07 +01:00
}
2018-08-27 10:33:11 +02:00
}
if (oldMessageObject != null) {
if (oldMessageObject.isFcmMessage()) {
pushMessagesDict.put(mid, messageObject);
int idxOld = pushMessages.indexOf(oldMessageObject);
if (idxOld >= 0) {
pushMessages.set(idxOld, messageObject);
2019-01-23 18:03:33 +01:00
popup = addToPopupMessages(popupArrayAdd, messageObject, lower_id, dialog_id, isChannel, preferences);
2018-07-30 04:07:02 +02:00
}
2019-05-14 14:08:05 +02:00
if (isFcm && (edited = messageObject.localEdit)) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().putPushMessage(messageObject);
2019-05-14 14:08:05 +02:00
}
2015-10-29 18:10:07 +01:00
}
2018-08-27 10:33:11 +02:00
continue;
}
2019-05-14 14:08:05 +02:00
if (edited) {
continue;
}
2019-01-23 18:03:33 +01:00
if (isFcm) {
2019-07-18 15:01:39 +02:00
getMessagesStorage().putPushMessage(messageObject);
2019-01-23 18:03:33 +01:00
}
2018-08-27 10:33:11 +02:00
long original_dialog_id = dialog_id;
if (dialog_id == opened_dialog_id && ApplicationLoader.isScreenOn) {
if (!isFcm) {
playInChatSound();
2015-10-29 18:10:07 +01:00
}
2018-08-27 10:33:11 +02:00
continue;
}
if (messageObject.messageOwner.mentioned) {
if (!allowPinned && messageObject.messageOwner.action instanceof TLRPC.TL_messageActionPinMessage) {
continue;
2015-10-29 18:10:07 +01:00
}
2020-09-30 15:48:47 +02:00
dialog_id = messageObject.getFromChatId();
2018-08-27 10:33:11 +02:00
}
if (isPersonalMessage(messageObject)) {
personal_count++;
}
added = true;
2015-10-29 18:10:07 +01:00
2018-08-27 10:33:11 +02:00
boolean isChat = lower_id < 0;
int index = settingsCache.indexOfKey(dialog_id);
boolean value;
if (index >= 0) {
value = settingsCache.valueAt(index);
} else {
int notifyOverride = getNotifyOverride(preferences, dialog_id);
if (notifyOverride == -1) {
2020-04-24 11:21:58 +02:00
value = isGlobalNotificationsEnabled(dialog_id, isChannel);
/*if (BuildVars.DEBUG_PRIVATE_VERSION && BuildVars.LOGS_ENABLED) {
FileLog.d("global notify settings for " + dialog_id + " = " + value);
}*/
2018-07-30 04:07:02 +02:00
} else {
2018-08-27 10:33:11 +02:00
value = notifyOverride != 2;
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
settingsCache.put(dialog_id, value);
}
2019-01-23 18:03:33 +01:00
2018-08-27 10:33:11 +02:00
if (value) {
2019-01-23 18:03:33 +01:00
if (!isFcm) {
popup = addToPopupMessages(popupArrayAdd, messageObject, lower_id, dialog_id, isChannel, preferences);
2018-08-27 10:33:11 +02:00
}
2019-09-10 12:56:11 +02:00
if (!hasScheduled) {
hasScheduled = messageObject.messageOwner.from_scheduled;
}
2018-08-27 10:33:11 +02:00
delayedPushMessages.add(messageObject);
pushMessages.add(0, messageObject);
if (mid != 0) {
pushMessagesDict.put(mid, messageObject);
} else if (random_id != 0) {
fcmRandomMessagesDict.put(random_id, messageObject);
}
if (original_dialog_id != dialog_id) {
2019-01-23 18:03:33 +01:00
Integer current = pushDialogsOverrideMention.get(original_dialog_id);
pushDialogsOverrideMention.put(original_dialog_id, current == null ? 1 : current + 1);
2018-08-27 10:33:11 +02:00
}
2015-10-29 18:10:07 +01:00
}
2018-08-27 10:33:11 +02:00
}
2018-07-30 04:07:02 +02:00
2018-08-27 10:33:11 +02:00
if (added) {
notifyCheck = isLast;
}
2020-01-23 07:15:40 +01:00
if (!popupArrayAdd.isEmpty() && !AndroidUtilities.needShowPasscode() && !SharedConfig.isWaitingForPasscodeEnter) {
2018-08-27 10:33:11 +02:00
final int popupFinal = popup;
AndroidUtilities.runOnUIThread(() -> {
popupMessages.addAll(0, popupArrayAdd);
2019-12-31 14:08:08 +01:00
if (ApplicationLoader.mainInterfacePaused || !ApplicationLoader.isScreenOn) {
2018-08-27 10:33:11 +02:00
if (popupFinal == 3 || popupFinal == 1 && ApplicationLoader.isScreenOn || popupFinal == 2 && !ApplicationLoader.isScreenOn) {
Intent popupIntent = new Intent(ApplicationLoader.applicationContext, PopupNotificationActivity.class);
popupIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_FROM_BACKGROUND);
2019-03-03 21:40:48 +01:00
try {
ApplicationLoader.applicationContext.startActivity(popupIntent);
} catch (Throwable ignore) {
}
2018-07-30 04:07:02 +02:00
}
}
2018-08-27 10:33:11 +02:00
});
}
2019-09-10 12:56:11 +02:00
if (isFcm || hasScheduled) {
2019-05-14 14:08:05 +02:00
if (edited) {
delayedPushMessages.clear();
showOrUpdateNotification(notifyCheck);
} else if (added) {
2020-04-24 11:21:58 +02:00
MessageObject messageObject = messageObjects.get(0);
long dialog_id = messageObject.getDialogId();
Boolean isChannel;
if (messageObject.isFcmMessage()) {
isChannel = messageObject.localChannel;
} else {
isChannel = null;
}
2019-05-14 14:08:05 +02:00
int old_unread_count = total_unread_count;
2018-08-27 10:33:11 +02:00
2019-05-14 14:08:05 +02:00
int notifyOverride = getNotifyOverride(preferences, dialog_id);
boolean canAddValue;
if (notifyOverride == -1) {
2020-04-24 11:21:58 +02:00
canAddValue = isGlobalNotificationsEnabled(dialog_id, isChannel);
/*if (BuildVars.DEBUG_PRIVATE_VERSION && BuildVars.LOGS_ENABLED) {
FileLog.d("global notify settings for " + dialog_id + " = " + canAddValue);
}*/
2019-05-14 14:08:05 +02:00
} else {
canAddValue = notifyOverride != 2;
}
2018-07-30 04:07:02 +02:00
2019-05-14 14:08:05 +02:00
Integer currentCount = pushDialogs.get(dialog_id);
2020-07-26 10:03:38 +02:00
int newCount = currentCount != null ? currentCount + 1 : 1;
2018-07-30 04:07:02 +02:00
2019-05-14 14:08:05 +02:00
if (notifyCheck && !canAddValue) {
Integer override = pushDialogsOverrideMention.get(dialog_id);
if (override != null && override != 0) {
canAddValue = true;
newCount = override;
}
2019-01-23 18:03:33 +01:00
}
2019-05-14 14:08:05 +02:00
if (canAddValue) {
if (currentCount != null) {
total_unread_count -= currentCount;
}
total_unread_count += newCount;
pushDialogs.put(dialog_id, newCount);
}
if (old_unread_count != total_unread_count) {
delayedPushMessages.clear();
showOrUpdateNotification(notifyCheck);
final int pushDialogsCount = pushDialogs.size();
AndroidUtilities.runOnUIThread(() -> {
NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.notificationsCountUpdated, currentAccount);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsUnreadCounterChanged, pushDialogsCount);
2019-05-14 14:08:05 +02:00
});
}
notifyCheck = false;
if (showBadgeNumber) {
setBadge(getTotalAllUnreadCount());
2018-07-30 04:07:02 +02:00
}
}
2015-10-29 18:10:07 +01:00
}
2019-01-23 18:03:33 +01:00
if (countDownLatch != null) {
countDownLatch.countDown();
}
2015-10-29 18:10:07 +01:00
});
}
2018-07-30 04:07:02 +02:00
public int getTotalUnreadCount() {
return total_unread_count;
}
public void processDialogsUpdateRead(final LongSparseArray<Integer> dialogsToUpdate) {
final ArrayList<MessageObject> popupArrayToRemove = new ArrayList<>();
2018-08-27 10:33:11 +02:00
notificationsQueue.postRunnable(() -> {
int old_unread_count = total_unread_count;
2019-07-18 15:01:39 +02:00
SharedPreferences preferences = getAccountInstance().getNotificationsSettings();
2018-08-27 10:33:11 +02:00
for (int b = 0; b < dialogsToUpdate.size(); b++) {
long dialog_id = dialogsToUpdate.keyAt(b);
2015-10-29 18:10:07 +01:00
2018-08-27 10:33:11 +02:00
int notifyOverride = getNotifyOverride(preferences, dialog_id);
boolean canAddValue;
if (notifyOverride == -1) {
2019-01-23 18:03:33 +01:00
canAddValue = isGlobalNotificationsEnabled(dialog_id);
2018-08-27 10:33:11 +02:00
} else {
canAddValue = notifyOverride != 2;
}
Integer currentCount = pushDialogs.get(dialog_id);
Integer newCount = dialogsToUpdate.get(dialog_id);
2019-01-23 18:03:33 +01:00
if (notifyCheck && !canAddValue) {
Integer override = pushDialogsOverrideMention.get(dialog_id);
if (override != null && override != 0) {
canAddValue = true;
newCount = override;
}
}
2018-08-27 10:33:11 +02:00
if (newCount == 0) {
smartNotificationsDialogs.remove(dialog_id);
}
if (newCount < 0) {
if (currentCount == null) {
continue;
2015-10-29 18:10:07 +01:00
}
2018-08-27 10:33:11 +02:00
newCount = currentCount + newCount;
}
if (canAddValue || newCount == 0) {
if (currentCount != null) {
total_unread_count -= currentCount;
2015-10-29 18:10:07 +01:00
}
}
2018-08-27 10:33:11 +02:00
if (newCount == 0) {
pushDialogs.remove(dialog_id);
pushDialogsOverrideMention.remove(dialog_id);
for (int a = 0; a < pushMessages.size(); a++) {
MessageObject messageObject = pushMessages.get(a);
2019-09-10 12:56:11 +02:00
if (!messageObject.messageOwner.from_scheduled && messageObject.getDialogId() == dialog_id) {
2018-08-27 10:33:11 +02:00
if (isPersonalMessage(messageObject)) {
personal_count--;
}
pushMessages.remove(a);
a--;
delayedPushMessages.remove(messageObject);
long mid = messageObject.getId();
2020-09-30 15:48:47 +02:00
if (messageObject.messageOwner.peer_id.channel_id != 0) {
mid |= ((long) messageObject.messageOwner.peer_id.channel_id) << 32;
2018-07-30 04:07:02 +02:00
}
2018-08-27 10:33:11 +02:00
pushMessagesDict.remove(mid);
popupArrayToRemove.add(messageObject);
2015-10-29 18:10:07 +01:00
}
}
2018-08-27 10:33:11 +02:00
} else if (canAddValue) {
total_unread_count += newCount;
pushDialogs.put(dialog_id, newCount);
2015-10-29 18:10:07 +01:00
}
2018-08-27 10:33:11 +02:00
}
if (!popupArrayToRemove.isEmpty()) {
AndroidUtilities.runOnUIThread(() -> {
for (int a = 0, size = popupArrayToRemove.size(); a < size; a++) {
popupMessages.remove(popupArrayToRemove.get(a));
}
2020-07-26 10:03:38 +02:00
NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.pushMessagesUpdated);
2018-08-27 10:33:11 +02:00
});
}
if (old_unread_count != total_unread_count) {
if (!notifyCheck) {
delayedPushMessages.clear();
showOrUpdateNotification(notifyCheck);
} else {
2019-07-18 15:01:39 +02:00
scheduleNotificationDelay(lastOnlineFromOtherDevice > getConnectionsManager().getCurrentTime());
2015-10-29 18:10:07 +01:00
}
2018-08-27 10:33:11 +02:00
final int pushDialogsCount = pushDialogs.size();
AndroidUtilities.runOnUIThread(() -> {
NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.notificationsCountUpdated, currentAccount);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsUnreadCounterChanged, pushDialogsCount);
2018-08-27 10:33:11 +02:00
});
}
notifyCheck = false;
if (showBadgeNumber) {
setBadge(getTotalAllUnreadCount());
2015-10-29 18:10:07 +01:00
}
});
}
2019-01-23 18:03:33 +01:00
public void processLoadedUnreadMessages(final LongSparseArray<Integer> dialogs, final ArrayList<TLRPC.Message> messages, ArrayList<MessageObject> push, final ArrayList<TLRPC.User> users, final ArrayList<TLRPC.Chat> chats, final ArrayList<TLRPC.EncryptedChat> encryptedChats) {
2019-07-18 15:01:39 +02:00
getMessagesController().putUsers(users, true);
getMessagesController().putChats(chats, true);
getMessagesController().putEncryptedChats(encryptedChats, true);
2015-10-29 18:10:07 +01:00
2018-08-27 10:33:11 +02:00
notificationsQueue.postRunnable(() -> {
pushDialogs.clear();
pushMessages.clear();
pushMessagesDict.clear();
total_unread_count = 0;
personal_count = 0;
2019-07-18 15:01:39 +02:00
SharedPreferences preferences = getAccountInstance().getNotificationsSettings();
2018-08-27 10:33:11 +02:00
LongSparseArray<Boolean> settingsCache = new LongSparseArray<>();
if (messages != null) {
for (int a = 0; a < messages.size(); a++) {
TLRPC.Message message = messages.get(a);
2019-05-14 14:08:05 +02:00
if (message != null && message.silent && (message.action instanceof TLRPC.TL_messageActionContactSignUp || message.action instanceof TLRPC.TL_messageActionUserJoined)) {
continue;
}
2018-08-27 10:33:11 +02:00
long mid = message.id;
2020-09-30 15:48:47 +02:00
if (message.peer_id.channel_id != 0) {
mid |= ((long) message.peer_id.channel_id) << 32;
2018-08-27 10:33:11 +02:00
}
if (pushMessagesDict.indexOfKey(mid) >= 0) {
continue;
}
2020-08-14 18:58:22 +02:00
MessageObject messageObject = new MessageObject(currentAccount, message, false, false);
2018-08-27 10:33:11 +02:00
if (isPersonalMessage(messageObject)) {
personal_count++;
}
long dialog_id = messageObject.getDialogId();
long original_dialog_id = dialog_id;
if (messageObject.messageOwner.mentioned) {
2020-09-30 15:48:47 +02:00
dialog_id = messageObject.getFromChatId();
2015-10-29 18:10:07 +01:00
}
2018-07-30 04:07:02 +02:00
int index = settingsCache.indexOfKey(dialog_id);
boolean value;
if (index >= 0) {
value = settingsCache.valueAt(index);
} else {
2015-10-29 18:10:07 +01:00
int notifyOverride = getNotifyOverride(preferences, dialog_id);
2018-07-30 04:07:02 +02:00
if (notifyOverride == -1) {
2019-01-23 18:03:33 +01:00
value = isGlobalNotificationsEnabled(dialog_id);
2018-07-30 04:07:02 +02:00
} else {
value = notifyOverride != 2;
}
2015-10-29 18:10:07 +01:00
settingsCache.put(dialog_id, value);
}
2018-08-27 10:33:11 +02:00
if (!value || dialog_id == opened_dialog_id && ApplicationLoader.isScreenOn) {
2015-10-29 18:10:07 +01:00
continue;
}
2018-08-27 10:33:11 +02:00
pushMessagesDict.put(mid, messageObject);
pushMessages.add(0, messageObject);
if (original_dialog_id != dialog_id) {
2019-01-23 18:03:33 +01:00
Integer current = pushDialogsOverrideMention.get(original_dialog_id);
pushDialogsOverrideMention.put(original_dialog_id, current == null ? 1 : current + 1);
2018-08-27 10:33:11 +02:00
}
2015-10-29 18:10:07 +01:00
}
2018-08-27 10:33:11 +02:00
}
for (int a = 0; a < dialogs.size(); a++) {
long dialog_id = dialogs.keyAt(a);
int index = settingsCache.indexOfKey(dialog_id);
boolean value;
if (index >= 0) {
value = settingsCache.valueAt(index);
} else {
int notifyOverride = getNotifyOverride(preferences, dialog_id);
if (notifyOverride == -1) {
2019-01-23 18:03:33 +01:00
value = isGlobalNotificationsEnabled(dialog_id);
2018-08-27 10:33:11 +02:00
} else {
value = notifyOverride != 2;
}
2019-01-23 18:03:33 +01:00
/*if (!value) {
Integer override = pushDialogsOverrideMention.get(dialog_id);
if (override != null && override != 0) {
value = true;
newCount = override;
}
}*/
2018-08-27 10:33:11 +02:00
settingsCache.put(dialog_id, value);
}
if (!value) {
continue;
}
int count = dialogs.valueAt(a);
pushDialogs.put(dialog_id, count);
total_unread_count += count;
}
2019-01-23 18:03:33 +01:00
if (push != null) {
for (int a = 0; a < push.size(); a++) {
MessageObject messageObject = push.get(a);
long mid = messageObject.getId();
2020-09-30 15:48:47 +02:00
if (messageObject.messageOwner.peer_id.channel_id != 0) {
mid |= ((long) messageObject.messageOwner.peer_id.channel_id) << 32;
2019-01-23 18:03:33 +01:00
}
if (pushMessagesDict.indexOfKey(mid) >= 0) {
continue;
}
if (isPersonalMessage(messageObject)) {
personal_count++;
}
long dialog_id = messageObject.getDialogId();
long original_dialog_id = dialog_id;
long random_id = messageObject.messageOwner.random_id;
if (messageObject.messageOwner.mentioned) {
2020-09-30 15:48:47 +02:00
dialog_id = messageObject.getFromChatId();
2019-01-23 18:03:33 +01:00
}
int index = settingsCache.indexOfKey(dialog_id);
boolean value;
if (index >= 0) {
value = settingsCache.valueAt(index);
} else {
int notifyOverride = getNotifyOverride(preferences, dialog_id);
if (notifyOverride == -1) {
value = isGlobalNotificationsEnabled(dialog_id);
} else {
value = notifyOverride != 2;
}
settingsCache.put(dialog_id, value);
}
if (!value || dialog_id == opened_dialog_id && ApplicationLoader.isScreenOn) {
continue;
}
if (mid != 0) {
pushMessagesDict.put(mid, messageObject);
} else if (random_id != 0) {
fcmRandomMessagesDict.put(random_id, messageObject);
}
pushMessages.add(0, messageObject);
if (original_dialog_id != dialog_id) {
Integer current = pushDialogsOverrideMention.get(original_dialog_id);
pushDialogsOverrideMention.put(original_dialog_id, current == null ? 1 : current + 1);
}
Integer currentCount = pushDialogs.get(dialog_id);
2020-07-26 10:03:38 +02:00
int newCount = currentCount != null ? currentCount + 1 : 1;
2019-01-23 18:03:33 +01:00
if (currentCount != null) {
total_unread_count -= currentCount;
}
total_unread_count += newCount;
pushDialogs.put(dialog_id, newCount);
}
}
2018-08-27 10:33:11 +02:00
final int pushDialogsCount = pushDialogs.size();
AndroidUtilities.runOnUIThread(() -> {
if (total_unread_count == 0) {
popupMessages.clear();
NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.pushMessagesUpdated);
2015-10-29 18:10:07 +01:00
}
2018-08-27 10:33:11 +02:00
NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.notificationsCountUpdated, currentAccount);
2019-07-18 15:01:39 +02:00
getNotificationCenter().postNotificationName(NotificationCenter.dialogsUnreadCounterChanged, pushDialogsCount);
2018-08-27 10:33:11 +02:00
});
showOrUpdateNotification(SystemClock.elapsedRealtime() / 1000 < 60);
if (showBadgeNumber) {
setBadge(getTotalAllUnreadCount());
2015-10-29 18:10:07 +01:00
}
});
}
2018-07-30 04:07:02 +02:00
private int getTotalAllUnreadCount() {
int count = 0;
for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
if (UserConfig.getInstance(a).isClientActivated()) {
NotificationsController controller = getInstance(a);
if (controller.showBadgeNumber) {
2019-01-23 18:03:33 +01:00
if (controller.showBadgeMessages) {
if (controller.showBadgeMuted) {
try {
2019-05-14 14:08:05 +02:00
for (int i = 0, N = MessagesController.getInstance(a).allDialogs.size(); i < N; i++) {
TLRPC.Dialog dialog = MessagesController.getInstance(a).allDialogs.get(i);
2019-01-23 18:03:33 +01:00
if (dialog.unread_count != 0) {
count += dialog.unread_count;
}
}
} catch (Exception e) {
FileLog.e(e);
}
} else {
count += controller.total_unread_count;
}
} else {
if (controller.showBadgeMuted) {
try {
2019-05-14 14:08:05 +02:00
for (int i = 0, N = MessagesController.getInstance(a).allDialogs.size(); i < N; i++) {
TLRPC.Dialog dialog = MessagesController.getInstance(a).allDialogs.get(i);
2019-01-23 18:03:33 +01:00
if (dialog.unread_count != 0) {
count++;
}
}
} catch (Exception e) {
FileLog.e(e);
}
} else {
count += controller.pushDialogs.size();
}
}
2018-07-30 04:07:02 +02:00
}
}
}
return count;
2015-10-29 18:10:07 +01:00
}
2019-01-23 18:03:33 +01:00
public void updateBadge() {
2018-08-27 10:33:11 +02:00
notificationsQueue.postRunnable(() -> setBadge(getTotalAllUnreadCount()));
2014-07-10 02:15:58 +02:00
}
2018-07-30 04:07:02 +02:00
private void setBadge(final int count) {
if (lastBadgeCount == count) {
return;
}
lastBadgeCount = count;
NotificationBadge.applyCount(count);
}
2019-05-14 14:08:05 +02:00
private String getShortStringForMessage(MessageObject messageObject, String[] userName, boolean[] preview) {
2020-01-23 07:15:40 +01:00
if (AndroidUtilities.needShowPasscode() || SharedConfig.isWaitingForPasscodeEnter) {
2019-12-31 14:08:08 +01:00
return LocaleController.getString("NotificationHiddenMessage", R.string.NotificationHiddenMessage);
2018-07-30 04:07:02 +02:00
}
2020-09-30 15:48:47 +02:00
long dialogId = messageObject.messageOwner.dialog_id;
int chat_id = messageObject.messageOwner.peer_id.chat_id != 0 ? messageObject.messageOwner.peer_id.chat_id : messageObject.messageOwner.peer_id.channel_id;
int fromId = messageObject.messageOwner.peer_id.user_id;
2019-01-23 18:03:33 +01:00
if (preview != null) {
preview[0] = true;
}
2019-07-18 15:01:39 +02:00
SharedPreferences preferences = getAccountInstance().getNotificationsSettings();
2020-09-30 15:48:47 +02:00
boolean dialogPreviewEnabled = preferences.getBoolean("content_preview_" + dialogId, true);
2018-07-30 04:07:02 +02:00
if (messageObject.isFcmMessage()) {
2020-09-30 15:48:47 +02:00
if (chat_id == 0 && fromId != 0) {
2019-07-18 15:01:39 +02:00
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O_MR1) {
userName[0] = messageObject.localName;
}
if (!dialogPreviewEnabled || !preferences.getBoolean("EnablePreviewAll", true)) {
2019-01-23 18:03:33 +01:00
if (preview != null) {
preview[0] = false;
}
2019-07-18 15:01:39 +02:00
return LocaleController.getString("Message", R.string.Message);
2018-07-30 04:07:02 +02:00
}
2019-07-18 15:01:39 +02:00
} else if (chat_id != 0) {
2020-09-30 15:48:47 +02:00
if (messageObject.messageOwner.peer_id.channel_id == 0 || messageObject.isMegagroup()) {
2019-07-18 15:01:39 +02:00
userName[0] = messageObject.localUserName;
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O_MR1) {
2018-07-30 04:07:02 +02:00
userName[0] = messageObject.localName;
}
2019-07-18 15:01:39 +02:00
if (!dialogPreviewEnabled || !messageObject.localChannel && !preferences.getBoolean("EnablePreviewGroup", true) || messageObject.localChannel && !preferences.getBoolean("EnablePreviewChannel", true)) {
2019-01-23 18:03:33 +01:00
if (preview != null) {
preview[0] = false;
}
2020-09-30 15:48:47 +02:00
if (!messageObject.isMegagroup() && messageObject.messageOwner.peer_id.channel_id != 0) {
2018-07-30 04:07:02 +02:00
return LocaleController.formatString("ChannelMessageNoText", R.string.ChannelMessageNoText, messageObject.localName);
} else {
return LocaleController.formatString("NotificationMessageGroupNoText", R.string.NotificationMessageGroupNoText, messageObject.localUserName, messageObject.localName);
}
}
}
return messageObject.messageOwner.message;
}
2020-09-30 15:48:47 +02:00
if (fromId == 0) {
2018-07-30 04:07:02 +02:00
if (messageObject.isFromUser() || messageObject.getId() < 0) {
2020-09-30 15:48:47 +02:00
fromId = messageObject.getFromChatId();
2018-07-30 04:07:02 +02:00
} else {
2020-09-30 15:48:47 +02:00
fromId = -chat_id;
2018-07-30 04:07:02 +02:00
}
2020-09-30 15:48:47 +02:00
} else if (fromId == getUserConfig().getClientUserId()) {
fromId = messageObject.getFromChatId();
2018-07-30 04:07:02 +02:00
}
2020-09-30 15:48:47 +02:00
if (dialogId == 0) {
2018-07-30 04:07:02 +02:00
if (chat_id != 0) {
2020-09-30 15:48:47 +02:00
dialogId = -chat_id;
} else if (fromId != 0) {
dialogId = fromId;
2018-07-30 04:07:02 +02:00
}
}
String name = null;
2020-09-30 15:48:47 +02:00
if (UserObject.isReplyUser(dialogId) && messageObject.messageOwner.fwd_from != null && messageObject.messageOwner.fwd_from.from_id != null) {
fromId = MessageObject.getPeerId(messageObject.messageOwner.fwd_from.from_id);
}
if (fromId > 0) {
TLRPC.User user = getMessagesController().getUser(fromId);
2018-07-30 04:07:02 +02:00
if (user != null) {
name = UserObject.getUserName(user);
if (chat_id != 0) {
userName[0] = name;
} else {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O_MR1) {
userName[0] = name;
} else {
userName[0] = null;
}
}
}
} else {
2020-09-30 15:48:47 +02:00
TLRPC.Chat chat = getMessagesController().getChat(-fromId);
2018-07-30 04:07:02 +02:00
if (chat != null) {
name = chat.title;
userName[0] = name;
}
}
2020-09-30 15:48:47 +02:00
if (name != null && fromId > 0 && UserObject.isReplyUser(dialogId) && messageObject.messageOwner.fwd_from != null && messageObject.messageOwner.fwd_from.saved_from_peer != null) {
int id = MessageObject.getPeerId(messageObject.messageOwner.fwd_from.saved_from_peer);
if (id < 0) {
TLRPC.Chat chat = getMessagesController().getChat(-id);
if (chat != null) {
name += " @ " + chat.title;
if (userName[0] != null) {
userName[0] = name;
}
}
}
}
2018-07-30 04:07:02 +02:00
if (name == null) {
return null;
}
TLRPC.Chat chat = null;
if (chat_id != 0) {
2019-07-18 15:01:39 +02:00
chat = getMessagesController().getChat(chat_id);
2018-07-30 04:07:02 +02:00
if (chat == null) {
return null;
} else if (ChatObject.isChannel(chat) && !chat.megagroup) {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1) {
userName[0] = null;
}
}
}
String msg = null;
2020-09-30 15:48:47 +02:00
if ((int) dialogId == 0) {
2019-01-23 18:03:33 +01:00
userName[0] = null;
2019-12-31 14:08:08 +01:00
return LocaleController.getString("NotificationHiddenMessage", R.string.NotificationHiddenMessage);
2018-07-30 04:07:02 +02:00
} else {
2019-01-23 18:03:33 +01:00
boolean isChannel = ChatObject.isChannel(chat) && !chat.megagroup;
2020-09-30 15:48:47 +02:00
if (dialogPreviewEnabled && (chat_id == 0 && fromId != 0 && preferences.getBoolean("EnablePreviewAll", true) || chat_id != 0 && (!isChannel && preferences.getBoolean("EnablePreviewGroup", true) || isChannel && preferences.getBoolean("EnablePreviewChannel", true)))) {
2018-07-30 04:07:02 +02:00
if (messageObject.messageOwner instanceof TLRPC.TL_messageService) {
userName[0] = null;
2019-01-23 18:03:33 +01:00
if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionUserJoined || messageObject.messageOwner.action instanceof TLRPC.TL_messageActionContactSignUp) {
2018-07-30 04:07:02 +02:00
return LocaleController.formatString("NotificationContactJoined", R.string.NotificationContactJoined, name);
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionUserUpdatedPhoto) {
return LocaleController.formatString("NotificationContactNewPhoto", R.string.NotificationContactNewPhoto, name);
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionLoginUnknownLocation) {
String date = LocaleController.formatString("formatDateAtTime", R.string.formatDateAtTime, LocaleController.getInstance().formatterYear.format(((long) messageObject.messageOwner.date) * 1000), LocaleController.getInstance().formatterDay.format(((long) messageObject.messageOwner.date) * 1000));
2019-07-18 15:01:39 +02:00
return LocaleController.formatString("NotificationUnrecognizedDevice", R.string.NotificationUnrecognizedDevice, getUserConfig().getCurrentUser().first_name, date, messageObject.messageOwner.action.title, messageObject.messageOwner.action.address);
2018-07-30 04:07:02 +02:00
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionGameScore || messageObject.messageOwner.action instanceof TLRPC.TL_messageActionPaymentSent) {
return messageObject.messageText.toString();
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionPhoneCall) {
2020-08-14 18:58:22 +02:00
if (messageObject.messageOwner.action.video) {
return LocaleController.getString("CallMessageVideoIncomingMissed", R.string.CallMessageVideoIncomingMissed);
} else {
return LocaleController.getString("CallMessageIncomingMissed", R.string.CallMessageIncomingMissed);
}
2018-07-30 04:07:02 +02:00
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChatAddUser) {
int singleUserId = messageObject.messageOwner.action.user_id;
if (singleUserId == 0 && messageObject.messageOwner.action.users.size() == 1) {
singleUserId = messageObject.messageOwner.action.users.get(0);
}
if (singleUserId != 0) {
2020-09-30 15:48:47 +02:00
if (messageObject.messageOwner.peer_id.channel_id != 0 && !chat.megagroup) {
2018-07-30 04:07:02 +02:00
return LocaleController.formatString("ChannelAddedByNotification", R.string.ChannelAddedByNotification, name, chat.title);
} else {
2019-07-18 15:01:39 +02:00
if (singleUserId == getUserConfig().getClientUserId()) {
2018-07-30 04:07:02 +02:00
return LocaleController.formatString("NotificationInvitedToGroup", R.string.NotificationInvitedToGroup, name, chat.title);
} else {
2019-07-18 15:01:39 +02:00
TLRPC.User u2 = getMessagesController().getUser(singleUserId);
2018-07-30 04:07:02 +02:00
if (u2 == null) {
return null;
}
2020-09-30 15:48:47 +02:00
if (fromId == u2.id) {
2018-07-30 04:07:02 +02:00
if (chat.megagroup) {
return LocaleController.formatString("NotificationGroupAddSelfMega", R.string.NotificationGroupAddSelfMega, name, chat.title);
} else {
return LocaleController.formatString("NotificationGroupAddSelf", R.string.NotificationGroupAddSelf, name, chat.title);
}
} else {
return LocaleController.formatString("NotificationGroupAddMember", R.string.NotificationGroupAddMember, name, chat.title, UserObject.getUserName(u2));
}
}
}
} else {
2019-01-23 18:03:33 +01:00
StringBuilder names = new StringBuilder();
2018-07-30 04:07:02 +02:00
for (int a = 0; a < messageObject.messageOwner.action.users.size(); a++) {
2019-07-18 15:01:39 +02:00
TLRPC.User user = getMessagesController().getUser(messageObject.messageOwner.action.users.get(a));
2018-07-30 04:07:02 +02:00
if (user != null) {
String name2 = UserObject.getUserName(user);
if (names.length() != 0) {
names.append(", ");
}
names.append(name2);
}
}
return LocaleController.formatString("NotificationGroupAddMember", R.string.NotificationGroupAddMember, name, chat.title, names.toString());
}
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChatJoinedByLink) {
return LocaleController.formatString("NotificationInvitedToGroupByLink", R.string.NotificationInvitedToGroupByLink, name, chat.title);
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChatEditTitle) {
return LocaleController.formatString("NotificationEditedGroupName", R.string.NotificationEditedGroupName, name, messageObject.messageOwner.action.title);
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChatEditPhoto || messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChatDeletePhoto) {
2020-09-30 15:48:47 +02:00
if (messageObject.messageOwner.peer_id.channel_id != 0 && !chat.megagroup) {
2020-07-26 10:03:38 +02:00
if (messageObject.isVideoAvatar()) {
return LocaleController.formatString("ChannelVideoEditNotification", R.string.ChannelVideoEditNotification, chat.title);
} else {
return LocaleController.formatString("ChannelPhotoEditNotification", R.string.ChannelPhotoEditNotification, chat.title);
}
2018-07-30 04:07:02 +02:00
} else {
2020-07-26 10:03:38 +02:00
if (messageObject.isVideoAvatar()) {
return LocaleController.formatString("NotificationEditedGroupVideo", R.string.NotificationEditedGroupVideo, name, chat.title);
} else {
return LocaleController.formatString("NotificationEditedGroupPhoto", R.string.NotificationEditedGroupPhoto, name, chat.title);
}
2018-07-30 04:07:02 +02:00
}
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChatDeleteUser) {
2019-07-18 15:01:39 +02:00
if (messageObject.messageOwner.action.user_id == getUserConfig().getClientUserId()) {
2018-07-30 04:07:02 +02:00
return LocaleController.formatString("NotificationGroupKickYou", R.string.NotificationGroupKickYou, name, chat.title);
2020-09-30 15:48:47 +02:00
} else if (messageObject.messageOwner.action.user_id == fromId) {
2018-07-30 04:07:02 +02:00
return LocaleController.formatString("NotificationGroupLeftMember", R.string.NotificationGroupLeftMember, name, chat.title);
} else {
2019-07-18 15:01:39 +02:00
TLRPC.User u2 = getMessagesController().getUser(messageObject.messageOwner.action.user_id);
2018-07-30 04:07:02 +02:00
if (u2 == null) {
return null;
}
return LocaleController.formatString("NotificationGroupKickMember", R.string.NotificationGroupKickMember, name, chat.title, UserObject.getUserName(u2));
}
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChatCreate) {
return messageObject.messageText.toString();
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChannelCreate) {
return messageObject.messageText.toString();
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChatMigrateTo) {
return LocaleController.formatString("ActionMigrateFromGroupNotify", R.string.ActionMigrateFromGroupNotify, chat.title);
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChannelMigrateFrom) {
return LocaleController.formatString("ActionMigrateFromGroupNotify", R.string.ActionMigrateFromGroupNotify, messageObject.messageOwner.action.title);
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionScreenshotTaken) {
return messageObject.messageText.toString();
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionPinMessage) {
2019-01-23 18:03:33 +01:00
if (chat != null && (!ChatObject.isChannel(chat) || chat.megagroup)) {
2018-07-30 04:07:02 +02:00
if (messageObject.replyMessageObject == null) {
return LocaleController.formatString("NotificationActionPinnedNoText", R.string.NotificationActionPinnedNoText, name, chat.title);
} else {
MessageObject object = messageObject.replyMessageObject;
if (object.isMusic()) {
return LocaleController.formatString("NotificationActionPinnedMusic", R.string.NotificationActionPinnedMusic, name, chat.title);
} else if (object.isVideo()) {
if (Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(object.messageOwner.message)) {
String message = "\uD83D\uDCF9 " + object.messageOwner.message;
return LocaleController.formatString("NotificationActionPinnedText", R.string.NotificationActionPinnedText, name, message, chat.title);
} else {
return LocaleController.formatString("NotificationActionPinnedVideo", R.string.NotificationActionPinnedVideo, name, chat.title);
}
} else if (object.isGif()) {
if (Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(object.messageOwner.message)) {
String message = "\uD83C\uDFAC " + object.messageOwner.message;
return LocaleController.formatString("NotificationActionPinnedText", R.string.NotificationActionPinnedText, name, message, chat.title);
} else {
return LocaleController.formatString("NotificationActionPinnedGif", R.string.NotificationActionPinnedGif, name, chat.title);
}
} else if (object.isVoice()) {
return LocaleController.formatString("NotificationActionPinnedVoice", R.string.NotificationActionPinnedVoice, name, chat.title);
} else if (object.isRoundVideo()) {
return LocaleController.formatString("NotificationActionPinnedRound", R.string.NotificationActionPinnedRound, name, chat.title);
2019-07-18 15:01:39 +02:00
} else if (object.isSticker() || object.isAnimatedSticker()) {
2018-07-30 04:07:02 +02:00
String emoji = object.getStickerEmoji();
if (emoji != null) {
return LocaleController.formatString("NotificationActionPinnedStickerEmoji", R.string.NotificationActionPinnedStickerEmoji, name, chat.title, emoji);
} else {
return LocaleController.formatString("NotificationActionPinnedSticker", R.string.NotificationActionPinnedSticker, name, chat.title);
}
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaDocument) {
if (Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(object.messageOwner.message)) {
String message = "\uD83D\uDCCE " + object.messageOwner.message;
return LocaleController.formatString("NotificationActionPinnedText", R.string.NotificationActionPinnedText, name, message, chat.title);
} else {
return LocaleController.formatString("NotificationActionPinnedFile", R.string.NotificationActionPinnedFile, name, chat.title);
}
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaGeo || object.messageOwner.media instanceof TLRPC.TL_messageMediaVenue) {
return LocaleController.formatString("NotificationActionPinnedGeo", R.string.NotificationActionPinnedGeo, name, chat.title);
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaGeoLive) {
return LocaleController.formatString("NotificationActionPinnedGeoLive", R.string.NotificationActionPinnedGeoLive, name, chat.title);
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaContact) {
2019-05-14 14:08:05 +02:00
TLRPC.TL_messageMediaContact mediaContact = (TLRPC.TL_messageMediaContact) object.messageOwner.media;
return LocaleController.formatString("NotificationActionPinnedContact2", R.string.NotificationActionPinnedContact2, name, chat.title, ContactsController.formatName(mediaContact.first_name, mediaContact.last_name));
2019-01-23 18:03:33 +01:00
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaPoll) {
2019-05-14 14:08:05 +02:00
TLRPC.TL_messageMediaPoll mediaPoll = (TLRPC.TL_messageMediaPoll) object.messageOwner.media;
2020-01-23 07:15:40 +01:00
if (mediaPoll.poll.quiz) {
return LocaleController.formatString("NotificationActionPinnedQuiz2", R.string.NotificationActionPinnedQuiz2, name, chat.title, mediaPoll.poll.question);
} else {
return LocaleController.formatString("NotificationActionPinnedPoll2", R.string.NotificationActionPinnedPoll2, name, chat.title, mediaPoll.poll.question);
}
2018-07-30 04:07:02 +02:00
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaPhoto) {
if (Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(object.messageOwner.message)) {
String message = "\uD83D\uDDBC " + object.messageOwner.message;
return LocaleController.formatString("NotificationActionPinnedText", R.string.NotificationActionPinnedText, name, message, chat.title);
} else {
return LocaleController.formatString("NotificationActionPinnedPhoto", R.string.NotificationActionPinnedPhoto, name, chat.title);
}
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaGame) {
return LocaleController.formatString("NotificationActionPinnedGame", R.string.NotificationActionPinnedGame, name, chat.title);
} else if (object.messageText != null && object.messageText.length() > 0) {
CharSequence message = object.messageText;
if (message.length() > 20) {
message = message.subSequence(0, 20) + "...";
}
return LocaleController.formatString("NotificationActionPinnedText", R.string.NotificationActionPinnedText, name, message, chat.title);
} else {
return LocaleController.formatString("NotificationActionPinnedNoText", R.string.NotificationActionPinnedNoText, name, chat.title);
}
}
} else {
if (messageObject.replyMessageObject == null) {
return LocaleController.formatString("NotificationActionPinnedNoTextChannel", R.string.NotificationActionPinnedNoTextChannel, chat.title);
} else {
MessageObject object = messageObject.replyMessageObject;
if (object.isMusic()) {
return LocaleController.formatString("NotificationActionPinnedMusicChannel", R.string.NotificationActionPinnedMusicChannel, chat.title);
} else if (object.isVideo()) {
if (Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(object.messageOwner.message)) {
String message = "\uD83D\uDCF9 " + object.messageOwner.message;
return LocaleController.formatString("NotificationActionPinnedTextChannel", R.string.NotificationActionPinnedTextChannel, chat.title, message);
} else {
return LocaleController.formatString("NotificationActionPinnedVideoChannel", R.string.NotificationActionPinnedVideoChannel, chat.title);
}
} else if (object.isGif()) {
if (Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(object.messageOwner.message)) {
String message = "\uD83C\uDFAC " + object.messageOwner.message;
return LocaleController.formatString("NotificationActionPinnedTextChannel", R.string.NotificationActionPinnedTextChannel, chat.title, message);
} else {
return LocaleController.formatString("NotificationActionPinnedGifChannel", R.string.NotificationActionPinnedGifChannel, chat.title);
}
} else if (object.isVoice()) {
return LocaleController.formatString("NotificationActionPinnedVoiceChannel", R.string.NotificationActionPinnedVoiceChannel, chat.title);
} else if (object.isRoundVideo()) {
return LocaleController.formatString("NotificationActionPinnedRoundChannel", R.string.NotificationActionPinnedRoundChannel, chat.title);
2019-07-18 15:01:39 +02:00
} else if (object.isSticker() || object.isAnimatedSticker()) {
2018-07-30 04:07:02 +02:00
String emoji = object.getStickerEmoji();
if (emoji != null) {
return LocaleController.formatString("NotificationActionPinnedStickerEmojiChannel", R.string.NotificationActionPinnedStickerEmojiChannel, chat.title, emoji);
} else {
return LocaleController.formatString("NotificationActionPinnedStickerChannel", R.string.NotificationActionPinnedStickerChannel, chat.title);
}
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaDocument) {
if (Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(object.messageOwner.message)) {
String message = "\uD83D\uDCCE " + object.messageOwner.message;
return LocaleController.formatString("NotificationActionPinnedTextChannel", R.string.NotificationActionPinnedTextChannel, chat.title, message);
} else {
return LocaleController.formatString("NotificationActionPinnedFileChannel", R.string.NotificationActionPinnedFileChannel, chat.title);
}
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaGeo || object.messageOwner.media instanceof TLRPC.TL_messageMediaVenue) {
return LocaleController.formatString("NotificationActionPinnedGeoChannel", R.string.NotificationActionPinnedGeoChannel, chat.title);
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaGeoLive) {
return LocaleController.formatString("NotificationActionPinnedGeoLiveChannel", R.string.NotificationActionPinnedGeoLiveChannel, chat.title);
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaContact) {
2019-05-14 14:08:05 +02:00
TLRPC.TL_messageMediaContact mediaContact = (TLRPC.TL_messageMediaContact) object.messageOwner.media;
return LocaleController.formatString("NotificationActionPinnedContactChannel2", R.string.NotificationActionPinnedContactChannel2, chat.title, ContactsController.formatName(mediaContact.first_name, mediaContact.last_name));
2019-01-23 18:03:33 +01:00
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaPoll) {
2019-05-14 14:08:05 +02:00
TLRPC.TL_messageMediaPoll mediaPoll = (TLRPC.TL_messageMediaPoll) object.messageOwner.media;
2020-01-23 07:15:40 +01:00
if (mediaPoll.poll.quiz) {
return LocaleController.formatString("NotificationActionPinnedQuizChannel2", R.string.NotificationActionPinnedQuizChannel2, chat.title, mediaPoll.poll.question);
} else {
return LocaleController.formatString("NotificationActionPinnedPollChannel2", R.string.NotificationActionPinnedPollChannel2, chat.title, mediaPoll.poll.question);
}
2018-07-30 04:07:02 +02:00
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaPhoto) {
if (Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(object.messageOwner.message)) {
String message = "\uD83D\uDDBC " + object.messageOwner.message;
return LocaleController.formatString("NotificationActionPinnedTextChannel", R.string.NotificationActionPinnedTextChannel, chat.title, message);
} else {
return LocaleController.formatString("NotificationActionPinnedPhotoChannel", R.string.NotificationActionPinnedPhotoChannel, chat.title);
}
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaGame) {
return LocaleController.formatString("NotificationActionPinnedGameChannel", R.string.NotificationActionPinnedGameChannel, chat.title);
} else if (object.messageText != null && object.messageText.length() > 0) {
CharSequence message = object.messageText;
if (message.length() > 20) {
message = message.subSequence(0, 20) + "...";
}
return LocaleController.formatString("NotificationActionPinnedTextChannel", R.string.NotificationActionPinnedTextChannel, chat.title, message);
} else {
return LocaleController.formatString("NotificationActionPinnedNoTextChannel", R.string.NotificationActionPinnedNoTextChannel, chat.title);
}
}
}
}
} else {
if (messageObject.isMediaEmpty()) {
if (!TextUtils.isEmpty(messageObject.messageOwner.message)) {
return messageObject.messageOwner.message;
} else {
return LocaleController.getString("Message", R.string.Message);
}
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaPhoto) {
if (Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(messageObject.messageOwner.message)) {
return "\uD83D\uDDBC " + messageObject.messageOwner.message;
} else if (messageObject.messageOwner.media.ttl_seconds != 0) {
return LocaleController.getString("AttachDestructingPhoto", R.string.AttachDestructingPhoto);
} else {
return LocaleController.getString("AttachPhoto", R.string.AttachPhoto);
}
} else if (messageObject.isVideo()) {
if (Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(messageObject.messageOwner.message)) {
return "\uD83D\uDCF9 " + messageObject.messageOwner.message;
} else if (messageObject.messageOwner.media.ttl_seconds != 0) {
return LocaleController.getString("AttachDestructingVideo", R.string.AttachDestructingVideo);
} else {
return LocaleController.getString("AttachVideo", R.string.AttachVideo);
}
} else if (messageObject.isGame()) {
return LocaleController.getString("AttachGame", R.string.AttachGame);
} else if (messageObject.isVoice()) {
return LocaleController.getString("AttachAudio", R.string.AttachAudio);
} else if (messageObject.isRoundVideo()) {
return LocaleController.getString("AttachRound", R.string.AttachRound);
} else if (messageObject.isMusic()) {
return LocaleController.getString("AttachMusic", R.string.AttachMusic);
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaContact) {
return LocaleController.getString("AttachContact", R.string.AttachContact);
2019-01-23 18:03:33 +01:00
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaPoll) {
2020-01-23 07:15:40 +01:00
if (((TLRPC.TL_messageMediaPoll) messageObject.messageOwner.media).poll.quiz) {
return LocaleController.getString("QuizPoll", R.string.QuizPoll);
} else {
return LocaleController.getString("Poll", R.string.Poll);
}
2018-07-30 04:07:02 +02:00
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaGeo || messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaVenue) {
return LocaleController.getString("AttachLocation", R.string.AttachLocation);
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaGeoLive) {
return LocaleController.getString("AttachLiveLocation", R.string.AttachLiveLocation);
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaDocument) {
2019-07-18 15:01:39 +02:00
if (messageObject.isSticker() || messageObject.isAnimatedSticker()) {
2018-07-30 04:07:02 +02:00
String emoji = messageObject.getStickerEmoji();
if (emoji != null) {
return emoji + " " + LocaleController.getString("AttachSticker", R.string.AttachSticker);
} else {
return LocaleController.getString("AttachSticker", R.string.AttachSticker);
}
} else if (messageObject.isGif()) {
if (Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(messageObject.messageOwner.message)) {
return "\uD83C\uDFAC " + messageObject.messageOwner.message;
} else {
return LocaleController.getString("AttachGif", R.string.AttachGif);
}
} else {
if (Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(messageObject.messageOwner.message)) {
2020-03-30 14:00:09 +02:00
return "\uD83D\uDCCE " + messageObject.messageOwner.message;
2018-07-30 04:07:02 +02:00
} else {
return LocaleController.getString("AttachDocument", R.string.AttachDocument);
}
}
2020-03-30 14:00:09 +02:00
} else if (!TextUtils.isEmpty(messageObject.messageText)) {
return messageObject.messageText.toString();
} else {
return LocaleController.getString("Message", R.string.Message);
2018-07-30 04:07:02 +02:00
}
}
} else {
2019-01-23 18:03:33 +01:00
if (preview != null) {
preview[0] = false;
}
2018-07-30 04:07:02 +02:00
return LocaleController.getString("Message", R.string.Message);
}
}
return null;
}
2019-05-14 14:08:05 +02:00
private String getStringForMessage(MessageObject messageObject, boolean shortMessage, boolean[] text, boolean[] preview) {
2020-01-23 07:15:40 +01:00
if (AndroidUtilities.needShowPasscode() || SharedConfig.isWaitingForPasscodeEnter) {
2018-07-30 04:07:02 +02:00
return LocaleController.getString("YouHaveNewMessage", R.string.YouHaveNewMessage);
}
2014-07-10 02:15:58 +02:00
long dialog_id = messageObject.messageOwner.dialog_id;
2020-09-30 15:48:47 +02:00
int chat_id = messageObject.messageOwner.peer_id.chat_id != 0 ? messageObject.messageOwner.peer_id.chat_id : messageObject.messageOwner.peer_id.channel_id;
int from_id = messageObject.messageOwner.peer_id.user_id;
2019-01-23 18:03:33 +01:00
if (preview != null) {
preview[0] = true;
}
2019-07-18 15:01:39 +02:00
SharedPreferences preferences = getAccountInstance().getNotificationsSettings();
boolean dialogPreviewEnabled = preferences.getBoolean("content_preview_" + dialog_id, true);
2018-07-30 04:07:02 +02:00
if (messageObject.isFcmMessage()) {
if (chat_id == 0 && from_id != 0) {
2019-07-18 15:01:39 +02:00
if (!dialogPreviewEnabled || !preferences.getBoolean("EnablePreviewAll", true)) {
2019-01-23 18:03:33 +01:00
if (preview != null) {
preview[0] = false;
}
2018-07-30 04:07:02 +02:00
return LocaleController.formatString("NotificationMessageNoText", R.string.NotificationMessageNoText, messageObject.localName);
}
} else if (chat_id != 0) {
2019-07-18 15:01:39 +02:00
if (!dialogPreviewEnabled || !messageObject.localChannel && !preferences.getBoolean("EnablePreviewGroup", true) || messageObject.localChannel && !preferences.getBoolean("EnablePreviewChannel", true)) {
2019-01-23 18:03:33 +01:00
if (preview != null) {
preview[0] = false;
}
2020-09-30 15:48:47 +02:00
if (!messageObject.isMegagroup() && messageObject.messageOwner.peer_id.channel_id != 0) {
2018-07-30 04:07:02 +02:00
return LocaleController.formatString("ChannelMessageNoText", R.string.ChannelMessageNoText, messageObject.localName);
} else {
return LocaleController.formatString("NotificationMessageGroupNoText", R.string.NotificationMessageGroupNoText, messageObject.localUserName, messageObject.localName);
}
}
}
text[0] = true;
return (String) messageObject.messageText;
}
2019-09-10 12:56:11 +02:00
int selfUsedId = getUserConfig().getClientUserId();
2015-09-24 22:52:02 +02:00
if (from_id == 0) {
2016-04-22 15:49:00 +02:00
if (messageObject.isFromUser() || messageObject.getId() < 0) {
2020-09-30 15:48:47 +02:00
from_id = messageObject.getFromChatId();
2016-03-06 02:49:31 +01:00
} else {
from_id = -chat_id;
}
2019-09-10 12:56:11 +02:00
} else if (from_id == selfUsedId) {
2020-09-30 15:48:47 +02:00
from_id = messageObject.getFromChatId();
2014-07-10 02:15:58 +02:00
}
if (dialog_id == 0) {
if (chat_id != 0) {
dialog_id = -chat_id;
2015-09-24 22:52:02 +02:00
} else if (from_id != 0) {
dialog_id = from_id;
2014-07-10 02:15:58 +02:00
}
}
2015-09-24 22:52:02 +02:00
String name = null;
if (from_id > 0) {
2019-09-10 12:56:11 +02:00
if (messageObject.messageOwner.from_scheduled) {
if (dialog_id == selfUsedId) {
name = LocaleController.getString("MessageScheduledReminderNotification", R.string.MessageScheduledReminderNotification);
} else {
name = LocaleController.getString("NotificationMessageScheduledName", R.string.NotificationMessageScheduledName);
}
} else {
TLRPC.User user = getMessagesController().getUser(from_id);
if (user != null) {
name = UserObject.getUserName(user);
}
2015-09-24 22:52:02 +02:00
}
} else {
2019-07-18 15:01:39 +02:00
TLRPC.Chat chat = getMessagesController().getChat(-from_id);
2015-09-24 22:52:02 +02:00
if (chat != null) {
name = chat.title;
}
}
if (name == null) {
2014-07-10 02:15:58 +02:00
return null;
}
TLRPC.Chat chat = null;
if (chat_id != 0) {
2019-07-18 15:01:39 +02:00
chat = getMessagesController().getChat(chat_id);
2014-07-10 02:15:58 +02:00
if (chat == null) {
return null;
}
}
String msg = null;
2018-07-30 04:07:02 +02:00
if ((int) dialog_id == 0) {
msg = LocaleController.getString("YouHaveNewMessage", R.string.YouHaveNewMessage);
} else {
2015-09-24 22:52:02 +02:00
if (chat_id == 0 && from_id != 0) {
2019-07-18 15:01:39 +02:00
if (dialogPreviewEnabled && preferences.getBoolean("EnablePreviewAll", true)) {
2014-07-10 02:15:58 +02:00
if (messageObject.messageOwner instanceof TLRPC.TL_messageService) {
2019-01-23 18:03:33 +01:00
if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionUserJoined || messageObject.messageOwner.action instanceof TLRPC.TL_messageActionContactSignUp) {
2015-09-24 22:52:02 +02:00
msg = LocaleController.formatString("NotificationContactJoined", R.string.NotificationContactJoined, name);
2014-07-10 02:15:58 +02:00
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionUserUpdatedPhoto) {
2015-09-24 22:52:02 +02:00
msg = LocaleController.formatString("NotificationContactNewPhoto", R.string.NotificationContactNewPhoto, name);
2014-07-10 02:15:58 +02:00
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionLoginUnknownLocation) {
2015-11-26 22:04:02 +01:00
String date = LocaleController.formatString("formatDateAtTime", R.string.formatDateAtTime, LocaleController.getInstance().formatterYear.format(((long) messageObject.messageOwner.date) * 1000), LocaleController.getInstance().formatterDay.format(((long) messageObject.messageOwner.date) * 1000));
2019-07-18 15:01:39 +02:00
msg = LocaleController.formatString("NotificationUnrecognizedDevice", R.string.NotificationUnrecognizedDevice, getUserConfig().getCurrentUser().first_name, date, messageObject.messageOwner.action.title, messageObject.messageOwner.action.address);
2017-03-31 01:58:05 +02:00
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionGameScore || messageObject.messageOwner.action instanceof TLRPC.TL_messageActionPaymentSent) {
2016-10-11 13:57:01 +02:00
msg = messageObject.messageText.toString();
2017-03-31 01:58:05 +02:00
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionPhoneCall) {
2020-08-14 18:58:22 +02:00
if (messageObject.messageOwner.action.video) {
msg = LocaleController.getString("CallMessageVideoIncomingMissed", R.string.CallMessageVideoIncomingMissed);
} else {
msg = LocaleController.getString("CallMessageIncomingMissed", R.string.CallMessageIncomingMissed);
}
2014-07-10 02:15:58 +02:00
}
} else {
2015-04-09 20:00:14 +02:00
if (messageObject.isMediaEmpty()) {
if (!shortMessage) {
2018-07-30 04:07:02 +02:00
if (!TextUtils.isEmpty(messageObject.messageOwner.message)) {
2015-09-24 22:52:02 +02:00
msg = LocaleController.formatString("NotificationMessageText", R.string.NotificationMessageText, name, messageObject.messageOwner.message);
2017-12-08 18:35:59 +01:00
text[0] = true;
} else {
2015-09-24 22:52:02 +02:00
msg = LocaleController.formatString("NotificationMessageNoText", R.string.NotificationMessageNoText, name);
}
2014-07-10 02:15:58 +02:00
} else {
2015-09-24 22:52:02 +02:00
msg = LocaleController.formatString("NotificationMessageNoText", R.string.NotificationMessageNoText, name);
2014-07-10 02:15:58 +02:00
}
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaPhoto) {
2018-07-30 04:07:02 +02:00
if (!shortMessage && Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(messageObject.messageOwner.message)) {
msg = LocaleController.formatString("NotificationMessageText", R.string.NotificationMessageText, name, "\uD83D\uDDBC " + messageObject.messageOwner.message);
2017-12-08 18:35:59 +01:00
text[0] = true;
2016-10-11 13:57:01 +02:00
} else {
if (messageObject.messageOwner.media.ttl_seconds != 0) {
msg = LocaleController.formatString("NotificationMessageSDPhoto", R.string.NotificationMessageSDPhoto, name);
} else {
msg = LocaleController.formatString("NotificationMessagePhoto", R.string.NotificationMessagePhoto, name);
}
2016-10-11 13:57:01 +02:00
}
2016-03-06 02:49:31 +01:00
} else if (messageObject.isVideo()) {
2018-07-30 04:07:02 +02:00
if (!shortMessage && Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(messageObject.messageOwner.message)) {
msg = LocaleController.formatString("NotificationMessageText", R.string.NotificationMessageText, name, "\uD83D\uDCF9 " + messageObject.messageOwner.message);
2017-12-08 18:35:59 +01:00
text[0] = true;
2016-10-11 13:57:01 +02:00
} else {
if (messageObject.messageOwner.media.ttl_seconds != 0) {
msg = LocaleController.formatString("NotificationMessageSDVideo", R.string.NotificationMessageSDVideo, name);
} else {
msg = LocaleController.formatString("NotificationMessageVideo", R.string.NotificationMessageVideo, name);
}
2016-10-11 13:57:01 +02:00
}
} else if (messageObject.isGame()) {
msg = LocaleController.formatString("NotificationMessageGame", R.string.NotificationMessageGame, name, messageObject.messageOwner.media.game.title);
2016-03-06 02:49:31 +01:00
} else if (messageObject.isVoice()) {
msg = LocaleController.formatString("NotificationMessageAudio", R.string.NotificationMessageAudio, name);
2017-07-08 18:32:04 +02:00
} else if (messageObject.isRoundVideo()) {
msg = LocaleController.formatString("NotificationMessageRound", R.string.NotificationMessageRound, name);
2016-03-16 13:26:32 +01:00
} else if (messageObject.isMusic()) {
msg = LocaleController.formatString("NotificationMessageMusic", R.string.NotificationMessageMusic, name);
2014-07-10 02:15:58 +02:00
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaContact) {
2019-05-14 14:08:05 +02:00
TLRPC.TL_messageMediaContact mediaContact = (TLRPC.TL_messageMediaContact) messageObject.messageOwner.media;
msg = LocaleController.formatString("NotificationMessageContact2", R.string.NotificationMessageContact2, name, ContactsController.formatName(mediaContact.first_name, mediaContact.last_name));
2019-01-23 18:03:33 +01:00
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaPoll) {
2019-05-14 14:08:05 +02:00
TLRPC.TL_messageMediaPoll mediaPoll = (TLRPC.TL_messageMediaPoll) messageObject.messageOwner.media;
2020-01-23 07:15:40 +01:00
if (mediaPoll.poll.quiz) {
msg = LocaleController.formatString("NotificationMessageQuiz2", R.string.NotificationMessageQuiz2, name, mediaPoll.poll.question);
} else {
msg = LocaleController.formatString("NotificationMessagePoll2", R.string.NotificationMessagePoll2, name, mediaPoll.poll.question);
}
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaGeo || messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaVenue) {
2015-09-24 22:52:02 +02:00
msg = LocaleController.formatString("NotificationMessageMap", R.string.NotificationMessageMap, name);
2018-07-30 04:07:02 +02:00
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaGeoLive) {
msg = LocaleController.formatString("NotificationMessageLiveLocation", R.string.NotificationMessageLiveLocation, name);
2014-07-10 02:15:58 +02:00
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaDocument) {
2019-07-18 15:01:39 +02:00
if (messageObject.isSticker() || messageObject.isAnimatedSticker()) {
2016-05-25 23:49:47 +02:00
String emoji = messageObject.getStickerEmoji();
if (emoji != null) {
msg = LocaleController.formatString("NotificationMessageStickerEmoji", R.string.NotificationMessageStickerEmoji, name, emoji);
} else {
msg = LocaleController.formatString("NotificationMessageSticker", R.string.NotificationMessageSticker, name);
}
} else if (messageObject.isGif()) {
2018-07-30 04:07:02 +02:00
if (!shortMessage && Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(messageObject.messageOwner.message)) {
msg = LocaleController.formatString("NotificationMessageText", R.string.NotificationMessageText, name, "\uD83C\uDFAC " + messageObject.messageOwner.message);
2017-12-08 18:35:59 +01:00
text[0] = true;
2016-10-11 13:57:01 +02:00
} else {
msg = LocaleController.formatString("NotificationMessageGif", R.string.NotificationMessageGif, name);
}
2015-01-02 23:15:07 +01:00
} else {
2018-07-30 04:07:02 +02:00
if (!shortMessage && Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(messageObject.messageOwner.message)) {
msg = LocaleController.formatString("NotificationMessageText", R.string.NotificationMessageText, name, "\uD83D\uDCCE " + messageObject.messageOwner.message);
2017-12-08 18:35:59 +01:00
text[0] = true;
2016-10-11 13:57:01 +02:00
} else {
msg = LocaleController.formatString("NotificationMessageDocument", R.string.NotificationMessageDocument, name);
}
2015-01-02 23:15:07 +01:00
}
2020-03-30 14:00:09 +02:00
} else {
if (!shortMessage && !TextUtils.isEmpty(messageObject.messageText)) {
msg = LocaleController.formatString("NotificationMessageText", R.string.NotificationMessageText, name, messageObject.messageText);
text[0] = true;
} else {
msg = LocaleController.formatString("NotificationMessageNoText", R.string.NotificationMessageNoText, name);
}
2014-07-10 02:15:58 +02:00
}
}
} else {
2019-01-23 18:03:33 +01:00
if (preview != null) {
preview[0] = false;
}
2015-09-24 22:52:02 +02:00
msg = LocaleController.formatString("NotificationMessageNoText", R.string.NotificationMessageNoText, name);
2014-07-10 02:15:58 +02:00
}
} else if (chat_id != 0) {
2019-01-23 18:03:33 +01:00
boolean isChannel = ChatObject.isChannel(chat) && !chat.megagroup;
2019-07-18 15:01:39 +02:00
if (dialogPreviewEnabled && (!isChannel && preferences.getBoolean("EnablePreviewGroup", true) || isChannel && preferences.getBoolean("EnablePreviewChannel", true))) {
2014-07-10 02:15:58 +02:00
if (messageObject.messageOwner instanceof TLRPC.TL_messageService) {
if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChatAddUser) {
2015-11-26 22:04:02 +01:00
int singleUserId = messageObject.messageOwner.action.user_id;
if (singleUserId == 0 && messageObject.messageOwner.action.users.size() == 1) {
singleUserId = messageObject.messageOwner.action.users.get(0);
}
if (singleUserId != 0) {
2020-09-30 15:48:47 +02:00
if (messageObject.messageOwner.peer_id.channel_id != 0 && !chat.megagroup) {
2015-11-26 22:04:02 +01:00
msg = LocaleController.formatString("ChannelAddedByNotification", R.string.ChannelAddedByNotification, name, chat.title);
2015-09-24 22:52:02 +02:00
} else {
2019-09-10 12:56:11 +02:00
if (singleUserId == selfUsedId) {
2015-11-26 22:04:02 +01:00
msg = LocaleController.formatString("NotificationInvitedToGroup", R.string.NotificationInvitedToGroup, name, chat.title);
} else {
2019-07-18 15:01:39 +02:00
TLRPC.User u2 = getMessagesController().getUser(singleUserId);
2015-11-26 22:04:02 +01:00
if (u2 == null) {
return null;
}
if (from_id == u2.id) {
2016-10-11 13:57:01 +02:00
if (chat.megagroup) {
2016-03-16 13:26:32 +01:00
msg = LocaleController.formatString("NotificationGroupAddSelfMega", R.string.NotificationGroupAddSelfMega, name, chat.title);
} else {
msg = LocaleController.formatString("NotificationGroupAddSelf", R.string.NotificationGroupAddSelf, name, chat.title);
}
2015-11-26 22:04:02 +01:00
} else {
msg = LocaleController.formatString("NotificationGroupAddMember", R.string.NotificationGroupAddMember, name, chat.title, UserObject.getUserName(u2));
}
}
2014-07-10 02:15:58 +02:00
}
2015-09-24 22:52:02 +02:00
} else {
2019-01-23 18:03:33 +01:00
StringBuilder names = new StringBuilder();
2015-11-26 22:04:02 +01:00
for (int a = 0; a < messageObject.messageOwner.action.users.size(); a++) {
2019-07-18 15:01:39 +02:00
TLRPC.User user = getMessagesController().getUser(messageObject.messageOwner.action.users.get(a));
2015-11-26 22:04:02 +01:00
if (user != null) {
String name2 = UserObject.getUserName(user);
if (names.length() != 0) {
names.append(", ");
}
names.append(name2);
2015-09-24 22:52:02 +02:00
}
}
2015-11-26 22:04:02 +01:00
msg = LocaleController.formatString("NotificationGroupAddMember", R.string.NotificationGroupAddMember, name, chat.title, names.toString());
2014-07-10 02:15:58 +02:00
}
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChatJoinedByLink) {
2015-09-24 22:52:02 +02:00
msg = LocaleController.formatString("NotificationInvitedToGroupByLink", R.string.NotificationInvitedToGroupByLink, name, chat.title);
2014-07-10 02:15:58 +02:00
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChatEditTitle) {
2015-09-24 22:52:02 +02:00
msg = LocaleController.formatString("NotificationEditedGroupName", R.string.NotificationEditedGroupName, name, messageObject.messageOwner.action.title);
2014-07-10 02:15:58 +02:00
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChatEditPhoto || messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChatDeletePhoto) {
2020-09-30 15:48:47 +02:00
if (messageObject.messageOwner.peer_id.channel_id != 0 && !chat.megagroup) {
2020-07-26 10:03:38 +02:00
if (messageObject.isVideoAvatar()) {
msg = LocaleController.formatString("ChannelVideoEditNotification", R.string.ChannelVideoEditNotification, chat.title);
} else {
msg = LocaleController.formatString("ChannelPhotoEditNotification", R.string.ChannelPhotoEditNotification, chat.title);
}
2015-09-24 22:52:02 +02:00
} else {
2020-07-26 10:03:38 +02:00
if (messageObject.isVideoAvatar()) {
msg = LocaleController.formatString("NotificationEditedGroupVideo", R.string.NotificationEditedGroupVideo, name, chat.title);
} else {
msg = LocaleController.formatString("NotificationEditedGroupPhoto", R.string.NotificationEditedGroupPhoto, name, chat.title);
}
2015-09-24 22:52:02 +02:00
}
2014-07-10 02:15:58 +02:00
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChatDeleteUser) {
2019-09-10 12:56:11 +02:00
if (messageObject.messageOwner.action.user_id == selfUsedId) {
2015-09-24 22:52:02 +02:00
msg = LocaleController.formatString("NotificationGroupKickYou", R.string.NotificationGroupKickYou, name, chat.title);
} else if (messageObject.messageOwner.action.user_id == from_id) {
msg = LocaleController.formatString("NotificationGroupLeftMember", R.string.NotificationGroupLeftMember, name, chat.title);
2014-07-10 02:15:58 +02:00
} else {
2019-07-18 15:01:39 +02:00
TLRPC.User u2 = getMessagesController().getUser(messageObject.messageOwner.action.user_id);
2014-07-10 02:15:58 +02:00
if (u2 == null) {
return null;
}
2015-09-24 22:52:02 +02:00
msg = LocaleController.formatString("NotificationGroupKickMember", R.string.NotificationGroupKickMember, name, chat.title, UserObject.getUserName(u2));
2014-07-10 02:15:58 +02:00
}
2014-07-30 09:49:39 +02:00
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChatCreate) {
msg = messageObject.messageText.toString();
2015-09-24 22:52:02 +02:00
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChannelCreate) {
msg = messageObject.messageText.toString();
2015-11-26 22:04:02 +01:00
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChatMigrateTo) {
msg = LocaleController.formatString("ActionMigrateFromGroupNotify", R.string.ActionMigrateFromGroupNotify, chat.title);
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionChannelMigrateFrom) {
msg = LocaleController.formatString("ActionMigrateFromGroupNotify", R.string.ActionMigrateFromGroupNotify, messageObject.messageOwner.action.title);
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionScreenshotTaken) {
msg = messageObject.messageText.toString();
2016-03-16 13:26:32 +01:00
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionPinMessage) {
2019-01-23 18:03:33 +01:00
if (chat != null && (!ChatObject.isChannel(chat) || chat.megagroup)) {
2017-12-08 18:35:59 +01:00
if (messageObject.replyMessageObject == null) {
2016-03-16 13:26:32 +01:00
msg = LocaleController.formatString("NotificationActionPinnedNoText", R.string.NotificationActionPinnedNoText, name, chat.title);
} else {
2017-12-08 18:35:59 +01:00
MessageObject object = messageObject.replyMessageObject;
if (object.isMusic()) {
2016-03-16 13:26:32 +01:00
msg = LocaleController.formatString("NotificationActionPinnedMusic", R.string.NotificationActionPinnedMusic, name, chat.title);
2017-12-08 18:35:59 +01:00
} else if (object.isVideo()) {
2018-07-30 04:07:02 +02:00
if (Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(object.messageOwner.message)) {
String message = "\uD83D\uDCF9 " + object.messageOwner.message;
2016-10-11 13:57:01 +02:00
msg = LocaleController.formatString("NotificationActionPinnedText", R.string.NotificationActionPinnedText, name, message, chat.title);
} else {
msg = LocaleController.formatString("NotificationActionPinnedVideo", R.string.NotificationActionPinnedVideo, name, chat.title);
}
2017-12-08 18:35:59 +01:00
} else if (object.isGif()) {
2018-07-30 04:07:02 +02:00
if (Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(object.messageOwner.message)) {
String message = "\uD83C\uDFAC " + object.messageOwner.message;
2016-10-11 13:57:01 +02:00
msg = LocaleController.formatString("NotificationActionPinnedText", R.string.NotificationActionPinnedText, name, message, chat.title);
} else {
msg = LocaleController.formatString("NotificationActionPinnedGif", R.string.NotificationActionPinnedGif, name, chat.title);
}
2017-12-08 18:35:59 +01:00
} else if (object.isVoice()) {
2016-03-16 13:26:32 +01:00
msg = LocaleController.formatString("NotificationActionPinnedVoice", R.string.NotificationActionPinnedVoice, name, chat.title);
2017-12-08 18:35:59 +01:00
} else if (object.isRoundVideo()) {
2017-07-08 18:32:04 +02:00
msg = LocaleController.formatString("NotificationActionPinnedRound", R.string.NotificationActionPinnedRound, name, chat.title);
2019-07-18 15:01:39 +02:00
} else if (object.isSticker() || object.isAnimatedSticker()) {
2017-12-08 18:35:59 +01:00
String emoji = object.getStickerEmoji();
if (emoji != null) {
2016-05-25 23:49:47 +02:00
msg = LocaleController.formatString("NotificationActionPinnedStickerEmoji", R.string.NotificationActionPinnedStickerEmoji, name, chat.title, emoji);
} else {
msg = LocaleController.formatString("NotificationActionPinnedSticker", R.string.NotificationActionPinnedSticker, name, chat.title);
}
2017-12-08 18:35:59 +01:00
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaDocument) {
2018-07-30 04:07:02 +02:00
if (Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(object.messageOwner.message)) {
String message = "\uD83D\uDCCE " + object.messageOwner.message;
2016-10-11 13:57:01 +02:00
msg = LocaleController.formatString("NotificationActionPinnedText", R.string.NotificationActionPinnedText, name, message, chat.title);
} else {
msg = LocaleController.formatString("NotificationActionPinnedFile", R.string.NotificationActionPinnedFile, name, chat.title);
}
2018-07-30 04:07:02 +02:00
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaGeo || object.messageOwner.media instanceof TLRPC.TL_messageMediaVenue) {
2016-03-16 13:26:32 +01:00
msg = LocaleController.formatString("NotificationActionPinnedGeo", R.string.NotificationActionPinnedGeo, name, chat.title);
2017-12-08 18:35:59 +01:00
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaGeoLive) {
msg = LocaleController.formatString("NotificationActionPinnedGeoLive", R.string.NotificationActionPinnedGeoLive, name, chat.title);
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaContact) {
2019-05-14 14:08:05 +02:00
TLRPC.TL_messageMediaContact mediaContact = (TLRPC.TL_messageMediaContact) messageObject.messageOwner.media;
msg = LocaleController.formatString("NotificationActionPinnedContact2", R.string.NotificationActionPinnedContact2, name, chat.title, ContactsController.formatName(mediaContact.first_name, mediaContact.last_name));
2019-01-23 18:03:33 +01:00
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaPoll) {
2019-05-14 14:08:05 +02:00
TLRPC.TL_messageMediaPoll mediaPoll = (TLRPC.TL_messageMediaPoll) object.messageOwner.media;
2020-01-23 07:15:40 +01:00
if (mediaPoll.poll.quiz) {
msg = LocaleController.formatString("NotificationActionPinnedQuiz2", R.string.NotificationActionPinnedQuiz2, name, chat.title, mediaPoll.poll.question);
} else {
msg = LocaleController.formatString("NotificationActionPinnedPoll2", R.string.NotificationActionPinnedPoll2, name, chat.title, mediaPoll.poll.question);
}
2017-12-08 18:35:59 +01:00
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaPhoto) {
2018-07-30 04:07:02 +02:00
if (Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(object.messageOwner.message)) {
String message = "\uD83D\uDDBC " + object.messageOwner.message;
2017-12-08 18:35:59 +01:00
msg = LocaleController.formatString("NotificationActionPinnedText", R.string.NotificationActionPinnedText, name, message, chat.title);
} else {
msg = LocaleController.formatString("NotificationActionPinnedPhoto", R.string.NotificationActionPinnedPhoto, name, chat.title);
}
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaGame) {
msg = LocaleController.formatString("NotificationActionPinnedGame", R.string.NotificationActionPinnedGame, name, chat.title);
} else if (object.messageText != null && object.messageText.length() > 0) {
CharSequence message = object.messageText;
if (message.length() > 20) {
message = message.subSequence(0, 20) + "...";
}
msg = LocaleController.formatString("NotificationActionPinnedText", R.string.NotificationActionPinnedText, name, message, chat.title);
2016-03-16 13:26:32 +01:00
} else {
2017-12-08 18:35:59 +01:00
msg = LocaleController.formatString("NotificationActionPinnedNoText", R.string.NotificationActionPinnedNoText, name, chat.title);
2016-03-16 13:26:32 +01:00
}
2017-12-08 18:35:59 +01:00
}
} else {
if (messageObject.replyMessageObject == null) {
msg = LocaleController.formatString("NotificationActionPinnedNoTextChannel", R.string.NotificationActionPinnedNoTextChannel, chat.title);
} else {
MessageObject object = messageObject.replyMessageObject;
if (object.isMusic()) {
msg = LocaleController.formatString("NotificationActionPinnedMusicChannel", R.string.NotificationActionPinnedMusicChannel, chat.title);
} else if (object.isVideo()) {
2018-07-30 04:07:02 +02:00
if (Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(object.messageOwner.message)) {
String message = "\uD83D\uDCF9 " + object.messageOwner.message;
2017-12-08 18:35:59 +01:00
msg = LocaleController.formatString("NotificationActionPinnedTextChannel", R.string.NotificationActionPinnedTextChannel, chat.title, message);
2016-10-11 13:57:01 +02:00
} else {
2017-12-08 18:35:59 +01:00
msg = LocaleController.formatString("NotificationActionPinnedVideoChannel", R.string.NotificationActionPinnedVideoChannel, chat.title);
}
} else if (object.isGif()) {
2018-07-30 04:07:02 +02:00
if (Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(object.messageOwner.message)) {
String message = "\uD83C\uDFAC " + object.messageOwner.message;
2016-10-11 13:57:01 +02:00
msg = LocaleController.formatString("NotificationActionPinnedTextChannel", R.string.NotificationActionPinnedTextChannel, chat.title, message);
2017-12-08 18:35:59 +01:00
} else {
msg = LocaleController.formatString("NotificationActionPinnedGifChannel", R.string.NotificationActionPinnedGifChannel, chat.title);
2016-10-11 13:57:01 +02:00
}
2017-12-08 18:35:59 +01:00
} else if (object.isVoice()) {
msg = LocaleController.formatString("NotificationActionPinnedVoiceChannel", R.string.NotificationActionPinnedVoiceChannel, chat.title);
} else if (object.isRoundVideo()) {
msg = LocaleController.formatString("NotificationActionPinnedRoundChannel", R.string.NotificationActionPinnedRoundChannel, chat.title);
2019-07-18 15:01:39 +02:00
} else if (object.isSticker() || object.isAnimatedSticker()) {
2017-12-08 18:35:59 +01:00
String emoji = object.getStickerEmoji();
if (emoji != null) {
msg = LocaleController.formatString("NotificationActionPinnedStickerEmojiChannel", R.string.NotificationActionPinnedStickerEmojiChannel, chat.title, emoji);
} else {
msg = LocaleController.formatString("NotificationActionPinnedStickerChannel", R.string.NotificationActionPinnedStickerChannel, chat.title);
}
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaDocument) {
2018-07-30 04:07:02 +02:00
if (Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(object.messageOwner.message)) {
String message = "\uD83D\uDCCE " + object.messageOwner.message;
2017-12-08 18:35:59 +01:00
msg = LocaleController.formatString("NotificationActionPinnedTextChannel", R.string.NotificationActionPinnedTextChannel, chat.title, message);
} else {
msg = LocaleController.formatString("NotificationActionPinnedFileChannel", R.string.NotificationActionPinnedFileChannel, chat.title);
}
2018-07-30 04:07:02 +02:00
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaGeo || object.messageOwner.media instanceof TLRPC.TL_messageMediaVenue) {
2017-12-08 18:35:59 +01:00
msg = LocaleController.formatString("NotificationActionPinnedGeoChannel", R.string.NotificationActionPinnedGeoChannel, chat.title);
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaGeoLive) {
msg = LocaleController.formatString("NotificationActionPinnedGeoLiveChannel", R.string.NotificationActionPinnedGeoLiveChannel, chat.title);
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaContact) {
2019-05-14 14:08:05 +02:00
TLRPC.TL_messageMediaContact mediaContact = (TLRPC.TL_messageMediaContact) messageObject.messageOwner.media;
msg = LocaleController.formatString("NotificationActionPinnedContactChannel2", R.string.NotificationActionPinnedContactChannel2, chat.title, ContactsController.formatName(mediaContact.first_name, mediaContact.last_name));
2019-01-23 18:03:33 +01:00
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaPoll) {
2019-05-14 14:08:05 +02:00
TLRPC.TL_messageMediaPoll mediaPoll = (TLRPC.TL_messageMediaPoll) object.messageOwner.media;
2020-01-23 07:15:40 +01:00
if (mediaPoll.poll.quiz) {
msg = LocaleController.formatString("NotificationActionPinnedQuizChannel2", R.string.NotificationActionPinnedQuizChannel2, chat.title, mediaPoll.poll.question);
} else {
msg = LocaleController.formatString("NotificationActionPinnedPollChannel2", R.string.NotificationActionPinnedPollChannel2, chat.title, mediaPoll.poll.question);
}
2017-12-08 18:35:59 +01:00
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaPhoto) {
2018-07-30 04:07:02 +02:00
if (Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(object.messageOwner.message)) {
String message = "\uD83D\uDDBC " + object.messageOwner.message;
2017-12-08 18:35:59 +01:00
msg = LocaleController.formatString("NotificationActionPinnedTextChannel", R.string.NotificationActionPinnedTextChannel, chat.title, message);
2016-10-11 13:57:01 +02:00
} else {
msg = LocaleController.formatString("NotificationActionPinnedPhotoChannel", R.string.NotificationActionPinnedPhotoChannel, chat.title);
}
2017-12-08 18:35:59 +01:00
} else if (object.messageOwner.media instanceof TLRPC.TL_messageMediaGame) {
2016-10-11 13:57:01 +02:00
msg = LocaleController.formatString("NotificationActionPinnedGameChannel", R.string.NotificationActionPinnedGameChannel, chat.title);
2017-12-08 18:35:59 +01:00
} else if (object.messageText != null && object.messageText.length() > 0) {
CharSequence message = object.messageText;
if (message.length() > 20) {
message = message.subSequence(0, 20) + "...";
}
2016-03-16 13:26:32 +01:00
msg = LocaleController.formatString("NotificationActionPinnedTextChannel", R.string.NotificationActionPinnedTextChannel, chat.title, message);
} else {
msg = LocaleController.formatString("NotificationActionPinnedNoTextChannel", R.string.NotificationActionPinnedNoTextChannel, chat.title);
}
}
}
2016-10-11 13:57:01 +02:00
} else if (messageObject.messageOwner.action instanceof TLRPC.TL_messageActionGameScore) {
msg = messageObject.messageText.toString();
2014-07-10 02:15:58 +02:00
}
2017-03-31 01:58:05 +02:00
} else if (ChatObject.isChannel(chat) && !chat.megagroup) {
2017-12-08 18:35:59 +01:00
if (messageObject.isMediaEmpty()) {
2020-03-30 14:00:09 +02:00
if (!shortMessage && !TextUtils.isEmpty(messageObject.messageOwner.message)) {
2017-12-08 18:35:59 +01:00
msg = LocaleController.formatString("NotificationMessageText", R.string.NotificationMessageText, name, messageObject.messageOwner.message);
text[0] = true;
} else {
msg = LocaleController.formatString("ChannelMessageNoText", R.string.ChannelMessageNoText, name);
2014-07-10 02:15:58 +02:00
}
2017-12-08 18:35:59 +01:00
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaPhoto) {
2018-07-30 04:07:02 +02:00
if (!shortMessage && Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(messageObject.messageOwner.message)) {
msg = LocaleController.formatString("NotificationMessageText", R.string.NotificationMessageText, name, "\uD83D\uDDBC " + messageObject.messageOwner.message);
2017-12-08 18:35:59 +01:00
text[0] = true;
} else {
msg = LocaleController.formatString("ChannelMessagePhoto", R.string.ChannelMessagePhoto, name);
}
} else if (messageObject.isVideo()) {
2018-07-30 04:07:02 +02:00
if (!shortMessage && Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(messageObject.messageOwner.message)) {
msg = LocaleController.formatString("NotificationMessageText", R.string.NotificationMessageText, name, "\uD83D\uDCF9 " + messageObject.messageOwner.message);
2017-12-08 18:35:59 +01:00
text[0] = true;
} else {
msg = LocaleController.formatString("ChannelMessageVideo", R.string.ChannelMessageVideo, name);
}
} else if (messageObject.isVoice()) {
msg = LocaleController.formatString("ChannelMessageAudio", R.string.ChannelMessageAudio, name);
} else if (messageObject.isRoundVideo()) {
msg = LocaleController.formatString("ChannelMessageRound", R.string.ChannelMessageRound, name);
} else if (messageObject.isMusic()) {
msg = LocaleController.formatString("ChannelMessageMusic", R.string.ChannelMessageMusic, name);
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaContact) {
2019-05-14 14:08:05 +02:00
TLRPC.TL_messageMediaContact mediaContact = (TLRPC.TL_messageMediaContact) messageObject.messageOwner.media;
msg = LocaleController.formatString("ChannelMessageContact2", R.string.ChannelMessageContact2, name, ContactsController.formatName(mediaContact.first_name, mediaContact.last_name));
2019-01-23 18:03:33 +01:00
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaPoll) {
2019-05-14 14:08:05 +02:00
TLRPC.TL_messageMediaPoll mediaPoll = (TLRPC.TL_messageMediaPoll) messageObject.messageOwner.media;
2020-01-23 07:15:40 +01:00
if (mediaPoll.poll.quiz) {
msg = LocaleController.formatString("ChannelMessageQuiz2", R.string.ChannelMessageQuiz2, name, mediaPoll.poll.question);
} else {
msg = LocaleController.formatString("ChannelMessagePoll2", R.string.ChannelMessagePoll2, name, mediaPoll.poll.question);
}
2017-12-08 18:35:59 +01:00
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaGeo || messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaVenue) {
msg = LocaleController.formatString("ChannelMessageMap", R.string.ChannelMessageMap, name);
2018-07-30 04:07:02 +02:00
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaGeoLive) {
msg = LocaleController.formatString("ChannelMessageLiveLocation", R.string.ChannelMessageLiveLocation, name);
2017-12-08 18:35:59 +01:00
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaDocument) {
2019-07-18 15:01:39 +02:00
if (messageObject.isSticker() || messageObject.isAnimatedSticker()) {
2017-12-08 18:35:59 +01:00
String emoji = messageObject.getStickerEmoji();
if (emoji != null) {
msg = LocaleController.formatString("ChannelMessageStickerEmoji", R.string.ChannelMessageStickerEmoji, name, emoji);
2015-09-24 22:52:02 +02:00
} else {
2017-12-08 18:35:59 +01:00
msg = LocaleController.formatString("ChannelMessageSticker", R.string.ChannelMessageSticker, name);
2015-09-24 22:52:02 +02:00
}
2017-12-08 18:35:59 +01:00
} else if (messageObject.isGif()) {
2018-07-30 04:07:02 +02:00
if (!shortMessage && Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(messageObject.messageOwner.message)) {
msg = LocaleController.formatString("NotificationMessageText", R.string.NotificationMessageText, name, "\uD83C\uDFAC " + messageObject.messageOwner.message);
2017-12-08 18:35:59 +01:00
text[0] = true;
2016-10-11 13:57:01 +02:00
} else {
2017-12-08 18:35:59 +01:00
msg = LocaleController.formatString("ChannelMessageGIF", R.string.ChannelMessageGIF, name);
2016-10-11 13:57:01 +02:00
}
2017-12-08 18:35:59 +01:00
} else {
2018-07-30 04:07:02 +02:00
if (!shortMessage && Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(messageObject.messageOwner.message)) {
msg = LocaleController.formatString("NotificationMessageText", R.string.NotificationMessageText, name, "\uD83D\uDCCE " + messageObject.messageOwner.message);
2017-12-08 18:35:59 +01:00
text[0] = true;
2015-09-24 22:52:02 +02:00
} else {
2017-12-08 18:35:59 +01:00
msg = LocaleController.formatString("ChannelMessageDocument", R.string.ChannelMessageDocument, name);
2015-09-24 22:52:02 +02:00
}
2015-01-02 23:15:07 +01:00
}
2020-03-30 14:00:09 +02:00
} else {
if (!shortMessage && !TextUtils.isEmpty(messageObject.messageText)) {
msg = LocaleController.formatString("NotificationMessageText", R.string.NotificationMessageText, name, messageObject.messageText);
text[0] = true;
} else {
msg = LocaleController.formatString("ChannelMessageNoText", R.string.ChannelMessageNoText, name);
}
2014-07-10 02:15:58 +02:00
}
2017-03-31 01:58:05 +02:00
} else {
if (messageObject.isMediaEmpty()) {
2020-03-30 14:00:09 +02:00
if (!shortMessage && !TextUtils.isEmpty(messageObject.messageOwner.message)) {
2017-03-31 01:58:05 +02:00
msg = LocaleController.formatString("NotificationMessageGroupText", R.string.NotificationMessageGroupText, name, chat.title, messageObject.messageOwner.message);
} else {
msg = LocaleController.formatString("NotificationMessageGroupNoText", R.string.NotificationMessageGroupNoText, name, chat.title);
}
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaPhoto) {
2018-07-30 04:07:02 +02:00
if (!shortMessage && Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(messageObject.messageOwner.message)) {
msg = LocaleController.formatString("NotificationMessageGroupText", R.string.NotificationMessageGroupText, name, chat.title, "\uD83D\uDDBC " + messageObject.messageOwner.message);
2017-03-31 01:58:05 +02:00
} else {
msg = LocaleController.formatString("NotificationMessageGroupPhoto", R.string.NotificationMessageGroupPhoto, name, chat.title);
}
} else if (messageObject.isVideo()) {
2018-07-30 04:07:02 +02:00
if (!shortMessage && Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(messageObject.messageOwner.message)) {
msg = LocaleController.formatString("NotificationMessageGroupText", R.string.NotificationMessageGroupText, name, chat.title, "\uD83D\uDCF9 " + messageObject.messageOwner.message);
2017-03-31 01:58:05 +02:00
} else {
2019-01-23 18:03:33 +01:00
msg = LocaleController.formatString(" ", R.string.NotificationMessageGroupVideo, name, chat.title);
2017-03-31 01:58:05 +02:00
}
} else if (messageObject.isVoice()) {
msg = LocaleController.formatString("NotificationMessageGroupAudio", R.string.NotificationMessageGroupAudio, name, chat.title);
2017-07-08 18:32:04 +02:00
} else if (messageObject.isRoundVideo()) {
msg = LocaleController.formatString("NotificationMessageGroupRound", R.string.NotificationMessageGroupRound, name, chat.title);
2017-03-31 01:58:05 +02:00
} else if (messageObject.isMusic()) {
msg = LocaleController.formatString("NotificationMessageGroupMusic", R.string.NotificationMessageGroupMusic, name, chat.title);
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaContact) {
2019-05-14 14:08:05 +02:00
TLRPC.TL_messageMediaContact mediaContact = (TLRPC.TL_messageMediaContact) messageObject.messageOwner.media;
msg = LocaleController.formatString("NotificationMessageGroupContact2", R.string.NotificationMessageGroupContact2, name, chat.title, ContactsController.formatName(mediaContact.first_name, mediaContact.last_name));
2019-01-23 18:03:33 +01:00
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaPoll) {
2019-05-14 14:08:05 +02:00
TLRPC.TL_messageMediaPoll mediaPoll = (TLRPC.TL_messageMediaPoll) messageObject.messageOwner.media;
2020-01-23 07:15:40 +01:00
if (mediaPoll.poll.quiz) {
msg = LocaleController.formatString("NotificationMessageGroupQuiz2", R.string.NotificationMessageGroupQuiz2, name, chat.title, mediaPoll.poll.question);
} else {
msg = LocaleController.formatString("NotificationMessageGroupPoll2", R.string.NotificationMessageGroupPoll2, name, chat.title, mediaPoll.poll.question);
}
2017-03-31 01:58:05 +02:00
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaGame) {
msg = LocaleController.formatString("NotificationMessageGroupGame", R.string.NotificationMessageGroupGame, name, chat.title, messageObject.messageOwner.media.game.title);
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaGeo || messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaVenue) {
msg = LocaleController.formatString("NotificationMessageGroupMap", R.string.NotificationMessageGroupMap, name, chat.title);
2018-07-30 04:07:02 +02:00
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaGeoLive) {
msg = LocaleController.formatString("NotificationMessageGroupLiveLocation", R.string.NotificationMessageGroupLiveLocation, name, chat.title);
2017-03-31 01:58:05 +02:00
} else if (messageObject.messageOwner.media instanceof TLRPC.TL_messageMediaDocument) {
2019-07-18 15:01:39 +02:00
if (messageObject.isSticker() || messageObject.isAnimatedSticker()) {
2017-03-31 01:58:05 +02:00
String emoji = messageObject.getStickerEmoji();
if (emoji != null) {
msg = LocaleController.formatString("NotificationMessageGroupStickerEmoji", R.string.NotificationMessageGroupStickerEmoji, name, chat.title, emoji);
} else {
msg = LocaleController.formatString("NotificationMessageGroupSticker", R.string.NotificationMessageGroupSticker, name, chat.title);
}
} else if (messageObject.isGif()) {
2018-07-30 04:07:02 +02:00
if (!shortMessage && Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(messageObject.messageOwner.message)) {
msg = LocaleController.formatString("NotificationMessageGroupText", R.string.NotificationMessageGroupText, name, chat.title, "\uD83C\uDFAC " + messageObject.messageOwner.message);
2017-03-31 01:58:05 +02:00
} else {
msg = LocaleController.formatString("NotificationMessageGroupGif", R.string.NotificationMessageGroupGif, name, chat.title);
}
} else {
2018-07-30 04:07:02 +02:00
if (!shortMessage && Build.VERSION.SDK_INT >= 19 && !TextUtils.isEmpty(messageObject.messageOwner.message)) {
msg = LocaleController.formatString("NotificationMessageGroupText", R.string.NotificationMessageGroupText, name, chat.title, "\uD83D\uDCCE " + messageObject.messageOwner.message);
2017-03-31 01:58:05 +02:00
} else {
msg = LocaleController.formatString("NotificationMessageGroupDocument", R.string.NotificationMessageGroupDocument, name, chat.title);
}
}
2020-03-30 14:00:09 +02:00
} else {
if (!shortMessage && !TextUtils.isEmpty(messageObject.messageText)) {
msg = LocaleController.formatString("NotificationMessageGroupText", R.string.NotificationMessageGroupText, name, chat.title, messageObject.messageText);
} else {
msg = LocaleController.formatString("NotificationMessageGroupNoText", R.string.NotificationMessageGroupNoText, name, chat.title);
}
2017-03-31 01:58:05 +02:00
}
2014-07-10 02:15:58 +02:00
}
} else {
2019-01-23 18:03:33 +01:00
if (preview != null) {
preview[0] = false;
}
2016-03-06 02:49:31 +01:00
if (ChatObject.isChannel(chat) && !chat.megagroup) {
2018-07-30 04:07:02 +02:00
msg = LocaleController.formatString("ChannelMessageNoText", R.string.ChannelMessageNoText, name);
2016-03-06 02:49:31 +01:00
} else {
msg = LocaleController.formatString("NotificationMessageGroupNoText", R.string.NotificationMessageGroupNoText, name, chat.title);
}
2014-07-10 02:15:58 +02:00
}
}
}
return msg;
}
private void scheduleNotificationRepeat() {
try {
2018-07-30 04:07:02 +02:00
Intent intent = new Intent(ApplicationLoader.applicationContext, NotificationRepeat.class);
intent.putExtra("currentAccount", currentAccount);
PendingIntent pintent = PendingIntent.getService(ApplicationLoader.applicationContext, 0, intent, 0);
2019-07-18 15:01:39 +02:00
SharedPreferences preferences = getAccountInstance().getNotificationsSettings();
2014-11-21 01:14:44 +01:00
int minutes = preferences.getInt("repeat_messages", 60);
2014-11-21 11:59:05 +01:00
if (minutes > 0 && personal_count > 0) {
2015-06-29 19:12:11 +02:00
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + minutes * 60 * 1000, pintent);
} else {
2015-06-29 19:12:11 +02:00
alarmManager.cancel(pintent);
}
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
}
}
2015-10-29 18:10:07 +01:00
private boolean isPersonalMessage(MessageObject messageObject) {
2020-09-30 15:48:47 +02:00
return messageObject.messageOwner.peer_id != null && messageObject.messageOwner.peer_id.chat_id == 0 && messageObject.messageOwner.peer_id.channel_id == 0
2015-10-29 18:10:07 +01:00
&& (messageObject.messageOwner.action == null || messageObject.messageOwner.action instanceof TLRPC.TL_messageActionEmpty);
}
private int getNotifyOverride(SharedPreferences preferences, long dialog_id) {
2018-07-30 04:07:02 +02:00
int notifyOverride = preferences.getInt("notify2_" + dialog_id, -1);
2015-10-29 18:10:07 +01:00
if (notifyOverride == 3) {
int muteUntil = preferences.getInt("notifyuntil_" + dialog_id, 0);
2019-07-18 15:01:39 +02:00
if (muteUntil >= getConnectionsManager().getCurrentTime()) {
2015-10-29 18:10:07 +01:00
notifyOverride = 2;
}
}
2020-04-24 11:21:58 +02:00
/*if (BuildVars.LOGS_ENABLED && BuildVars.DEBUG_VERSION) {
FileLog.d("notify override for " + dialog_id + " = " + notifyOverride);
}*/
2015-10-29 18:10:07 +01:00
return notifyOverride;
}
2019-05-14 14:08:05 +02:00
public void showNotifications() {
notificationsQueue.postRunnable(() -> showOrUpdateNotification(false));
}
public void hideNotifications() {
notificationsQueue.postRunnable(() -> {
notificationManager.cancel(notificationId);
lastWearNotifiedMessageId.clear();
for (int a = 0; a < wearNotificationsIds.size(); a++) {
notificationManager.cancel(wearNotificationsIds.valueAt(a));
}
wearNotificationsIds.clear();
});
}
2015-10-29 18:10:07 +01:00
private void dismissNotification() {
try {
2018-07-30 04:07:02 +02:00
notificationManager.cancel(notificationId);
2015-10-29 18:10:07 +01:00
pushMessages.clear();
pushMessagesDict.clear();
2018-07-30 04:07:02 +02:00
lastWearNotifiedMessageId.clear();
for (int a = 0; a < wearNotificationsIds.size(); a++) {
2020-07-26 10:03:38 +02:00
long did = wearNotificationsIds.keyAt(a);
if (openedInBubbleDialogs.contains(did)) {
continue;
}
2018-07-30 04:07:02 +02:00
notificationManager.cancel(wearNotificationsIds.valueAt(a));
2015-10-29 18:10:07 +01:00
}
wearNotificationsIds.clear();
2018-08-27 10:33:11 +02:00
AndroidUtilities.runOnUIThread(() -> NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.pushMessagesUpdated));
2018-07-30 04:07:02 +02:00
if (WearDataLayerListenerService.isWatchConnected()) {
try {
JSONObject o = new JSONObject();
2019-07-18 15:01:39 +02:00
o.put("id", getUserConfig().getClientUserId());
2018-07-30 04:07:02 +02:00
o.put("cancel_all", true);
2019-05-14 14:08:05 +02:00
WearDataLayerListenerService.sendMessageToWatch("/notify", o.toString().getBytes(), "remote_notifications");
2018-07-30 04:07:02 +02:00
} catch (JSONException ignore) {
}
}
2015-10-29 18:10:07 +01:00
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2015-10-29 18:10:07 +01:00
}
}
private void playInChatSound() {
2016-03-06 02:49:31 +01:00
if (!inChatSoundEnabled || MediaController.getInstance().isRecordingAudio()) {
2015-10-29 18:10:07 +01:00
return;
}
try {
if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
return;
}
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2015-10-29 18:10:07 +01:00
}
try {
2019-07-18 15:01:39 +02:00
SharedPreferences preferences = getAccountInstance().getNotificationsSettings();
2016-03-16 13:26:32 +01:00
int notifyOverride = getNotifyOverride(preferences, opened_dialog_id);
2015-10-29 18:10:07 +01:00
if (notifyOverride == 2) {
return;
}
2018-08-27 10:33:11 +02:00
notificationsQueue.postRunnable(() -> {
if (Math.abs(System.currentTimeMillis() - lastSoundPlay) <= 500) {
return;
}
try {
if (soundPool == null) {
soundPool = new SoundPool(3, AudioManager.STREAM_SYSTEM, 0);
soundPool.setOnLoadCompleteListener((soundPool, sampleId, status) -> {
if (status == 0) {
try {
soundPool.play(sampleId, 1.0f, 1.0f, 1, 0, 1.0f);
} catch (Exception e) {
FileLog.e(e);
2015-10-29 18:10:07 +01:00
}
2017-03-31 01:58:05 +02:00
}
2018-08-27 10:33:11 +02:00
});
}
if (soundIn == 0 && !soundInLoaded) {
soundInLoaded = true;
soundIn = soundPool.load(ApplicationLoader.applicationContext, R.raw.sound_in, 1);
}
if (soundIn != 0) {
try {
soundPool.play(soundIn, 1.0f, 1.0f, 1, 0, 1.0f);
} catch (Exception e) {
FileLog.e(e);
2015-10-29 18:10:07 +01:00
}
}
2018-08-27 10:33:11 +02:00
} catch (Exception e) {
FileLog.e(e);
2015-10-29 18:10:07 +01:00
}
});
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2015-10-29 18:10:07 +01:00
}
}
2015-02-01 19:51:02 +01:00
private void scheduleNotificationDelay(boolean onlineReason) {
try {
2018-07-30 04:07:02 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("delay notification start, onlineReason = " + onlineReason);
}
2015-06-29 19:12:11 +02:00
notificationDelayWakelock.acquire(10000);
2018-07-30 04:07:02 +02:00
notificationsQueue.cancelRunnable(notificationDelayRunnable);
notificationsQueue.postRunnable(notificationDelayRunnable, (onlineReason ? 3 * 1000 : 1000));
2015-02-01 19:51:02 +01:00
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2015-06-29 19:12:11 +02:00
showOrUpdateNotification(notifyCheck);
}
2015-02-01 19:51:02 +01:00
}
protected void repeatNotificationMaybe() {
2018-08-27 10:33:11 +02:00
notificationsQueue.postRunnable(() -> {
int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
if (hour >= 11 && hour <= 22) {
notificationManager.cancel(notificationId);
showOrUpdateNotification(true);
} else {
scheduleNotificationRepeat();
2015-02-01 19:51:02 +01:00
}
});
}
2018-07-30 04:07:02 +02:00
private boolean isEmptyVibration(long[] pattern) {
if (pattern == null || pattern.length == 0) {
return false;
}
for (int a = 0; a < pattern.length; a++) {
2019-01-23 18:03:33 +01:00
if (pattern[a] != 0) {
2018-07-30 04:07:02 +02:00
return false;
}
}
return true;
}
2019-06-04 12:14:50 +02:00
@TargetApi(26)
public void deleteNotificationChannel(long dialogId) {
notificationsQueue.postRunnable(() -> {
if (Build.VERSION.SDK_INT < 26) {
return;
}
try {
2019-07-18 15:01:39 +02:00
SharedPreferences preferences = getAccountInstance().getNotificationsSettings();
2019-06-04 12:14:50 +02:00
String key = "org.telegram.key" + dialogId;
String channelId = preferences.getString(key, null);
if (channelId != null) {
preferences.edit().remove(key).remove(key + "_s").commit();
systemNotificationManager.deleteNotificationChannel(channelId);
}
} catch (Exception e) {
FileLog.e(e);
}
});
}
@TargetApi(26)
public void deleteAllNotificationChannels() {
notificationsQueue.postRunnable(() -> {
if (Build.VERSION.SDK_INT < 26) {
return;
}
try {
2019-07-18 15:01:39 +02:00
SharedPreferences preferences = getAccountInstance().getNotificationsSettings();
2019-06-04 12:14:50 +02:00
Map<String, ?> values = preferences.getAll();
SharedPreferences.Editor editor = preferences.edit();
for (Map.Entry<String, ?> entry : values.entrySet()) {
String key = entry.getKey();
if (key.startsWith("org.telegram.key")) {
if (!key.endsWith("_s")) {
systemNotificationManager.deleteNotificationChannel((String) entry.getValue());
}
editor.remove(key);
}
}
editor.commit();
} catch (Exception e) {
FileLog.e(e);
}
});
}
2020-08-14 18:58:22 +02:00
private boolean unsupportedNotificationShortcut() {
return Build.VERSION.SDK_INT < 29 || !SharedConfig.chatBubbles;
}
2019-06-04 12:14:50 +02:00
2020-07-26 10:03:38 +02:00
@SuppressLint("RestrictedApi")
private void createNotificationShortcut(NotificationCompat.Builder builder, int did, String name, TLRPC.User user, TLRPC.Chat chat, Person person) {
if (unsupportedNotificationShortcut() || ChatObject.isChannel(chat) && !chat.megagroup) {
2020-07-26 10:03:38 +02:00
return;
}
try {
String id = "ndid_" + did;
ShortcutInfoCompat.Builder shortcutBuilder = new ShortcutInfoCompat.Builder(ApplicationLoader.applicationContext, id)
.setShortLabel(chat != null ? name : UserObject.getFirstName(user))
.setLongLabel(name)
.setIntent(new Intent(Intent.ACTION_DEFAULT))
.setLongLived(true);
Bitmap avatar = null;
if (person != null) {
shortcutBuilder.setPerson(person);
shortcutBuilder.setIcon(person.getIcon());
if (person.getIcon() != null) {
avatar = person.getIcon().getBitmap();
}
}
ArrayList<ShortcutInfoCompat> arrayList = new ArrayList<>(1);
arrayList.add(shortcutBuilder.build());
ShortcutManagerCompat.addDynamicShortcuts(ApplicationLoader.applicationContext, arrayList);
builder.setShortcutId(id);
NotificationCompat.BubbleMetadata.Builder bubbleBuilder = new NotificationCompat.BubbleMetadata.Builder();
Intent intent = new Intent(ApplicationLoader.applicationContext, BubbleActivity.class);
intent.setAction("com.tmessages.openchat" + Math.random() + Integer.MAX_VALUE);
if (did > 0) {
intent.putExtra("userId", did);
} else {
intent.putExtra("chatId", -did);
}
intent.putExtra("currentAccount", currentAccount);
bubbleBuilder.setIntent(PendingIntent.getActivity(ApplicationLoader.applicationContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT));
bubbleBuilder.setSuppressNotification(true);
bubbleBuilder.setAutoExpandBubble(false);
bubbleBuilder.setDesiredHeight(AndroidUtilities.dp(640));
if (avatar != null) {
bubbleBuilder.setIcon(IconCompat.createWithAdaptiveBitmap(avatar));
} else {
if (user != null) {
bubbleBuilder.setIcon(IconCompat.createWithResource(ApplicationLoader.applicationContext, user.bot ? R.drawable.book_bot : R.drawable.book_user));
} else {
bubbleBuilder.setIcon(IconCompat.createWithResource(ApplicationLoader.applicationContext, R.drawable.book_group));
}
}
builder.setBubbleMetadata(bubbleBuilder.build());
} catch (Exception e) {
FileLog.e(e);
}
}
2018-07-30 04:07:02 +02:00
@TargetApi(26)
private String validateChannelId(long dialogId, String name, long[] vibrationPattern, int ledColor, Uri sound, int importance, long[] configVibrationPattern, Uri configSound, int configImportance) {
2019-07-18 15:01:39 +02:00
SharedPreferences preferences = getAccountInstance().getNotificationsSettings();
2018-07-30 04:07:02 +02:00
String key = "org.telegram.key" + dialogId;
String channelId = preferences.getString(key, null);
String settings = preferences.getString(key + "_s", null);
boolean edited = false;
StringBuilder newSettings = new StringBuilder();
String newSettingsHash;
/*NotificationChannel existingChannel = systemNotificationManager.getNotificationChannel(channelId);
if (existingChannel != null) {
int channelImportance = existingChannel.getImportance();
Uri channelSound = existingChannel.getSound();
long[] channelVibrationPattern = existingChannel.getVibrationPattern();
int channelLedColor = existingChannel.getLightColor();
if (channelVibrationPattern != null) {
for (int a = 0; a < channelVibrationPattern.length; a++) {
newSettings.append(channelVibrationPattern[a]);
}
}
newSettings.append(channelLedColor);
if (channelSound != null) {
newSettings.append(channelSound.toString());
}
newSettings.append(channelImportance);
newSettingsHash = Utilities.MD5(newSettings.toString());
newSettings.setLength(0);
if (!settings.equals(newSettingsHash)) {
SharedPreferences.Editor editor = null;
if (channelImportance != configImportance) {
if (editor == null) {
editor = preferences.edit();
}
int priority;
if (channelImportance == NotificationManager.IMPORTANCE_HIGH || channelImportance == NotificationManager.IMPORTANCE_MAX) {
priority = 1;
} else if (channelImportance == NotificationManager.IMPORTANCE_MIN) {
priority = 4;
} else if (channelImportance == NotificationManager.IMPORTANCE_LOW) {
priority = 5;
} else {
priority = 0;
}
editor.putInt("priority_" + dialogId, priority);
if (configImportance == importance) {
importance = channelImportance;
edited = true;
}
}
if (configSound == null || channelSound != null || configSound != null && channelSound == null || !configSound.equals(channelSound)) {
if (editor == null) {
editor = preferences.edit();
}
String newSound;
if (channelSound == null) {
newSound = "NoSound";
editor.putString("sound_" + dialogId, "NoSound");
} else {
newSound = channelSound.toString();
Ringtone rng = RingtoneManager.getRingtone(ApplicationLoader.applicationContext, channelSound);
String ringtoneName = null;
if (rng != null) {
if (channelSound.equals(Settings.System.DEFAULT_RINGTONE_URI)) {
ringtoneName = LocaleController.getString("DefaultRingtone", R.string.DefaultRingtone);
} else {
ringtoneName = rng.getTitle(ApplicationLoader.applicationContext);
}
rng.stop();
}
if (ringtoneName != null) {
editor.putString("sound_" + dialogId, ringtoneName);
}
}
editor.putString("sound_path_" + dialogId, newSound);
if (configSound == null && sound == null || configSound != null && sound != null || configSound.equals(sound)) {
sound = channelSound;
edited = true;
}
}
boolean vibrate = existingChannel.shouldVibrate();
if (isEmptyVibration(configVibrationPattern) != vibrate) {
if (editor == null) {
editor = preferences.edit();
}
editor.putInt("vibrate_" + dialogId, vibrate ? 0 : 2);
}
if (editor != null) {
editor.putBoolean("custom_" + dialogId, true);
editor.commit();
}
}
}*/
2019-12-31 14:08:08 +01:00
boolean secretChat = (int) dialogId == 0;
2018-07-30 04:07:02 +02:00
for (int a = 0; a < vibrationPattern.length; a++) {
newSettings.append(vibrationPattern[a]);
}
newSettings.append(ledColor);
if (sound != null) {
newSettings.append(sound.toString());
}
newSettings.append(importance);
2019-12-31 14:08:08 +01:00
if (secretChat) {
newSettings.append("secret");
}
2018-07-30 04:07:02 +02:00
newSettingsHash = Utilities.MD5(newSettings.toString());
if (channelId != null && !settings.equals(newSettingsHash)) {
if (edited) {
preferences.edit().putString(key, channelId).putString(key + "_s", newSettingsHash).commit();
} else {
systemNotificationManager.deleteNotificationChannel(channelId);
channelId = null;
}
}
if (channelId == null) {
channelId = currentAccount + "channel" + dialogId + "_" + Utilities.random.nextLong();
2019-12-31 14:08:08 +01:00
NotificationChannel notificationChannel = new NotificationChannel(channelId, secretChat ? LocaleController.getString("SecretChatName", R.string.SecretChatName) : name, importance);
2018-07-30 04:07:02 +02:00
if (ledColor != 0) {
notificationChannel.enableLights(true);
notificationChannel.setLightColor(ledColor);
}
if (!isEmptyVibration(vibrationPattern)) {
notificationChannel.enableVibration(true);
if (vibrationPattern != null && vibrationPattern.length > 0) {
notificationChannel.setVibrationPattern(vibrationPattern);
}
} else {
notificationChannel.enableVibration(false);
}
AudioAttributes.Builder builder = new AudioAttributes.Builder();
builder.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION);
builder.setUsage(AudioAttributes.USAGE_NOTIFICATION);
if (sound != null) {
notificationChannel.setSound(sound, builder.build());
} else {
notificationChannel.setSound(null, builder.build());
}
systemNotificationManager.createNotificationChannel(notificationChannel);
preferences.edit().putString(key, channelId).putString(key + "_s", newSettingsHash).commit();
}
return channelId;
}
2014-07-10 02:15:58 +02:00
private void showOrUpdateNotification(boolean notifyAboutLast) {
2019-07-18 15:01:39 +02:00
if (!getUserConfig().isClientActivated() || pushMessages.isEmpty() || !SharedConfig.showNotificationsForAllAccounts && currentAccount != UserConfig.selectedAccount) {
2014-07-10 02:15:58 +02:00
dismissNotification();
return;
}
try {
2019-07-18 15:01:39 +02:00
getConnectionsManager().resumeNetworkMaybe();
2014-07-10 02:15:58 +02:00
2014-07-11 15:54:17 +02:00
MessageObject lastMessageObject = pushMessages.get(0);
2019-07-18 15:01:39 +02:00
SharedPreferences preferences = getAccountInstance().getNotificationsSettings();
int dismissDate = preferences.getInt("dismissDate", 0);
if (lastMessageObject.messageOwner.date <= dismissDate) {
dismissNotification();
return;
}
2014-07-10 02:15:58 +02:00
long dialog_id = lastMessageObject.getDialogId();
2019-01-23 18:03:33 +01:00
boolean isChannel = false;
long override_dialog_id = dialog_id;
2015-11-26 22:04:02 +01:00
if (lastMessageObject.messageOwner.mentioned) {
2020-09-30 15:48:47 +02:00
override_dialog_id = lastMessageObject.getFromChatId();
}
int mid = lastMessageObject.getId();
2020-09-30 15:48:47 +02:00
int chat_id = lastMessageObject.messageOwner.peer_id.chat_id != 0 ? lastMessageObject.messageOwner.peer_id.chat_id : lastMessageObject.messageOwner.peer_id.channel_id;
int user_id = lastMessageObject.messageOwner.peer_id.user_id;
if (lastMessageObject.isFromUser() && (user_id == 0 || user_id == getUserConfig().getClientUserId())) {
user_id = lastMessageObject.messageOwner.from_id.user_id;
2014-07-10 02:15:58 +02:00
}
2019-07-18 15:01:39 +02:00
TLRPC.User user = getMessagesController().getUser(user_id);
2014-07-15 21:57:09 +02:00
TLRPC.Chat chat = null;
if (chat_id != 0) {
2019-07-18 15:01:39 +02:00
chat = getMessagesController().getChat(chat_id);
2020-04-24 11:21:58 +02:00
if (chat == null && lastMessageObject.isFcmMessage()) {
isChannel = lastMessageObject.localChannel;
} else {
isChannel = ChatObject.isChannel(chat) && !chat.megagroup;
}
2014-07-15 21:57:09 +02:00
}
2014-07-10 02:15:58 +02:00
TLRPC.FileLocation photoPath = null;
boolean notifyDisabled = false;
int needVibrate = 0;
2018-07-30 04:07:02 +02:00
String choosenSoundPath;
2017-03-31 01:58:05 +02:00
int ledColor = 0xff0000ff;
2014-11-21 01:14:44 +01:00
int priority = 0;
2014-07-10 02:15:58 +02:00
int notifyOverride = getNotifyOverride(preferences, override_dialog_id);
2018-07-30 04:07:02 +02:00
boolean value;
if (notifyOverride == -1) {
2020-04-24 11:21:58 +02:00
value = isGlobalNotificationsEnabled(dialog_id, isChannel);
2018-07-30 04:07:02 +02:00
} else {
value = notifyOverride != 2;
}
if (!notifyAboutLast || !value) {
2014-07-10 02:15:58 +02:00
notifyDisabled = true;
}
if (!notifyDisabled && dialog_id == override_dialog_id && chat != null) {
2017-03-31 01:58:05 +02:00
int notifyMaxCount;
int notifyDelay;
if (preferences.getBoolean("custom_" + dialog_id, false)) {
notifyMaxCount = preferences.getInt("smart_max_count_" + dialog_id, 2);
notifyDelay = preferences.getInt("smart_delay_" + dialog_id, 3 * 60);
} else {
notifyMaxCount = 2;
notifyDelay = 3 * 60;
}
if (notifyMaxCount != 0) {
Point dialogInfo = smartNotificationsDialogs.get(dialog_id);
if (dialogInfo == null) {
dialogInfo = new Point(1, (int) (System.currentTimeMillis() / 1000));
smartNotificationsDialogs.put(dialog_id, dialogInfo);
} else {
int lastTime = dialogInfo.y;
if (lastTime + notifyDelay < System.currentTimeMillis() / 1000) {
dialogInfo.set(1, (int) (System.currentTimeMillis() / 1000));
} else {
int count = dialogInfo.x;
if (count < notifyMaxCount) {
dialogInfo.set(count + 1, (int) (System.currentTimeMillis() / 1000));
} else {
notifyDisabled = true;
}
}
}
}
}
2014-07-10 02:15:58 +02:00
String defaultPath = Settings.System.DEFAULT_NOTIFICATION_URI.getPath();
2018-07-30 04:07:02 +02:00
boolean inAppSounds = preferences.getBoolean("EnableInAppSounds", true);
boolean inAppVibrate = preferences.getBoolean("EnableInAppVibrate", true);
boolean inAppPreview = preferences.getBoolean("EnableInAppPreview", true);
boolean inAppPriority = preferences.getBoolean("EnableInAppPriority", false);
boolean custom;
int vibrateOverride;
int priorityOverride;
if (custom = preferences.getBoolean("custom_" + dialog_id, false)) {
vibrateOverride = preferences.getInt("vibrate_" + dialog_id, 0);
priorityOverride = preferences.getInt("priority_" + dialog_id, 3);
choosenSoundPath = preferences.getString("sound_path_" + dialog_id, null);
} else {
vibrateOverride = 0;
priorityOverride = 3;
choosenSoundPath = null;
}
boolean vibrateOnlyIfSilent = false;
2017-03-31 01:58:05 +02:00
2018-07-30 04:07:02 +02:00
if (chat_id != 0) {
2019-01-23 18:03:33 +01:00
if (isChannel) {
if (choosenSoundPath != null && choosenSoundPath.equals(defaultPath)) {
choosenSoundPath = null;
} else if (choosenSoundPath == null) {
choosenSoundPath = preferences.getString("ChannelSoundPath", defaultPath);
}
needVibrate = preferences.getInt("vibrate_channel", 0);
priority = preferences.getInt("priority_channel", 1);
ledColor = preferences.getInt("ChannelLed", 0xff0000ff);
} else {
if (choosenSoundPath != null && choosenSoundPath.equals(defaultPath)) {
choosenSoundPath = null;
} else if (choosenSoundPath == null) {
choosenSoundPath = preferences.getString("GroupSoundPath", defaultPath);
}
needVibrate = preferences.getInt("vibrate_group", 0);
priority = preferences.getInt("priority_group", 1);
ledColor = preferences.getInt("GroupLed", 0xff0000ff);
2018-07-30 04:07:02 +02:00
}
} else if (user_id != 0) {
if (choosenSoundPath != null && choosenSoundPath.equals(defaultPath)) {
choosenSoundPath = null;
} else if (choosenSoundPath == null) {
choosenSoundPath = preferences.getString("GlobalSoundPath", defaultPath);
2014-07-10 02:15:58 +02:00
}
2018-07-30 04:07:02 +02:00
needVibrate = preferences.getInt("vibrate_messages", 0);
priority = preferences.getInt("priority_messages", 1);
ledColor = preferences.getInt("MessagesLed", 0xff0000ff);
}
if (custom) {
if (preferences.contains("color_" + dialog_id)) {
ledColor = preferences.getInt("color_" + dialog_id, 0);
2014-07-10 02:15:58 +02:00
}
2018-07-30 04:07:02 +02:00
}
2014-07-10 02:15:58 +02:00
2018-07-30 04:07:02 +02:00
if (priorityOverride != 3) {
priority = priorityOverride;
}
2014-11-21 01:14:44 +01:00
2018-07-30 04:07:02 +02:00
if (needVibrate == 4) {
vibrateOnlyIfSilent = true;
needVibrate = 0;
}
if (needVibrate == 2 && (vibrateOverride == 1 || vibrateOverride == 3) || needVibrate != 2 && vibrateOverride == 2 || vibrateOverride != 0 && vibrateOverride != 4) {
needVibrate = vibrateOverride;
}
if (!ApplicationLoader.mainInterfacePaused) {
if (!inAppSounds) {
choosenSoundPath = null;
2015-03-27 11:32:33 +01:00
}
2018-07-30 04:07:02 +02:00
if (!inAppVibrate) {
needVibrate = 2;
2014-07-10 02:15:58 +02:00
}
2018-07-30 04:07:02 +02:00
if (!inAppPriority) {
priority = 0;
} else if (priority == 2) {
priority = 1;
}
}
if (vibrateOnlyIfSilent && needVibrate != 2) {
try {
int mode = audioManager.getRingerMode();
if (mode != AudioManager.RINGER_MODE_SILENT && mode != AudioManager.RINGER_MODE_VIBRATE) {
needVibrate = 2;
2014-07-10 02:15:58 +02:00
}
2018-07-30 04:07:02 +02:00
} catch (Exception e) {
FileLog.e(e);
2014-07-10 02:15:58 +02:00
}
2018-07-30 04:07:02 +02:00
}
2019-05-14 14:08:05 +02:00
2018-07-30 04:07:02 +02:00
Uri configSound = null;
2019-05-14 14:08:05 +02:00
long[] configVibrationPattern = null;
2018-07-30 04:07:02 +02:00
int configImportance = 0;
if (Build.VERSION.SDK_INT >= 26) {
if (needVibrate == 2) {
configVibrationPattern = new long[]{0, 0};
} else if (needVibrate == 1) {
configVibrationPattern = new long[]{0, 100, 0, 100};
} else if (needVibrate == 0 || needVibrate == 4) {
configVibrationPattern = new long[]{};
} else if (needVibrate == 3) {
configVibrationPattern = new long[]{0, 1000};
}
if (choosenSoundPath != null && !choosenSoundPath.equals("NoSound")) {
if (choosenSoundPath.equals(defaultPath)) {
configSound = Settings.System.DEFAULT_NOTIFICATION_URI;
} else {
configSound = Uri.parse(choosenSoundPath);
2015-03-27 11:32:33 +01:00
}
}
2018-07-30 04:07:02 +02:00
if (priority == 0) {
configImportance = NotificationManager.IMPORTANCE_DEFAULT;
} else if (priority == 1 || priority == 2) {
configImportance = NotificationManager.IMPORTANCE_HIGH;
} else if (priority == 4) {
configImportance = NotificationManager.IMPORTANCE_MIN;
} else if (priority == 5) {
configImportance = NotificationManager.IMPORTANCE_LOW;
}
}
if (notifyDisabled) {
needVibrate = 0;
priority = 0;
ledColor = 0;
choosenSoundPath = null;
2014-07-10 02:15:58 +02:00
}
Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class);
intent.setAction("com.tmessages.openchat" + Math.random() + Integer.MAX_VALUE);
2018-07-30 04:07:02 +02:00
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
if ((int) dialog_id != 0) {
2014-07-23 01:27:00 +02:00
if (pushDialogs.size() == 1) {
if (chat_id != 0) {
intent.putExtra("chatId", chat_id);
} else if (user_id != 0) {
intent.putExtra("userId", user_id);
}
2014-07-10 02:15:58 +02:00
}
2020-01-23 07:15:40 +01:00
if (AndroidUtilities.needShowPasscode() || SharedConfig.isWaitingForPasscodeEnter) {
photoPath = null;
} else {
2018-08-27 10:33:11 +02:00
if (pushDialogs.size() == 1 && Build.VERSION.SDK_INT < 28) {
if (chat != null) {
if (chat.photo != null && chat.photo.photo_small != null && chat.photo.photo_small.volume_id != 0 && chat.photo.photo_small.local_id != 0) {
photoPath = chat.photo.photo_small;
}
2015-10-29 18:10:07 +01:00
} else if (user != null) {
if (user.photo != null && user.photo.photo_small != null && user.photo.photo_small.volume_id != 0 && user.photo.photo_small.local_id != 0) {
photoPath = user.photo.photo_small;
}
2014-07-15 21:57:09 +02:00
}
}
2014-07-10 02:15:58 +02:00
}
} else {
2018-07-30 04:07:02 +02:00
if (pushDialogs.size() == 1 && dialog_id != globalSecretChatId) {
2014-07-23 01:27:00 +02:00
intent.putExtra("encId", (int) (dialog_id >> 32));
}
2014-07-10 02:15:58 +02:00
}
2018-07-30 04:07:02 +02:00
intent.putExtra("currentAccount", currentAccount);
2014-07-10 02:15:58 +02:00
PendingIntent contentIntent = PendingIntent.getActivity(ApplicationLoader.applicationContext, 0, intent, PendingIntent.FLAG_ONE_SHOT);
2015-05-21 23:27:27 +02:00
String name;
2018-07-30 04:07:02 +02:00
String chatName;
2014-07-10 02:15:58 +02:00
boolean replace = true;
2018-07-30 04:07:02 +02:00
if (((chat_id != 0 && chat == null) || user == null) && lastMessageObject.isFcmMessage()) {
chatName = lastMessageObject.localName;
} else if (chat != null) {
chatName = chat.title;
} else {
chatName = UserObject.getUserName(user);
}
2020-01-23 07:15:40 +01:00
boolean passcode = AndroidUtilities.needShowPasscode() || SharedConfig.isWaitingForPasscodeEnter;
2019-12-31 14:08:08 +01:00
if ((int) dialog_id == 0 || pushDialogs.size() > 1 || passcode) {
if (passcode) {
if (chat_id != 0) {
name = LocaleController.getString("NotificationHiddenChatName", R.string.NotificationHiddenChatName);
} else {
name = LocaleController.getString("NotificationHiddenName", R.string.NotificationHiddenName);
}
} else {
name = LocaleController.getString("AppName", R.string.AppName);
}
2014-07-10 02:15:58 +02:00
replace = false;
} else {
2018-07-30 04:07:02 +02:00
name = chatName;
2014-07-10 02:15:58 +02:00
}
2015-05-21 23:27:27 +02:00
String detailText;
2018-07-30 04:07:02 +02:00
if (UserConfig.getActivatedAccountsCount() > 1) {
if (pushDialogs.size() == 1) {
2019-07-18 15:01:39 +02:00
detailText = UserObject.getFirstName(getUserConfig().getCurrentUser());
2018-07-30 04:07:02 +02:00
} else {
2019-07-18 15:01:39 +02:00
detailText = UserObject.getFirstName(getUserConfig().getCurrentUser()) + "";
2018-07-30 04:07:02 +02:00
}
2014-07-10 02:15:58 +02:00
} else {
2018-07-30 04:07:02 +02:00
detailText = "";
}
if (pushDialogs.size() != 1 || Build.VERSION.SDK_INT < 23) {
if (pushDialogs.size() == 1) {
detailText += LocaleController.formatPluralString("NewMessages", total_unread_count);
} else {
detailText += LocaleController.formatString("NotificationMessagesPeopleDisplayOrder", R.string.NotificationMessagesPeopleDisplayOrder, LocaleController.formatPluralString("NewMessages", total_unread_count), LocaleController.formatPluralString("FromChats", pushDialogs.size()));
}
2014-07-10 02:15:58 +02:00
}
2014-07-10 13:30:55 +02:00
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ApplicationLoader.applicationContext)
.setContentTitle(name)
.setSmallIcon(R.drawable.notification)
.setAutoCancel(true)
2014-08-29 23:06:04 +02:00
.setNumber(total_unread_count)
2014-10-04 17:56:09 +02:00
.setContentIntent(contentIntent)
2018-07-30 04:07:02 +02:00
.setGroup(notificationGroup)
2015-01-02 23:15:07 +01:00
.setGroupSummary(true)
2018-07-30 04:07:02 +02:00
.setShowWhen(true)
.setWhen(((long) lastMessageObject.messageOwner.date) * 1000)
2019-05-14 14:08:05 +02:00
.setColor(0xff11acfa);
2014-07-10 13:30:55 +02:00
2019-05-14 14:08:05 +02:00
long[] vibrationPattern = null;
2018-07-30 04:07:02 +02:00
int importance = 0;
Uri sound = null;
2014-11-21 20:36:21 +01:00
mBuilder.setCategory(NotificationCompat.CATEGORY_MESSAGE);
2014-11-24 15:14:40 +01:00
if (chat == null && user != null && user.phone != null && user.phone.length() > 0) {
mBuilder.addPerson("tel:+" + user.phone);
}
2014-11-21 20:36:21 +01:00
2016-03-06 02:49:31 +01:00
int silent = 2;
2014-07-10 02:15:58 +02:00
String lastMessage = null;
boolean hasNewMessages = false;
2014-07-20 01:31:49 +02:00
if (pushMessages.size() == 1) {
2016-03-06 02:49:31 +01:00
MessageObject messageObject = pushMessages.get(0);
2019-05-14 14:08:05 +02:00
boolean[] text = new boolean[1];
2019-01-23 18:03:33 +01:00
String message = lastMessage = getStringForMessage(messageObject, false, text, null);
2016-03-06 02:49:31 +01:00
silent = messageObject.messageOwner.silent ? 1 : 0;
2014-07-10 02:15:58 +02:00
if (message == null) {
2014-07-20 01:31:49 +02:00
return;
2014-07-10 02:15:58 +02:00
}
2014-07-20 01:31:49 +02:00
if (replace) {
if (chat != null) {
message = message.replace(" @ " + name, "");
} else {
2017-12-08 18:35:59 +01:00
if (text[0]) {
message = message.replace(name + ": ", "");
} else {
message = message.replace(name + " ", "");
}
2014-07-20 01:31:49 +02:00
}
2014-07-10 02:15:58 +02:00
}
2014-07-23 01:27:00 +02:00
mBuilder.setContentText(message);
2014-07-20 01:31:49 +02:00
mBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(message));
} else {
2014-07-23 01:27:00 +02:00
mBuilder.setContentText(detailText);
2014-07-20 01:31:49 +02:00
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.setBigContentTitle(name);
int count = Math.min(10, pushMessages.size());
2019-05-14 14:08:05 +02:00
boolean[] text = new boolean[1];
2014-07-20 01:31:49 +02:00
for (int i = 0; i < count; i++) {
2016-03-06 02:49:31 +01:00
MessageObject messageObject = pushMessages.get(i);
2019-01-23 18:03:33 +01:00
String message = getStringForMessage(messageObject, false, text, null);
if (message == null || messageObject.messageOwner.date <= dismissDate) {
2014-07-20 01:31:49 +02:00
continue;
}
2016-03-06 02:49:31 +01:00
if (silent == 2) {
2015-07-22 20:56:37 +02:00
lastMessage = message;
2016-03-06 02:49:31 +01:00
silent = messageObject.messageOwner.silent ? 1 : 0;
2014-07-20 01:31:49 +02:00
}
if (pushDialogs.size() == 1) {
if (replace) {
if (chat != null) {
message = message.replace(" @ " + name, "");
} else {
2017-12-08 18:35:59 +01:00
if (text[0]) {
message = message.replace(name + ": ", "");
} else {
message = message.replace(name + " ", "");
}
2014-07-20 01:31:49 +02:00
}
2014-07-15 21:57:09 +02:00
}
2014-07-10 02:15:58 +02:00
}
2014-07-20 01:31:49 +02:00
inboxStyle.addLine(message);
2014-07-10 02:15:58 +02:00
}
2014-07-20 01:31:49 +02:00
inboxStyle.setSummaryText(detailText);
mBuilder.setStyle(inboxStyle);
2014-07-10 02:15:58 +02:00
}
Intent dismissIntent = new Intent(ApplicationLoader.applicationContext, NotificationDismissReceiver.class);
dismissIntent.putExtra("messageDate", lastMessageObject.messageOwner.date);
2018-07-30 04:07:02 +02:00
dismissIntent.putExtra("currentAccount", currentAccount);
mBuilder.setDeleteIntent(PendingIntent.getBroadcast(ApplicationLoader.applicationContext, 1, dismissIntent, PendingIntent.FLAG_UPDATE_CURRENT));
2014-07-10 02:15:58 +02:00
if (photoPath != null) {
2015-02-01 19:51:02 +01:00
BitmapDrawable img = ImageLoader.getInstance().getImageFromMemory(photoPath, null, "50_50");
2014-07-10 02:15:58 +02:00
if (img != null) {
mBuilder.setLargeIcon(img.getBitmap());
} else {
try {
2017-07-23 19:29:17 +02:00
File file = FileLoader.getPathToAttach(photoPath, true);
if (file.exists()) {
float scaleFactor = 160.0f / AndroidUtilities.dp(50);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = scaleFactor < 1 ? 1 : (int) scaleFactor;
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
if (bitmap != null) {
mBuilder.setLargeIcon(bitmap);
}
}
2019-05-14 14:08:05 +02:00
} catch (Throwable ignore) {
}
2014-07-10 02:15:58 +02:00
}
}
2016-03-06 02:49:31 +01:00
if (!notifyAboutLast || silent == 1) {
mBuilder.setPriority(NotificationCompat.PRIORITY_LOW);
2018-07-30 04:07:02 +02:00
if (Build.VERSION.SDK_INT >= 26) {
importance = NotificationManager.IMPORTANCE_LOW;
}
2016-03-06 02:49:31 +01:00
} else {
if (priority == 0) {
mBuilder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
2018-07-30 04:07:02 +02:00
if (Build.VERSION.SDK_INT >= 26) {
importance = NotificationManager.IMPORTANCE_DEFAULT;
}
} else if (priority == 1 || priority == 2) {
2016-03-06 02:49:31 +01:00
mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
2018-07-30 04:07:02 +02:00
if (Build.VERSION.SDK_INT >= 26) {
importance = NotificationManager.IMPORTANCE_HIGH;
}
} else if (priority == 4) {
mBuilder.setPriority(NotificationCompat.PRIORITY_MIN);
if (Build.VERSION.SDK_INT >= 26) {
importance = NotificationManager.IMPORTANCE_MIN;
}
} else if (priority == 5) {
mBuilder.setPriority(NotificationCompat.PRIORITY_LOW);
if (Build.VERSION.SDK_INT >= 26) {
importance = NotificationManager.IMPORTANCE_LOW;
}
2016-03-06 02:49:31 +01:00
}
}
if (silent != 1 && !notifyDisabled) {
2014-07-10 02:15:58 +02:00
if (ApplicationLoader.mainInterfacePaused || inAppPreview) {
if (lastMessage.length() > 100) {
2016-04-22 15:49:00 +02:00
lastMessage = lastMessage.substring(0, 100).replace('\n', ' ').trim() + "...";
}
2014-07-10 02:15:58 +02:00
mBuilder.setTicker(lastMessage);
}
2016-03-06 02:49:31 +01:00
if (!MediaController.getInstance().isRecordingAudio()) {
if (choosenSoundPath != null && !choosenSoundPath.equals("NoSound")) {
2018-07-30 04:07:02 +02:00
if (Build.VERSION.SDK_INT >= 26) {
if (choosenSoundPath.equals(defaultPath)) {
sound = Settings.System.DEFAULT_NOTIFICATION_URI;
} else {
sound = Uri.parse(choosenSoundPath);
}
2016-03-06 02:49:31 +01:00
} else {
2018-07-30 04:07:02 +02:00
if (choosenSoundPath.equals(defaultPath)) {
mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, AudioManager.STREAM_NOTIFICATION);
} else {
2019-05-14 14:08:05 +02:00
if (Build.VERSION.SDK_INT >= 24 && choosenSoundPath.startsWith("file://") && !AndroidUtilities.isInternalUri(Uri.parse(choosenSoundPath))) {
2019-06-04 12:14:50 +02:00
try {
Uri uri = FileProvider.getUriForFile(ApplicationLoader.applicationContext, BuildConfig.APPLICATION_ID + ".provider", new File(choosenSoundPath.replace("file://", "")));
ApplicationLoader.applicationContext.grantUriPermission("com.android.systemui", uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
mBuilder.setSound(uri, AudioManager.STREAM_NOTIFICATION);
} catch (Exception e) {
mBuilder.setSound(Uri.parse(choosenSoundPath), AudioManager.STREAM_NOTIFICATION);
}
2019-05-14 14:08:05 +02:00
} else {
mBuilder.setSound(Uri.parse(choosenSoundPath), AudioManager.STREAM_NOTIFICATION);
}
2018-07-30 04:07:02 +02:00
}
2016-03-06 02:49:31 +01:00
}
2014-07-10 02:15:58 +02:00
}
}
if (ledColor != 0) {
mBuilder.setLights(ledColor, 1000, 1000);
}
2016-03-06 02:49:31 +01:00
if (needVibrate == 2 || MediaController.getInstance().isRecordingAudio()) {
2018-07-30 04:07:02 +02:00
mBuilder.setVibrate(vibrationPattern = new long[]{0, 0});
} else if (needVibrate == 1) {
2018-07-30 04:07:02 +02:00
mBuilder.setVibrate(vibrationPattern = new long[]{0, 100, 0, 100});
} else if (needVibrate == 0 || needVibrate == 4) {
mBuilder.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
2018-07-30 04:07:02 +02:00
vibrationPattern = new long[]{};
} else if (needVibrate == 3) {
2018-07-30 04:07:02 +02:00
mBuilder.setVibrate(vibrationPattern = new long[]{0, 1000});
2014-07-10 02:15:58 +02:00
}
} else {
2018-07-30 04:07:02 +02:00
mBuilder.setVibrate(vibrationPattern = new long[]{0, 0});
}
boolean hasCallback = false;
2020-01-23 07:15:40 +01:00
if (!AndroidUtilities.needShowPasscode() && !SharedConfig.isWaitingForPasscodeEnter && lastMessageObject.getDialogId() == 777000) {
2018-07-30 04:07:02 +02:00
if (lastMessageObject.messageOwner.reply_markup != null) {
ArrayList<TLRPC.TL_keyboardButtonRow> rows = lastMessageObject.messageOwner.reply_markup.rows;
for (int a = 0, size = rows.size(); a < size; a++) {
TLRPC.TL_keyboardButtonRow row = rows.get(a);
for (int b = 0, size2 = row.buttons.size(); b < size2; b++) {
TLRPC.KeyboardButton button = row.buttons.get(b);
if (button instanceof TLRPC.TL_keyboardButtonCallback) {
Intent callbackIntent = new Intent(ApplicationLoader.applicationContext, NotificationCallbackReceiver.class);
callbackIntent.putExtra("currentAccount", currentAccount);
callbackIntent.putExtra("did", dialog_id);
if (button.data != null) {
callbackIntent.putExtra("data", button.data);
}
callbackIntent.putExtra("mid", lastMessageObject.getId());
mBuilder.addAction(0, button.text, PendingIntent.getBroadcast(ApplicationLoader.applicationContext, lastButtonId++, callbackIntent, PendingIntent.FLAG_UPDATE_CURRENT));
hasCallback = true;
}
}
}
}
2014-07-10 02:15:58 +02:00
}
2018-07-30 04:07:02 +02:00
if (!hasCallback && Build.VERSION.SDK_INT < 24 && SharedConfig.passcodeHash.length() == 0 && hasMessagesToReply()) {
2016-10-11 13:57:01 +02:00
Intent replyIntent = new Intent(ApplicationLoader.applicationContext, PopupReplyReceiver.class);
2018-07-30 04:07:02 +02:00
replyIntent.putExtra("currentAccount", currentAccount);
2016-10-11 13:57:01 +02:00
if (Build.VERSION.SDK_INT <= 19) {
mBuilder.addAction(R.drawable.ic_ab_reply2, LocaleController.getString("Reply", R.string.Reply), PendingIntent.getBroadcast(ApplicationLoader.applicationContext, 2, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT));
} else {
mBuilder.addAction(R.drawable.ic_ab_reply, LocaleController.getString("Reply", R.string.Reply), PendingIntent.getBroadcast(ApplicationLoader.applicationContext, 2, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT));
}
}
2018-07-30 04:07:02 +02:00
if (Build.VERSION.SDK_INT >= 26) {
mBuilder.setChannelId(validateChannelId(dialog_id, chatName, vibrationPattern, ledColor, sound, importance, configVibrationPattern, configSound, configImportance));
}
showExtraNotifications(mBuilder, notifyAboutLast, detailText);
scheduleNotificationRepeat();
2014-07-10 02:15:58 +02:00
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2014-07-10 02:15:58 +02:00
}
}
2020-07-26 10:03:38 +02:00
@SuppressLint("NewApi")
private void setNotificationChannel(Notification mainNotification, NotificationCompat.Builder builder, boolean useSummaryNotification) {
if (useSummaryNotification) {
builder.setChannelId(OTHER_NOTIFICATIONS_CHANNEL);
} else {
builder.setChannelId(mainNotification.getChannelId());
}
}
2015-05-21 23:27:27 +02:00
@SuppressLint("InlinedApi")
2018-07-30 04:07:02 +02:00
private void showExtraNotifications(NotificationCompat.Builder notificationBuilder, boolean notifyAboutLast, String summary) {
Notification mainNotification = notificationBuilder.build();
2015-09-24 22:52:02 +02:00
if (Build.VERSION.SDK_INT < 18) {
2018-07-30 04:07:02 +02:00
notificationManager.notify(notificationId, mainNotification);
2018-08-27 10:33:11 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.d("show summary notification by SDK check");
}
2014-10-04 17:56:09 +02:00
return;
}
2015-06-29 19:12:11 +02:00
2019-09-10 12:56:11 +02:00
SharedPreferences preferences = getAccountInstance().getNotificationsSettings();
2015-01-02 23:15:07 +01:00
ArrayList<Long> sortedDialogs = new ArrayList<>();
2018-07-30 04:07:02 +02:00
LongSparseArray<ArrayList<MessageObject>> messagesByDialogs = new LongSparseArray<>();
2015-06-29 19:12:11 +02:00
for (int a = 0; a < pushMessages.size(); a++) {
MessageObject messageObject = pushMessages.get(a);
2014-10-04 17:56:09 +02:00
long dialog_id = messageObject.getDialogId();
2019-09-10 12:56:11 +02:00
int dismissDate = preferences.getInt("dismissDate" + dialog_id, 0);
if (messageObject.messageOwner.date <= dismissDate) {
continue;
}
2014-10-04 17:56:09 +02:00
ArrayList<MessageObject> arrayList = messagesByDialogs.get(dialog_id);
if (arrayList == null) {
2015-01-02 23:15:07 +01:00
arrayList = new ArrayList<>();
2014-10-04 17:56:09 +02:00
messagesByDialogs.put(dialog_id, arrayList);
sortedDialogs.add(0, dialog_id);
}
arrayList.add(messageObject);
}
2018-07-30 04:07:02 +02:00
LongSparseArray<Integer> oldIdsWear = wearNotificationsIds.clone();
2015-05-21 23:27:27 +02:00
wearNotificationsIds.clear();
2018-07-30 04:07:02 +02:00
class NotificationHolder {
int id;
Notification notification;
NotificationHolder(int i, Notification n) {
id = i;
notification = n;
}
void call() {
2018-08-27 10:33:11 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.w("show dialog notification with id " + id);
}
2018-07-30 04:07:02 +02:00
notificationManager.notify(id, notification);
}
}
ArrayList<NotificationHolder> holders = new ArrayList<>();
JSONArray serializedNotifications = null;
if (WearDataLayerListenerService.isWatchConnected()) {
serializedNotifications = new JSONArray();
}
2018-08-27 10:33:11 +02:00
boolean useSummaryNotification = Build.VERSION.SDK_INT <= Build.VERSION_CODES.O_MR1 || Build.VERSION.SDK_INT > Build.VERSION_CODES.O_MR1 && sortedDialogs.size() > 1;
if (useSummaryNotification && Build.VERSION.SDK_INT >= 26) {
checkOtherNotificationsChannel();
}
2019-09-10 12:56:11 +02:00
int selfUserId = getUserConfig().getClientUserId();
2020-01-23 07:15:40 +01:00
boolean waitingForPasscode = AndroidUtilities.needShowPasscode() || SharedConfig.isWaitingForPasscodeEnter;
2019-09-10 12:56:11 +02:00
2020-08-22 01:59:49 +02:00
int maxCount;
if (UserConfig.getActivatedAccountsCount() >= 3) {
maxCount = 7;
} else {
maxCount = 10;
}
2018-07-30 04:07:02 +02:00
for (int b = 0, size = sortedDialogs.size(); b < size; b++) {
2020-08-22 01:59:49 +02:00
if (holders.size() >= maxCount) {
2020-08-14 18:58:22 +02:00
break;
}
2015-06-29 19:12:11 +02:00
long dialog_id = sortedDialogs.get(b);
2014-10-04 17:56:09 +02:00
ArrayList<MessageObject> messageObjects = messagesByDialogs.get(dialog_id);
int max_id = messageObjects.get(0).getId();
2018-07-30 04:07:02 +02:00
int lowerId = (int) dialog_id;
int highId = (int) (dialog_id >> 32);
Integer internalId = oldIdsWear.get(dialog_id);
if (internalId == null) {
if (lowerId != 0) {
internalId = lowerId;
} else {
internalId = highId;
2014-10-04 17:56:09 +02:00
}
} else {
2018-07-30 04:07:02 +02:00
oldIdsWear.remove(dialog_id);
}
JSONObject serializedChat = null;
if (serializedNotifications != null) {
serializedChat = new JSONObject();
}
/*if (lastWearNotifiedMessageId.get(dialog_id, 0) == max_id) {
continue;
2014-10-04 17:56:09 +02:00
}
2018-07-30 04:07:02 +02:00
lastWearNotifiedMessageId.put(dialog_id, max_id);*/
MessageObject lastMessageObject = messageObjects.get(0);
int max_date = lastMessageObject.messageOwner.date;
TLRPC.Chat chat = null;
TLRPC.User user = null;
boolean isChannel = false;
boolean isSupergroup = false;
String name;
2015-11-26 22:04:02 +01:00
TLRPC.FileLocation photoPath = null;
2018-08-27 10:33:11 +02:00
Bitmap avatarBitmap = null;
File avatalFile = null;
2018-07-30 04:07:02 +02:00
boolean canReply;
2018-08-27 10:33:11 +02:00
LongSparseArray<Person> personCache = new LongSparseArray<>();
2018-07-30 04:07:02 +02:00
if (lowerId != 0) {
2018-08-27 10:33:11 +02:00
canReply = lowerId != 777000;
2018-07-30 04:07:02 +02:00
if (lowerId > 0) {
2019-07-18 15:01:39 +02:00
user = getMessagesController().getUser(lowerId);
2018-07-30 04:07:02 +02:00
if (user == null) {
if (lastMessageObject.isFcmMessage()) {
name = lastMessageObject.localName;
} else {
2018-08-27 10:33:11 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.w("not found user to show dialog notification " + lowerId);
}
2018-07-30 04:07:02 +02:00
continue;
}
} else {
name = UserObject.getUserName(user);
if (user.photo != null && user.photo.photo_small != null && user.photo.photo_small.volume_id != 0 && user.photo.photo_small.local_id != 0) {
photoPath = user.photo.photo_small;
}
}
2020-09-30 15:48:47 +02:00
if (UserObject.isReplyUser(lowerId)) {
name = LocaleController.getString("RepliesTitle", R.string.RepliesTitle);
} else if (lowerId == selfUserId) {
2019-09-10 12:56:11 +02:00
name = LocaleController.getString("MessageScheduledReminderNotification", R.string.MessageScheduledReminderNotification);
}
2015-11-26 22:04:02 +01:00
} else {
2019-07-18 15:01:39 +02:00
chat = getMessagesController().getChat(-lowerId);
2018-07-30 04:07:02 +02:00
if (chat == null) {
if (lastMessageObject.isFcmMessage()) {
isSupergroup = lastMessageObject.isMegagroup();
name = lastMessageObject.localName;
isChannel = lastMessageObject.localChannel;
} else {
2018-08-27 10:33:11 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.w("not found chat to show dialog notification " + lowerId);
}
2018-07-30 04:07:02 +02:00
continue;
}
} else {
isSupergroup = chat.megagroup;
isChannel = ChatObject.isChannel(chat) && !chat.megagroup;
name = chat.title;
if (chat.photo != null && chat.photo.photo_small != null && chat.photo.photo_small.volume_id != 0 && chat.photo.photo_small.local_id != 0) {
photoPath = chat.photo.photo_small;
}
}
2015-11-26 22:04:02 +01:00
}
2018-07-30 04:07:02 +02:00
} else {
canReply = false;
if (dialog_id != globalSecretChatId) {
2019-07-18 15:01:39 +02:00
TLRPC.EncryptedChat encryptedChat = getMessagesController().getEncryptedChat(highId);
2018-07-30 04:07:02 +02:00
if (encryptedChat == null) {
2018-08-27 10:33:11 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.w("not found secret chat to show dialog notification " + highId);
}
2018-07-30 04:07:02 +02:00
continue;
2015-11-26 22:04:02 +01:00
}
2019-07-18 15:01:39 +02:00
user = getMessagesController().getUser(encryptedChat.user_id);
2018-07-30 04:07:02 +02:00
if (user == null) {
2018-08-27 10:33:11 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.w("not found secret chat user to show dialog notification " + encryptedChat.user_id);
}
2018-07-30 04:07:02 +02:00
continue;
2015-11-26 22:04:02 +01:00
}
}
2018-07-30 04:07:02 +02:00
name = LocaleController.getString("SecretChatName", R.string.SecretChatName);
photoPath = null;
serializedChat = null;
2014-10-04 17:56:09 +02:00
}
2019-12-31 14:08:08 +01:00
if (waitingForPasscode) {
if (lowerId < 0) {
name = LocaleController.getString("NotificationHiddenChatName", R.string.NotificationHiddenChatName);
} else {
name = LocaleController.getString("NotificationHiddenName", R.string.NotificationHiddenName);
}
2018-07-30 04:07:02 +02:00
photoPath = null;
canReply = false;
2015-05-21 23:27:27 +02:00
}
2018-08-27 10:33:11 +02:00
if (photoPath != null) {
avatalFile = FileLoader.getPathToAttach(photoPath, true);
2019-07-18 15:01:39 +02:00
if (Build.VERSION.SDK_INT < 28) {
BitmapDrawable img = ImageLoader.getInstance().getImageFromMemory(photoPath, null, "50_50");
if (img != null) {
avatarBitmap = img.getBitmap();
} else {
try {
if (avatalFile.exists()) {
float scaleFactor = 160.0f / AndroidUtilities.dp(50);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = scaleFactor < 1 ? 1 : (int) scaleFactor;
avatarBitmap = BitmapFactory.decodeFile(avatalFile.getAbsolutePath(), options);
}
} catch (Throwable ignore) {
2018-08-27 10:33:11 +02:00
2019-07-18 15:01:39 +02:00
}
2018-08-27 10:33:11 +02:00
}
}
}
2015-09-24 22:52:02 +02:00
NotificationCompat.Action wearReplyAction = null;
2020-09-30 15:48:47 +02:00
if ((!isChannel || isSupergroup) && canReply && !SharedConfig.isWaitingForPasscodeEnter && selfUserId != lowerId && !UserObject.isReplyUser(lowerId)) {
2015-09-24 22:52:02 +02:00
Intent replyIntent = new Intent(ApplicationLoader.applicationContext, WearReplyReceiver.class);
replyIntent.putExtra("dialog_id", dialog_id);
replyIntent.putExtra("max_id", max_id);
2018-07-30 04:07:02 +02:00
replyIntent.putExtra("currentAccount", currentAccount);
PendingIntent replyPendingIntent = PendingIntent.getBroadcast(ApplicationLoader.applicationContext, internalId, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
2015-09-24 22:52:02 +02:00
RemoteInput remoteInputWear = new RemoteInput.Builder(EXTRA_VOICE_REPLY).setLabel(LocaleController.getString("Reply", R.string.Reply)).build();
String replyToString;
2018-07-30 04:07:02 +02:00
if (lowerId < 0) {
2015-09-24 22:52:02 +02:00
replyToString = LocaleController.formatString("ReplyToGroup", R.string.ReplyToGroup, name);
} else {
replyToString = LocaleController.formatString("ReplyToUser", R.string.ReplyToUser, name);
}
2019-05-14 14:08:05 +02:00
wearReplyAction = new NotificationCompat.Action.Builder(R.drawable.ic_reply_icon, replyToString, replyPendingIntent)
.setAllowGeneratedReplies(true)
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY)
.addRemoteInput(remoteInputWear)
.setShowsUserInterface(false)
.build();
2015-06-29 19:12:11 +02:00
}
2014-10-04 17:56:09 +02:00
2016-10-11 13:57:01 +02:00
Integer count = pushDialogs.get(dialog_id);
if (count == null) {
count = 0;
}
2019-05-14 14:08:05 +02:00
int n = Math.max(count, messageObjects.size());
String conversationName;
if (n <= 1 || Build.VERSION.SDK_INT >= 28) {
conversationName = name;
} else {
conversationName = String.format("%1$s (%2$d)", name, n);
}
2018-08-27 10:33:11 +02:00
2019-12-31 14:08:08 +01:00
Person selfPerson = personCache.get(selfUserId);
if (Build.VERSION.SDK_INT >= 28 && selfPerson == null) {
TLRPC.User sender = getMessagesController().getUser(selfUserId);
if (sender == null) {
sender = getUserConfig().getCurrentUser();
}
try {
if (sender != null && sender.photo != null && sender.photo.photo_small != null && sender.photo.photo_small.volume_id != 0 && sender.photo.photo_small.local_id != 0) {
Person.Builder personBuilder = new Person.Builder().setName(LocaleController.getString("FromYou", R.string.FromYou));
File avatar = FileLoader.getPathToAttach(sender.photo.photo_small, true);
loadRoundAvatar(avatar, personBuilder);
selfPerson = personBuilder.build();
personCache.put(selfUserId, selfPerson);
}
} catch (Throwable e) {
FileLog.e(e);
}
}
NotificationCompat.MessagingStyle messagingStyle;
if (selfPerson != null) {
messagingStyle = new NotificationCompat.MessagingStyle(selfPerson);
} else {
messagingStyle = new NotificationCompat.MessagingStyle("");
}
2020-09-30 15:48:47 +02:00
if (Build.VERSION.SDK_INT < 28 || lowerId < 0 && !isChannel || UserObject.isReplyUser(lowerId)) {
2019-05-14 14:08:05 +02:00
messagingStyle.setConversationTitle(conversationName);
2018-08-27 10:33:11 +02:00
}
2020-09-30 15:48:47 +02:00
messagingStyle.setGroupConversation(Build.VERSION.SDK_INT < 28 || !isChannel && lowerId < 0 || UserObject.isReplyUser(lowerId));
2016-10-11 13:57:01 +02:00
2017-12-08 18:35:59 +01:00
StringBuilder text = new StringBuilder();
2019-05-14 14:08:05 +02:00
String[] senderName = new String[1];
boolean[] preview = new boolean[1];
2018-07-30 04:07:02 +02:00
ArrayList<TLRPC.TL_keyboardButtonRow> rows = null;
int rowsMid = 0;
JSONArray serializedMsgs = null;
if (serializedChat != null) {
serializedMsgs = new JSONArray();
}
2015-05-21 23:27:27 +02:00
for (int a = messageObjects.size() - 1; a >= 0; a--) {
MessageObject messageObject = messageObjects.get(a);
2019-01-23 18:03:33 +01:00
String message = getShortStringForMessage(messageObject, senderName, preview);
2019-09-10 12:56:11 +02:00
if (dialog_id == selfUserId) {
senderName[0] = name;
} else if (lowerId < 0 && messageObject.messageOwner.from_scheduled) {
senderName[0] = LocaleController.getString("NotificationMessageScheduledName", R.string.NotificationMessageScheduledName);
}
2014-10-04 17:56:09 +02:00
if (message == null) {
2018-08-27 10:33:11 +02:00
if (BuildVars.LOGS_ENABLED) {
FileLog.w("message text is null for " + messageObject.getId() + " did = " + messageObject.getDialogId());
}
2014-10-04 17:56:09 +02:00
continue;
}
if (text.length() > 0) {
2017-12-08 18:35:59 +01:00
text.append("\n\n");
2014-10-04 17:56:09 +02:00
}
2019-09-10 12:56:11 +02:00
if (dialog_id != selfUserId && messageObject.messageOwner.from_scheduled && lowerId > 0) {
message = String.format("%1$s: %2$s", LocaleController.getString("NotificationMessageScheduledName", R.string.NotificationMessageScheduledName), message);
2018-07-30 04:07:02 +02:00
text.append(message);
2019-09-10 12:56:11 +02:00
} else {
if (senderName[0] != null) {
text.append(String.format("%1$s: %2$s", senderName[0], message));
} else {
text.append(message);
}
2018-07-30 04:07:02 +02:00
}
2015-05-21 23:27:27 +02:00
2018-08-27 10:33:11 +02:00
long uid;
if (lowerId > 0) {
uid = lowerId;
} else if (isChannel) {
uid = -lowerId;
} else if (lowerId < 0) {
2020-09-30 15:48:47 +02:00
uid = messageObject.getSenderId();
2018-08-27 10:33:11 +02:00
} else {
uid = dialog_id;
}
Person person = personCache.get(uid);
2019-12-31 14:08:08 +01:00
String personName = "";
if (senderName[0] == null) {
if (waitingForPasscode) {
if (lowerId < 0) {
if (isChannel) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O_MR1) {
personName = LocaleController.getString("NotificationHiddenChatName", R.string.NotificationHiddenChatName);
}
} else {
personName = LocaleController.getString("NotificationHiddenChatUserName", R.string.NotificationHiddenChatUserName);
}
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O_MR1) {
personName = LocaleController.getString("NotificationHiddenName", R.string.NotificationHiddenName);
}
}
} else {
personName = senderName[0];
}
if (person == null || !TextUtils.equals(person.getName(), personName)) {
Person.Builder personBuilder = new Person.Builder().setName(personName);
2019-01-23 18:03:33 +01:00
if (preview[0] && lowerId != 0 && Build.VERSION.SDK_INT >= 28) {
2018-08-27 10:33:11 +02:00
File avatar = null;
if (lowerId > 0 || isChannel) {
avatar = avatalFile;
} else if (lowerId < 0) {
2020-09-30 15:48:47 +02:00
int fromId = messageObject.getSenderId();
2019-07-18 15:01:39 +02:00
TLRPC.User sender = getMessagesController().getUser(fromId);
2018-08-27 10:33:11 +02:00
if (sender == null) {
2019-07-18 15:01:39 +02:00
sender = getMessagesStorage().getUserSync(fromId);
2018-08-27 10:33:11 +02:00
if (sender != null) {
2019-07-18 15:01:39 +02:00
getMessagesController().putUser(sender, true);
2018-08-27 10:33:11 +02:00
}
}
if (sender != null && sender.photo != null && sender.photo.photo_small != null && sender.photo.photo_small.volume_id != 0 && sender.photo.photo_small.local_id != 0) {
avatar = FileLoader.getPathToAttach(sender.photo.photo_small, true);
}
}
2019-05-14 14:08:05 +02:00
loadRoundAvatar(avatar, personBuilder);
2018-08-27 10:33:11 +02:00
}
person = personBuilder.build();
personCache.put(uid, person);
}
2019-01-23 18:03:33 +01:00
if (lowerId != 0) {
2019-12-31 14:08:08 +01:00
boolean setPhoto = false;
2020-09-30 15:48:47 +02:00
if (preview[0] && Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && !((ActivityManager) ApplicationLoader.applicationContext.getSystemService(Context.ACTIVITY_SERVICE)).isLowRamDevice()) {
2019-12-31 14:08:08 +01:00
if (!waitingForPasscode && !messageObject.isSecretMedia() && (messageObject.type == 1 || messageObject.isSticker())) {
2019-01-23 18:03:33 +01:00
File attach = FileLoader.getPathToMessage(messageObject.messageOwner);
NotificationCompat.MessagingStyle.Message msg = new NotificationCompat.MessagingStyle.Message(message, ((long) messageObject.messageOwner.date) * 1000L, person);
String mimeType = messageObject.isSticker() ? "image/webp" : "image/jpeg";
2020-07-26 10:03:38 +02:00
Uri uri;
2019-01-23 18:03:33 +01:00
if (attach.exists()) {
2020-07-26 10:03:38 +02:00
try {
uri = FileProvider.getUriForFile(ApplicationLoader.applicationContext, BuildConfig.APPLICATION_ID + ".provider", attach);
} catch (Exception e) {
FileLog.e(e);
uri = null;
}
2019-07-18 15:01:39 +02:00
} else if (getFileLoader().isLoadingFile(attach.getName())) {
2019-01-23 18:03:33 +01:00
Uri.Builder _uri = new Uri.Builder()
.scheme("content")
.authority(NotificationImageProvider.AUTHORITY)
.appendPath("msg_media_raw")
.appendPath(currentAccount + "")
.appendPath(attach.getName())
.appendQueryParameter("final_path", attach.getAbsolutePath());
uri = _uri.build();
} else {
2018-07-30 04:07:02 +02:00
uri = null;
}
2019-01-23 18:03:33 +01:00
if (uri != null) {
msg.setData(mimeType, uri);
messagingStyle.addMessage(msg);
2020-07-26 10:03:38 +02:00
final Uri uriFinal = uri;
2019-01-23 18:03:33 +01:00
ApplicationLoader.applicationContext.grantUriPermission("com.android.systemui", uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
2020-07-26 10:03:38 +02:00
AndroidUtilities.runOnUIThread(() -> ApplicationLoader.applicationContext.revokeUriPermission(uriFinal, Intent.FLAG_GRANT_READ_URI_PERMISSION), 20000);
2019-01-23 18:03:33 +01:00
if (!TextUtils.isEmpty(messageObject.caption)) {
messagingStyle.addMessage(messageObject.caption, ((long) messageObject.messageOwner.date) * 1000, person);
}
2019-12-31 14:08:08 +01:00
setPhoto = true;
2019-01-23 18:03:33 +01:00
}
2018-07-30 04:07:02 +02:00
}
2019-12-31 14:08:08 +01:00
}
if (!setPhoto) {
2019-01-23 18:03:33 +01:00
messagingStyle.addMessage(message, ((long) messageObject.messageOwner.date) * 1000, person);
}
2020-09-30 15:48:47 +02:00
if (preview[0] && !waitingForPasscode && messageObject.isVoice()) {
2019-01-23 18:03:33 +01:00
List<NotificationCompat.MessagingStyle.Message> messages = messagingStyle.getMessages();
if (!messages.isEmpty()) {
File f = FileLoader.getPathToMessage(messageObject.messageOwner);
Uri uri;
if (Build.VERSION.SDK_INT >= 24) {
try {
uri = FileProvider.getUriForFile(ApplicationLoader.applicationContext, BuildConfig.APPLICATION_ID + ".provider", f);
} catch (Exception ignore) {
uri = null;
}
} else {
uri = Uri.fromFile(f);
}
if (uri != null) {
NotificationCompat.MessagingStyle.Message addedMessage = messages.get(messages.size() - 1);
addedMessage.setData("audio/ogg", uri);
}
2018-07-30 04:07:02 +02:00
}
}
2019-01-23 18:03:33 +01:00
} else {
messagingStyle.addMessage(message, ((long) messageObject.messageOwner.date) * 1000, person);
2018-07-30 04:07:02 +02:00
}
if (serializedMsgs != null) {
try {
JSONObject jmsg = new JSONObject();
jmsg.put("text", message);
jmsg.put("date", messageObject.messageOwner.date);
if (messageObject.isFromUser() && lowerId < 0) {
2020-09-30 15:48:47 +02:00
TLRPC.User sender = getMessagesController().getUser(messageObject.getSenderId());
2018-07-30 04:07:02 +02:00
if (sender != null) {
jmsg.put("fname", sender.first_name);
jmsg.put("lname", sender.last_name);
}
}
serializedMsgs.put(jmsg);
} catch (JSONException ignore) {
}
}
if (dialog_id == 777000 && messageObject.messageOwner.reply_markup != null) {
rows = messageObject.messageOwner.reply_markup.rows;
rowsMid = messageObject.getId();
}
2014-10-04 17:56:09 +02:00
}
Intent intent = new Intent(ApplicationLoader.applicationContext, LaunchActivity.class);
intent.setAction("com.tmessages.openchat" + Math.random() + Integer.MAX_VALUE);
2018-07-30 04:07:02 +02:00
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
2019-01-23 18:03:33 +01:00
intent.addCategory(Intent.CATEGORY_LAUNCHER);
2018-07-30 04:07:02 +02:00
if (lowerId != 0) {
if (lowerId > 0) {
intent.putExtra("userId", lowerId);
} else {
intent.putExtra("chatId", -lowerId);
}
} else {
intent.putExtra("encId", highId);
}
2018-07-30 04:07:02 +02:00
intent.putExtra("currentAccount", currentAccount);
PendingIntent contentIntent = PendingIntent.getActivity(ApplicationLoader.applicationContext, 0, intent, PendingIntent.FLAG_ONE_SHOT);
2015-09-24 22:52:02 +02:00
NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender();
if (wearReplyAction != null) {
wearableExtender.addAction(wearReplyAction);
}
2019-05-14 14:08:05 +02:00
Intent msgHeardIntent = new Intent(ApplicationLoader.applicationContext, AutoMessageHeardReceiver.class);
msgHeardIntent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
msgHeardIntent.setAction("org.telegram.messenger.ACTION_MESSAGE_HEARD");
msgHeardIntent.putExtra("dialog_id", dialog_id);
msgHeardIntent.putExtra("max_id", max_id);
msgHeardIntent.putExtra("currentAccount", currentAccount);
2018-07-30 04:07:02 +02:00
PendingIntent readPendingIntent = PendingIntent.getBroadcast(ApplicationLoader.applicationContext, internalId, msgHeardIntent, PendingIntent.FLAG_UPDATE_CURRENT);
2019-05-14 14:08:05 +02:00
NotificationCompat.Action readAction = new NotificationCompat.Action.Builder(R.drawable.menu_read, LocaleController.getString("MarkAsRead", R.string.MarkAsRead), readPendingIntent)
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_MARK_AS_READ)
.setShowsUserInterface(false)
.build();
2015-09-24 22:52:02 +02:00
2018-07-30 04:07:02 +02:00
String dismissalID;
if (lowerId != 0) {
if (lowerId > 0) {
dismissalID = "tguser" + lowerId + "_" + max_id;
} else {
dismissalID = "tgchat" + (-lowerId) + "_" + max_id;
}
} else if (dialog_id != globalSecretChatId) {
dismissalID = "tgenc" + highId + "_" + max_id;
} else {
dismissalID = null;
}
2017-03-31 01:58:05 +02:00
2018-07-30 04:07:02 +02:00
if (dismissalID != null) {
wearableExtender.setDismissalId(dismissalID);
NotificationCompat.WearableExtender summaryExtender = new NotificationCompat.WearableExtender();
summaryExtender.setDismissalId("summary_" + dismissalID);
notificationBuilder.extend(summaryExtender);
}
2019-09-10 12:56:11 +02:00
wearableExtender.setBridgeTag("tgaccount" + selfUserId);
2017-03-31 01:58:05 +02:00
2018-07-30 04:07:02 +02:00
long date = ((long) messageObjects.get(0).messageOwner.date) * 1000;
2017-03-31 01:58:05 +02:00
2014-10-04 17:56:09 +02:00
NotificationCompat.Builder builder = new NotificationCompat.Builder(ApplicationLoader.applicationContext)
.setContentTitle(name)
.setSmallIcon(R.drawable.notification)
2017-12-08 18:35:59 +01:00
.setContentText(text.toString())
.setAutoCancel(true)
2016-10-11 13:57:01 +02:00
.setNumber(messageObjects.size())
2019-05-14 14:08:05 +02:00
.setColor(0xff11acfa)
2014-10-04 17:56:09 +02:00
.setGroupSummary(false)
2018-07-30 04:07:02 +02:00
.setWhen(date)
.setShowWhen(true)
2016-10-11 13:57:01 +02:00
.setStyle(messagingStyle)
.setContentIntent(contentIntent)
2015-09-24 22:52:02 +02:00
.extend(wearableExtender)
2018-07-30 04:07:02 +02:00
.setSortKey("" + (Long.MAX_VALUE - date))
2014-11-21 20:36:21 +01:00
.setCategory(NotificationCompat.CATEGORY_MESSAGE);
2018-07-30 04:07:02 +02:00
2019-09-10 12:56:11 +02:00
Intent dismissIntent = new Intent(ApplicationLoader.applicationContext, NotificationDismissReceiver.class);
dismissIntent.putExtra("messageDate", max_date);
dismissIntent.putExtra("dialogId", dialog_id);
dismissIntent.putExtra("currentAccount", currentAccount);
2019-12-31 14:08:08 +01:00
builder.setDeleteIntent(PendingIntent.getBroadcast(ApplicationLoader.applicationContext, internalId, dismissIntent, PendingIntent.FLAG_UPDATE_CURRENT));
2019-09-10 12:56:11 +02:00
2018-08-27 10:33:11 +02:00
if (useSummaryNotification) {
builder.setGroup(notificationGroup);
builder.setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_SUMMARY);
}
2018-07-30 04:07:02 +02:00
if (wearReplyAction != null) {
builder.addAction(wearReplyAction);
}
2019-12-31 14:08:08 +01:00
if (!waitingForPasscode) {
builder.addAction(readAction);
}
if (sortedDialogs.size() == 1 && !TextUtils.isEmpty(summary)) {
2018-07-30 04:07:02 +02:00
builder.setSubText(summary);
}
if (lowerId == 0) {
builder.setLocalOnly(true);
}
2019-07-18 15:01:39 +02:00
if (avatarBitmap != null) {
2018-08-27 10:33:11 +02:00
builder.setLargeIcon(avatarBitmap);
2015-05-21 23:27:27 +02:00
}
2014-10-04 17:56:09 +02:00
2020-07-26 10:03:38 +02:00
if (!AndroidUtilities.needShowPasscode(false) && !SharedConfig.isWaitingForPasscodeEnter) {
if (rows != null) {
for (int r = 0, rc = rows.size(); r < rc; r++) {
TLRPC.TL_keyboardButtonRow row = rows.get(r);
for (int c = 0, cc = row.buttons.size(); c < cc; c++) {
TLRPC.KeyboardButton button = row.buttons.get(c);
if (button instanceof TLRPC.TL_keyboardButtonCallback) {
Intent callbackIntent = new Intent(ApplicationLoader.applicationContext, NotificationCallbackReceiver.class);
callbackIntent.putExtra("currentAccount", currentAccount);
callbackIntent.putExtra("did", dialog_id);
if (button.data != null) {
callbackIntent.putExtra("data", button.data);
}
callbackIntent.putExtra("mid", rowsMid);
builder.addAction(0, button.text, PendingIntent.getBroadcast(ApplicationLoader.applicationContext, lastButtonId++, callbackIntent, PendingIntent.FLAG_UPDATE_CURRENT));
2018-07-30 04:07:02 +02:00
}
}
}
}
2020-07-26 10:03:38 +02:00
if (Build.VERSION.SDK_INT >= 29) {
if (lowerId != 0) {
createNotificationShortcut(builder, lowerId, name, user, chat, personCache.get(lowerId));
}
}
2018-07-30 04:07:02 +02:00
}
2014-11-24 15:14:40 +01:00
if (chat == null && user != null && user.phone != null && user.phone.length() > 0) {
builder.addPerson("tel:+" + user.phone);
}
2018-07-30 04:07:02 +02:00
if (Build.VERSION.SDK_INT >= 26) {
2020-07-26 10:03:38 +02:00
setNotificationChannel(mainNotification, builder, useSummaryNotification);
2018-07-30 04:07:02 +02:00
}
holders.add(new NotificationHolder(internalId, builder.build()));
wearNotificationsIds.put(dialog_id, internalId);
if (lowerId != 0) {
try {
if (serializedChat != null) {
serializedChat.put("reply", canReply);
serializedChat.put("name", name);
serializedChat.put("max_id", max_id);
serializedChat.put("max_date", max_date);
serializedChat.put("id", Math.abs(lowerId));
if (photoPath != null) {
serializedChat.put("photo", photoPath.dc_id + "_" + photoPath.volume_id + "_" + photoPath.secret);
}
if (serializedMsgs != null) {
serializedChat.put("msgs", serializedMsgs);
}
if (lowerId > 0) {
serializedChat.put("type", "user");
} else if (lowerId < 0) {
if (isChannel || isSupergroup) {
serializedChat.put("type", "channel");
} else {
serializedChat.put("type", "group");
}
}
serializedNotifications.put(serializedChat);
}
} catch (JSONException ignore) {
}
}
}
2018-08-27 10:33:11 +02:00
if (useSummaryNotification) {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("show summary with id " + notificationId);
}
notificationManager.notify(notificationId, mainNotification);
} else {
2020-07-26 10:03:38 +02:00
if (openedInBubbleDialogs.isEmpty()) {
notificationManager.cancel(notificationId);
}
2018-08-27 10:33:11 +02:00
}
ArrayList<String> ids = new ArrayList<>(holders.size());
2018-07-30 04:07:02 +02:00
for (int a = 0, size = holders.size(); a < size; a++) {
NotificationHolder holder = holders.get(a);
holder.call();
2020-08-14 18:58:22 +02:00
if (!unsupportedNotificationShortcut()) {
ids.add(holder.notification.getShortcutId());
}
}
if (!unsupportedNotificationShortcut()) {
ShortcutManagerCompat.removeDynamicShortcuts(ApplicationLoader.applicationContext, ids);
2014-10-04 17:56:09 +02:00
}
2018-07-30 04:07:02 +02:00
for (int a = 0; a < oldIdsWear.size(); a++) {
2020-07-26 10:03:38 +02:00
long did = oldIdsWear.keyAt(a);
if (openedInBubbleDialogs.contains(did)) {
continue;
}
2018-08-27 10:33:11 +02:00
Integer id = oldIdsWear.valueAt(a);
if (BuildVars.LOGS_ENABLED) {
2020-07-26 10:03:38 +02:00
FileLog.d("cancel notification id " + id);
2018-08-27 10:33:11 +02:00
}
notificationManager.cancel(id);
2018-07-30 04:07:02 +02:00
}
if (serializedNotifications != null) {
try {
JSONObject s = new JSONObject();
2019-09-10 12:56:11 +02:00
s.put("id", selfUserId);
2018-07-30 04:07:02 +02:00
s.put("n", serializedNotifications);
2019-05-14 14:08:05 +02:00
WearDataLayerListenerService.sendMessageToWatch("/notify", s.toString().getBytes(), "remote_notifications");
2018-07-30 04:07:02 +02:00
} catch (Exception ignore) {
}
2014-10-04 17:56:09 +02:00
}
}
2019-05-14 14:08:05 +02:00
@TargetApi(Build.VERSION_CODES.P)
private void loadRoundAvatar(File avatar, Person.Builder personBuilder) {
if (avatar != null) {
try {
Bitmap bitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(avatar), (decoder, info, src) -> decoder.setPostProcessor((canvas) -> {
Path path = new Path();
path.setFillType(Path.FillType.INVERSE_EVEN_ODD);
int width = canvas.getWidth();
int height = canvas.getHeight();
path.addRoundRect(0, 0, width, height, width / 2, width / 2, Path.Direction.CW);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.TRANSPARENT);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
canvas.drawPath(path, paint);
return PixelFormat.TRANSLUCENT;
}));
IconCompat icon = IconCompat.createWithBitmap(bitmap);
personBuilder.setIcon(icon);
} catch (Throwable ignore) {
}
}
}
2015-03-26 18:34:47 +01:00
public void playOutChatSound() {
2016-03-06 02:49:31 +01:00
if (!inChatSoundEnabled || MediaController.getInstance().isRecordingAudio()) {
2015-03-26 18:34:47 +01:00
return;
}
try {
2015-03-27 11:32:33 +01:00
if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
return;
}
2015-03-26 18:34:47 +01:00
} catch (Exception e) {
2017-03-31 01:58:05 +02:00
FileLog.e(e);
2015-03-26 18:34:47 +01:00
}
2018-08-27 10:33:11 +02:00
notificationsQueue.postRunnable(() -> {
try {
if (Math.abs(System.currentTimeMillis() - lastSoundOutPlay) <= 100) {
return;
}
lastSoundOutPlay = System.currentTimeMillis();
if (soundPool == null) {
soundPool = new SoundPool(3, AudioManager.STREAM_SYSTEM, 0);
soundPool.setOnLoadCompleteListener((soundPool, sampleId, status) -> {
if (status == 0) {
try {
soundPool.play(sampleId, 1.0f, 1.0f, 1, 0, 1.0f);
} catch (Exception e) {
FileLog.e(e);
2015-05-21 23:27:27 +02:00
}
2017-03-31 01:58:05 +02:00
}
2018-08-27 10:33:11 +02:00
});
}
if (soundOut == 0 && !soundOutLoaded) {
soundOutLoaded = true;
soundOut = soundPool.load(ApplicationLoader.applicationContext, R.raw.sound_out, 1);
}
if (soundOut != 0) {
try {
soundPool.play(soundOut, 1.0f, 1.0f, 1, 0, 1.0f);
} catch (Exception e) {
FileLog.e(e);
2015-06-29 19:12:11 +02:00
}
2015-03-27 11:32:33 +01:00
}
2018-08-27 10:33:11 +02:00
} catch (Exception e) {
FileLog.e(e);
2015-03-27 11:32:33 +01:00
}
});
2015-03-26 18:34:47 +01:00
}
2019-05-14 14:08:05 +02:00
public static final int SETTING_MUTE_HOUR = 0;
public static final int SETTING_MUTE_8_HOURS = 1;
public static final int SETTING_MUTE_2_DAYS = 2;
public static final int SETTING_MUTE_FOREVER = 3;
public static final int SETTING_MUTE_UNMUTE = 4;
2020-07-26 10:03:38 +02:00
public void clearDialogNotificationsSettings(long did) {
SharedPreferences preferences = getAccountInstance().getNotificationsSettings();
SharedPreferences.Editor editor = preferences.edit();
editor.remove("notify2_" + did).remove("custom_" + did);
getMessagesStorage().setDialogFlags(did, 0);
TLRPC.Dialog dialog = getMessagesController().dialogs_dict.get(did);
if (dialog != null) {
dialog.notify_settings = new TLRPC.TL_peerNotifySettings();
}
editor.commit();
getNotificationsController().updateServerNotificationsSettings(did, true);
}
2019-05-14 14:08:05 +02:00
public void setDialogNotificationsSettings(long dialog_id, int setting) {
2019-07-18 15:01:39 +02:00
SharedPreferences preferences = getAccountInstance().getNotificationsSettings();
2019-05-14 14:08:05 +02:00
SharedPreferences.Editor editor = preferences.edit();
TLRPC.Dialog dialog = MessagesController.getInstance(UserConfig.selectedAccount).dialogs_dict.get(dialog_id);
if (setting == SETTING_MUTE_UNMUTE) {
2019-07-18 15:01:39 +02:00
boolean defaultEnabled = isGlobalNotificationsEnabled(dialog_id);
2019-05-14 14:08:05 +02:00
if (defaultEnabled) {
editor.remove("notify2_" + dialog_id);
} else {
editor.putInt("notify2_" + dialog_id, 0);
}
2019-07-18 15:01:39 +02:00
getMessagesStorage().setDialogFlags(dialog_id, 0);
2019-05-14 14:08:05 +02:00
if (dialog != null) {
dialog.notify_settings = new TLRPC.TL_peerNotifySettings();
}
} else {
int untilTime = ConnectionsManager.getInstance(UserConfig.selectedAccount).getCurrentTime();
if (setting == SETTING_MUTE_HOUR) {
untilTime += 60 * 60;
} else if (setting == SETTING_MUTE_8_HOURS) {
untilTime += 60 * 60 * 8;
} else if (setting == SETTING_MUTE_2_DAYS) {
untilTime += 60 * 60 * 48;
} else if (setting == SETTING_MUTE_FOREVER) {
untilTime = Integer.MAX_VALUE;
}
long flags;
if (setting == SETTING_MUTE_FOREVER) {
editor.putInt("notify2_" + dialog_id, 2);
flags = 1;
} else {
editor.putInt("notify2_" + dialog_id, 3);
editor.putInt("notifyuntil_" + dialog_id, untilTime);
flags = ((long) untilTime << 32) | 1;
}
NotificationsController.getInstance(UserConfig.selectedAccount).removeNotificationsForDialog(dialog_id);
MessagesStorage.getInstance(UserConfig.selectedAccount).setDialogFlags(dialog_id, flags);
if (dialog != null) {
dialog.notify_settings = new TLRPC.TL_peerNotifySettings();
dialog.notify_settings.mute_until = untilTime;
}
}
editor.commit();
updateServerNotificationsSettings(dialog_id);
}
2018-07-30 04:07:02 +02:00
public void updateServerNotificationsSettings(long dialog_id) {
2019-07-18 15:01:39 +02:00
updateServerNotificationsSettings(dialog_id, true);
}
public void updateServerNotificationsSettings(long dialog_id, boolean post) {
if (post) {
getNotificationCenter().postNotificationName(NotificationCenter.notificationsSettingsUpdated);
}
2016-03-06 02:49:31 +01:00
if ((int) dialog_id == 0) {
2015-02-01 19:51:02 +01:00
return;
}
2019-07-18 15:01:39 +02:00
SharedPreferences preferences = getAccountInstance().getNotificationsSettings();
2015-02-01 19:51:02 +01:00
TLRPC.TL_account_updateNotifySettings req = new TLRPC.TL_account_updateNotifySettings();
req.settings = new TLRPC.TL_inputPeerNotifySettings();
2018-07-30 04:07:02 +02:00
req.settings.flags |= 1;
2019-07-18 15:01:39 +02:00
req.settings.show_previews = preferences.getBoolean("content_preview_" + dialog_id, true);
2018-07-30 04:07:02 +02:00
req.settings.flags |= 2;
2016-03-06 02:49:31 +01:00
req.settings.silent = preferences.getBoolean("silent_" + dialog_id, false);
2018-07-30 04:07:02 +02:00
int mute_type = preferences.getInt("notify2_" + dialog_id, -1);
if (mute_type != -1) {
req.settings.flags |= 4;
if (mute_type == 3) {
req.settings.mute_until = preferences.getInt("notifyuntil_" + dialog_id, 0);
} else {
req.settings.mute_until = mute_type != 2 ? 0 : Integer.MAX_VALUE;
}
}
2015-02-01 19:51:02 +01:00
req.peer = new TLRPC.TL_inputNotifyPeer();
2019-07-18 15:01:39 +02:00
((TLRPC.TL_inputNotifyPeer) req.peer).peer = getMessagesController().getInputPeer((int) dialog_id);
getConnectionsManager().sendRequest(req, (response, error) -> {
2015-02-01 19:51:02 +01:00
});
}
2019-01-23 18:03:33 +01:00
public final static int TYPE_GROUP = 0;
public final static int TYPE_PRIVATE = 1;
public final static int TYPE_CHANNEL = 2;
public void updateServerNotificationsSettings(int type) {
2019-07-18 15:01:39 +02:00
SharedPreferences preferences = getAccountInstance().getNotificationsSettings();
2019-01-23 18:03:33 +01:00
TLRPC.TL_account_updateNotifySettings req = new TLRPC.TL_account_updateNotifySettings();
req.settings = new TLRPC.TL_inputPeerNotifySettings();
req.settings.flags = 5;
if (type == TYPE_GROUP) {
req.peer = new TLRPC.TL_inputNotifyChats();
req.settings.mute_until = preferences.getInt("EnableGroup2", 0);
req.settings.show_previews = preferences.getBoolean("EnablePreviewGroup", true);
} else if (type == TYPE_PRIVATE) {
req.peer = new TLRPC.TL_inputNotifyUsers();
req.settings.mute_until = preferences.getInt("EnableAll2", 0);
req.settings.show_previews = preferences.getBoolean("EnablePreviewAll", true);
} else {
req.peer = new TLRPC.TL_inputNotifyBroadcasts();
req.settings.mute_until = preferences.getInt("EnableChannel2", 0);
req.settings.show_previews = preferences.getBoolean("EnablePreviewChannel", true);
}
2019-07-18 15:01:39 +02:00
getConnectionsManager().sendRequest(req, (response, error) -> {
2019-01-23 18:03:33 +01:00
});
}
public boolean isGlobalNotificationsEnabled(long did) {
2020-03-30 18:49:17 +02:00
return isGlobalNotificationsEnabled(did, null);
}
2020-04-24 11:21:58 +02:00
public boolean isGlobalNotificationsEnabled(long did, Boolean forceChannel) {
2019-01-23 18:03:33 +01:00
int type;
int lower_id = (int) did;
if (lower_id < 0) {
2020-04-24 11:21:58 +02:00
if (forceChannel != null) {
if (forceChannel) {
type = TYPE_CHANNEL;
} else {
type = TYPE_GROUP;
}
2019-01-23 18:03:33 +01:00
} else {
2020-04-24 11:21:58 +02:00
TLRPC.Chat chat = getMessagesController().getChat(-lower_id);
if (ChatObject.isChannel(chat) && !chat.megagroup) {
type = TYPE_CHANNEL;
} else {
type = TYPE_GROUP;
}
2019-01-23 18:03:33 +01:00
}
} else {
type = TYPE_PRIVATE;
}
return isGlobalNotificationsEnabled(type);
}
public boolean isGlobalNotificationsEnabled(int type) {
2019-07-18 15:01:39 +02:00
return getAccountInstance().getNotificationsSettings().getInt(getGlobalNotificationsKey(type), 0) < getConnectionsManager().getCurrentTime();
2019-01-23 18:03:33 +01:00
}
public void setGlobalNotificationsEnabled(int type, int time) {
2019-07-18 15:01:39 +02:00
getAccountInstance().getNotificationsSettings().edit().putInt(getGlobalNotificationsKey(type), time).commit();
updateServerNotificationsSettings(type);
2020-03-30 14:00:09 +02:00
getMessagesStorage().updateMutedDialogsFiltersCounters();
2019-01-23 18:03:33 +01:00
}
public String getGlobalNotificationsKey(int type) {
if (type == TYPE_GROUP) {
return "EnableGroup2";
} else if (type == TYPE_PRIVATE) {
return "EnableAll2";
} else {
return "EnableChannel2";
}
}
2014-07-10 02:15:58 +02:00
}