Merge pull request #1 from PioneerAxon/master

Smart notifications for group chat
This commit is contained in:
rafalense 2015-03-06 07:46:53 +01:00
commit afc67309a8
7 changed files with 335 additions and 25 deletions

View File

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

View File

@ -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);

View File

@ -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<java.util.Date> sound_timestamps = null;
}
public static class TL_chatForbidden extends Chat {

View File

@ -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 {

View File

@ -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<String> adapter = new ArrayAdapter<String> (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) {

View File

@ -0,0 +1,116 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:weightSum="1"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/settings_smart_notify_begin"
android:id="@+id/beginTextView"
android:layout_gravity="center"
android:autoText="false"
android:enabled="true"
android:focusable="false"
android:inputType="none"
android:singleLine="true"
android:textStyle="normal"
android:visibility="visible"
android:gravity="center"
android:layout_margin="5dp" />
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/maxcountNumberPicker"
android:clickable="true"
android:gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/settings_smart_notify_mid1"
android:id="@+id/midTextView1"
android:layout_gravity="center"
android:autoText="false"
android:enabled="true"
android:focusable="false"
android:inputType="none"
android:singleLine="true"
android:textStyle="normal"
android:visibility="visible"
android:editable="true"
android:gravity="center"
android:layout_margin="5dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:weightSum="1"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/settings_smart_notify_mid2"
android:id="@+id/midTextView2"
android:layout_gravity="center"
android:autoText="false"
android:enabled="true"
android:focusable="false"
android:inputType="none"
android:singleLine="true"
android:textStyle="normal"
android:visibility="visible"
android:editable="true"
android:gravity="center"
android:layout_margin="5dp" />
<NumberPicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/timeframeNumberPicker"
android:gravity="center" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/timeframeunitSpinner"
android:spinnerMode="dropdown"
android:clickable="true"
android:gravity="center"
android:layout_gravity="center"
android:dropDownWidth="fill_parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/settings_smart_notify_end"
android:id="@+id/endTextView"
android:layout_gravity="center"
android:autoText="false"
android:enabled="true"
android:focusable="false"
android:inputType="none"
android:singleLine="true"
android:textStyle="normal"
android:visibility="visible"
android:gravity="center"
android:layout_margin="5dp" />
</LinearLayout>
</LinearLayout>

View File

@ -266,6 +266,15 @@
<string name="ImportContacts">Import Contacts</string>
<string name="SortFirstName">First name</string>
<string name="SortLastName">Last name</string>
<string name="SmartNotification">Smart Notification</string>
<string name="TimeUnitSeconds">Seconds</string>
<string name="TimeUnitMinutes">Minutes</string>
<string name="TimeUnitHours">Hours</string>
<string name="TimeUnitDays">Days</string>
<string name="settings_smart_notify_begin">Sound at most</string>
<string name="settings_smart_notify_mid1">times</string>
<string name="settings_smart_notify_mid2">within</string>
<string name="settings_smart_notify_end">period.</string>
<string name="LedColor">LED Color</string>
<string name="PopupNotification">Popup Notifications</string>
<string name="NoPopup">No popup</string>