Restore unread count after app close

This commit is contained in:
DrKLO 2014-07-11 17:54:17 +04:00
parent f44955380f
commit adbe66c860
4 changed files with 117 additions and 37 deletions

View file

@ -20,6 +20,7 @@ dependencies {
compile 'com.android.support:support-v4:19.0.+'
compile 'com.google.android.gms:play-services:4.1.+'
compile 'net.hockeyapp.android:HockeySDK:3.0.1'
compile 'com.googlecode.mp4parser:isoparser:1.0.+'
}
android {
@ -82,7 +83,7 @@ android {
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
versionCode 272
versionCode 273
versionName "1.6.0"
}
}

View file

@ -1193,7 +1193,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter
}
}
public void processDialogsUpdateRead(final HashMap<Long, Integer>dialogsToUpdate) {
public void processDialogsUpdateRead(final HashMap<Long, Integer> dialogsToUpdate) {
Utilities.RunOnUIThread(new Runnable() {
@Override
public void run() {
@ -1203,6 +1203,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter
currentDialog.unread_count = entry.getValue();
}
}
NotificationsController.getInstance().processDialogsUpdateRead(dialogsToUpdate, true);
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
}
});
@ -1215,6 +1216,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter
final HashMap<Long, TLRPC.TL_dialog> new_dialogs_dict = new HashMap<Long, TLRPC.TL_dialog>();
final HashMap<Integer, MessageObject> new_dialogMessage = new HashMap<Integer, MessageObject>();
final HashMap<Integer, TLRPC.User> usersLocal = new HashMap<Integer, TLRPC.User>();
final HashMap<Long, Integer> dialogsToUpdate = new HashMap<Long, Integer>();
for (TLRPC.User u : dialogsRes.users) {
usersLocal.put(u.id, u);
@ -1238,6 +1240,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter
}
}
new_dialogs_dict.put(d.id, d);
dialogsToUpdate.put(d.id, d.unread_count);
}
Utilities.RunOnUIThread(new Runnable() {
@ -1299,6 +1302,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter
dialogsServerOnly.add(d);
}
}
NotificationsController.getInstance().processDialogsUpdateRead(dialogsToUpdate, true);
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
}
});
@ -1580,6 +1584,9 @@ public class MessagesController implements NotificationCenter.NotificationCenter
dialog.unread_count = 0;
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
}
HashMap<Long, Integer> dialogsToUpdate = new HashMap<Long, Integer>();
dialogsToUpdate.put(dialog_id, 0);
NotificationsController.getInstance().processDialogsUpdateRead(dialogsToUpdate, true);
}
}
});
@ -1629,6 +1636,9 @@ public class MessagesController implements NotificationCenter.NotificationCenter
dialog.unread_count = 0;
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
}
HashMap<Long, Integer> dialogsToUpdate = new HashMap<Long, Integer>();
dialogsToUpdate.put(dialog_id, 0);
NotificationsController.getInstance().processDialogsUpdateRead(dialogsToUpdate, true);
}
});
}
@ -2634,7 +2644,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter
}
});
}
}), true, RPCRequest.RPCRequestClassGeneric | RPCRequest.RPCRequestClassFailOnServerErrors | RPCRequest.RPCRequestClassCanCompress, ConnectionsManager.DEFAULT_DATACENTER_ID);
}), true, RPCRequest.RPCRequestClassGeneric | RPCRequest.RPCRequestClassCanCompress, ConnectionsManager.DEFAULT_DATACENTER_ID);
}
private void putToDelayedMessages(String location, DelayedMessage message) {
@ -4286,6 +4296,7 @@ public class MessagesController implements NotificationCenter.NotificationCenter
dialog.unread_count += entry.getValue();
}
}
NotificationsController.getInstance().processDialogsUpdateRead(values, false);
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
}
});
@ -4865,8 +4876,8 @@ public class MessagesController implements NotificationCenter.NotificationCenter
dialogsServerOnly.add(d);
}
}
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
MessagesStorage.getInstance().putEncryptedChat(chat, user, dialog);
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
NotificationCenter.getInstance().postNotificationName(encryptedChatCreated, chat);
Utilities.stageQueue.postRunnable(new Runnable() {
@Override

View file

@ -183,6 +183,8 @@ public class MessagesStorage {
database.executeFast("CREATE TABLE IF NOT EXISTS sent_files_v2(uid TEXT, type INTEGER, data BLOB, PRIMARY KEY (uid, type))").stepThis().dispose();
database.executeFast("CREATE INDEX IF NOT EXISTS unread_count_idx_dialogs ON dialogs(unread_count);").stepThis().dispose();
loadUnreadMessages();
}
} catch (Exception e) {
FileLog.e("tmessages", e);
@ -276,20 +278,17 @@ public class MessagesStorage {
public void run() {
try {
final HashMap<Long, Integer> pushDialogs = new HashMap<Long, Integer>();
int totalCount = 0;
SQLiteCursor cursor = database.queryFinalized("SELECT did, unread_count FROM dialogs WHERE unread_count != 0");
while (cursor.next()) {
long did = cursor.longValue(0);
int count = cursor.intValue(1);
pushDialogs.put(did, count);
totalCount += count;
}
cursor.dispose();
final int totalCountFinal = totalCount;
Utilities.RunOnUIThread(new Runnable() {
@Override
public void run() {
NotificationsController.getInstance().processLoadedUnreadMessages(pushDialogs, totalCountFinal);
NotificationsController.getInstance().processLoadedUnreadMessages(pushDialogs);
}
});
} catch (Exception e) {
@ -2908,6 +2907,8 @@ public class MessagesStorage {
}
database.commitTransaction();
loadUnreadMessages();
} catch (Exception e) {
FileLog.e("tmessages", e);
}

View file

@ -13,6 +13,8 @@ import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.media.AudioManager;
import android.net.Uri;
@ -36,6 +38,7 @@ import org.telegram.ui.PopupNotificationActivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class NotificationsController {
@ -45,6 +48,7 @@ public class NotificationsController {
private HashMap<Long, Integer> pushDialogs = new HashMap<Long, Integer>();
public ArrayList<MessageObject> popupMessages = new ArrayList<MessageObject>();
private long openned_dialog_id = 0;
private int total_unread_count = 0;
private boolean notifyCheck = false;
public static final int pushMessagesUpdated = 27;
@ -69,6 +73,7 @@ public class NotificationsController {
public void cleanup() {
openned_dialog_id = 0;
total_unread_count = 0;
pushMessages.clear();
pushMessagesDict.clear();
pushDialogs.clear();
@ -218,7 +223,7 @@ public class NotificationsController {
try {
ConnectionsManager.getInstance().resumeNetworkMaybe();
MessageObject lastMessageObject = pushMessages.get(pushMessages.size() - 1);
MessageObject lastMessageObject = pushMessages.get(0);
long dialog_id = lastMessageObject.getDialogId();
int chat_id = lastMessageObject.messageOwner.to_id.chat_id;
@ -320,9 +325,9 @@ public class NotificationsController {
String detailText = null;
if (pushDialogs.size() == 1) {
detailText = LocaleController.formatPluralString("NewMessages", pushMessages.size());
detailText = LocaleController.formatPluralString("NewMessages", total_unread_count);
} else {
detailText = String.format("%s %s", LocaleController.formatPluralString("NewMessages", pushMessages.size()), LocaleController.formatPluralString("FromContacts", pushDialogs.size()));
detailText = String.format("%s %s", LocaleController.formatPluralString("NewMessages", total_unread_count), LocaleController.formatPluralString("FromContacts", pushDialogs.size()));
}
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ApplicationLoader.applicationContext)
@ -424,6 +429,7 @@ public class NotificationsController {
public void processReadMessages(ArrayList<Integer> readMessages, long dialog_id, int max_date, int max_id) {
int oldCount = popupMessages.size();
int oldCount2 = pushMessages.size();
if (readMessages != null) {
for (Integer id : readMessages) {
MessageObject messageObject = pushMessagesDict.get(id);
@ -431,15 +437,6 @@ public class NotificationsController {
pushMessages.remove(messageObject);
popupMessages.remove(messageObject);
pushMessagesDict.remove(id);
long dialogId = messageObject.getDialogId();
Integer count = pushDialogs.get(dialogId);
if (count != null) {
if (count == 1) {
pushDialogs.remove(dialogId);
} else {
pushDialogs.put(dialogId, --count);
}
}
}
}
}
@ -458,14 +455,6 @@ public class NotificationsController {
}
}
if (remove) {
Integer count = pushDialogs.get(dialog_id);
if (count != null) {
if (count == 1) {
pushDialogs.remove(dialog_id);
} else {
pushDialogs.put(dialog_id, --count);
}
}
pushMessages.remove(a);
popupMessages.remove(messageObject);
pushMessagesDict.remove(messageObject.messageOwner.id);
@ -477,8 +466,9 @@ public class NotificationsController {
if (oldCount != popupMessages.size()) {
NotificationCenter.getInstance().postNotificationName(pushMessagesUpdated);
}
showOrUpdateNotification(notifyCheck);
notifyCheck = false;
// if (readMessages != null || oldCount2 != pushMessages.size() || readMessages == null && dialog_id == 0) {
// showOrUpdateNotification(notifyCheck);
// }
}
public void processNewMessages(ArrayList<MessageObject> messageObjects, boolean isLast) {
@ -515,12 +505,6 @@ public class NotificationsController {
}
pushMessagesDict.put(messageObject.messageOwner.id, messageObject);
pushMessages.add(0, messageObject);
Integer currentCount = pushDialogs.get(dialog_id);
if (currentCount == null) {
currentCount = 0;
}
pushDialogs.put(dialog_id, ++currentCount);
}
}
@ -536,7 +520,90 @@ public class NotificationsController {
}
}
public void processLoadedUnreadMessages(HashMap<Long, Integer> dialogs, int totalCount) {
public void processDialogsUpdateRead(final HashMap<Long, Integer> dialogsToUpdate, boolean replace) {
int old_unread_count = total_unread_count;
for (HashMap.Entry<Long, Integer> entry : dialogsToUpdate.entrySet()) {
Long dialog_id = entry.getKey();
Integer currentCount = pushDialogs.get(dialog_id);
Integer newCount = entry.getValue();
if (replace) {
if (currentCount != null) {
total_unread_count -= currentCount;
}
if (newCount == 0) {
pushDialogs.remove(dialog_id);
} else {
total_unread_count += newCount;
pushDialogs.put(dialog_id, newCount);
}
} else {
if (currentCount == null) {
currentCount = 0;
}
currentCount += newCount;
total_unread_count += newCount;
pushDialogs.put(dialog_id, currentCount);
}
}
if (old_unread_count != total_unread_count) {
showOrUpdateNotification(notifyCheck);
setBadge(ApplicationLoader.applicationContext, total_unread_count);
notifyCheck = false;
}
}
public void processLoadedUnreadMessages(HashMap<Long, Integer> dialogs) {
pushDialogs.clear();
SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Context.MODE_PRIVATE);
String dialogsToLoad = "";
for (HashMap.Entry<Long, Integer> entry : dialogs.entrySet()) {
long dialog_id = entry.getKey();
int notify_override = preferences.getInt("notify2_" + dialog_id, 0);
boolean isChat = (int)dialog_id < 0;
if (!(notify_override == 2 || (!preferences.getBoolean("EnableAll", true) || isChat && !preferences.getBoolean("EnableGroup", true)) && notify_override == 0)) {
pushDialogs.put(dialog_id, entry.getValue());
total_unread_count += entry.getValue();
if (dialogsToLoad.length() != 0) {
dialogsToLoad += ",";
}
dialogsToLoad += "" + dialog_id;
}
}
}
private void setBadge(Context context, int count) {
try {
String launcherClassName = getLauncherClassName(context);
if (launcherClassName == null) {
return;
}
Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
intent.putExtra("badge_count", count);
intent.putExtra("badge_count_package_name", context.getPackageName());
intent.putExtra("badge_count_class_name", launcherClassName);
context.sendBroadcast(intent);
} catch (Throwable e) {
FileLog.e("tmessages", e);
}
}
public static String getLauncherClassName(Context context) {
try {
PackageManager pm = context.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0);
for (ResolveInfo resolveInfo : resolveInfos) {
String pkgName = resolveInfo.activityInfo.applicationInfo.packageName;
if (pkgName.equalsIgnoreCase(context.getPackageName())) {
return resolveInfo.activityInfo.name;
}
}
} catch (Throwable e) {
FileLog.e("tmessages", e);
}
return null;
}
}