aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml
diff options
context:
space:
mode:
authorDocile-Alligator <chineseperson5@gmail.com>2022-05-31 10:00:21 +0000
committerDocile-Alligator <chineseperson5@gmail.com>2022-05-31 10:00:21 +0000
commitdb08be065ecf3e164ee9271c542f4b7ce7a4ac04 (patch)
treece6c52dc5f5de26bc566b58b23981aef66b7325b /app/src/main/java/ml
parent7f91f895992383d1f1fb89124bd6f706cc6b24a3 (diff)
downloadinfinity-for-reddit-db08be065ecf3e164ee9271c542f4b7ce7a4ac04.tar
infinity-for-reddit-db08be065ecf3e164ee9271c542f4b7ce7a4ac04.tar.gz
infinity-for-reddit-db08be065ecf3e164ee9271c542f4b7ce7a4ac04.tar.bz2
infinity-for-reddit-db08be065ecf3e164ee9271c542f4b7ce7a4ac04.tar.lz
infinity-for-reddit-db08be065ecf3e164ee9271c542f4b7ce7a4ac04.tar.xz
infinity-for-reddit-db08be065ecf3e164ee9271c542f4b7ce7a4ac04.tar.zst
infinity-for-reddit-db08be065ecf3e164ee9271c542f4b7ce7a4ac04.zip
Add a notification action to delete the media after downloading.
Diffstat (limited to 'app/src/main/java/ml')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/DownloadedMediaDeleteActionBroadcastReceiver.java23
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadMediaService.java7
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/services/DownloadRedditVideoService.java11
3 files changed, 39 insertions, 2 deletions
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());
}