Download notification now handles >1 files

This commit is contained in:
Ming Ming 2021-07-10 22:18:04 +08:00
parent f6bc230a2c
commit 4ae69b3349
5 changed files with 80 additions and 58 deletions

View file

@ -22,7 +22,8 @@ import kotlin.math.max
* Show notification on device * Show notification on device
* *
* Methods: * Methods:
* fun notifyItemDownloadSuccessful(fileUri: String, mimeType: String): Unit * fun notifyItemsDownloadSuccessful(fileUris: List<String>,
* mimeTypes: List<String>): Unit
*/ */
class NotificationChannelHandler(activity: Activity) class NotificationChannelHandler(activity: Activity)
: MethodChannel.MethodCallHandler { : MethodChannel.MethodCallHandler {
@ -38,10 +39,12 @@ class NotificationChannelHandler(activity: Activity)
} }
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) { override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
if (call.method == "notifyItemDownloadSuccessful") { if (call.method == "notifyItemsDownloadSuccessful") {
try { try {
notifyItemDownloadSuccessful(call.argument<String>("fileUri")!!, notifyItemsDownloadSuccessful(
call.argument<String>("mimeType")!!, result) call.argument<List<String>>("fileUris")!!,
call.argument<List<String?>>("mimeTypes")!!,
result)
} catch (e: Throwable) { } catch (e: Throwable) {
result.error("systemException", e.toString(), null) result.error("systemException", e.toString(), null)
} }
@ -50,23 +53,66 @@ class NotificationChannelHandler(activity: Activity)
} }
} }
private fun notifyItemDownloadSuccessful(fileUri: String, mimeType: String, private fun notifyItemsDownloadSuccessful(fileUris: List<String>,
result: MethodChannel.Result) { mimeTypes: List<String?>, result: MethodChannel.Result) {
val uriStr = fileUri assert(fileUris.isNotEmpty())
val uri = Uri.parse(uriStr) assert(fileUris.size == mimeTypes.size)
val uris = fileUris.map { Uri.parse(it) }
val builder = NotificationCompat.Builder(_context, DOWNLOAD_CHANNEL_ID)
.setSmallIcon(R.drawable.baseline_download_white_18)
.setWhen(System.currentTimeMillis())
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSound(RingtoneManager.getDefaultUri(
RingtoneManager.TYPE_NOTIFICATION))
.setOnlyAlertOnce(true)
.setAutoCancel(true)
.setLocalOnly(true)
if (uris.size == 1) {
builder.setTicker(_context.getString(
R.string.download_successful_notification_title))
.setContentTitle(_context.getString(
R.string.download_successful_notification_title))
.setContentText(_context.getString(
R.string.download_successful_notification_text))
val openIntent = Intent().apply { val openIntent = Intent().apply {
action = Intent.ACTION_VIEW action = Intent.ACTION_VIEW
setDataAndType(uri, mimeType) setDataAndType(uris[0], mimeTypes[0])
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION) addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
} }
val openPendingIntent = PendingIntent.getActivity(_context, 0, val openPendingIntent = PendingIntent.getActivity(_context, 0,
openIntent, PendingIntent.FLAG_UPDATE_CURRENT) openIntent, PendingIntent.FLAG_UPDATE_CURRENT)
builder.setContentIntent(openPendingIntent)
val shareIntent = Intent().apply { // show preview if available
if (mimeTypes[0]?.startsWith("image/") == true) {
val preview = loadNotificationImage(uris[0])
if (preview != null) {
builder.setStyle(NotificationCompat.BigPictureStyle()
.bigPicture(loadNotificationImage(uris[0])))
}
}
} else {
val title = _context.getString(
R.string.download_multiple_successful_notification_title,
fileUris.size)
builder.setTicker(title)
.setContentTitle(title)
}
val shareIntent = if (uris.size == 1) Intent().apply {
action = Intent.ACTION_SEND action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_STREAM, uri) putExtra(Intent.EXTRA_STREAM, uris[0])
type = mimeType type = mimeTypes[0] ?: "*/*"
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
} else Intent().apply {
action = Intent.ACTION_SEND_MULTIPLE
putParcelableArrayListExtra(Intent.EXTRA_STREAM, ArrayList(uris))
type = if (mimeTypes.all { it?.startsWith("image/") == true })
"image/*" else "*/*"
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION) addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
} }
@ -74,34 +120,9 @@ class NotificationChannelHandler(activity: Activity)
R.string.download_successful_notification_action_share_chooser)) R.string.download_successful_notification_action_share_chooser))
val sharePendingIntent = PendingIntent.getActivity(_context, 1, val sharePendingIntent = PendingIntent.getActivity(_context, 1,
shareChooser, PendingIntent.FLAG_UPDATE_CURRENT) shareChooser, PendingIntent.FLAG_UPDATE_CURRENT)
builder.addAction(0, _context.getString(
val builder = NotificationCompat.Builder(_context, DOWNLOAD_CHANNEL_ID)
.setSmallIcon(R.drawable.baseline_download_white_18)
.setTicker(_context.getString(
R.string.download_successful_notification_title))
.setContentTitle(_context.getString(
R.string.download_successful_notification_title))
.setContentText(_context.getString(
R.string.download_successful_notification_text))
.setWhen(System.currentTimeMillis())
.setContentIntent(openPendingIntent)
.addAction(0, _context.getString(
R.string.download_successful_notification_action_share), R.string.download_successful_notification_action_share),
sharePendingIntent) sharePendingIntent)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSound(RingtoneManager.getDefaultUri(
RingtoneManager.TYPE_NOTIFICATION))
.setOnlyAlertOnce(false)
.setAutoCancel(true)
.setLocalOnly(true)
// show preview if available
val preview = if (mimeType.startsWith("image/"))
loadNotificationImage(uri) else null;
if (preview != null) {
builder.setStyle(NotificationCompat.BigPictureStyle()
.bigPicture(loadNotificationImage(uri)))
}
with(NotificationManagerCompat.from(_context)) { with(NotificationManagerCompat.from(_context)) {
notify(DOWNLOAD_NOTIFICATION_ID, builder.build()) notify(DOWNLOAD_NOTIFICATION_ID, builder.build())

View file

@ -8,4 +8,5 @@
<string name="download_successful_notification_text">Tap to view your downloaded item</string> <string name="download_successful_notification_text">Tap to view your downloaded item</string>
<string name="download_successful_notification_action_share">SHARE</string> <string name="download_successful_notification_action_share">SHARE</string>
<string name="download_successful_notification_action_share_chooser">Share with:</string> <string name="download_successful_notification_action_share_chooser">Share with:</string>
<string name="download_multiple_successful_notification_title">Downloaded %1$s items successfully</string>
</resources> </resources>

View file

@ -1,11 +1,11 @@
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
class Notification { class Notification {
static Future<void> notifyItemDownloadSuccessful( static Future<void> notifyItemsDownloadSuccessful(
String fileUri, String mimeType) => List<String> fileUris, List<String> mimeTypes) =>
_channel.invokeMethod("notifyItemDownloadSuccessful", <String, dynamic>{ _channel.invokeMethod("notifyItemsDownloadSuccessful", <String, dynamic>{
"fileUri": fileUri, "fileUris": fileUris,
"mimeType": mimeType, "mimeTypes": mimeTypes,
}); });
static const _channel = static const _channel =

View file

@ -3,13 +3,13 @@ import 'package:nc_photos/platform/notification.dart' as itf;
class AndroidItemDownloadSuccessfulNotification class AndroidItemDownloadSuccessfulNotification
extends itf.ItemDownloadSuccessfulNotification { extends itf.ItemDownloadSuccessfulNotification {
AndroidItemDownloadSuccessfulNotification(this.fileUri, this.mimeType); AndroidItemDownloadSuccessfulNotification(this.fileUris, this.mimeTypes);
@override @override
Future<void> notify() { Future<void> notify() {
return Notification.notifyItemDownloadSuccessful(fileUri, mimeType); return Notification.notifyItemsDownloadSuccessful(fileUris, mimeTypes);
} }
final String fileUri; final List<String> fileUris;
final String mimeType; final List<String> mimeTypes;
} }

View file

@ -586,8 +586,8 @@ class _ViewerState extends State<Viewer> {
void _onDownloadSuccessful(File file, dynamic result) { void _onDownloadSuccessful(File file, dynamic result) {
var notif; var notif;
if (platform_k.isAndroid) { if (platform_k.isAndroid) {
notif = notif = AndroidItemDownloadSuccessfulNotification(
AndroidItemDownloadSuccessfulNotification(result, file.contentType); [result], [file.contentType]);
} }
if (notif != null) { if (notif != null) {
try { try {