Open app from the new chapters found notification. Fixes #22

This commit is contained in:
inorichi 2016-01-15 14:38:24 +01:00
parent 5bb72429ea
commit 1508bf42fb
3 changed files with 41 additions and 51 deletions

View File

@ -1,11 +1,14 @@
package eu.kanade.mangafeed.data.sync; package eu.kanade.mangafeed.data.sync;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.IBinder; import android.os.IBinder;
import android.os.PowerManager; import android.os.PowerManager;
import android.support.v4.app.NotificationCompat;
import android.util.Pair; import android.util.Pair;
import java.util.ArrayList; import java.util.ArrayList;
@ -21,9 +24,9 @@ import eu.kanade.mangafeed.data.database.DatabaseHelper;
import eu.kanade.mangafeed.data.database.models.Manga; import eu.kanade.mangafeed.data.database.models.Manga;
import eu.kanade.mangafeed.data.preference.PreferencesHelper; import eu.kanade.mangafeed.data.preference.PreferencesHelper;
import eu.kanade.mangafeed.data.source.SourceManager; import eu.kanade.mangafeed.data.source.SourceManager;
import eu.kanade.mangafeed.ui.main.MainActivity;
import eu.kanade.mangafeed.util.AndroidComponentUtil; import eu.kanade.mangafeed.util.AndroidComponentUtil;
import eu.kanade.mangafeed.util.NetworkUtil; import eu.kanade.mangafeed.util.NetworkUtil;
import eu.kanade.mangafeed.util.NotificationUtil;
import rx.Observable; import rx.Observable;
import rx.Subscription; import rx.Subscription;
import rx.schedulers.Schedulers; import rx.schedulers.Schedulers;
@ -87,8 +90,7 @@ public class LibraryUpdateService extends Service {
.flatMap(this::updateLibrary) .flatMap(this::updateLibrary)
.subscribe(next -> {}, .subscribe(next -> {},
error -> { error -> {
NotificationUtil.create(this, UPDATE_NOTIFICATION_ID, showNotification(getString(R.string.notification_update_error), "");
getString(R.string.notification_update_error), "");
stopSelf(startId); stopSelf(startId);
}, () -> { }, () -> {
Timber.i("Library updated"); Timber.i("Library updated");
@ -109,7 +111,7 @@ public class LibraryUpdateService extends Service {
.toList().toBlocking().single(); .toList().toBlocking().single();
return Observable.from(mangas) return Observable.from(mangas)
.doOnNext(manga -> NotificationUtil.create(this, UPDATE_NOTIFICATION_ID, .doOnNext(manga -> showNotification(
getString(R.string.notification_update_progress, getString(R.string.notification_update_progress,
count.incrementAndGet(), mangas.size()), manga.title)) count.incrementAndGet(), mangas.size()), manga.title))
.concatMap(manga -> updateManga(manga) .concatMap(manga -> updateManga(manga)
@ -121,8 +123,7 @@ public class LibraryUpdateService extends Service {
.filter(pair -> pair.first > 0) .filter(pair -> pair.first > 0)
.map(pair -> new MangaUpdate(manga, pair.first))) .map(pair -> new MangaUpdate(manga, pair.first)))
.doOnNext(updates::add) .doOnNext(updates::add)
.doOnCompleted(() -> NotificationUtil.createBigText(this, UPDATE_NOTIFICATION_ID, .doOnCompleted(() -> showBigNotification(getString(R.string.notification_update_completed),
getString(R.string.notification_update_completed),
getUpdatedMangas(updates, failedUpdates))); getUpdatedMangas(updates, failedUpdates)));
} }
@ -172,6 +173,38 @@ public class LibraryUpdateService extends Service {
} }
} }
private void showNotification(String title, String body) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_action_refresh)
.setContentTitle(title)
.setContentText(body);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(UPDATE_NOTIFICATION_ID, builder.build());
}
private void showBigNotification(String title, String body) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_action_refresh)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle().bigText(body))
.setContentIntent(getNotificationIntent())
.setAutoCancel(true);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(UPDATE_NOTIFICATION_ID, builder.build());
}
private PendingIntent getNotificationIntent() {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
return PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public static class SyncOnConnectionAvailable extends BroadcastReceiver { public static class SyncOnConnectionAvailable extends BroadcastReceiver {
@Override @Override

View File

@ -36,13 +36,14 @@ public class MainActivity extends BaseActivity {
@Override @Override
protected void onCreate(Bundle savedState) { protected void onCreate(Bundle savedState) {
super.onCreate(savedState);
// Do not let the launcher create a new activity // Do not let the launcher create a new activity
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) { if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
finish(); finish();
return; return;
} }
super.onCreate(savedState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
ButterKnife.bind(this); ButterKnife.bind(this);

View File

@ -1,44 +0,0 @@
package eu.kanade.mangafeed.util;
import android.app.NotificationManager;
import android.content.Context;
import android.support.v4.app.NotificationCompat;
import eu.kanade.mangafeed.R;
public class NotificationUtil {
public static void create(Context context, int nId, String title, String body, int iconRes) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(iconRes == -1 ? R.drawable.ic_action_refresh : iconRes)
.setContentTitle(title)
.setContentText(body);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(nId, mBuilder.build());
}
public static void createBigText(Context context, int nId, String title, String body, int iconRes) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(iconRes == -1 ? R.drawable.ic_action_refresh : iconRes)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle().bigText(body));
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(nId, mBuilder.build());
}
public static void create(Context context, int nId, String title, String body) {
create(context, nId, title, body, -1);
}
public static void createBigText(Context context, int nId, String title, String body) {
createBigText(context, nId, title, body, -1);
}
}