From e828739666873de536361b789d6a58d2fed4643a Mon Sep 17 00:00:00 2001 From: Florens Douwes Date: Sun, 6 Apr 2014 21:41:14 +0200 Subject: [PATCH] Pins database and notifications changes Pins are saved in the database when pause pins is pressed from the notification. This because it can be (and will possibly be) the last thing the app does. Pins are not notified about if there was an error with them. In the future it should be a retry system, where if a pin failed to load 3 times, indicate it as an error. Also differentiate between 404 and load error in the future. --- .../org/floens/chan/core/watch/PinWatcher.java | 18 ++++++++++++++++-- .../floens/chan/core/watch/WatchNotifier.java | 14 ++++++++++---- .../org/floens/chan/service/WatchService.java | 2 -- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/Chan/src/org/floens/chan/core/watch/PinWatcher.java b/Chan/src/org/floens/chan/core/watch/PinWatcher.java index 573cd1dd..f7760b69 100644 --- a/Chan/src/org/floens/chan/core/watch/PinWatcher.java +++ b/Chan/src/org/floens/chan/core/watch/PinWatcher.java @@ -20,6 +20,7 @@ public class PinWatcher implements Loader.LoaderListener { private boolean isError = false; private final List posts = new ArrayList(); + private boolean wereNewQuotes = false; public PinWatcher(Pin pin) { this.pin = pin; @@ -77,6 +78,15 @@ public class PinWatcher implements Loader.LoaderListener { } } + public boolean getWereNewQuotes() { + if (wereNewQuotes) { + wereNewQuotes = false; + return true; + } else { + return false; + } + } + public boolean isError() { return isError; } @@ -85,8 +95,6 @@ public class PinWatcher implements Loader.LoaderListener { public void onError(VolleyError error) { Logger.e(TAG, "PinWatcher onError: ", error); isError = true; - pin.watchLastCount = 0; - pin.watchNewCount = 0; WatchService.onPinWatcherResult(); } @@ -114,6 +122,8 @@ public class PinWatcher implements Loader.LoaderListener { } } + int lastQuoteCount = pin.quoteNewCount; + // Find posts quoting these saved posts pin.quoteNewCount = 0; for (Post resultPost : result) { @@ -125,6 +135,10 @@ public class PinWatcher implements Loader.LoaderListener { } } + if (pin.quoteNewCount > lastQuoteCount) { + wereNewQuotes = true; + } + WatchService.onPinWatcherResult(); } } diff --git a/Chan/src/org/floens/chan/core/watch/WatchNotifier.java b/Chan/src/org/floens/chan/core/watch/WatchNotifier.java index dd7fa690..dc01f06f 100644 --- a/Chan/src/org/floens/chan/core/watch/WatchNotifier.java +++ b/Chan/src/org/floens/chan/core/watch/WatchNotifier.java @@ -21,6 +21,8 @@ import android.media.RingtoneManager; import android.support.v4.app.NotificationCompat; public class WatchNotifier { + private static final String TAG = "WatchNotifier"; + private final int NOTIFICATION_ID = 1; private final WatchService pinnedService; @@ -58,6 +60,7 @@ public class WatchNotifier { } ChanApplication.getPinnedManager().onPinsChanged(); + ChanApplication.getPinnedManager().updateAll(); // Can be the last thing this app does } private void prepareNotification() { @@ -68,11 +71,11 @@ public class WatchNotifier { int newQuotesCount = 0; List posts = new ArrayList(); boolean makeSound = false; - boolean show = false; + boolean show = true; for (Pin pin : watchingPins) { PinWatcher watcher = pin.getPinWatcher(); - if (watcher == null) + if (watcher == null || watcher.isError()) continue; boolean add = false; @@ -91,10 +94,13 @@ public class WatchNotifier { if (watcher.getNewQuoteCount() > 0) { newQuotesCount += watcher.getNewQuoteCount(); show = true; - makeSound = true; add = true; } + if (watcher.getWereNewQuotes()) { + makeSound = true; + } + if (add) { pins.add(pin); } @@ -173,7 +179,7 @@ public class WatchNotifier { builder.setStyle(style); - Logger.test("SHOWING NOTIFICATION!"); + Logger.i(TAG, "Showing notification"); nm.notify(NOTIFICATION_ID, builder.getNotification()); } diff --git a/Chan/src/org/floens/chan/service/WatchService.java b/Chan/src/org/floens/chan/service/WatchService.java index 7084f326..263b18f5 100644 --- a/Chan/src/org/floens/chan/service/WatchService.java +++ b/Chan/src/org/floens/chan/service/WatchService.java @@ -127,8 +127,6 @@ public class WatchService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { - Logger.test("Startcommand " + startId); - if (intent.getExtras() != null && intent.getExtras().getBoolean("pause_pins", false)) { if (watchNotifier != null) { watchNotifier.onPausePinsClicked();