Skip to content

Commit

Permalink
Restore unread count after app close
Browse files Browse the repository at this point in the history
  • Loading branch information
DrKLO committed Jul 11, 2014
1 parent f449553 commit adbe66c
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 37 deletions.
3 changes: 2 additions & 1 deletion TMessagesProj/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -82,7 +83,7 @@ android {
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
versionCode 272
versionCode 273
versionName "1.6.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,7 @@ private void applyDialogsNotificationsSettings(ArrayList<TLRPC.TL_dialog> dialog
}
}

public void processDialogsUpdateRead(final HashMap<Long, Integer>dialogsToUpdate) {
public void processDialogsUpdateRead(final HashMap<Long, Integer> dialogsToUpdate) {
Utilities.RunOnUIThread(new Runnable() {
@Override
public void run() {
Expand All @@ -1203,6 +1203,7 @@ public void run() {
currentDialog.unread_count = entry.getValue();
}
}
NotificationsController.getInstance().processDialogsUpdateRead(dialogsToUpdate, true);
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
}
});
Expand All @@ -1215,6 +1216,7 @@ public void run() {
final HashMap<Long, TLRPC.TL_dialog> new_dialogs_dict = new HashMap<Long, TLRPC.TL_dialog>();
final HashMap<Integer, MessageObject> new_dialogMessage = new HashMap<Integer, MessageObject>();
final HashMap<Integer, TLRPC.User> usersLocal = new HashMap<Integer, TLRPC.User>();
final HashMap<Long, Integer> dialogsToUpdate = new HashMap<Long, Integer>();

for (TLRPC.User u : dialogsRes.users) {
usersLocal.put(u.id, u);
Expand All @@ -1238,6 +1240,7 @@ public void run() {
}
}
new_dialogs_dict.put(d.id, d);
dialogsToUpdate.put(d.id, d.unread_count);
}

Utilities.RunOnUIThread(new Runnable() {
Expand Down Expand Up @@ -1299,6 +1302,7 @@ public int compare(TLRPC.TL_dialog tl_dialog, TLRPC.TL_dialog tl_dialog2) {
dialogsServerOnly.add(d);
}
}
NotificationsController.getInstance().processDialogsUpdateRead(dialogsToUpdate, true);
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
}
});
Expand Down Expand Up @@ -1580,6 +1584,9 @@ public void run() {
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);
}
}
});
Expand Down Expand Up @@ -1629,6 +1636,9 @@ public void run() {
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);
}
});
}
Expand Down Expand Up @@ -2634,7 +2644,7 @@ public void run() {
}
});
}
}), 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) {
Expand Down Expand Up @@ -4286,6 +4296,7 @@ public void run() {
dialog.unread_count += entry.getValue();
}
}
NotificationsController.getInstance().processDialogsUpdateRead(values, false);
NotificationCenter.getInstance().postNotificationName(dialogsNeedReload);
}
});
Expand Down Expand Up @@ -4865,8 +4876,8 @@ public int compare(TLRPC.TL_dialog tl_dialog, TLRPC.TL_dialog tl_dialog2) {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ public void openDatabase() {
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);
Expand Down Expand Up @@ -276,20 +278,17 @@ public void loadUnreadMessages() {
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) {
Expand Down Expand Up @@ -2908,6 +2907,8 @@ public void run() {
}

database.commitTransaction();

loadUnreadMessages();
} catch (Exception e) {
FileLog.e("tmessages", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
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;
Expand All @@ -36,6 +38,7 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class NotificationsController {

Expand All @@ -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;
Expand All @@ -69,6 +73,7 @@ public NotificationsController() {

public void cleanup() {
openned_dialog_id = 0;
total_unread_count = 0;
pushMessages.clear();
pushMessagesDict.clear();
pushDialogs.clear();
Expand Down Expand Up @@ -218,7 +223,7 @@ private void showOrUpdateNotification(boolean notifyAboutLast) {
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;
Expand Down Expand Up @@ -320,9 +325,9 @@ private void showOrUpdateNotification(boolean notifyAboutLast) {

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)
Expand Down Expand Up @@ -424,22 +429,14 @@ private void sendAlertToPebble(String message) {

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);
if (messageObject != null) {
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);
}
}
}
}
}
Expand All @@ -458,14 +455,6 @@ public void processReadMessages(ArrayList<Integer> readMessages, long dialog_id,
}
}
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);
Expand All @@ -477,8 +466,9 @@ public void processReadMessages(ArrayList<Integer> readMessages, long dialog_id,
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) {
Expand Down Expand Up @@ -515,12 +505,6 @@ public void processNewMessages(ArrayList<MessageObject> messageObjects, boolean
}
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);
}
}

Expand All @@ -536,7 +520,90 @@ public void processNewMessages(ArrayList<MessageObject> messageObjects, boolean
}
}

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;
}
}

0 comments on commit adbe66c

Please sign in to comment.