Notifications with big views about new posts and quotes

captchafix
Florens Douwes 11 years ago
parent 20a50a3d28
commit 714bdc143a
  1. 1
      Chan/AndroidManifest.xml
  2. 4
      Chan/src/org/floens/chan/core/manager/PinnedManager.java
  3. 20
      Chan/src/org/floens/chan/core/model/Pin.java
  4. 104
      Chan/src/org/floens/chan/core/watch/PinWatcher.java
  5. 95
      Chan/src/org/floens/chan/core/watch/WatchNotifier.java
  6. 5
      Chan/src/org/floens/chan/service/WatchService.java
  7. 2
      Chan/src/org/floens/chan/ui/activity/BaseActivity.java
  8. 2
      Chan/src/org/floens/chan/ui/adapter/PinnedAdapter.java

@ -21,6 +21,7 @@
android:theme="@style/AppTheme" >
<activity
android:name="org.floens.chan.ui.activity.BoardActivity"
android:launchMode="singleTask"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="@string/app_name" >
<intent-filter>

@ -119,7 +119,9 @@ public class PinnedManager {
}
public void onPinViewed(Pin pin) {
pin.onViewed();
if (pin.getPinWatcher() != null) {
pin.getPinWatcher().onViewed();
}
onPinsChanged();
}

@ -34,6 +34,14 @@ public class Pin {
@DatabaseField
public int watchNewCount;
public int quoteLastCount;
public int quoteNewCount;
public PinWatcher getPinWatcher() {
return pinWatcher;
}
public void updateWatch() {
if (pinWatcher == null) {
pinWatcher = new PinWatcher(this);
@ -42,18 +50,6 @@ public class Pin {
pinWatcher.update();
}
public int getNewPostCount() {
if (pinWatcher != null) {
return pinWatcher.getNewPostCount();
} else {
return 0;
}
}
public void onViewed() {
watchLastCount = watchNewCount;
}
public void destroyWatcher() {
if (pinWatcher != null) {
pinWatcher.destroy();

@ -1,5 +1,6 @@
package org.floens.chan.core.watch;
import java.util.ArrayList;
import java.util.List;
import org.floens.chan.core.loader.Loader;
@ -18,6 +19,14 @@ public class PinWatcher implements Loader.LoaderListener {
private Loader loader;
private boolean isError = false;
private final List<Post> posts = new ArrayList<Post>();
private boolean wereNewPosts = false;
private boolean wereNewQuotes = false;
private int postLastLoad;
private int quoteLastLoad;
public PinWatcher(Pin pin) {
this.pin = pin;
@ -37,7 +46,20 @@ public class PinWatcher implements Loader.LoaderListener {
}
}
public int getNewPostCount() {
public void onViewed() {
pin.watchLastCount = pin.watchNewCount;
pin.quoteLastCount = pin.quoteNewCount;
}
public List<Post> getNewPosts() {
if (posts.size() == 0) {
return posts;
} else {
return posts.subList(Math.max(0, posts.size() - getNewPostsCount()), posts.size());
}
}
public int getNewPostsCount() {
if (pin.watchLastCount <= 0) {
return 0;
} else {
@ -45,6 +67,40 @@ public class PinWatcher implements Loader.LoaderListener {
}
}
public boolean getWereNewPosts() {
if (wereNewPosts) {
wereNewPosts = false;
return true;
} else {
return false;
}
}
public List<Post> getNewQuotes() {
if (posts.size() == 0) {
return posts;
} else {
return posts.subList(Math.max(0, posts.size() - getNewQuoteCount()), posts.size());
}
}
public int getNewQuoteCount() {
if (pin.quoteLastCount <= 0) {
return 0;
} else {
return Math.max(0, pin.quoteNewCount - pin.quoteLastCount);
}
}
public boolean getWereNewQuotes() {
if (wereNewQuotes) {
wereNewQuotes = false;
return true;
} else {
return false;
}
}
public boolean isError() {
return isError;
}
@ -63,15 +119,51 @@ public class PinWatcher implements Loader.LoaderListener {
public void onData(List<Post> result, boolean append) {
isError = false;
int count = result.size();
Logger.test("PinWatcher onData, Post size: " + count);
posts.clear();
posts.addAll(result);
if (pin.watchLastCount <= 0) {
if (pin.watchLastCount <= 0)
pin.watchLastCount = pin.watchNewCount;
if (pin.quoteLastCount <= 0)
pin.quoteLastCount = pin.quoteNewCount;
pin.watchNewCount = result.size();
if (postLastLoad == 0)
postLastLoad = pin.watchNewCount;
if (pin.watchNewCount > postLastLoad) {
wereNewPosts = true;
postLastLoad = pin.watchNewCount;
}
// Get list of saved posts
List<Post> savedPosts = new ArrayList<Post>();
for (Post saved : result) {
if (saved.isSavedReply) {
savedPosts.add(saved);
}
}
pin.watchNewCount = count;
// Find posts quoting these saved posts
pin.quoteNewCount = 0;
for (Post resultPost : result) {
// This post replies to me
for (Post savedPost : savedPosts) {
if (resultPost.repliesTo.contains(savedPost.no)) {
pin.quoteNewCount++;
}
}
}
if (quoteLastLoad == 0)
quoteLastLoad = pin.quoteNewCount;
if (pin.quoteNewCount > quoteLastLoad) {
wereNewQuotes = true;
quoteLastLoad = pin.quoteNewCount;
}
WatchService.onPinWatcherResult();
}

@ -6,14 +6,17 @@ 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.core.model.Post;
import org.floens.chan.service.WatchService;
import org.floens.chan.ui.activity.BoardActivity;
import org.floens.chan.utils.Logger;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.support.v4.app.NotificationCompat;
public class WatchNotifier {
private final int NOTIFICATION_ID = 1;
@ -21,8 +24,6 @@ public class WatchNotifier {
private final WatchService pinnedService;
private final NotificationManager nm;
private int lastNewPosts;
public WatchNotifier(WatchService pinnedService) {
this.pinnedService = pinnedService;
nm = (NotificationManager) pinnedService.getSystemService(Context.NOTIFICATION_SERVICE);
@ -47,48 +48,78 @@ public class WatchNotifier {
}
private void prepareNotification() {
List<Pin> pins = ChanApplication.getPinnedManager().getWatchingPins();
List<Pin> watchingPins = ChanApplication.getPinnedManager().getWatchingPins();
List<Pin> pins = new ArrayList<Pin>();
int newPostsCount = 0;
int newQuotesCount = 0;
List<Post> newPosts = new ArrayList<Post>();
boolean makeSound = false;
boolean show = false;
int newPosts = 0;
List<Pin> pinsWithNewPosts = new ArrayList<Pin>();
for (Pin pin : watchingPins) {
PinWatcher watcher = pin.getPinWatcher();
if (watcher == null)
continue;
for (Pin pin : pins) {
if (pin.getNewPostCount() > 0) {
newPosts += pin.getNewPostCount();
pinsWithNewPosts.add(pin);
boolean add = false;
if (watcher.getWereNewPosts()) {
newPostsCount += watcher.getNewPostsCount();
newPosts.addAll(watcher.getNewPosts());
show = true;
add = true;
}
}
boolean show = false;
if (watcher.getWereNewQuotes()) {
newQuotesCount += watcher.getNewQuoteCount();
show = true;
makeSound = true;
add = true;
}
if (lastNewPosts != newPosts && newPosts > 0) {
show = true;
if (add) {
pins.add(pin);
}
}
lastNewPosts = newPosts;
if (show) {
// "33 new posts, 3 quoting you"
String title = newPostsCount + " new posts";
if (newQuotesCount > 0) {
title += ", " + newQuotesCount + " quoting you";
}
// "234 new posts in DPT"
// "234 new posts in 5 threads"
String descriptor;
if (pinsWithNewPosts.size() == 1) {
descriptor = pinsWithNewPosts.get(0).loadable.title;
if (pins.size() == 1) {
descriptor = pins.get(0).loadable.title;
} else {
descriptor = pinsWithNewPosts.size() + " threads";
descriptor = pins.size() + " threads";
}
String content = newPosts + " new posts in " + descriptor;
String title = "New posts";
String content = newPostsCount + " new posts in " + descriptor;
List<CharSequence> lines = new ArrayList<CharSequence>();
for (int i = newPosts.size() - 1; i >= 0; i--) {
lines.add(newPosts.get(i).comment);
}
showNotification(content, title, content, Integer.toString(newPosts));
showNotification(content, title, content, Integer.toString(newPostsCount), lines, makeSound);
}
}
@SuppressWarnings("deprecation")
private void showNotification(String tickerText, String title, String content, String contentInfo) {
private void showNotification(String tickerText, String title, String content, String contentInfo,
List<CharSequence> lines, boolean makeSound) {
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);
NotificationCompat.Builder builder = new NotificationCompat.Builder(pinnedService);
builder.setContentIntent(pending);
builder.setTicker(tickerText);
@ -97,6 +128,20 @@ public class WatchNotifier {
builder.setContentInfo(contentInfo);
builder.setSmallIcon(R.drawable.ic_stat_notify);
if (makeSound) {
builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
}
NotificationCompat.InboxStyle style = new NotificationCompat.InboxStyle();
for (CharSequence line : lines.subList(Math.max(0, lines.size() - 10), lines.size())) {
style.addLine(line);
}
style.setBigContentTitle(title);
style.setSummaryText(content);
builder.setStyle(style);
Logger.test("SHOWING NOTIFICATION!");
nm.notify(NOTIFICATION_ID, builder.getNotification());
}
}

@ -128,6 +128,11 @@ public class WatchService extends Service {
Logger.i(TAG, "WatchService destroyed");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
private void startThread() {
running = true;

@ -87,7 +87,7 @@ public abstract class BaseActivity extends Activity implements PanelSlideListene
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
pinDrawer.openDrawer(pinDrawerView);
// pinDrawer.openDrawer(pinDrawerView);
}
private void initPane() {

@ -53,7 +53,7 @@ public class PinnedAdapter extends ArrayAdapter<Pin> {
if (item.isError()) {
itemCount.setText("404");
} else {
int count = item.getNewPostCount();
int count = item.getPinWatcher() == null ? 0 : item.getPinWatcher().getNewPostsCount();
String total = Integer.toString(count);
if (count > 999) {
total = "1k+";

Loading…
Cancel
Save