FilesExportWork: use getActivity for PendingIntent

With the broadcast, the notification drawer will not autoclose to show the file browser, which is not good UX.

With this approach the notification will not be autodismissed, but that's the lesser of two evils.

Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
This commit is contained in:
Álvaro Brey 2022-06-30 14:17:36 +02:00 committed by tobiasKaminsky
parent 1d69dcfcaa
commit db4e9887b1
No known key found for this signature in database
GPG key ID: 0E00D4D47D0C5AF7
2 changed files with 3 additions and 36 deletions

View file

@ -156,9 +156,6 @@
<receiver <receiver
android:name="com.nextcloud.client.jobs.NotificationWork$NotificationReceiver" android:name="com.nextcloud.client.jobs.NotificationWork$NotificationReceiver"
android:exported="false" /> android:exported="false" />
<receiver
android:name="com.nextcloud.client.jobs.FilesExportWork$NotificationReceiver"
android:exported="false" />
<activity <activity
android:name=".ui.activity.UploadFilesActivity" android:name=".ui.activity.UploadFilesActivity"

View file

@ -22,18 +22,14 @@
package com.nextcloud.client.jobs package com.nextcloud.client.jobs
import android.app.Activity
import android.app.DownloadManager import android.app.DownloadManager
import android.app.NotificationManager import android.app.NotificationManager
import android.app.PendingIntent import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.ContentResolver import android.content.ContentResolver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import android.content.pm.PackageManager
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import android.widget.Toast
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.work.Worker import androidx.work.Worker
import androidx.work.WorkerParameters import androidx.work.WorkerParameters
@ -154,10 +150,10 @@ class FilesExportWork(
.setContentText(message) .setContentText(message)
.setAutoCancel(true) .setAutoCancel(true)
val actionIntent = Intent(appContext, NotificationReceiver::class.java).apply { val actionIntent = Intent(DownloadManager.ACTION_VIEW_DOWNLOADS).apply {
putExtra(NUMERIC_NOTIFICATION_ID, notificationId) flags = FLAG_ACTIVITY_NEW_TASK
} }
val actionPendingIntent = PendingIntent.getBroadcast( val actionPendingIntent = PendingIntent.getActivity(
appContext, appContext,
notificationId, notificationId,
actionIntent, actionIntent,
@ -174,30 +170,4 @@ class FilesExportWork(
val notificationManager = appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val notificationManager = appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(notificationId, notificationBuilder.build()) notificationManager.notify(notificationId, notificationBuilder.build())
} }
class NotificationReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
// open file chooser
val openIntent = Intent(DownloadManager.ACTION_VIEW_DOWNLOADS).apply {
flags = FLAG_ACTIVITY_NEW_TASK
}
// check if intent can be resolved
if (context.packageManager.queryIntentActivities(openIntent, PackageManager.GET_RESOLVED_FILTER)
.isNotEmpty()
) {
context.startActivity(openIntent)
} else {
Toast.makeText(context, R.string.open_download_folder, Toast.LENGTH_LONG).show()
}
// remove notification
val numericNotificationId = intent.getIntExtra(NUMERIC_NOTIFICATION_ID, 0)
if (numericNotificationId != 0) {
val notificationManager = context.getSystemService(Activity.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.cancel(numericNotificationId)
}
}
}
} }