]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/helpers/DownloadLocationHelper.java
Add a download location preference. https://redmine.stoutner.com/issues/32
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / helpers / DownloadLocationHelper.java
1 /*
2  * Copyright © 2020 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
6  * Privacy Browser is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Privacy Browser is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package com.stoutner.privacybrowser.helpers;
21
22 import android.Manifest;
23 import android.content.Context;
24 import android.content.SharedPreferences;
25 import android.content.pm.PackageManager;
26 import android.os.Environment;
27
28 import androidx.core.content.ContextCompat;
29 import androidx.preference.PreferenceManager;
30
31 import com.stoutner.privacybrowser.R;
32
33 import java.io.File;
34
35 public class DownloadLocationHelper {
36     public String getDownloadLocation(Context context) {
37         // Get the download location entry values string array.
38         String[] downloadLocationEntryValuesStringArray = context.getResources().getStringArray(R.array.download_location_entry_values);
39
40         // Get the two standard download directories.
41         File publicDownloadDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
42         File publicAppFilesDirectory = context.getExternalFilesDir(null);
43
44         // Remove the incorrect lint warning below that the public app files directory might be null.
45         assert publicAppFilesDirectory != null;
46
47         // Convert the download directories to strings.
48         String publicDownloadDirectoryString = publicDownloadDirectory.toString();
49         String publicAppFilesDirectoryString = publicAppFilesDirectory.toString();
50
51         // Get a handle for the shared preferences.
52         SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
53
54         // Get the download location strings from the preferences.
55         String downloadLocationString = sharedPreferences.getString("download_location", context.getString(R.string.download_location_default_value));
56         String downloadCustomLocationString = sharedPreferences.getString("download_custom_location", context.getString(R.string.download_custom_location_default_value));
57
58         // Define a string for the default file path.
59         String defaultFilePath;
60
61         // Set the default file path according to the download location.
62         if (downloadLocationString.equals(downloadLocationEntryValuesStringArray[0])) {  // the download location is set to auto.
63             // Set the download location summary text according to the storage permission status.
64             if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {  // The storage permission has been granted.
65                 // Use the public download directory.
66                 defaultFilePath = publicDownloadDirectoryString;
67             } else {  // The storage permission has not been granted.
68                 // Use the public app files directory.
69                 defaultFilePath = publicAppFilesDirectoryString;
70             }
71         } else if (downloadLocationString.equals(downloadLocationEntryValuesStringArray[1])) {  // The download location is set to the app directory.
72             // Use the public app files directory.
73             defaultFilePath = publicAppFilesDirectoryString;
74         } else if (downloadLocationString.equals(downloadLocationEntryValuesStringArray[2])) {  // The download location is set to the public directory.
75             // Use the public download directory.
76             defaultFilePath = publicDownloadDirectoryString;
77         } else {  // The download location is set to custom.
78             // Use the download custom location.
79             defaultFilePath = downloadCustomLocationString;
80         }
81
82         // Return the default file path.
83         return defaultFilePath;
84     }
85 }