aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml/docilealligator/infinityforreddit/broadcastreceivers/DownloadedMediaDeleteActionBroadcastReceiver.java
blob: 323c78eb64d28756a76124b0d24ea3c56fd7296a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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;

import androidx.documentfile.provider.DocumentFile;

import java.io.File;

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) {
            try {
                context.getContentResolver().delete(mediaUri, null, null);
            } catch (Exception e) {
                DocumentFile file = DocumentFile.fromSingleUri(context, mediaUri);
                if (file != null) {
                    if (!file.delete()) {
                        new File(mediaUri.toString()).delete();
                    }
                }
            }

        }

        NotificationManager manager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
        manager.cancel(intent.getIntExtra(EXTRA_NOTIFICATION_ID, -1));
    }
}