From 1b545c9e4da8229ed4c066c538bd1457d2ad82f2 Mon Sep 17 00:00:00 2001 From: Jay Date: Sun, 3 Nov 2019 23:35:17 -0800 Subject: [PATCH] Fixes to notifcation actions, such as starting a opening a chapter --- .../data/download/DownloadNotifier.kt | 3 +- .../data/library/LibraryUpdateService.kt | 3 +- .../data/notification/NotificationReceiver.kt | 41 ++++++++++--------- .../tachiyomi/ui/reader/ReaderActivity.kt | 10 ++++- .../tachiyomi/ui/reader/SaveImageNotifier.kt | 2 + app/src/main/res/values-night/colors.xml | 1 + app/src/main/res/values/colors.xml | 1 + 7 files changed, 39 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadNotifier.kt b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadNotifier.kt index e20f5b2a28..c98b0dbf88 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadNotifier.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/download/DownloadNotifier.kt @@ -170,7 +170,8 @@ internal class DownloadNotifier(private val context: Context) { setSmallIcon(android.R.drawable.stat_sys_download_done) setAutoCancel(true) clearActions() - setContentIntent(NotificationReceiver.openChapterPendingBroadcast(context, download.manga, download.chapter)) + setContentIntent(NotificationReceiver.openChapterPendingBroadcast(context, download + .manga, download.chapter, Notifications.ID_DOWNLOAD_CHAPTER)) setProgress(0, 0, false) } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt index 969879be4d..458a03522e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/library/LibraryUpdateService.kt @@ -480,7 +480,8 @@ class LibraryUpdateService( val chapter = chapters.sortedByDescending { it.source_order }.find { !it.read } if (chapter != null) { addAction(R.drawable.ic_in_library_24dp, getString(R.string.action_start_reading), - NotificationReceiver.openChapterPendingBroadcast(context, onlyManga, chapter)) + NotificationReceiver.openChapterPendingBroadcast(context, onlyManga, + chapter, Notifications.ID_NEW_CHAPTERS)) } } priority = NotificationCompat.PRIORITY_HIGH diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationReceiver.kt b/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationReceiver.kt index c97a94acad..4a9e76efe3 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationReceiver.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/notification/NotificationReceiver.kt @@ -1,12 +1,17 @@ package eu.kanade.tachiyomi.data.notification +import android.app.Activity +import android.app.KeyguardManager import android.app.Notification import android.app.PendingIntent import android.content.BroadcastReceiver import android.content.ClipData import android.content.Context import android.content.Intent +import android.os.Build +import android.os.Bundle import android.os.Handler +import androidx.appcompat.app.AppCompatActivity import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.DatabaseHelper import eu.kanade.tachiyomi.data.database.models.Chapter @@ -82,6 +87,7 @@ class NotificationReceiver : BroadcastReceiver() { * @param notificationId id of notification */ private fun shareImage(context: Context, path: String, notificationId: Int) { + val km = context.getSystemService(Activity.KEYGUARD_SERVICE) as KeyguardManager // Create intent val intent = Intent(Intent.ACTION_SEND).apply { val uri = File(path).getUriCompat(context) @@ -91,12 +97,7 @@ class NotificationReceiver : BroadcastReceiver() { type = "image/*" } // Close Navigation Shade - context.sendBroadcast(Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) - // Launch share activity - val shareIntent = Intent.createChooser(intent, context.getString(R.string - .action_share)) - shareIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION - context.startActivity(shareIntent) + } /** @@ -262,12 +263,17 @@ class NotificationReceiver : BroadcastReceiver() { * @return [PendingIntent] */ internal fun shareImagePendingBroadcast(context: Context, path: String, notificationId: Int): PendingIntent { - val intent = Intent(context, NotificationReceiver::class.java).apply { - action = ACTION_SHARE_IMAGE - putExtra(EXTRA_FILE_LOCATION, path) - putExtra(EXTRA_NOTIFICATION_ID, notificationId) + //val shareIntent = ShareStartingActivity.newIntent(context, path) + val shareIntent = Intent(Intent.ACTION_SEND).apply { + val uri = File(path).getUriCompat(context) + putExtra(Intent.EXTRA_STREAM, uri) + flags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_ACTIVITY_CLEAR_TOP + clipData = ClipData.newRawUri(null, uri) + type = "image/*" } - return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) + //val shareIntent2 = Intent.createChooser(shareIntent, context.getString(R.string.action_share)) + return PendingIntent.getActivity(context, 0, shareIntent, PendingIntent + .FLAG_CANCEL_CURRENT) } /** @@ -294,13 +300,10 @@ class NotificationReceiver : BroadcastReceiver() { * @param manga manga of chapter * @param chapter chapter that needs to be opened */ - internal fun openChapterPendingBroadcast(context: Context, manga: Manga, chapter: Chapter): PendingIntent { - val intent = Intent(context, NotificationReceiver::class.java).apply { - action = ACTION_OPEN_CHAPTER - putExtra(EXTRA_MANGA_ID, manga.id) - putExtra(EXTRA_CHAPTER_ID, chapter.id) - } - return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) + internal fun openChapterPendingBroadcast(context: Context, manga: Manga, chapter: + Chapter, notificationId: Int): PendingIntent { + val newIntent = ReaderActivity.newIntent(context, manga, chapter, notificationId) + return PendingIntent.getActivity(context, 0, newIntent, PendingIntent.FLAG_UPDATE_CURRENT) } /** @@ -316,4 +319,4 @@ class NotificationReceiver : BroadcastReceiver() { return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) } } -} +} \ No newline at end of file diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt index cf1ef64ba8..47edae77d6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt @@ -21,6 +21,7 @@ import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Manga +import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.ui.base.activity.BaseRxActivity @@ -119,10 +120,13 @@ class ReaderActivity : BaseRxActivity(), const val VERTICAL = 3 const val WEBTOON = 4 - fun newIntent(context: Context, manga: Manga, chapter: Chapter): Intent { + fun newIntent(context: Context, manga: Manga, chapter: Chapter, notificationId:Int? = + null): + Intent { val intent = Intent(context, ReaderActivity::class.java) intent.putExtra("manga", manga.id) intent.putExtra("chapter", chapter.id) + intent.putExtra("notificationId", notificationId) return intent } } @@ -142,6 +146,10 @@ class ReaderActivity : BaseRxActivity(), if (presenter.needsInit()) { val manga = intent.extras!!.getLong("manga", -1) val chapter = intent.extras!!.getLong("chapter", -1) + val notificationId = intent.extras!!.getInt("notificationId", -1) + if (notificationId > 0) { + applicationContext.notificationManager.cancel(notificationId) + } if (manga == -1L || chapter == -1L) { finish() diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/SaveImageNotifier.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/SaveImageNotifier.kt index d69ced1e2b..dfa24d7de8 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/SaveImageNotifier.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/SaveImageNotifier.kt @@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.ui.reader import android.content.Context import android.graphics.Bitmap import androidx.core.app.NotificationCompat +import androidx.core.content.ContextCompat import com.bumptech.glide.load.engine.DiskCacheStrategy import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.data.glide.GlideApp @@ -57,6 +58,7 @@ class SaveImageNotifier(private val context: Context) { setStyle(NotificationCompat.BigPictureStyle().bigPicture(image)) setLargeIcon(image) setAutoCancel(true) + color = ContextCompat.getColor(context, R.color.colorAccentLight) // Clear old actions if they exist if (!mActions.isEmpty()) mActions.clear() diff --git a/app/src/main/res/values-night/colors.xml b/app/src/main/res/values-night/colors.xml index 57cf1782ce..e8658d3fc4 100644 --- a/app/src/main/res/values-night/colors.xml +++ b/app/src/main/res/values-night/colors.xml @@ -7,4 +7,5 @@ @color/colorDarkPrimary @color/md_white_1000_20 @color/md_grey_800 + #3399FF \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 7d40bb55a3..10c8cde17a 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -9,6 +9,7 @@ #323232 @color/md_white_1000 @color/md_black_1000_12 + @color/md_blue_A400 #212121 #212121