1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.documentsui.util;
18 
19 import androidx.annotation.NonNull;
20 
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.Objects;
24 
25 public class FileUtils {
26 
27     /**
28      * Returns the canonical pathname string of the provided abstract pathname.
29      *
30      * @return The canonical pathname string denoting the same file or directory as this abstract
31      *         pathname.
32      * @see File#getCanonicalPath()
33      */
34     @NonNull
getCanonicalPath(@onNull String path)35     public static String getCanonicalPath(@NonNull String path) throws IOException {
36         Objects.requireNonNull(path);
37         return new File(path).getCanonicalPath();
38     }
39 
40     /**
41      * This is basically a very slightly tweaked fork of
42      * {@link com.android.externalstorage.ExternalStorageProvider#getPathFromDocId(String)}.
43      * The difference between this fork and the "original" method is that here we do not strip
44      * the leading and trailing "/"s (because we don't worry about those).
45      *
46      * @return canonicalized file path.
47      */
getPathFromStorageDocId(String docId)48     public static String getPathFromStorageDocId(String docId) throws IOException {
49         // Remove the root tag from the docId, e.g. "primary:", which should leave with the file
50         // path.
51         final String docIdPath = docId.substring(docId.indexOf(':', 1) + 1);
52 
53         return getCanonicalPath(docIdPath);
54     }
55 
FileUtils()56     private FileUtils() {
57     }
58 }
59