Skip to content

Commit

Permalink
Fixed spam filtering on Android 8+. Made default settings more sane. …
Browse files Browse the repository at this point in the history
…Made Notification Channel settings easier to access.
  • Loading branch information
murgo committed Nov 12, 2018
1 parent 4be06bb commit d4a7576
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ public void handle(Context context, IrcMessage msg) {
builder.setAutoCancel(true);
builder.setContentText(notificationMessage);
builder.setContentTitle(titleText);
builder.setChannelId(NotificationChannelCreator.CHANNEL_ID);

if (currentUnreadCount > 1) {
builder.setNumber(currentUnreadCount);
Expand All @@ -151,9 +150,10 @@ public void handle(Context context, IrcMessage msg) {
}
}

if ((!prefs.isSpamFilterEnabled() || new Date().getTime() > IrcNotificationManager
.getInstance().getLastSoundDate()
+ (1000L * prefs.getSpamFilterTime()))) {
if ((!prefs.isSpamFilterEnabled() || new Date().getTime() > IrcNotificationManager.getInstance().getLastSoundDate() + (1000L * prefs.getSpamFilterTime()))) {
// no spam filter going on

// legacy, this stuff affects only pre-android 8 phones
if (prefs.isSoundEnabled()) {
builder.setSound(prefs.getNotificationSound());
}
Expand All @@ -163,6 +163,9 @@ public void handle(Context context, IrcMessage msg) {
}

lastSoundDate = new Date().getTime();
builder.setChannelId(NotificationChannelCreator.CHANNEL_DEFAULT_ID);
} else {
builder.setChannelId(NotificationChannelCreator.CHANNEL_LOWPRIO_ID);
}

builder.setDefaults(defaults);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void onCreate(Bundle savedInstanceState) {
channelToView = savedInstanceState.getString("channelToView");
}

NotificationChannelCreator.createNotificationChannel(this);
NotificationChannelCreator.createNotificationChannels(this);

IrcNotificationManager.getInstance().mainActivityOpened(this);
startMainApp(b);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,84 @@
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.media.AudioAttributes;
import android.os.Build;
import android.provider.Settings;
import android.widget.Toast;

public class NotificationChannelCreator {
public static final String CHANNEL_ID = "IrssiNotifierDefaultChannel";
public static final String CHANNEL_DEFAULT_ID = "IrssiNotifierDefaultChannel";
public static final String CHANNEL_LOWPRIO_ID = "IrssiNotifierLowPriorityChannel";

public static void createNotificationChannel(Context ctx) {
public static void createNotificationChannels(Context ctx) {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "IrssiNotifier default";
String description = "Default notifications from Irssi hilights.";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);

Preferences prefs = new Preferences(ctx);
channel.enableLights(prefs.isLightsEnabled());
channel.setLightColor(prefs.getCustomLightColor());
channel.enableVibration(prefs.isVibrationEnabled());

// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = ctx.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
{
CharSequence name = "IrssiNotifier default";
String description = "Default notifications for Irssi hilights.";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_DEFAULT_ID, name, importance);
channel.setDescription(description);

Preferences prefs = new Preferences(ctx);
channel.enableLights(prefs.isLightsEnabled());
channel.setLightColor(prefs.getCustomLightColor());
channel.enableVibration(prefs.isVibrationEnabled());

if (prefs.isSoundEnabled()) {
AudioAttributes att = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION_COMMUNICATION_INSTANT)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build();
channel.setSound(prefs.getNotificationSound(), att);
} else {
channel.setSound(null, null);
}

// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager notificationManager = ctx.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}

{
CharSequence name = "IrssiNotifier low priority";
String description = "Repeated hilights, you can set spam filter duration in app settings.";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel channel = new NotificationChannel(CHANNEL_LOWPRIO_ID, name, importance);
channel.setDescription(description);

NotificationManager notificationManager = ctx.getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}

private static void deleteNotificationChannel(Context ctx) {
private static void deleteNotificationChannels(Context ctx) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = ctx.getSystemService(NotificationManager.class);
notificationManager.deleteNotificationChannel(CHANNEL_ID);
notificationManager.deleteNotificationChannel(CHANNEL_DEFAULT_ID);
notificationManager.deleteNotificationChannel(CHANNEL_LOWPRIO_ID);
}
}

public static void recreateNotificationChannel(Context ctx) {
deleteNotificationChannel(ctx);
createNotificationChannel(ctx);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
deleteNotificationChannels(ctx);
createNotificationChannels(ctx);

Toast.makeText(ctx, "Notification Channel settings applied", Toast.LENGTH_SHORT).show();
}
}

public static void openNotificationChannelSettings(Context ctx) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, ctx.getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, CHANNEL_DEFAULT_ID);
ctx.startActivity(intent);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,11 @@ public boolean isSoundEnabled() {
}

public boolean isSpamFilterEnabled() {
long time = Long.parseLong(sharedPreferences.getString(
SPAM_FILTER_TIME, "-1"));
return time >= 0;
return getSpamFilterTime() >= 0;
}

public long getSpamFilterTime() {
return Long.parseLong(sharedPreferences.getString(SPAM_FILTER_TIME,
"-1"));
return Long.parseLong(sharedPreferences.getString(SPAM_FILTER_TIME, "60"));
}

public boolean isNotificationsEnabled() {
Expand Down Expand Up @@ -256,6 +253,6 @@ public boolean isPullMechanismInUse() {
}

public boolean isPebbleEnabled() {
return sharedPreferences.getBoolean(PEBBLE_ENABLED, true);
return sharedPreferences.getBoolean(PEBBLE_ENABLED, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,22 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
}
});

Preference openNotificationChannelSettings = findPreference("openNotificationChannelSettings");
openNotificationChannelSettings.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
NotificationChannelCreator.openNotificationChannelSettings(SettingsActivity.this);
return true;
}
});

Preference applyNotificationSettings = findPreference("applyNotificationSettings");
applyNotificationSettings.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
NotificationChannelCreator.recreateNotificationChannel(SettingsActivity.this);
return true;
}
});

Preference initialSettingsPref = findPreference("redoInitialSettings");
initialSettingsPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Expand Down
14 changes: 11 additions & 3 deletions Android/mobile/src/main/res/xml/preference_screen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
<PreferenceScreen
android:title="Notification style"
android:summary="Select sound, vibration, lights etc." >
<Preference
android:key="openNotificationChannelSettings"
android:title="Open notification channel settings"
android:summary="On Android 8+, click here to go to notification channel settings. Settings below might not do anything on new phones." />
<PreferenceCategory android:title="Sound" >
<CheckBoxPreference
android:defaultValue="true"
Expand Down Expand Up @@ -80,14 +84,18 @@
android:title="Pick custom light color"
android:dependency="UseDefaultLightColor" />
</PreferenceCategory>
<Preference
android:key="applyNotificationSettings"
android:title="Apply settings to Notification Channels"
android:summary="Click here to (try to) apply notification settings on newer phones." />
</PreferenceScreen>
</PreferenceCategory>

<PreferenceCategory android:title="Wearables" android:key="WearableCategory" >
<CheckBoxPreference
android:key="PebbleEnabled"
android:defaultValue="true"
android:title="Send notifications to Pebble" />
android:defaultValue="false"
android:key="PebbleEnabled"
android:title="Send notifications to Pebble" />
</PreferenceCategory>

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

0 comments on commit d4a7576

Please sign in to comment.