From 8e08f0fa5680b653702da507c931a5f7fa4427e9 Mon Sep 17 00:00:00 2001 From: Floens Date: Mon, 22 Apr 2019 14:21:02 +0200 Subject: [PATCH] watcher: remove old settings for watch settings controller. implement notifications for = Build.VERSION_CODES.O) { + setSettingViewVisibility(normalChannel, false, false); + setSettingViewVisibility(mentionChannel, false, false); + } } } @@ -90,10 +96,10 @@ public class WatchSettingsController extends SettingsController implements Compo if (item == enableBackground) { boolean enabled = ChanSettings.watchBackground.get(); setSettingViewVisibility(backgroundTimeout, enabled, true); - setSettingViewVisibility(notifyMode, enabled, true); - setSettingViewVisibility(soundMode, enabled, true); - setSettingViewVisibility(peekMode, enabled, true); - setSettingViewVisibility(ledMode, enabled, true); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + setSettingViewVisibility(normalChannel, enabled, true); + setSettingViewVisibility(mentionChannel, 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)); int[] timeouts = new int[]{ + 60 * 1000, 10 * 60 * 1000, 15 * 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, - context.getResources().getStringArray(R.array.setting_watch_notify_modes), new String[]{"all", "quotes"})); - - soundMode = settings.add(new ListSettingView<>(this, ChanSettings.watchSound, R.string.setting_watch_sound, - context.getResources().getStringArray(R.array.setting_watch_sounds), new String[]{"all", "quotes"})); - - peekMode = settings.add(new BooleanSettingView(this, ChanSettings.watchPeek, R.string.setting_watch_peek, R.string.setting_watch_peek_description)); - - 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"})); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + injector().instance(ThreadWatchNotifications.class).ensureChannels(); + + normalChannel = settings.add(new LinkSettingView(this, R.string.setting_watch_channel_normal, 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_NORMAL); + AndroidUtils.openIntent(intent); + })); + + 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); } diff --git a/Clover/app/src/main/java/org/floens/chan/ui/notification/ThreadWatchNotifications.java b/Clover/app/src/main/java/org/floens/chan/ui/notification/ThreadWatchNotifications.java index c4f12cfb..f4b627c5 100644 --- a/Clover/app/src/main/java/org/floens/chan/ui/notification/ThreadWatchNotifications.java +++ b/Clover/app/src/main/java/org/floens/chan/ui/notification/ThreadWatchNotifications.java @@ -22,8 +22,8 @@ import javax.inject.Singleton; @Singleton public class ThreadWatchNotifications extends NotificationHelper { - private 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_NORMAL = "watch:normal"; + 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_MASK = 0xffff; private static final int NOTIFICATION_ID_WATCH_MENTION = 0x20000; @@ -39,20 +39,18 @@ public class ThreadWatchNotifications extends NotificationHelper { } public void showForWatchers(List pinWatchers) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - showPinSummaries(pinWatchers); - } else { - // legacy notification - } + showPinSummaries(pinWatchers); } - public void hide() { - + public void hideAll() { + notificationManager.cancelAll(); } @TargetApi(Build.VERSION_CODES.O) private void showPinSummaries(List pinWatchers) { - ensureChannels(); + if (isOreo()) { + ensureChannels(); + } for (WatchManager.PinWatcher pinWatcher : pinWatchers) { if (!pinWatcher.requiresNotificationUpdate()) { @@ -60,62 +58,70 @@ public class ThreadWatchNotifications extends NotificationHelper { } // Normal thread posts. + int normalId = NOTIFICATION_ID_WATCH_NORMAL + + (pinWatcher.getPinId() & NOTIFICATION_ID_WATCH_NORMAL_MASK); if (!pinWatcher.getUnviewedPosts().isEmpty()) { NotificationCompat.Builder builder = - new NotificationCompat.Builder(applicationContext, - CHANNEL_ID_WATCH_NORMAL); - - NotificationCompat.MessagingStyle messagingStyle = - new NotificationCompat.MessagingStyle(""); - - 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()); + buildMessagingStyleNotification(pinWatcher, pinWatcher.getUnviewedPosts(), + false, CHANNEL_ID_WATCH_NORMAL); + notificationManager.notify(normalId, builder.build()); + } else { + notificationManager.cancel(normalId); } // Posts that mention you. + int mentionId = NOTIFICATION_ID_WATCH_MENTION + + (pinWatcher.getPinId() & NOTIFICATION_ID_WATCH_MENTION_MASK); if (!pinWatcher.getUnviewedQuotes().isEmpty()) { NotificationCompat.Builder builder = - new NotificationCompat.Builder(applicationContext, - CHANNEL_ID_WATCH_MENTION); + buildMessagingStyleNotification(pinWatcher, pinWatcher.getUnviewedQuotes(), + true, CHANNEL_ID_WATCH_MENTION); - NotificationCompat.MessagingStyle messagingStyle = - new NotificationCompat.MessagingStyle(""); + notificationManager.notify(mentionId, builder.build()); + } else { + notificationManager.cancel(mentionId); + } - builder.setSmallIcon(R.drawable.ic_stat_notify_alert); - builder.setSubText("Mentions"); - if (pinWatcher.getThumbnailBitmap() != null) { - builder.setLargeIcon(pinWatcher.getThumbnailBitmap()); - } + pinWatcher.hadNotificationUpdate(); + } + } - String subTitle = "(" + pinWatcher.getUnviewedQuotes().size() + " mentions)"; - messagingStyle.setConversationTitle(pinWatcher.getTitle() + " " + subTitle); - messagingStyle.setGroupConversation(true); - addPostsToMessagingStyle(messagingStyle, pinWatcher.getUnviewedQuotes()); - builder.setStyle(messagingStyle); + private NotificationCompat.Builder buildMessagingStyleNotification( + WatchManager.PinWatcher pinWatcher, List posts, boolean mentions, + String channelId) { + NotificationCompat.Builder builder = + new NotificationCompat.Builder(applicationContext, channelId); - setNotificationIntent(builder); + NotificationCompat.MessagingStyle messagingStyle = + new NotificationCompat.MessagingStyle(""); - int id = NOTIFICATION_ID_WATCH_MENTION + - (pinWatcher.getPinId() & NOTIFICATION_ID_WATCH_MENTION_MASK); - notificationManager.notify(id, builder.build()); - } + builder.setSmallIcon(!mentions ? + R.drawable.ic_stat_notify : R.drawable.ic_stat_notify_alert); + 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, @@ -145,7 +151,6 @@ public class ThreadWatchNotifications extends NotificationHelper { } } - @TargetApi(Build.VERSION_CODES.O) private void setNotificationIntent(NotificationCompat.Builder builder) { Intent intent = new Intent(applicationContext, BoardActivity.class); intent.setAction(Intent.ACTION_MAIN); @@ -162,7 +167,7 @@ public class ThreadWatchNotifications extends NotificationHelper { } @TargetApi(Build.VERSION_CODES.O) - private void ensureChannels() { + public void ensureChannels() { NotificationChannel normalChannel = new NotificationChannel( CHANNEL_ID_WATCH_NORMAL, "Thread updates", NotificationManager.IMPORTANCE_DEFAULT); @@ -177,4 +182,8 @@ public class ThreadWatchNotifications extends NotificationHelper { mentionChannel.enableLights(true); notificationManager.createNotificationChannel(mentionChannel); } + + private boolean isOreo() { + return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O; + } } diff --git a/Clover/app/src/main/res/values/strings.xml b/Clover/app/src/main/res/values/strings.xml index 71002459..00123752 100644 --- a/Clover/app/src/main/res/values/strings.xml +++ b/Clover/app/src/main/res/values/strings.xml @@ -408,6 +408,8 @@ Enable \"Play videos with external player\" in the settings to play videos with Blue Purple + Notification settings for thread updates + Notification settings for thread mentions About