watcher: remove old settings for watch settings controller.

implement notifications for <Oreo.
feature/sentry
Floens 6 years ago
parent 327489f844
commit 8e08f0fa56
  1. 2
      Clover/app/src/main/java/org/floens/chan/core/manager/WatchManager.java
  2. 59
      Clover/app/src/main/java/org/floens/chan/ui/controller/WatchSettingsController.java
  3. 117
      Clover/app/src/main/java/org/floens/chan/ui/notification/ThreadWatchNotifications.java
  4. 2
      Clover/app/src/main/res/values/strings.xml

@ -565,7 +565,7 @@ public class WatchManager {
threadWatchNotifications.showForWatchers(pinWatchers); threadWatchNotifications.showForWatchers(pinWatchers);
} else { } else {
threadWatchNotifications.hide(); threadWatchNotifications.hideAll();
} }
} }

@ -18,17 +18,25 @@
package org.floens.chan.ui.controller; package org.floens.chan.ui.controller;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.provider.Settings;
import android.support.v7.widget.SwitchCompat; import android.support.v7.widget.SwitchCompat;
import android.widget.CompoundButton; import android.widget.CompoundButton;
import org.floens.chan.R; import org.floens.chan.R;
import org.floens.chan.core.settings.ChanSettings; import org.floens.chan.core.settings.ChanSettings;
import org.floens.chan.ui.notification.ThreadWatchNotifications;
import org.floens.chan.ui.settings.BooleanSettingView; import org.floens.chan.ui.settings.BooleanSettingView;
import org.floens.chan.ui.settings.LinkSettingView;
import org.floens.chan.ui.settings.ListSettingView; import org.floens.chan.ui.settings.ListSettingView;
import org.floens.chan.ui.settings.SettingView; import org.floens.chan.ui.settings.SettingView;
import org.floens.chan.ui.settings.SettingsController; import org.floens.chan.ui.settings.SettingsController;
import org.floens.chan.ui.settings.SettingsGroup; import org.floens.chan.ui.settings.SettingsGroup;
import org.floens.chan.ui.view.CrossfadeView; import org.floens.chan.ui.view.CrossfadeView;
import org.floens.chan.utils.AndroidUtils;
import static org.floens.chan.Chan.injector;
public class WatchSettingsController extends SettingsController implements CompoundButton.OnCheckedChangeListener { public class WatchSettingsController extends SettingsController implements CompoundButton.OnCheckedChangeListener {
private CrossfadeView crossfadeView; private CrossfadeView crossfadeView;
@ -36,10 +44,8 @@ public class WatchSettingsController extends SettingsController implements Compo
private SettingView enableBackground; private SettingView enableBackground;
private SettingView backgroundTimeout; private SettingView backgroundTimeout;
private SettingView notifyMode; private SettingView normalChannel;
private SettingView soundMode; private SettingView mentionChannel;
private SettingView peekMode;
private SettingView ledMode;
public WatchSettingsController(Context context) { public WatchSettingsController(Context context) {
super(context); super(context);
@ -70,10 +76,10 @@ public class WatchSettingsController extends SettingsController implements Compo
if (!ChanSettings.watchBackground.get()) { if (!ChanSettings.watchBackground.get()) {
setSettingViewVisibility(backgroundTimeout, false, false); setSettingViewVisibility(backgroundTimeout, false, false);
setSettingViewVisibility(notifyMode, false, false); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setSettingViewVisibility(soundMode, false, false); setSettingViewVisibility(normalChannel, false, false);
setSettingViewVisibility(peekMode, false, false); setSettingViewVisibility(mentionChannel, false, false);
setSettingViewVisibility(ledMode, false, false); }
} }
} }
@ -90,10 +96,10 @@ public class WatchSettingsController extends SettingsController implements Compo
if (item == enableBackground) { if (item == enableBackground) {
boolean enabled = ChanSettings.watchBackground.get(); boolean enabled = ChanSettings.watchBackground.get();
setSettingViewVisibility(backgroundTimeout, enabled, true); setSettingViewVisibility(backgroundTimeout, enabled, true);
setSettingViewVisibility(notifyMode, enabled, true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setSettingViewVisibility(soundMode, enabled, true); setSettingViewVisibility(normalChannel, enabled, true);
setSettingViewVisibility(peekMode, enabled, true); setSettingViewVisibility(mentionChannel, enabled, true);
setSettingViewVisibility(ledMode, enabled, true); }
} }
} }
@ -104,6 +110,7 @@ public class WatchSettingsController extends SettingsController implements Compo
enableBackground = settings.add(new BooleanSettingView(this, ChanSettings.watchBackground, R.string.setting_watch_enable_background, R.string.setting_watch_enable_background_description)); enableBackground = settings.add(new BooleanSettingView(this, ChanSettings.watchBackground, R.string.setting_watch_enable_background, R.string.setting_watch_enable_background_description));
int[] timeouts = new int[]{ int[] timeouts = new int[]{
60 * 1000,
10 * 60 * 1000, 10 * 60 * 1000,
15 * 60 * 1000, 15 * 60 * 1000,
30 * 60 * 1000, 30 * 60 * 1000,
@ -124,17 +131,23 @@ public class WatchSettingsController extends SettingsController implements Compo
} }
}); });
notifyMode = settings.add(new ListSettingView<>(this, ChanSettings.watchNotifyMode, R.string.setting_watch_notify_mode, if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.getResources().getStringArray(R.array.setting_watch_notify_modes), new String[]{"all", "quotes"})); injector().instance(ThreadWatchNotifications.class).ensureChannels();
soundMode = settings.add(new ListSettingView<>(this, ChanSettings.watchSound, R.string.setting_watch_sound, normalChannel = settings.add(new LinkSettingView(this, R.string.setting_watch_channel_normal, 0, v -> {
context.getResources().getStringArray(R.array.setting_watch_sounds), new String[]{"all", "quotes"})); Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
peekMode = settings.add(new BooleanSettingView(this, ChanSettings.watchPeek, R.string.setting_watch_peek, R.string.setting_watch_peek_description)); intent.putExtra(Settings.EXTRA_CHANNEL_ID, ThreadWatchNotifications.CHANNEL_ID_WATCH_NORMAL);
AndroidUtils.openIntent(intent);
ledMode = settings.add(new ListSettingView<>(this, ChanSettings.watchLed, R.string.setting_watch_led, }));
context.getResources().getStringArray(R.array.setting_watch_leds),
new String[]{"-1", "ffffffff", "ffff0000", "ffffff00", "ff00ff00", "ff00ffff", "ff0000ff", "ffff00ff"})); mentionChannel = settings.add(new LinkSettingView(this, R.string.setting_watch_channel_mention, 0, v -> {
Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, ThreadWatchNotifications.CHANNEL_ID_WATCH_MENTION);
AndroidUtils.openIntent(intent);
}));
}
groups.add(settings); groups.add(settings);
} }

@ -22,8 +22,8 @@ import javax.inject.Singleton;
@Singleton @Singleton
public class ThreadWatchNotifications extends NotificationHelper { public class ThreadWatchNotifications extends NotificationHelper {
private static final String CHANNEL_ID_WATCH_NORMAL = "watch:normal"; public static final String CHANNEL_ID_WATCH_NORMAL = "watch:normal";
private static final String CHANNEL_ID_WATCH_MENTION = "watch:mention"; public static final String CHANNEL_ID_WATCH_MENTION = "watch:mention";
private static final int NOTIFICATION_ID_WATCH_NORMAL = 0x10000; private static final int NOTIFICATION_ID_WATCH_NORMAL = 0x10000;
private static final int NOTIFICATION_ID_WATCH_NORMAL_MASK = 0xffff; private static final int NOTIFICATION_ID_WATCH_NORMAL_MASK = 0xffff;
private static final int NOTIFICATION_ID_WATCH_MENTION = 0x20000; private static final int NOTIFICATION_ID_WATCH_MENTION = 0x20000;
@ -39,20 +39,18 @@ public class ThreadWatchNotifications extends NotificationHelper {
} }
public void showForWatchers(List<WatchManager.PinWatcher> pinWatchers) { public void showForWatchers(List<WatchManager.PinWatcher> pinWatchers) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { showPinSummaries(pinWatchers);
showPinSummaries(pinWatchers);
} else {
// legacy notification
}
} }
public void hide() { public void hideAll() {
notificationManager.cancelAll();
} }
@TargetApi(Build.VERSION_CODES.O) @TargetApi(Build.VERSION_CODES.O)
private void showPinSummaries(List<WatchManager.PinWatcher> pinWatchers) { private void showPinSummaries(List<WatchManager.PinWatcher> pinWatchers) {
ensureChannels(); if (isOreo()) {
ensureChannels();
}
for (WatchManager.PinWatcher pinWatcher : pinWatchers) { for (WatchManager.PinWatcher pinWatcher : pinWatchers) {
if (!pinWatcher.requiresNotificationUpdate()) { if (!pinWatcher.requiresNotificationUpdate()) {
@ -60,62 +58,70 @@ public class ThreadWatchNotifications extends NotificationHelper {
} }
// Normal thread posts. // Normal thread posts.
int normalId = NOTIFICATION_ID_WATCH_NORMAL +
(pinWatcher.getPinId() & NOTIFICATION_ID_WATCH_NORMAL_MASK);
if (!pinWatcher.getUnviewedPosts().isEmpty()) { if (!pinWatcher.getUnviewedPosts().isEmpty()) {
NotificationCompat.Builder builder = NotificationCompat.Builder builder =
new NotificationCompat.Builder(applicationContext, buildMessagingStyleNotification(pinWatcher, pinWatcher.getUnviewedPosts(),
CHANNEL_ID_WATCH_NORMAL); false, CHANNEL_ID_WATCH_NORMAL);
notificationManager.notify(normalId, builder.build());
NotificationCompat.MessagingStyle messagingStyle = } else {
new NotificationCompat.MessagingStyle(""); notificationManager.cancel(normalId);
builder.setSmallIcon(R.drawable.ic_stat_notify);
if (pinWatcher.getThumbnailBitmap() != null) {
builder.setLargeIcon(pinWatcher.getThumbnailBitmap());
}
String subTitle = "(" + pinWatcher.getUnviewedPosts().size() + ")";
messagingStyle.setConversationTitle(pinWatcher.getTitle() + " " + subTitle);
messagingStyle.setGroupConversation(true);
addPostsToMessagingStyle(messagingStyle, pinWatcher.getUnviewedPosts());
builder.setStyle(messagingStyle);
setNotificationIntent(builder);
int id = NOTIFICATION_ID_WATCH_NORMAL +
(pinWatcher.getPinId() & NOTIFICATION_ID_WATCH_NORMAL_MASK);
notificationManager.notify(id, builder.build());
} }
// Posts that mention you. // Posts that mention you.
int mentionId = NOTIFICATION_ID_WATCH_MENTION +
(pinWatcher.getPinId() & NOTIFICATION_ID_WATCH_MENTION_MASK);
if (!pinWatcher.getUnviewedQuotes().isEmpty()) { if (!pinWatcher.getUnviewedQuotes().isEmpty()) {
NotificationCompat.Builder builder = NotificationCompat.Builder builder =
new NotificationCompat.Builder(applicationContext, buildMessagingStyleNotification(pinWatcher, pinWatcher.getUnviewedQuotes(),
CHANNEL_ID_WATCH_MENTION); true, CHANNEL_ID_WATCH_MENTION);
NotificationCompat.MessagingStyle messagingStyle = notificationManager.notify(mentionId, builder.build());
new NotificationCompat.MessagingStyle(""); } else {
notificationManager.cancel(mentionId);
}
builder.setSmallIcon(R.drawable.ic_stat_notify_alert); pinWatcher.hadNotificationUpdate();
builder.setSubText("Mentions"); }
if (pinWatcher.getThumbnailBitmap() != null) { }
builder.setLargeIcon(pinWatcher.getThumbnailBitmap());
}
String subTitle = "(" + pinWatcher.getUnviewedQuotes().size() + " mentions)"; private NotificationCompat.Builder buildMessagingStyleNotification(
messagingStyle.setConversationTitle(pinWatcher.getTitle() + " " + subTitle); WatchManager.PinWatcher pinWatcher, List<Post> posts, boolean mentions,
messagingStyle.setGroupConversation(true); String channelId) {
addPostsToMessagingStyle(messagingStyle, pinWatcher.getUnviewedQuotes()); NotificationCompat.Builder builder =
builder.setStyle(messagingStyle); new NotificationCompat.Builder(applicationContext, channelId);
setNotificationIntent(builder); NotificationCompat.MessagingStyle messagingStyle =
new NotificationCompat.MessagingStyle("");
int id = NOTIFICATION_ID_WATCH_MENTION + builder.setSmallIcon(!mentions ?
(pinWatcher.getPinId() & NOTIFICATION_ID_WATCH_MENTION_MASK); R.drawable.ic_stat_notify : R.drawable.ic_stat_notify_alert);
notificationManager.notify(id, builder.build()); if (mentions) {
} builder.setSubText("Mentions");
}
if (pinWatcher.getThumbnailBitmap() != null) {
builder.setLargeIcon(pinWatcher.getThumbnailBitmap());
}
if (mentions && !isOreo()) {
builder.setDefaults(NotificationCompat.DEFAULT_SOUND |
NotificationCompat.DEFAULT_VIBRATE);
}
pinWatcher.hadNotificationUpdate(); String subTitle;
if (!mentions) {
subTitle = "(" + posts.size() + ")";
} else {
subTitle = "(" + posts.size() + " mentions)";
} }
messagingStyle.setConversationTitle(pinWatcher.getTitle() + " " + subTitle);
messagingStyle.setGroupConversation(true);
addPostsToMessagingStyle(messagingStyle, posts);
builder.setStyle(messagingStyle);
setNotificationIntent(builder);
return builder;
} }
private void addPostsToMessagingStyle(NotificationCompat.MessagingStyle messagingStyle, private void addPostsToMessagingStyle(NotificationCompat.MessagingStyle messagingStyle,
@ -145,7 +151,6 @@ public class ThreadWatchNotifications extends NotificationHelper {
} }
} }
@TargetApi(Build.VERSION_CODES.O)
private void setNotificationIntent(NotificationCompat.Builder builder) { private void setNotificationIntent(NotificationCompat.Builder builder) {
Intent intent = new Intent(applicationContext, BoardActivity.class); Intent intent = new Intent(applicationContext, BoardActivity.class);
intent.setAction(Intent.ACTION_MAIN); intent.setAction(Intent.ACTION_MAIN);
@ -162,7 +167,7 @@ public class ThreadWatchNotifications extends NotificationHelper {
} }
@TargetApi(Build.VERSION_CODES.O) @TargetApi(Build.VERSION_CODES.O)
private void ensureChannels() { public void ensureChannels() {
NotificationChannel normalChannel = new NotificationChannel( NotificationChannel normalChannel = new NotificationChannel(
CHANNEL_ID_WATCH_NORMAL, "Thread updates", CHANNEL_ID_WATCH_NORMAL, "Thread updates",
NotificationManager.IMPORTANCE_DEFAULT); NotificationManager.IMPORTANCE_DEFAULT);
@ -177,4 +182,8 @@ public class ThreadWatchNotifications extends NotificationHelper {
mentionChannel.enableLights(true); mentionChannel.enableLights(true);
notificationManager.createNotificationChannel(mentionChannel); notificationManager.createNotificationChannel(mentionChannel);
} }
private boolean isOreo() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;
}
} }

@ -408,6 +408,8 @@ Enable \"Play videos with external player\" in the settings to play videos with
<item>Blue</item> <item>Blue</item>
<item>Purple</item> <item>Purple</item>
</string-array> </string-array>
<string name="setting_watch_channel_normal">Notification settings for thread updates</string>
<string name="setting_watch_channel_mention">Notification settings for thread mentions</string>
<!-- Main About group --> <!-- Main About group -->
<string name="settings_group_about">About</string> <string name="settings_group_about">About</string>

Loading…
Cancel
Save