From db08be065ecf3e164ee9271c542f4b7ce7a4ac04 Mon Sep 17 00:00:00 2001 From: Docile-Alligator Date: Tue, 31 May 2022 18:00:21 +0800 Subject: Add a notification action to delete the media after downloading. --- app/src/main/AndroidManifest.xml | 2 ++ ...wnloadedMediaDeleteActionBroadcastReceiver.java | 23 ++++++++++++++++++++++ .../services/DownloadMediaService.java | 7 +++++++ .../services/DownloadRedditVideoService.java | 11 +++++++++-- 4 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/DownloadedMediaDeleteActionBroadcastReceiver.java (limited to 'app/src/main') diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 26fd8b3f..654a9708 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -445,6 +445,8 @@ android:name=".services.EditProfileService" android:enabled="true" android:exported="false" /> + + \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/DownloadedMediaDeleteActionBroadcastReceiver.java b/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/DownloadedMediaDeleteActionBroadcastReceiver.java new file mode 100644 index 00000000..22e2e2f1 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/DownloadedMediaDeleteActionBroadcastReceiver.java @@ -0,0 +1,23 @@ +package ml.docilealligator.infinityforreddit.broadcastreceivers; + +import static android.content.Context.NOTIFICATION_SERVICE; + +import android.app.NotificationManager; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.net.Uri; + +public class DownloadedMediaDeleteActionBroadcastReceiver extends BroadcastReceiver { + public static final String EXTRA_NOTIFICATION_ID = "ENI"; + @Override + public void onReceive(Context context, Intent intent) { + Uri mediaUri = intent.getData(); + if (mediaUri != null) { + context.getContentResolver().delete(mediaUri, null, null); + } + + NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); + manager.cancel(intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1)); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadMediaService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadMediaService.java index 770433d0..bc3c3352 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadMediaService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadMediaService.java @@ -43,6 +43,7 @@ import ml.docilealligator.infinityforreddit.DownloadProgressResponseBody; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.apis.DownloadFile; +import ml.docilealligator.infinityforreddit.broadcastreceivers.DownloadedMediaDeleteActionBroadcastReceiver; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.DownloadMediaEvent; import ml.docilealligator.infinityforreddit.utils.NotificationUtils; @@ -436,6 +437,12 @@ public class DownloadMediaService extends Service { PendingIntent shareActionPendingIntent = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.getActivity(this, 1, intentAction, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE) : PendingIntent.getActivity(this, 1, intentAction, PendingIntent.FLAG_CANCEL_CURRENT); builder.addAction(new NotificationCompat.Action(R.drawable.ic_notification, getString(R.string.share), shareActionPendingIntent)); + + Intent deleteIntent = new Intent(this, DownloadedMediaDeleteActionBroadcastReceiver.class); + deleteIntent.setData(mediaUri); + deleteIntent.putExtra(DownloadedMediaDeleteActionBroadcastReceiver.EXTRA_NOTIFICATION_ID, getNotificationId(mediaType, randomNotificationIdOffset)); + PendingIntent deleteActionPendingIntent = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.getBroadcast(this, 2, deleteIntent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE) : PendingIntent.getBroadcast(this, 2, deleteIntent, PendingIntent.FLAG_CANCEL_CURRENT); + builder.addAction(new NotificationCompat.Action(R.drawable.ic_notification, getString(R.string.delete), deleteActionPendingIntent)); } notificationManager.notify(getNotificationId(mediaType, randomNotificationIdOffset), builder.build()); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java b/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java index 65949d19..3f49d6c7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java @@ -1,5 +1,7 @@ package ml.docilealligator.infinityforreddit.services; +import static android.os.Environment.getExternalStoragePublicDirectory; + import android.app.Notification; import android.app.PendingIntent; import android.app.Service; @@ -47,6 +49,7 @@ import ml.docilealligator.infinityforreddit.DownloadProgressResponseBody; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.apis.DownloadFile; +import ml.docilealligator.infinityforreddit.broadcastreceivers.DownloadedMediaDeleteActionBroadcastReceiver; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.DownloadRedditVideoEvent; import ml.docilealligator.infinityforreddit.utils.NotificationUtils; @@ -56,8 +59,6 @@ import okhttp3.ResponseBody; import retrofit2.Response; import retrofit2.Retrofit; -import static android.os.Environment.getExternalStoragePublicDirectory; - public class DownloadRedditVideoService extends Service { public static final String EXTRA_VIDEO_URL = "EVU"; @@ -609,6 +610,12 @@ public class DownloadRedditVideoService extends Service { Intent intentAction = Intent.createChooser(shareIntent, getString(R.string.share)); PendingIntent shareActionPendingIntent = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.getActivity(this, 1, intentAction, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE) : PendingIntent.getActivity(this, 1, intentAction, PendingIntent.FLAG_CANCEL_CURRENT); builder.addAction(new NotificationCompat.Action(R.drawable.ic_notification, getString(R.string.share), shareActionPendingIntent)); + + Intent deleteIntent = new Intent(this, DownloadedMediaDeleteActionBroadcastReceiver.class); + deleteIntent.setData(mediaUri); + deleteIntent.putExtra(DownloadedMediaDeleteActionBroadcastReceiver.EXTRA_NOTIFICATION_ID, NotificationUtils.DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID + randomNotificationIdOffset); + PendingIntent deleteActionPendingIntent = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? PendingIntent.getBroadcast(this, 2, deleteIntent, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE) : PendingIntent.getBroadcast(this, 2, deleteIntent, PendingIntent.FLAG_CANCEL_CURRENT); + builder.addAction(new NotificationCompat.Action(R.drawable.ic_notification, getString(R.string.delete), deleteActionPendingIntent)); } notificationManager.notify(NotificationUtils.DOWNLOAD_REDDIT_VIDEO_NOTIFICATION_ID + randomNotificationIdOffset, builder.build()); } -- cgit v1.2.3