|
|
|
@ -1,11 +1,12 @@ |
|
|
|
|
package org.floens.chan.core.watch; |
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
import org.floens.chan.ChanApplication; |
|
|
|
|
import org.floens.chan.R; |
|
|
|
|
import org.floens.chan.core.model.Pin; |
|
|
|
|
import org.floens.chan.service.PinnedService; |
|
|
|
|
import org.floens.chan.service.WatchService; |
|
|
|
|
import org.floens.chan.ui.activity.BoardActivity; |
|
|
|
|
|
|
|
|
|
import android.app.Notification; |
|
|
|
@ -17,57 +18,81 @@ import android.content.Intent; |
|
|
|
|
public class WatchNotifier { |
|
|
|
|
private final int NOTIFICATION_ID = 1; |
|
|
|
|
|
|
|
|
|
private final PinnedService pinnedService; |
|
|
|
|
private final WatchService pinnedService; |
|
|
|
|
private final NotificationManager nm; |
|
|
|
|
|
|
|
|
|
public WatchNotifier(PinnedService pinnedService) { |
|
|
|
|
private int lastNewPosts; |
|
|
|
|
|
|
|
|
|
public WatchNotifier(WatchService pinnedService) { |
|
|
|
|
this.pinnedService = pinnedService; |
|
|
|
|
nm = (NotificationManager) pinnedService.getSystemService(Context.NOTIFICATION_SERVICE); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void update() { |
|
|
|
|
List<Pin> pins = ChanApplication.getPinnedManager().getPins(); |
|
|
|
|
if (!WatchService.getActivityInForeground()) { |
|
|
|
|
prepareNotification(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public void onForegroundChanged() { |
|
|
|
|
if (WatchService.getActivityInForeground()) { |
|
|
|
|
nm.cancel(NOTIFICATION_ID); |
|
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void prepareNotification() { |
|
|
|
|
List<Pin> pins = ChanApplication.getPinnedManager().getWatchingPins(); |
|
|
|
|
|
|
|
|
|
int count = 0; |
|
|
|
|
int pinCount = 0; |
|
|
|
|
int newPosts = 0; |
|
|
|
|
List<Pin> pinsWithNewPosts = new ArrayList<Pin>(); |
|
|
|
|
|
|
|
|
|
for (Pin pin : pins) { |
|
|
|
|
count += pin.getNewPostCount(); |
|
|
|
|
pinCount++; |
|
|
|
|
if (pin.getNewPostCount() > 0) { |
|
|
|
|
newPosts += pin.getNewPostCount(); |
|
|
|
|
pinsWithNewPosts.add(pin); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!PinnedService.getActivityInForeground()) { |
|
|
|
|
showNotification(count + " new posts in " + pinCount + " threads\n" + System.currentTimeMillis()); |
|
|
|
|
boolean show = false; |
|
|
|
|
|
|
|
|
|
if (lastNewPosts != newPosts && newPosts > 0) { |
|
|
|
|
show = true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
lastNewPosts = newPosts; |
|
|
|
|
|
|
|
|
|
public void onForegroundChanged() { |
|
|
|
|
if (PinnedService.getActivityInForeground()) { |
|
|
|
|
nm.cancel(NOTIFICATION_ID); |
|
|
|
|
} else { |
|
|
|
|
if (show) { |
|
|
|
|
String descriptor; |
|
|
|
|
if (pinsWithNewPosts.size() == 1) { |
|
|
|
|
descriptor = pinsWithNewPosts.get(0).loadable.title; |
|
|
|
|
} else { |
|
|
|
|
descriptor = pinsWithNewPosts.size() + " threads"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
String content = newPosts + " new posts in " + descriptor; |
|
|
|
|
String title = "New posts"; |
|
|
|
|
|
|
|
|
|
showNotification(content, title, content, Integer.toString(newPosts)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@SuppressWarnings("deprecation") |
|
|
|
|
private void showNotification(String text) { |
|
|
|
|
private void showNotification(String tickerText, String title, String content, String contentInfo) { |
|
|
|
|
Intent intent = new Intent(pinnedService, BoardActivity.class); |
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); |
|
|
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP |
|
|
|
|
| Intent.FLAG_ACTIVITY_NEW_TASK); |
|
|
|
|
PendingIntent pending = PendingIntent.getActivity(pinnedService, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); |
|
|
|
|
|
|
|
|
|
Notification.Builder builder = new Notification.Builder(pinnedService); |
|
|
|
|
builder.setContentIntent(pending); |
|
|
|
|
|
|
|
|
|
builder.setTicker(text); |
|
|
|
|
builder.setContentTitle(text); |
|
|
|
|
builder.setContentText(text); |
|
|
|
|
builder.setTicker(tickerText); |
|
|
|
|
builder.setContentTitle(title); |
|
|
|
|
builder.setContentText(content); |
|
|
|
|
builder.setContentInfo(contentInfo); |
|
|
|
|
builder.setSmallIcon(R.drawable.ic_stat_notify); |
|
|
|
|
|
|
|
|
|
nm.notify(NOTIFICATION_ID, builder.getNotification()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|