2 * Copyright © 2019-2020 Soren Stoutner <soren@stoutner.com>.
4 * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
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.
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.
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/>.
20 package com.stoutner.privacybrowser.helpers;
22 import android.net.Uri;
23 import android.os.Environment;
25 public class FileNameHelper {
26 public String convertUriToFileNamePath(Uri Uri) {
27 // Initialize a file name path string.
28 String fileNamePath = "";
30 // Convert the URI to a raw file name path.
31 String rawFileNamePath = Uri.getPath();
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);
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;
53 // Everything else for the primary user should be in `/document/primary`.
54 case "/document/primary":
55 fileNamePath = Environment.getExternalStorageDirectory() + "/" + fileNameFinalPath;
58 // Just in case, catch everything else and place it in the external storage directory.
60 fileNamePath = Environment.getExternalStorageDirectory() + "/" + fileNameFinalPath;
64 } else { // The path does not contain a `:`.
65 // Use the raw file name path.
66 fileNamePath = rawFileNamePath;
70 // Return the file name path string.