summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/android/util/DownloadsFileSaver.java
blob: 7db46fa9770c0b3d56c725eadbb1eb790a3c7f01 (plain)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * Copyright © 2019 WireGuard LLC. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

package com.wireguard.android.util;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;

import com.wireguard.android.R;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class DownloadsFileSaver {

    public static class DownloadsFile {
        private Context context;
        private OutputStream outputStream;
        private String fileName;
        private Uri uri;

        private DownloadsFile(final Context context, final OutputStream outputStream, final String fileName, final Uri uri) {
            this.context = context;
            this.outputStream = outputStream;
            this.fileName = fileName;
            this.uri = uri;
        }

        public OutputStream getOutputStream() { return outputStream; }
        public String getFileName() { return fileName; }

        public void delete() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
                context.getContentResolver().delete(uri, null, null);
            else
                new File(fileName).delete();
        }
    }

    public static DownloadsFile save(final Context context, final String name, final String mimeType, final boolean overwriteExisting) throws Exception {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            final ContentResolver contentResolver = context.getContentResolver();
            if (overwriteExisting)
                contentResolver.delete(MediaStore.Downloads.EXTERNAL_CONTENT_URI, String.format("%s = ?", MediaColumns.DISPLAY_NAME), new String[]{name});
            final ContentValues contentValues = new ContentValues();
            contentValues.put(MediaColumns.DISPLAY_NAME, name);
            contentValues.put(MediaColumns.MIME_TYPE, mimeType);
            final Uri contentUri = contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues);
            if (contentUri == null)
                throw new IOException(context.getString(R.string.create_downloads_file_error));
            final OutputStream contentStream = contentResolver.openOutputStream(contentUri);
            if (contentStream == null)
                throw new IOException(context.getString(R.string.create_downloads_file_error));
            @SuppressWarnings("deprecation")
            Cursor cursor = contentResolver.query(contentUri, new String[]{MediaColumns.DATA}, null, null, null);
            String path = null;
            if (cursor != null) {
                try {
                    if (cursor.moveToFirst())
                        path = cursor.getString(0);
                } finally {
                    cursor.close();
                }
            }
            if (path == null) {
                path = "Download/";
                cursor = contentResolver.query(contentUri, new String[]{MediaColumns.DISPLAY_NAME}, null, null, null);
                if (cursor != null) {
                    try {
                        if (cursor.moveToFirst())
                            path += cursor.getString(0);
                    } finally {
                        cursor.close();
                    }
                }
            }
            return new DownloadsFile(context, contentStream, path, contentUri);
        } else {
            @SuppressWarnings("deprecation")
            final File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
            final File file = new File(path, name);
            if (!path.isDirectory() && !path.mkdirs())
                throw new IOException(context.getString(R.string.create_output_dir_error));
            return new DownloadsFile(context, new FileOutputStream(file), file.getAbsolutePath(), null);
        }
    }
}