From acf3a8608bab104c8077e0f3cb72a61565ef2dc0 Mon Sep 17 00:00:00 2001 From: PioneerAxon Date: Thu, 5 Mar 2015 02:01:11 +0530 Subject: [PATCH] Smart notifications for group chat --- .../telegram/android/MessagesController.java | 5 +- .../android/NotificationsController.java | 27 +++ .../java/org/telegram/messenger/TLRPC.java | 4 + .../java/org/telegram/ui/ChatActivity.java | 4 + .../ui/ProfileNotificationsActivity.java | 193 +++++++++++++++--- .../main/res/layout/settings_smart_notify.xml | 116 +++++++++++ TMessagesProj/src/main/res/values/strings.xml | 11 +- 7 files changed, 335 insertions(+), 25 deletions(-) create mode 100644 TMessagesProj/src/main/res/layout/settings_smart_notify.xml diff --git a/TMessagesProj/src/main/java/org/telegram/android/MessagesController.java b/TMessagesProj/src/main/java/org/telegram/android/MessagesController.java index a7c97325..dcd9bb67 100644 --- a/TMessagesProj/src/main/java/org/telegram/android/MessagesController.java +++ b/TMessagesProj/src/main/java/org/telegram/android/MessagesController.java @@ -3399,7 +3399,10 @@ public class MessagesController implements NotificationCenter.NotificationCenter if (dialog != null) { dialog.notify_settings.mute_until = 0; } - editor.remove("notify2_" + dialog_id); + SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Activity.MODE_PRIVATE); + if (preferences.getInt("notify2_" + dialog_id, 0) != 4) { + editor.remove("notify2_" + dialog_id); + } MessagesStorage.getInstance().setDialogFlags(dialog_id, 0); } diff --git a/TMessagesProj/src/main/java/org/telegram/android/NotificationsController.java b/TMessagesProj/src/main/java/org/telegram/android/NotificationsController.java index cef1faa8..d044fc74 100644 --- a/TMessagesProj/src/main/java/org/telegram/android/NotificationsController.java +++ b/TMessagesProj/src/main/java/org/telegram/android/NotificationsController.java @@ -43,7 +43,9 @@ import org.telegram.ui.PopupNotificationActivity; import java.util.ArrayList; import java.util.Calendar; +import java.util.Date; import java.util.HashMap; +import java.util.LinkedList; import java.util.List; public class NotificationsController { @@ -353,6 +355,31 @@ public class NotificationsController { notifyDisabled = true; } + boolean use_smart_notify = preferences.getBoolean("smart_notify_" + dialog_id, false); + Long smart_notify_timeframe = preferences.getLong("smart_notify_timeframe_" + dialog_id, 1); + int smart_notify_max_count = preferences.getInt("smart_notify_max_count_" + dialog_id, 1); + if (chat_id != 0 && use_smart_notify) + { + if (chat.sound_timestamps == null) + chat.sound_timestamps = new LinkedList<>(); + boolean shouldAdd = true; + Date firstNotification = chat.sound_timestamps.peek(); + Date currentDate = new Date(); + if (firstNotification != null) { + if (currentDate.getTime() - firstNotification.getTime() < smart_notify_timeframe * 1000 && chat.sound_timestamps.size () >= smart_notify_max_count) { + shouldAdd = false; + } + } + if (!shouldAdd) { + notifyDisabled = true; + } + else { + if (chat.sound_timestamps.size() >= smart_notify_max_count) + chat.sound_timestamps.poll(); + chat.sound_timestamps.add(currentDate); + } + } + String defaultPath = Settings.System.DEFAULT_NOTIFICATION_URI.getPath(); if (!notifyDisabled) { inAppSounds = preferences.getBoolean("EnableInAppSounds", true); diff --git a/TMessagesProj/src/main/java/org/telegram/messenger/TLRPC.java b/TMessagesProj/src/main/java/org/telegram/messenger/TLRPC.java index 6ce1e519..d941fe45 100644 --- a/TMessagesProj/src/main/java/org/telegram/messenger/TLRPC.java +++ b/TMessagesProj/src/main/java/org/telegram/messenger/TLRPC.java @@ -9,7 +9,10 @@ package org.telegram.messenger; import java.util.ArrayList; +import java.util.Date; +import java.util.LinkedList; import java.util.Locale; +import java.util.Queue; @SuppressWarnings("unchecked") public class TLRPC { @@ -5857,6 +5860,7 @@ public class TLRPC { public boolean checked_in; public int version; public boolean left; + public java.util.Queue sound_timestamps = null; } public static class TL_chatForbidden extends Chat { diff --git a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java index 1bbd3576..22059a08 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/ChatActivity.java @@ -276,6 +276,10 @@ public class ChatActivity extends BaseFragment implements NotificationCenter.Not return false; } } + if (chatId > 0){ + if (currentChat.sound_timestamps != null) + currentChat.sound_timestamps.clear(); + } if (chatId > 0) { dialog_id = -chatId; } else { diff --git a/TMessagesProj/src/main/java/org/telegram/ui/ProfileNotificationsActivity.java b/TMessagesProj/src/main/java/org/telegram/ui/ProfileNotificationsActivity.java index 9e30daad..39d6ad10 100644 --- a/TMessagesProj/src/main/java/org/telegram/ui/ProfileNotificationsActivity.java +++ b/TMessagesProj/src/main/java/org/telegram/ui/ProfileNotificationsActivity.java @@ -24,8 +24,12 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; +import android.widget.ArrayAdapter; import android.widget.FrameLayout; import android.widget.ListView; +import android.widget.NumberPicker; +import android.widget.Spinner; +import android.widget.Toast; import org.telegram.android.AndroidUtilities; import org.telegram.android.MessagesController; @@ -55,6 +59,7 @@ public class ProfileNotificationsActivity extends BaseFragment implements Notifi private int settingsVibrateRow; private int settingsSoundRow; private int settingsPriorityRow; + private int settingsSmartNotifyRow; private int settingsLedRow; private int rowCount = 0; @@ -73,6 +78,12 @@ public class ProfileNotificationsActivity extends BaseFragment implements Notifi } else { settingsPriorityRow = -1; } + if (dialog_id < 0) { + settingsSmartNotifyRow = rowCount++; + } + else { + settingsSmartNotifyRow = -1; + } settingsLedRow = rowCount++; NotificationCenter.getInstance().addObserver(this, NotificationCenter.notificationsSettingsUpdated); return super.onFragmentCreate(); @@ -155,31 +166,67 @@ public class ProfileNotificationsActivity extends BaseFragment implements Notifi } AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity()); builder.setTitle(LocaleController.getString("AppName", R.string.AppName)); - builder.setItems(new CharSequence[] { - LocaleController.getString("Default", R.string.Default), - LocaleController.getString("Enabled", R.string.Enabled), - LocaleController.getString("NotificationsDisabled", R.string.NotificationsDisabled) - }, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface d, int which) { - SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Activity.MODE_PRIVATE); - SharedPreferences.Editor editor = preferences.edit(); - editor.putInt("notify2_" + dialog_id, which); - MessagesStorage.getInstance().setDialogFlags(dialog_id, which == 2 ? 1 : 0); - editor.commit(); - TLRPC.TL_dialog dialog = MessagesController.getInstance().dialogs_dict.get(dialog_id); - if (dialog != null) { - dialog.notify_settings = new TLRPC.TL_peerNotifySettings(); - if (which == 2) { - dialog.notify_settings.mute_until = Integer.MAX_VALUE; + if (dialog_id < 0) { + builder.setItems(new CharSequence[]{ + LocaleController.getString("Default", R.string.Default), + LocaleController.getString("Enabled", R.string.Enabled), + LocaleController.getString("NotificationsDisabled", R.string.NotificationsDisabled), + LocaleController.getString("Smart Notification", R.string.SmartNotification) + }, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Activity.MODE_PRIVATE); + SharedPreferences.Editor editor = preferences.edit(); + if (which == 3) { + which = 4; //Leave space for "Mute group for D duration" + editor.putBoolean("smart_notify_" + dialog_id, true); + } else { + editor.putBoolean("smart_notify_" + dialog_id, false); } + editor.putInt("notify2_" + dialog_id, which); + MessagesStorage.getInstance().setDialogFlags(dialog_id, which == 2 ? 1 : 0); + editor.commit(); + TLRPC.TL_dialog tl_dialog = MessagesController.getInstance().dialogs_dict.get(dialog_id); + if (tl_dialog != null) { + tl_dialog.notify_settings = new TLRPC.TL_peerNotifySettings(); + if (which == 2) { + tl_dialog.notify_settings.mute_until = Integer.MAX_VALUE; + } + } + if (listView != null) { + listView.invalidateViews(); + } + NotificationsController.updateServerNotificationsSettings(dialog_id); } - if (listView != null) { - listView.invalidateViews(); + }); + } + else { + builder.setItems(new CharSequence[]{ + LocaleController.getString("Default", R.string.Default), + LocaleController.getString("Enabled", R.string.Enabled), + LocaleController.getString("NotificationsDisabled", R.string.NotificationsDisabled), + }, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Activity.MODE_PRIVATE); + SharedPreferences.Editor editor = preferences.edit(); + editor.putInt("notify2_" + dialog_id, which); + MessagesStorage.getInstance().setDialogFlags(dialog_id, which == 2 ? 1 : 0); + editor.commit(); + TLRPC.TL_dialog tl_dialog = MessagesController.getInstance().dialogs_dict.get(dialog_id); + if (tl_dialog != null) { + tl_dialog.notify_settings = new TLRPC.TL_peerNotifySettings(); + if (which == 2) { + tl_dialog.notify_settings.mute_until = Integer.MAX_VALUE; + } + } + if (listView != null) { + listView.invalidateViews(); + } + NotificationsController.updateServerNotificationsSettings(dialog_id); } - NotificationsController.updateServerNotificationsSettings(dialog_id); - } - }); + }); + } builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null); showAlertDialog(builder); } else if (i == settingsSoundRow) { @@ -290,6 +337,70 @@ public class ProfileNotificationsActivity extends BaseFragment implements Notifi }); builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null); showAlertDialog(builder); + } else if (i == settingsSmartNotifyRow) { + if (getParentActivity() == null) { + return; + } + + LayoutInflater li = (LayoutInflater)getParentActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); + view = li.inflate(R.layout.settings_smart_notify, null, false); + + String[] timeUnits = { + LocaleController.getString("Seconds", R.string.TimeUnitSeconds), + LocaleController.getString("Minutes", R.string.TimeUnitMinutes), + LocaleController.getString("Hours", R.string.TimeUnitHours), + LocaleController.getString("Days", R.string.TimeUnitDays) + }; + final Spinner timeframeUnitSpinner = (Spinner) view.findViewById(R.id.timeframeunitSpinner); + ArrayAdapter adapter = new ArrayAdapter (li.getContext(), android.R.layout.simple_spinner_item, timeUnits); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + timeframeUnitSpinner.setAdapter(adapter); + + SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Activity.MODE_PRIVATE); + + final NumberPicker maxcountNumberPicker = (NumberPicker) view.findViewById(R.id.maxcountNumberPicker); + maxcountNumberPicker.setMinValue(1); + maxcountNumberPicker.setMaxValue(10); + maxcountNumberPicker.setValue(preferences.getInt ("smart_notify_max_count_" + dialog_id, 1)); + + final NumberPicker timeframeNumberPicker = (NumberPicker) view.findViewById(R.id.timeframeNumberPicker); + timeframeNumberPicker.setMinValue(1); + timeframeNumberPicker.setMaxValue(100); + + long timeframe = preferences.getLong("smart_notify_timeframe_" + dialog_id, 1); + long multiplier = (timeframe % 86400L == 0L) ? 86400L : ((timeframe % 3600L == 0L) ? 3600L : ((timeframe % 60L == 0L) ? 60L : 1L)); + timeframe = timeframe / multiplier; + timeframeUnitSpinner.setSelection((multiplier == 1L) ? 0 : ((multiplier == 60L) ? 1 : (multiplier == 3600L) ? 2 : 3)); + timeframeNumberPicker.setValue((int)timeframe); + + AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity()); + builder.setTitle(LocaleController.getString("Smart Notification", R.string.SmartNotification)); + builder.setView(view); + builder.setPositiveButton(LocaleController.getString("Set", R.string.Set), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialogInterface, int which) { + final SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Activity.MODE_PRIVATE); + SharedPreferences.Editor editor = preferences.edit(); + int unit = timeframeUnitSpinner.getSelectedItemPosition(); + long multiplier = (unit == 0) ? 1L : ((unit == 1) ? 60L : ((unit == 2) ? 3600L : 86400L)); + editor.putInt("smart_notify_max_count_" + dialog_id, maxcountNumberPicker.getValue()); + editor.putLong("smart_notify_timeframe_" + dialog_id, timeframeNumberPicker.getValue() * multiplier); + editor.commit(); + listView.invalidateViews(); + } + }); + builder.setNegativeButton(LocaleController.getString("Default", R.string.Default), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + final SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Activity.MODE_PRIVATE); + SharedPreferences.Editor editor = preferences.edit(); + editor.putInt("smart_notify_max_count_" + dialog_id, 1); + editor.putLong("smart_notify_timeframe_" + dialog_id, 1); + editor.commit(); + listView.invalidateViews(); + } + }); + showAlertDialog(builder); } } }); @@ -355,11 +466,18 @@ public class ProfileNotificationsActivity extends BaseFragment implements Notifi @Override public boolean areAllItemsEnabled() { - return true; + return false; } @Override public boolean isEnabled(int i) { + if (i < 0) + return false; + if (i == settingsSmartNotifyRow) { + SharedPreferences preferences = ApplicationLoader.applicationContext.getSharedPreferences("Notifications", Activity.MODE_PRIVATE); + if (!preferences.getBoolean("smart_notify_" + dialog_id, false)) + return false; + } return true; } @@ -435,6 +553,8 @@ public class ProfileNotificationsActivity extends BaseFragment implements Notifi } else { textCell.setTextAndValue(LocaleController.getString("Notifications", R.string.Notifications), LocaleController.getString("NotificationsDisabled", R.string.NotificationsDisabled), true); } + } else if (value == 4) { + textCell.setTextAndValue(LocaleController.getString("Notifications", R.string.Notifications), LocaleController.getString("Smart Notification", R.string.SmartNotification), true); } } else if (i == settingsSoundRow) { String value = preferences.getString("sound_" + dialog_id, LocaleController.getString("Default", R.string.Default)); @@ -453,6 +573,33 @@ public class ProfileNotificationsActivity extends BaseFragment implements Notifi } else if (value == 3) { textCell.setTextAndValue(LocaleController.getString("NotificationsPriority", R.string.NotificationsPriority), LocaleController.getString("SettingsDefault", R.string.SettingsDefault), true); } + } else if (i == settingsSmartNotifyRow) { + String value = LocaleController.getString("Disabled", R.string.Disabled); + if (preferences.getBoolean("smart_notify_" + dialog_id, false)) { + long timeframe = preferences.getLong("smart_notify_timeframe_" + dialog_id, 1); + long multiplier = (timeframe % 86400L == 0L) ? 86400L : ((timeframe % 3600L == 0L) ? 3600L : ((timeframe % 60L == 0L) ? 60L : 1L)); + timeframe = timeframe / multiplier; + String[] timeUnits = { + LocaleController.getString("Seconds", R.string.TimeUnitSeconds), + LocaleController.getString("Minutes", R.string.TimeUnitMinutes), + LocaleController.getString("Hours", R.string.TimeUnitHours), + LocaleController.getString("Days", R.string.TimeUnitDays) + }; + value = LocaleController.getString("Sound at most", R.string.settings_smart_notify_begin); + value += " "; + value += preferences.getInt("smart_notify_max_count_" + dialog_id, 1); + value += " "; + value += LocaleController.getString("time(s)", R.string.settings_smart_notify_mid1); + value += " "; + value += LocaleController.getString("within", R.string.settings_smart_notify_mid2); + value += " "; + value += timeframe; + value += " "; + value += timeUnits [((multiplier == 1L)? 0: (multiplier == 60L)? 1 : (multiplier == 3600L)? 2 : 3)]; + value += " "; + value += LocaleController.getString(".", R.string.settings_smart_notify_end); + } + textCell.setTextAndValue(LocaleController.getString("Smart Notification", R.string.SmartNotification), value, true); } } else if (type == 1) { if (view == null) { diff --git a/TMessagesProj/src/main/res/layout/settings_smart_notify.xml b/TMessagesProj/src/main/res/layout/settings_smart_notify.xml new file mode 100644 index 00000000..97450957 --- /dev/null +++ b/TMessagesProj/src/main/res/layout/settings_smart_notify.xml @@ -0,0 +1,116 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/TMessagesProj/src/main/res/values/strings.xml b/TMessagesProj/src/main/res/values/strings.xml index 1c52e992..6de48756 100644 --- a/TMessagesProj/src/main/res/values/strings.xml +++ b/TMessagesProj/src/main/res/values/strings.xml @@ -266,6 +266,15 @@ Import Contacts First name Last name + Smart Notification + Seconds + Minutes + Hours + Days + Sound at most + times + within + period. LED Color Popup Notifications No popup @@ -698,4 +707,4 @@ Floating Pencil Color Floating Background Color G+ Community - \ No newline at end of file +