fixes wrong getBroadcast calls from imageNotification (#585)

This commit is contained in:
Bram van de Kerkhof 2016-12-18 15:15:44 +01:00 committed by GitHub
parent 79705df499
commit cc43d9daed
3 changed files with 33 additions and 56 deletions

View file

@ -4,12 +4,11 @@ import android.app.PendingIntent
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.net.Uri
import android.support.v4.content.FileProvider import android.support.v4.content.FileProvider
import eu.kanade.tachiyomi.BuildConfig import eu.kanade.tachiyomi.BuildConfig
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.util.notificationManager import eu.kanade.tachiyomi.util.notificationManager
import java.io.File import java.io.File
import eu.kanade.tachiyomi.Constants.NOTIFICATION_DOWNLOAD_IMAGE_ID as defaultNotification
/** /**
* The BroadcastReceiver of [ImageNotifier] * The BroadcastReceiver of [ImageNotifier]
@ -18,21 +17,16 @@ import java.io.File
class ImageNotificationReceiver : BroadcastReceiver() { class ImageNotificationReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
when (intent.action) { when (intent.action) {
ACTION_SHARE_IMAGE -> {
shareImage(context, intent.getStringExtra(EXTRA_FILE_LOCATION))
context.notificationManager.cancel(intent.getIntExtra(NOTIFICATION_ID, 5))
}
ACTION_SHOW_IMAGE ->
showImage(context, intent.getStringExtra(EXTRA_FILE_LOCATION))
ACTION_DELETE_IMAGE -> { ACTION_DELETE_IMAGE -> {
deleteImage(intent.getStringExtra(EXTRA_FILE_LOCATION)) deleteImage(intent.getStringExtra(EXTRA_FILE_LOCATION))
context.notificationManager.cancel(intent.getIntExtra(NOTIFICATION_ID, 5)) context.notificationManager.cancel(intent.getIntExtra(NOTIFICATION_ID, defaultNotification))
} }
} }
} }
/** /**
* Called to delete image * Called to delete image
*
* @param path path of file * @param path path of file
*/ */
private fun deleteImage(path: String) { private fun deleteImage(path: String) {
@ -40,60 +34,42 @@ class ImageNotificationReceiver : BroadcastReceiver() {
if (file.exists()) file.delete() if (file.exists()) file.delete()
} }
/**
* Called to start share intent to share image
* @param context context of application
* @param path path of file
*/
private fun shareImage(context: Context, path: String) {
val intent = Intent(Intent.ACTION_SEND).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_MULTIPLE_TASK
putExtra(Intent.EXTRA_STREAM, Uri.parse(path))
type = "image/*"
}
context.startActivity(Intent.createChooser(intent, context.getString(R.string.action_share)))
}
/**
* Called to show image in gallery application
* @param context context of application
* @param path path of file
*/
private fun showImage(context: Context, path: String) {
val intent = Intent(Intent.ACTION_VIEW).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_MULTIPLE_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION
val uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", File(path))
setDataAndType(uri, "image/*")
}
context.startActivity(intent)
}
companion object { companion object {
private const val ACTION_SHARE_IMAGE = "eu.kanade.SHARE_IMAGE"
private const val ACTION_SHOW_IMAGE = "eu.kanade.SHOW_IMAGE"
private const val ACTION_DELETE_IMAGE = "eu.kanade.DELETE_IMAGE" private const val ACTION_DELETE_IMAGE = "eu.kanade.DELETE_IMAGE"
private const val EXTRA_FILE_LOCATION = "file_location" private const val EXTRA_FILE_LOCATION = "file_location"
private const val NOTIFICATION_ID = "notification_id" private const val NOTIFICATION_ID = "notification_id"
internal fun shareImageIntent(context: Context, path: String, notificationId: Int): PendingIntent { /**
val intent = Intent(context, ImageNotificationReceiver::class.java).apply { * Called to start share intent to share image
action = ACTION_SHARE_IMAGE *
putExtra(EXTRA_FILE_LOCATION, path) * @param context context of application
putExtra(NOTIFICATION_ID, notificationId) * @param path path of file
*/
internal fun shareImageIntent(context: Context, path: String): PendingIntent {
val intent = Intent(Intent.ACTION_SEND).apply {
val uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", File(path))
putExtra(Intent.EXTRA_STREAM, uri)
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION
type = "image/*"
} }
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
} }
/**
* Called to show image in gallery application
*
* @param context context of application
* @param path path of file
*/
internal fun showImageIntent(context: Context, path: String): PendingIntent { internal fun showImageIntent(context: Context, path: String): PendingIntent {
val intent = Intent(context, ImageNotificationReceiver::class.java).apply { val intent = Intent(Intent.ACTION_VIEW).apply {
action = ACTION_SHOW_IMAGE flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION
putExtra(EXTRA_FILE_LOCATION, path) val uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", File(path))
setDataAndType(uri, "image/*")
} }
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
} }
internal fun deleteImageIntent(context: Context, path: String, notificationId: Int): PendingIntent { internal fun deleteImageIntent(context: Context, path: String, notificationId: Int): PendingIntent {

View file

@ -62,7 +62,7 @@ class ImageNotifier(private val context: Context) {
// Share action // Share action
addAction(R.drawable.ic_share_grey_24dp, addAction(R.drawable.ic_share_grey_24dp,
context.getString(R.string.action_share), context.getString(R.string.action_share),
ImageNotificationReceiver.shareImageIntent(context, file.absolutePath, notificationId)) ImageNotificationReceiver.shareImageIntent(context, file.absolutePath))
// Delete action // Delete action
addAction(R.drawable.ic_delete_grey_24dp, addAction(R.drawable.ic_delete_grey_24dp,
context.getString(R.string.action_delete), context.getString(R.string.action_delete),

View file

@ -6,6 +6,11 @@
android:title="@string/action_download" android:title="@string/action_download"
android:visible="true" /> android:visible="true" />
<item
android:id="@+id/action_delete"
android:title="@string/action_delete"
android:visible="false"/>
<item android:id="@+id/action_bookmark" <item android:id="@+id/action_bookmark"
android:title="@string/action_bookmark" android:title="@string/action_bookmark"
android:visible="true" /> android:visible="true" />
@ -14,10 +19,6 @@
android:title="@string/action_remove_bookmark" android:title="@string/action_remove_bookmark"
android:visible="true" /> android:visible="true" />
<item android:id="@+id/action_delete"
android:title="@string/action_delete"
android:visible="false" />
<item android:id="@+id/action_mark_as_read" <item android:id="@+id/action_mark_as_read"
android:title="@string/action_mark_as_read" /> android:title="@string/action_mark_as_read" />