]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/helpers/FileNameHelper.java
5eb194842c18def2bb775b33f8d2187a2776d42c
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / helpers / FileNameHelper.java
1 /*
2  * Copyright © 2019 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.net.Uri;
23 import android.os.Environment;
24
25 public class FileNameHelper {
26     public String convertUriToFileNamePath(Uri Uri) {
27         // Initialize a file name path string.
28         String fileNamePath = "";
29
30         // Convert the URI to a raw file name path.
31         String rawFileNamePath = Uri.getPath();
32
33         // Only process the raw file name path if it is not null.
34         if (rawFileNamePath != null) {
35             // Check to see if the file name Path includes a valid storage location.
36             if (rawFileNamePath.contains(":")) {  // The path contains a `:`.
37                 // Split the path into the initial content uri and the final path information.
38                 String fileNameContentPath = rawFileNamePath.substring(0, rawFileNamePath.indexOf(":"));
39                 String fileNameFinalPath = rawFileNamePath.substring(rawFileNamePath.indexOf(":") + 1);
40
41                 // Check to see if the current file name final patch is a complete, valid path
42                 if (fileNameFinalPath.startsWith("/storage/emulated/")) {  // The existing file name final path is a complete, valid path.
43                     // Use the provided file name path as is.
44                     fileNamePath = fileNameFinalPath;
45                 } else { // The existing file name final path is not a complete, valid path.
46                     // Construct the file name path.
47                     switch (fileNameContentPath) {
48                         // The documents home has a special content path.
49                         case "/document/home":
50                             fileNamePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/" + fileNameFinalPath;
51                             break;
52
53                         // Everything else for the primary user should be in `/document/primary`.
54                         case "/document/primary":
55                             fileNamePath = Environment.getExternalStorageDirectory() + "/" + fileNameFinalPath;
56                             break;
57
58                         // Just in case, catch everything else and place it in the external storage directory.
59                         default:
60                             fileNamePath = Environment.getExternalStorageDirectory() + "/" + fileNameFinalPath;
61                             break;
62                     }
63                 }
64             } else {  // The path does not contain a `:`.
65                 // Use the raw file name path.
66                 fileNamePath = rawFileNamePath;
67             }
68         }
69
70         // Return the file name path string.
71         return fileNamePath;
72     }
73 }