Add a notification action to delete the media after downloading.

This commit is contained in:
Docile-Alligator 2022-05-31 18:00:21 +08:00
parent 7f91f89599
commit db08be065e
4 changed files with 41 additions and 2 deletions

View File

@ -445,6 +445,8 @@
android:name=".services.EditProfileService"
android:enabled="true"
android:exported="false" />
<receiver android:name=".broadcastreceivers.DownloadedMediaDeleteActionBroadcastReceiver" />
</application>
</manifest>

View File

@ -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));
}
}

View File

@ -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());
}

View File

@ -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());
}