From 1508bf42fbe61546729ea2270d3c4f07951e2918 Mon Sep 17 00:00:00 2001 From: inorichi Date: Fri, 15 Jan 2016 14:38:24 +0100 Subject: [PATCH] Open app from the new chapters found notification. Fixes #22 --- .../data/sync/LibraryUpdateService.java | 45 ++++++++++++++++--- .../mangafeed/ui/main/MainActivity.java | 3 +- .../mangafeed/util/NotificationUtil.java | 44 ------------------ 3 files changed, 41 insertions(+), 51 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/mangafeed/util/NotificationUtil.java diff --git a/app/src/main/java/eu/kanade/mangafeed/data/sync/LibraryUpdateService.java b/app/src/main/java/eu/kanade/mangafeed/data/sync/LibraryUpdateService.java index 267ed5d13f..46605c1edf 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/sync/LibraryUpdateService.java +++ b/app/src/main/java/eu/kanade/mangafeed/data/sync/LibraryUpdateService.java @@ -1,11 +1,14 @@ package eu.kanade.mangafeed.data.sync; +import android.app.NotificationManager; +import android.app.PendingIntent; import android.app.Service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.IBinder; import android.os.PowerManager; +import android.support.v4.app.NotificationCompat; import android.util.Pair; 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.preference.PreferencesHelper; 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.NetworkUtil; -import eu.kanade.mangafeed.util.NotificationUtil; import rx.Observable; import rx.Subscription; import rx.schedulers.Schedulers; @@ -87,8 +90,7 @@ public class LibraryUpdateService extends Service { .flatMap(this::updateLibrary) .subscribe(next -> {}, error -> { - NotificationUtil.create(this, UPDATE_NOTIFICATION_ID, - getString(R.string.notification_update_error), ""); + showNotification(getString(R.string.notification_update_error), ""); stopSelf(startId); }, () -> { Timber.i("Library updated"); @@ -109,7 +111,7 @@ public class LibraryUpdateService extends Service { .toList().toBlocking().single(); return Observable.from(mangas) - .doOnNext(manga -> NotificationUtil.create(this, UPDATE_NOTIFICATION_ID, + .doOnNext(manga -> showNotification( getString(R.string.notification_update_progress, count.incrementAndGet(), mangas.size()), manga.title)) .concatMap(manga -> updateManga(manga) @@ -121,8 +123,7 @@ public class LibraryUpdateService extends Service { .filter(pair -> pair.first > 0) .map(pair -> new MangaUpdate(manga, pair.first))) .doOnNext(updates::add) - .doOnCompleted(() -> NotificationUtil.createBigText(this, UPDATE_NOTIFICATION_ID, - getString(R.string.notification_update_completed), + .doOnCompleted(() -> showBigNotification(getString(R.string.notification_update_completed), 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 { @Override diff --git a/app/src/main/java/eu/kanade/mangafeed/ui/main/MainActivity.java b/app/src/main/java/eu/kanade/mangafeed/ui/main/MainActivity.java index 7d85b1f4e4..4c24019604 100644 --- a/app/src/main/java/eu/kanade/mangafeed/ui/main/MainActivity.java +++ b/app/src/main/java/eu/kanade/mangafeed/ui/main/MainActivity.java @@ -36,13 +36,14 @@ public class MainActivity extends BaseActivity { @Override protected void onCreate(Bundle savedState) { + super.onCreate(savedState); + // Do not let the launcher create a new activity if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) { finish(); return; } - super.onCreate(savedState); setContentView(R.layout.activity_main); ButterKnife.bind(this); diff --git a/app/src/main/java/eu/kanade/mangafeed/util/NotificationUtil.java b/app/src/main/java/eu/kanade/mangafeed/util/NotificationUtil.java deleted file mode 100644 index 3f20bf7297..0000000000 --- a/app/src/main/java/eu/kanade/mangafeed/util/NotificationUtil.java +++ /dev/null @@ -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); - } - -}