Skip to content

Commit d4a7576

Browse files
committed
Fixed spam filtering on Android 8+. Made default settings more sane. Made Notification Channel settings easier to access.
1 parent 4be06bb commit d4a7576

File tree

6 files changed

+101
-35
lines changed

6 files changed

+101
-35
lines changed

Android/mobile/src/main/java/fi/iki/murgo/irssinotifier/IrcNotificationManager.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ public void handle(Context context, IrcMessage msg) {
135135
builder.setAutoCancel(true);
136136
builder.setContentText(notificationMessage);
137137
builder.setContentTitle(titleText);
138-
builder.setChannelId(NotificationChannelCreator.CHANNEL_ID);
139138

140139
if (currentUnreadCount > 1) {
141140
builder.setNumber(currentUnreadCount);
@@ -151,9 +150,10 @@ public void handle(Context context, IrcMessage msg) {
151150
}
152151
}
153152

154-
if ((!prefs.isSpamFilterEnabled() || new Date().getTime() > IrcNotificationManager
155-
.getInstance().getLastSoundDate()
156-
+ (1000L * prefs.getSpamFilterTime()))) {
153+
if ((!prefs.isSpamFilterEnabled() || new Date().getTime() > IrcNotificationManager.getInstance().getLastSoundDate() + (1000L * prefs.getSpamFilterTime()))) {
154+
// no spam filter going on
155+
156+
// legacy, this stuff affects only pre-android 8 phones
157157
if (prefs.isSoundEnabled()) {
158158
builder.setSound(prefs.getNotificationSound());
159159
}
@@ -163,6 +163,9 @@ public void handle(Context context, IrcMessage msg) {
163163
}
164164

165165
lastSoundDate = new Date().getTime();
166+
builder.setChannelId(NotificationChannelCreator.CHANNEL_DEFAULT_ID);
167+
} else {
168+
builder.setChannelId(NotificationChannelCreator.CHANNEL_LOWPRIO_ID);
166169
}
167170

168171
builder.setDefaults(defaults);

Android/mobile/src/main/java/fi/iki/murgo/irssinotifier/IrssiNotifierActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public void onCreate(Bundle savedInstanceState) {
9797
channelToView = savedInstanceState.getString("channelToView");
9898
}
9999

100-
NotificationChannelCreator.createNotificationChannel(this);
100+
NotificationChannelCreator.createNotificationChannels(this);
101101

102102
IrcNotificationManager.getInstance().mainActivityOpened(this);
103103
startMainApp(b);

Android/mobile/src/main/java/fi/iki/murgo/irssinotifier/NotificationChannelCreator.java

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,84 @@
33
import android.app.NotificationChannel;
44
import android.app.NotificationManager;
55
import android.content.Context;
6+
import android.content.Intent;
7+
import android.media.AudioAttributes;
68
import android.os.Build;
9+
import android.provider.Settings;
10+
import android.widget.Toast;
711

812
public class NotificationChannelCreator {
9-
public static final String CHANNEL_ID = "IrssiNotifierDefaultChannel";
13+
public static final String CHANNEL_DEFAULT_ID = "IrssiNotifierDefaultChannel";
14+
public static final String CHANNEL_LOWPRIO_ID = "IrssiNotifierLowPriorityChannel";
1015

11-
public static void createNotificationChannel(Context ctx) {
16+
public static void createNotificationChannels(Context ctx) {
1217
// Create the NotificationChannel, but only on API 26+ because
1318
// the NotificationChannel class is new and not in the support library
1419
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
15-
CharSequence name = "IrssiNotifier default";
16-
String description = "Default notifications from Irssi hilights.";
17-
int importance = NotificationManager.IMPORTANCE_HIGH;
18-
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
19-
channel.setDescription(description);
20-
21-
Preferences prefs = new Preferences(ctx);
22-
channel.enableLights(prefs.isLightsEnabled());
23-
channel.setLightColor(prefs.getCustomLightColor());
24-
channel.enableVibration(prefs.isVibrationEnabled());
25-
26-
// Register the channel with the system; you can't change the importance
27-
// or other notification behaviors after this
28-
NotificationManager notificationManager = ctx.getSystemService(NotificationManager.class);
29-
notificationManager.createNotificationChannel(channel);
20+
{
21+
CharSequence name = "IrssiNotifier default";
22+
String description = "Default notifications for Irssi hilights.";
23+
int importance = NotificationManager.IMPORTANCE_DEFAULT;
24+
NotificationChannel channel = new NotificationChannel(CHANNEL_DEFAULT_ID, name, importance);
25+
channel.setDescription(description);
26+
27+
Preferences prefs = new Preferences(ctx);
28+
channel.enableLights(prefs.isLightsEnabled());
29+
channel.setLightColor(prefs.getCustomLightColor());
30+
channel.enableVibration(prefs.isVibrationEnabled());
31+
32+
if (prefs.isSoundEnabled()) {
33+
AudioAttributes att = new AudioAttributes.Builder()
34+
.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT)
35+
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
36+
.build();
37+
channel.setSound(prefs.getNotificationSound(), att);
38+
} else {
39+
channel.setSound(null, null);
40+
}
41+
42+
// Register the channel with the system; you can't change the importance
43+
// or other notification behaviors after this
44+
NotificationManager notificationManager = ctx.getSystemService(NotificationManager.class);
45+
notificationManager.createNotificationChannel(channel);
46+
}
47+
48+
{
49+
CharSequence name = "IrssiNotifier low priority";
50+
String description = "Repeated hilights, you can set spam filter duration in app settings.";
51+
int importance = NotificationManager.IMPORTANCE_LOW;
52+
NotificationChannel channel = new NotificationChannel(CHANNEL_LOWPRIO_ID, name, importance);
53+
channel.setDescription(description);
54+
55+
NotificationManager notificationManager = ctx.getSystemService(NotificationManager.class);
56+
notificationManager.createNotificationChannel(channel);
57+
}
3058
}
3159
}
3260

33-
private static void deleteNotificationChannel(Context ctx) {
61+
private static void deleteNotificationChannels(Context ctx) {
3462
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
3563
NotificationManager notificationManager = ctx.getSystemService(NotificationManager.class);
36-
notificationManager.deleteNotificationChannel(CHANNEL_ID);
64+
notificationManager.deleteNotificationChannel(CHANNEL_DEFAULT_ID);
65+
notificationManager.deleteNotificationChannel(CHANNEL_LOWPRIO_ID);
3766
}
3867
}
3968

4069
public static void recreateNotificationChannel(Context ctx) {
41-
deleteNotificationChannel(ctx);
42-
createNotificationChannel(ctx);
70+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
71+
deleteNotificationChannels(ctx);
72+
createNotificationChannels(ctx);
73+
74+
Toast.makeText(ctx, "Notification Channel settings applied", Toast.LENGTH_SHORT).show();
75+
}
76+
}
77+
78+
public static void openNotificationChannelSettings(Context ctx) {
79+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
80+
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
81+
intent.putExtra(Settings.EXTRA_APP_PACKAGE, ctx.getPackageName());
82+
intent.putExtra(Settings.EXTRA_CHANNEL_ID, CHANNEL_DEFAULT_ID);
83+
ctx.startActivity(intent);
84+
}
4385
}
4486
}

Android/mobile/src/main/java/fi/iki/murgo/irssinotifier/Preferences.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,11 @@ public boolean isSoundEnabled() {
147147
}
148148

149149
public boolean isSpamFilterEnabled() {
150-
long time = Long.parseLong(sharedPreferences.getString(
151-
SPAM_FILTER_TIME, "-1"));
152-
return time >= 0;
150+
return getSpamFilterTime() >= 0;
153151
}
154152

155153
public long getSpamFilterTime() {
156-
return Long.parseLong(sharedPreferences.getString(SPAM_FILTER_TIME,
157-
"-1"));
154+
return Long.parseLong(sharedPreferences.getString(SPAM_FILTER_TIME, "60"));
158155
}
159156

160157
public boolean isNotificationsEnabled() {
@@ -256,6 +253,6 @@ public boolean isPullMechanismInUse() {
256253
}
257254

258255
public boolean isPebbleEnabled() {
259-
return sharedPreferences.getBoolean(PEBBLE_ENABLED, true);
256+
return sharedPreferences.getBoolean(PEBBLE_ENABLED, false);
260257
}
261258
}

Android/mobile/src/main/java/fi/iki/murgo/irssinotifier/SettingsActivity.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
109109
}
110110
});
111111

112+
Preference openNotificationChannelSettings = findPreference("openNotificationChannelSettings");
113+
openNotificationChannelSettings.setOnPreferenceClickListener(new OnPreferenceClickListener() {
114+
public boolean onPreferenceClick(Preference preference) {
115+
NotificationChannelCreator.openNotificationChannelSettings(SettingsActivity.this);
116+
return true;
117+
}
118+
});
119+
120+
Preference applyNotificationSettings = findPreference("applyNotificationSettings");
121+
applyNotificationSettings.setOnPreferenceClickListener(new OnPreferenceClickListener() {
122+
public boolean onPreferenceClick(Preference preference) {
123+
NotificationChannelCreator.recreateNotificationChannel(SettingsActivity.this);
124+
return true;
125+
}
126+
});
127+
112128
Preference initialSettingsPref = findPreference("redoInitialSettings");
113129
initialSettingsPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
114130
public boolean onPreferenceClick(Preference preference) {

Android/mobile/src/main/res/xml/preference_screen.xml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@
4242
<PreferenceScreen
4343
android:title="Notification style"
4444
android:summary="Select sound, vibration, lights etc." >
45+
<Preference
46+
android:key="openNotificationChannelSettings"
47+
android:title="Open notification channel settings"
48+
android:summary="On Android 8+, click here to go to notification channel settings. Settings below might not do anything on new phones." />
4549
<PreferenceCategory android:title="Sound" >
4650
<CheckBoxPreference
4751
android:defaultValue="true"
@@ -80,14 +84,18 @@
8084
android:title="Pick custom light color"
8185
android:dependency="UseDefaultLightColor" />
8286
</PreferenceCategory>
87+
<Preference
88+
android:key="applyNotificationSettings"
89+
android:title="Apply settings to Notification Channels"
90+
android:summary="Click here to (try to) apply notification settings on newer phones." />
8391
</PreferenceScreen>
8492
</PreferenceCategory>
8593

8694
<PreferenceCategory android:title="Wearables" android:key="WearableCategory" >
8795
<CheckBoxPreference
88-
android:key="PebbleEnabled"
89-
android:defaultValue="true"
90-
android:title="Send notifications to Pebble" />
96+
android:defaultValue="false"
97+
android:key="PebbleEnabled"
98+
android:title="Send notifications to Pebble" />
9199
</PreferenceCategory>
92100

93101
<PreferenceCategory android:title="Irssi ConnectBot integration" android:key="IcbCategory" >

0 commit comments

Comments
 (0)