mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-01-23 01:06:21 +01:00
Download notification now handles >1 files
This commit is contained in:
parent
f6bc230a2c
commit
4ae69b3349
5 changed files with 80 additions and 58 deletions
|
@ -22,7 +22,8 @@ import kotlin.math.max
|
|||
* Show notification on device
|
||||
*
|
||||
* Methods:
|
||||
* fun notifyItemDownloadSuccessful(fileUri: String, mimeType: String): Unit
|
||||
* fun notifyItemsDownloadSuccessful(fileUris: List<String>,
|
||||
* mimeTypes: List<String>): Unit
|
||||
*/
|
||||
class NotificationChannelHandler(activity: Activity)
|
||||
: MethodChannel.MethodCallHandler {
|
||||
|
@ -38,10 +39,12 @@ class NotificationChannelHandler(activity: Activity)
|
|||
}
|
||||
|
||||
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
|
||||
if (call.method == "notifyItemDownloadSuccessful") {
|
||||
if (call.method == "notifyItemsDownloadSuccessful") {
|
||||
try {
|
||||
notifyItemDownloadSuccessful(call.argument<String>("fileUri")!!,
|
||||
call.argument<String>("mimeType")!!, result)
|
||||
notifyItemsDownloadSuccessful(
|
||||
call.argument<List<String>>("fileUris")!!,
|
||||
call.argument<List<String?>>("mimeTypes")!!,
|
||||
result)
|
||||
} catch (e: Throwable) {
|
||||
result.error("systemException", e.toString(), null)
|
||||
}
|
||||
|
@ -50,23 +53,66 @@ class NotificationChannelHandler(activity: Activity)
|
|||
}
|
||||
}
|
||||
|
||||
private fun notifyItemDownloadSuccessful(fileUri: String, mimeType: String,
|
||||
result: MethodChannel.Result) {
|
||||
val uriStr = fileUri
|
||||
val uri = Uri.parse(uriStr)
|
||||
private fun notifyItemsDownloadSuccessful(fileUris: List<String>,
|
||||
mimeTypes: List<String?>, result: MethodChannel.Result) {
|
||||
assert(fileUris.isNotEmpty())
|
||||
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 {
|
||||
action = Intent.ACTION_VIEW
|
||||
setDataAndType(uri, mimeType)
|
||||
setDataAndType(uris[0], mimeTypes[0])
|
||||
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
|
||||
}
|
||||
val openPendingIntent = PendingIntent.getActivity(_context, 0,
|
||||
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
|
||||
putExtra(Intent.EXTRA_STREAM, uri)
|
||||
type = mimeType
|
||||
putExtra(Intent.EXTRA_STREAM, uris[0])
|
||||
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_WRITE_URI_PERMISSION)
|
||||
}
|
||||
|
@ -74,34 +120,9 @@ class NotificationChannelHandler(activity: Activity)
|
|||
R.string.download_successful_notification_action_share_chooser))
|
||||
val sharePendingIntent = PendingIntent.getActivity(_context, 1,
|
||||
shareChooser, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
|
||||
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(
|
||||
builder.addAction(0, _context.getString(
|
||||
R.string.download_successful_notification_action_share),
|
||||
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)) {
|
||||
notify(DOWNLOAD_NOTIFICATION_ID, builder.build())
|
||||
|
|
|
@ -8,4 +8,5 @@
|
|||
<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_chooser">Share with:</string>
|
||||
<string name="download_multiple_successful_notification_title">Downloaded %1$s items successfully</string>
|
||||
</resources>
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
import 'package:flutter/services.dart';
|
||||
|
||||
class Notification {
|
||||
static Future<void> notifyItemDownloadSuccessful(
|
||||
String fileUri, String mimeType) =>
|
||||
_channel.invokeMethod("notifyItemDownloadSuccessful", <String, dynamic>{
|
||||
"fileUri": fileUri,
|
||||
"mimeType": mimeType,
|
||||
static Future<void> notifyItemsDownloadSuccessful(
|
||||
List<String> fileUris, List<String> mimeTypes) =>
|
||||
_channel.invokeMethod("notifyItemsDownloadSuccessful", <String, dynamic>{
|
||||
"fileUris": fileUris,
|
||||
"mimeTypes": mimeTypes,
|
||||
});
|
||||
|
||||
static const _channel =
|
||||
|
|
|
@ -3,13 +3,13 @@ import 'package:nc_photos/platform/notification.dart' as itf;
|
|||
|
||||
class AndroidItemDownloadSuccessfulNotification
|
||||
extends itf.ItemDownloadSuccessfulNotification {
|
||||
AndroidItemDownloadSuccessfulNotification(this.fileUri, this.mimeType);
|
||||
AndroidItemDownloadSuccessfulNotification(this.fileUris, this.mimeTypes);
|
||||
|
||||
@override
|
||||
Future<void> notify() {
|
||||
return Notification.notifyItemDownloadSuccessful(fileUri, mimeType);
|
||||
return Notification.notifyItemsDownloadSuccessful(fileUris, mimeTypes);
|
||||
}
|
||||
|
||||
final String fileUri;
|
||||
final String mimeType;
|
||||
final List<String> fileUris;
|
||||
final List<String> mimeTypes;
|
||||
}
|
||||
|
|
|
@ -586,8 +586,8 @@ class _ViewerState extends State<Viewer> {
|
|||
void _onDownloadSuccessful(File file, dynamic result) {
|
||||
var notif;
|
||||
if (platform_k.isAndroid) {
|
||||
notif =
|
||||
AndroidItemDownloadSuccessfulNotification(result, file.contentType);
|
||||
notif = AndroidItemDownloadSuccessfulNotification(
|
||||
[result], [file.contentType]);
|
||||
}
|
||||
if (notif != null) {
|
||||
try {
|
||||
|
|
Loading…
Reference in a new issue