The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 android.os; |
| 18 | |
Jeff Sharkey | a30e5c3 | 2019-02-28 12:02:10 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Jeff Sharkey | 70eb34a | 2018-09-13 11:57:03 -0600 | [diff] [blame] | 20 | import android.annotation.SystemApi; |
Philip P. Moltmann | f80809f | 2018-04-04 11:20:44 -0700 | [diff] [blame] | 21 | import android.annotation.TestApi; |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 22 | import android.annotation.UnsupportedAppUsage; |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 23 | import android.app.admin.DevicePolicyManager; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 25 | import android.os.storage.StorageManager; |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 26 | import android.os.storage.StorageVolume; |
Jeff Sharkey | bd0e9e4 | 2015-04-30 16:04:50 -0700 | [diff] [blame] | 27 | import android.text.TextUtils; |
Kenny Root | b2278dc | 2011-01-18 13:03:28 -0800 | [diff] [blame] | 28 | import android.util.Log; |
San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 29 | |
Gilles Debunne | ee1d630 | 2011-05-13 10:09:32 -0700 | [diff] [blame] | 30 | import java.io.File; |
Jeff Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 31 | import java.util.LinkedList; |
Gilles Debunne | ee1d630 | 2011-05-13 10:09:32 -0700 | [diff] [blame] | 32 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | /** |
| 34 | * Provides access to environment variables. |
| 35 | */ |
| 36 | public class Environment { |
Kenny Root | b2278dc | 2011-01-18 13:03:28 -0800 | [diff] [blame] | 37 | private static final String TAG = "Environment"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | |
Jeff Sharkey | dd02e33 | 2018-06-27 14:41:57 -0600 | [diff] [blame] | 39 | // NOTE: keep credential-protected paths in sync with StrictMode.java |
| 40 | |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 41 | private static final String ENV_EXTERNAL_STORAGE = "EXTERNAL_STORAGE"; |
Jeff Sharkey | 63d0a06 | 2013-03-01 16:12:55 -0800 | [diff] [blame] | 42 | private static final String ENV_ANDROID_ROOT = "ANDROID_ROOT"; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 43 | private static final String ENV_ANDROID_DATA = "ANDROID_DATA"; |
Jeff Sharkey | cf3f0a1 | 2016-03-17 19:57:58 -0600 | [diff] [blame] | 44 | private static final String ENV_ANDROID_EXPAND = "ANDROID_EXPAND"; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 45 | private static final String ENV_ANDROID_STORAGE = "ANDROID_STORAGE"; |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 46 | private static final String ENV_DOWNLOAD_CACHE = "DOWNLOAD_CACHE"; |
Jeff Sharkey | 1be762c | 2014-03-06 09:56:23 -0800 | [diff] [blame] | 47 | private static final String ENV_OEM_ROOT = "OEM_ROOT"; |
Hung-ying Tyan | bdc9d58 | 2015-11-20 11:53:39 +0800 | [diff] [blame] | 48 | private static final String ENV_ODM_ROOT = "ODM_ROOT"; |
Christopher Tate | 740888f | 2014-04-18 12:24:57 -0700 | [diff] [blame] | 49 | private static final String ENV_VENDOR_ROOT = "VENDOR_ROOT"; |
Jaekyun Seok | 1713d9e | 2018-01-12 21:47:26 +0900 | [diff] [blame] | 50 | private static final String ENV_PRODUCT_ROOT = "PRODUCT_ROOT"; |
Dario Freni | 2bef176 | 2018-06-01 14:02:08 +0100 | [diff] [blame] | 51 | private static final String ENV_PRODUCT_SERVICES_ROOT = "PRODUCT_SERVICES_ROOT"; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 52 | |
Jeff Sharkey | dfa4530 | 2012-09-12 16:25:22 -0700 | [diff] [blame] | 53 | /** {@hide} */ |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 54 | public static final String DIR_ANDROID = "Android"; |
Sudheer Shanka | e93db51 | 2019-01-11 16:05:28 -0800 | [diff] [blame] | 55 | private static final String DIR_SANDBOX = "sandbox"; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 56 | private static final String DIR_DATA = "data"; |
| 57 | private static final String DIR_MEDIA = "media"; |
| 58 | private static final String DIR_OBB = "obb"; |
| 59 | private static final String DIR_FILES = "files"; |
| 60 | private static final String DIR_CACHE = "cache"; |
| 61 | |
| 62 | /** {@hide} */ |
| 63 | @Deprecated |
| 64 | public static final String DIRECTORY_ANDROID = DIR_ANDROID; |
Jeff Sharkey | dfa4530 | 2012-09-12 16:25:22 -0700 | [diff] [blame] | 65 | |
Jeff Sharkey | 63d0a06 | 2013-03-01 16:12:55 -0800 | [diff] [blame] | 66 | private static final File DIR_ANDROID_ROOT = getDirectory(ENV_ANDROID_ROOT, "/system"); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 67 | private static final File DIR_ANDROID_DATA = getDirectory(ENV_ANDROID_DATA, "/data"); |
Jeff Sharkey | cf3f0a1 | 2016-03-17 19:57:58 -0600 | [diff] [blame] | 68 | private static final File DIR_ANDROID_EXPAND = getDirectory(ENV_ANDROID_EXPAND, "/mnt/expand"); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 69 | private static final File DIR_ANDROID_STORAGE = getDirectory(ENV_ANDROID_STORAGE, "/storage"); |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 70 | private static final File DIR_DOWNLOAD_CACHE = getDirectory(ENV_DOWNLOAD_CACHE, "/cache"); |
Jeff Sharkey | 1be762c | 2014-03-06 09:56:23 -0800 | [diff] [blame] | 71 | private static final File DIR_OEM_ROOT = getDirectory(ENV_OEM_ROOT, "/oem"); |
Hung-ying Tyan | bdc9d58 | 2015-11-20 11:53:39 +0800 | [diff] [blame] | 72 | private static final File DIR_ODM_ROOT = getDirectory(ENV_ODM_ROOT, "/odm"); |
Christopher Tate | 740888f | 2014-04-18 12:24:57 -0700 | [diff] [blame] | 73 | private static final File DIR_VENDOR_ROOT = getDirectory(ENV_VENDOR_ROOT, "/vendor"); |
Jaekyun Seok | 1713d9e | 2018-01-12 21:47:26 +0900 | [diff] [blame] | 74 | private static final File DIR_PRODUCT_ROOT = getDirectory(ENV_PRODUCT_ROOT, "/product"); |
Dario Freni | 2bef176 | 2018-06-01 14:02:08 +0100 | [diff] [blame] | 75 | private static final File DIR_PRODUCT_SERVICES_ROOT = getDirectory(ENV_PRODUCT_SERVICES_ROOT, |
| 76 | "/product_services"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 78 | @UnsupportedAppUsage |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 79 | private static UserEnvironment sCurrentUser; |
| 80 | private static boolean sUserRequired; |
Kenny Root | e1ff214 | 2010-10-12 11:20:01 -0700 | [diff] [blame] | 81 | |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 82 | static { |
| 83 | initForCurrentUser(); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** {@hide} */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 87 | @UnsupportedAppUsage |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 88 | public static void initForCurrentUser() { |
| 89 | final int userId = UserHandle.myUserId(); |
| 90 | sCurrentUser = new UserEnvironment(userId); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /** {@hide} */ |
| 94 | public static class UserEnvironment { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 95 | private final int mUserId; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 96 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 97 | @UnsupportedAppUsage |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 98 | public UserEnvironment(int userId) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 99 | mUserId = userId; |
| 100 | } |
Jeff Sharkey | 44cbdec | 2013-10-07 16:49:47 -0700 | [diff] [blame] | 101 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 102 | @UnsupportedAppUsage |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 103 | public File[] getExternalDirs() { |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 104 | final StorageVolume[] volumes = StorageManager.getVolumeList(mUserId, |
| 105 | StorageManager.FLAG_FOR_WRITE); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 106 | final File[] files = new File[volumes.length]; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 107 | for (int i = 0; i < volumes.length; i++) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 108 | files[i] = volumes[i].getPathFile(); |
Jeff Sharkey | 3fe5bf6 | 2012-09-18 15:54:52 -0700 | [diff] [blame] | 109 | } |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 110 | return files; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 113 | @UnsupportedAppUsage |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 114 | @Deprecated |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 115 | public File getExternalStorageDirectory() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 116 | return getExternalDirs()[0]; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 119 | @UnsupportedAppUsage |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 120 | @Deprecated |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 121 | public File getExternalStoragePublicDirectory(String type) { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 122 | return buildExternalStoragePublicDirs(type)[0]; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 125 | public File[] buildExternalStoragePublicDirs(String type) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 126 | return buildPaths(getExternalDirs(), type); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Sudheer Shanka | e93db51 | 2019-01-11 16:05:28 -0800 | [diff] [blame] | 129 | public File[] buildExternalStorageAndroidSandboxDirs() { |
| 130 | return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_SANDBOX); |
| 131 | } |
| 132 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 133 | public File[] buildExternalStorageAndroidDataDirs() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 134 | return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 137 | public File[] buildExternalStorageAndroidObbDirs() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 138 | return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 141 | public File[] buildExternalStorageAppDataDirs(String packageName) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 142 | return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 143 | } |
Jeff Sharkey | 3fe5bf6 | 2012-09-18 15:54:52 -0700 | [diff] [blame] | 144 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 145 | public File[] buildExternalStorageAppMediaDirs(String packageName) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 146 | return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_MEDIA, packageName); |
Jeff Sharkey | 2ee3c1e | 2014-05-30 15:38:35 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 149 | public File[] buildExternalStorageAppObbDirs(String packageName) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 150 | return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_OBB, packageName); |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | public File[] buildExternalStorageAppFilesDirs(String packageName) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 154 | return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_FILES); |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | public File[] buildExternalStorageAppCacheDirs(String packageName) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 158 | return buildPaths(getExternalDirs(), DIR_ANDROID, DIR_DATA, packageName, DIR_CACHE); |
Jeff Sharkey | 3fe5bf6 | 2012-09-18 15:54:52 -0700 | [diff] [blame] | 159 | } |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 160 | } |
San Mehat | 7fd0fee | 2009-12-17 07:12:23 -0800 | [diff] [blame] | 161 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | /** |
Jeff Sharkey | 1be762c | 2014-03-06 09:56:23 -0800 | [diff] [blame] | 163 | * Return root of the "system" partition holding the core Android OS. |
| 164 | * Always present and mounted read-only. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 165 | */ |
Jeff Sharkey | a30e5c3 | 2019-02-28 12:02:10 -0700 | [diff] [blame] | 166 | public static @NonNull File getRootDirectory() { |
Jeff Sharkey | 63d0a06 | 2013-03-01 16:12:55 -0800 | [diff] [blame] | 167 | return DIR_ANDROID_ROOT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 170 | /** {@hide} */ |
Jeff Sharkey | c609116 | 2018-06-29 17:15:40 -0600 | [diff] [blame] | 171 | @TestApi |
Jeff Sharkey | a30e5c3 | 2019-02-28 12:02:10 -0700 | [diff] [blame] | 172 | public static @NonNull File getStorageDirectory() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 173 | return DIR_ANDROID_STORAGE; |
| 174 | } |
| 175 | |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 176 | /** |
Jeff Sharkey | 1be762c | 2014-03-06 09:56:23 -0800 | [diff] [blame] | 177 | * Return root directory of the "oem" partition holding OEM customizations, |
| 178 | * if any. If present, the partition is mounted read-only. |
| 179 | * |
| 180 | * @hide |
| 181 | */ |
Jeff Sharkey | 70eb34a | 2018-09-13 11:57:03 -0600 | [diff] [blame] | 182 | @SystemApi |
Jeff Sharkey | a30e5c3 | 2019-02-28 12:02:10 -0700 | [diff] [blame] | 183 | public static @NonNull File getOemDirectory() { |
Jeff Sharkey | 1be762c | 2014-03-06 09:56:23 -0800 | [diff] [blame] | 184 | return DIR_OEM_ROOT; |
| 185 | } |
| 186 | |
| 187 | /** |
Hung-ying Tyan | bdc9d58 | 2015-11-20 11:53:39 +0800 | [diff] [blame] | 188 | * Return root directory of the "odm" partition holding ODM customizations, |
| 189 | * if any. If present, the partition is mounted read-only. |
| 190 | * |
| 191 | * @hide |
| 192 | */ |
Jeff Sharkey | 70eb34a | 2018-09-13 11:57:03 -0600 | [diff] [blame] | 193 | @SystemApi |
Jeff Sharkey | a30e5c3 | 2019-02-28 12:02:10 -0700 | [diff] [blame] | 194 | public static @NonNull File getOdmDirectory() { |
Hung-ying Tyan | bdc9d58 | 2015-11-20 11:53:39 +0800 | [diff] [blame] | 195 | return DIR_ODM_ROOT; |
| 196 | } |
| 197 | |
| 198 | /** |
Christopher Tate | 740888f | 2014-04-18 12:24:57 -0700 | [diff] [blame] | 199 | * Return root directory of the "vendor" partition that holds vendor-provided |
| 200 | * software that should persist across simple reflashing of the "system" partition. |
| 201 | * @hide |
| 202 | */ |
Jeff Sharkey | 70eb34a | 2018-09-13 11:57:03 -0600 | [diff] [blame] | 203 | @SystemApi |
Jeff Sharkey | a30e5c3 | 2019-02-28 12:02:10 -0700 | [diff] [blame] | 204 | public static @NonNull File getVendorDirectory() { |
Christopher Tate | 740888f | 2014-04-18 12:24:57 -0700 | [diff] [blame] | 205 | return DIR_VENDOR_ROOT; |
| 206 | } |
| 207 | |
Jason parks | a3cdaa5 | 2011-01-13 14:15:43 -0600 | [diff] [blame] | 208 | /** |
Jaekyun Seok | 1713d9e | 2018-01-12 21:47:26 +0900 | [diff] [blame] | 209 | * Return root directory of the "product" partition holding product-specific |
| 210 | * customizations if any. If present, the partition is mounted read-only. |
| 211 | * |
| 212 | * @hide |
| 213 | */ |
Jeff Sharkey | 70eb34a | 2018-09-13 11:57:03 -0600 | [diff] [blame] | 214 | @SystemApi |
Anton Hansson | 09e47be | 2018-11-08 12:56:18 +0000 | [diff] [blame] | 215 | @TestApi |
Jeff Sharkey | a30e5c3 | 2019-02-28 12:02:10 -0700 | [diff] [blame] | 216 | public static @NonNull File getProductDirectory() { |
Jaekyun Seok | 1713d9e | 2018-01-12 21:47:26 +0900 | [diff] [blame] | 217 | return DIR_PRODUCT_ROOT; |
| 218 | } |
| 219 | |
| 220 | /** |
Dario Freni | 2bef176 | 2018-06-01 14:02:08 +0100 | [diff] [blame] | 221 | * Return root directory of the "product_services" partition holding middleware |
| 222 | * services if any. If present, the partition is mounted read-only. |
| 223 | * |
| 224 | * @hide |
| 225 | */ |
Jeff Sharkey | 70eb34a | 2018-09-13 11:57:03 -0600 | [diff] [blame] | 226 | @SystemApi |
Jeff Sharkey | a30e5c3 | 2019-02-28 12:02:10 -0700 | [diff] [blame] | 227 | public static @NonNull File getProductServicesDirectory() { |
Dario Freni | 2bef176 | 2018-06-01 14:02:08 +0100 | [diff] [blame] | 228 | return DIR_PRODUCT_SERVICES_ROOT; |
| 229 | } |
| 230 | |
| 231 | /** |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 232 | * Return the system directory for a user. This is for use by system |
| 233 | * services to store files relating to the user. This directory will be |
| 234 | * automatically deleted when the user is removed. |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 235 | * |
Jeff Sharkey | 3a6a61e | 2018-10-03 10:45:51 -0600 | [diff] [blame] | 236 | * @deprecated This directory is valid and still exists, but but callers |
| 237 | * should <em>strongly</em> consider switching to using either |
| 238 | * {@link #getDataSystemCeDirectory(int)} or |
| 239 | * {@link #getDataSystemDeDirectory(int)}, both of which support |
| 240 | * fast user wipe. |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 241 | * @hide |
| 242 | */ |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 243 | @Deprecated |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 244 | public static File getUserSystemDirectory(int userId) { |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 245 | return new File(new File(getDataSystemDirectory(), "users"), Integer.toString(userId)); |
Amith Yamasani | 61f5737 | 2012-08-31 12:12:28 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /** |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 249 | * Returns the config directory for a user. This is for use by system |
| 250 | * services to store files relating to the user which should be readable by |
| 251 | * any app running as that user. |
Robin Lee | 6959133 | 2014-04-28 16:03:22 +0100 | [diff] [blame] | 252 | * |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 253 | * @deprecated This directory is valid and still exists, but callers should |
| 254 | * <em>strongly</em> consider switching to |
| 255 | * {@link #getDataMiscCeDirectory(int)} which is protected with |
| 256 | * user credentials or {@link #getDataMiscDeDirectory(int)} |
| 257 | * which supports fast user wipe. |
Robin Lee | 6959133 | 2014-04-28 16:03:22 +0100 | [diff] [blame] | 258 | * @hide |
| 259 | */ |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 260 | @Deprecated |
Robin Lee | 6959133 | 2014-04-28 16:03:22 +0100 | [diff] [blame] | 261 | public static File getUserConfigDirectory(int userId) { |
| 262 | return new File(new File(new File( |
| 263 | getDataDirectory(), "misc"), "user"), Integer.toString(userId)); |
| 264 | } |
| 265 | |
| 266 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 267 | * Return the user data directory. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | */ |
| 269 | public static File getDataDirectory() { |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 270 | return DIR_ANDROID_DATA; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 271 | } |
| 272 | |
Jeff Sharkey | bd0e9e4 | 2015-04-30 16:04:50 -0700 | [diff] [blame] | 273 | /** {@hide} */ |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 274 | public static File getDataDirectory(String volumeUuid) { |
Jeff Sharkey | bd0e9e4 | 2015-04-30 16:04:50 -0700 | [diff] [blame] | 275 | if (TextUtils.isEmpty(volumeUuid)) { |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 276 | return DIR_ANDROID_DATA; |
Jeff Sharkey | bd0e9e4 | 2015-04-30 16:04:50 -0700 | [diff] [blame] | 277 | } else { |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 278 | return new File("/mnt/expand/" + volumeUuid); |
Jeff Sharkey | bd0e9e4 | 2015-04-30 16:04:50 -0700 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 282 | /** {@hide} */ |
Jeff Sharkey | cf3f0a1 | 2016-03-17 19:57:58 -0600 | [diff] [blame] | 283 | public static File getExpandDirectory() { |
| 284 | return DIR_ANDROID_EXPAND; |
| 285 | } |
| 286 | |
| 287 | /** {@hide} */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 288 | @UnsupportedAppUsage |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 289 | public static File getDataSystemDirectory() { |
| 290 | return new File(getDataDirectory(), "system"); |
| 291 | } |
| 292 | |
Amith Yamasani | d04aaa3 | 2016-06-13 12:09:36 -0700 | [diff] [blame] | 293 | /** |
| 294 | * Returns the base directory for per-user system directory, device encrypted. |
| 295 | * {@hide} |
| 296 | */ |
| 297 | public static File getDataSystemDeDirectory() { |
| 298 | return buildPath(getDataDirectory(), "system_de"); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Returns the base directory for per-user system directory, credential encrypted. |
| 303 | * {@hide} |
| 304 | */ |
| 305 | public static File getDataSystemCeDirectory() { |
| 306 | return buildPath(getDataDirectory(), "system_ce"); |
| 307 | } |
| 308 | |
Jeff Sharkey | 3a6a61e | 2018-10-03 10:45:51 -0600 | [diff] [blame] | 309 | /** |
| 310 | * Return the "credential encrypted" system directory for a user. This is |
| 311 | * for use by system services to store files relating to the user. This |
| 312 | * directory supports fast user wipe, and will be automatically deleted when |
| 313 | * the user is removed. |
| 314 | * <p> |
| 315 | * Data stored under this path is "credential encrypted", which uses an |
| 316 | * encryption key that is entangled with user credentials, such as a PIN or |
| 317 | * password. The contents will only be available once the user has been |
| 318 | * unlocked, as reported by {@code SystemService.onUnlockUser()}. |
| 319 | * <p> |
| 320 | * New code should <em>strongly</em> prefer storing sensitive data in these |
| 321 | * credential encrypted areas. |
| 322 | * |
| 323 | * @hide |
| 324 | */ |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 325 | public static File getDataSystemCeDirectory(int userId) { |
| 326 | return buildPath(getDataDirectory(), "system_ce", String.valueOf(userId)); |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Jeff Sharkey | 3a6a61e | 2018-10-03 10:45:51 -0600 | [diff] [blame] | 329 | /** |
| 330 | * Return the "device encrypted" system directory for a user. This is for |
| 331 | * use by system services to store files relating to the user. This |
| 332 | * directory supports fast user wipe, and will be automatically deleted when |
| 333 | * the user is removed. |
| 334 | * <p> |
| 335 | * Data stored under this path is "device encrypted", which uses an |
| 336 | * encryption key that is tied to the physical device. The contents will |
| 337 | * only be available once the device has finished a {@code dm-verity} |
| 338 | * protected boot. |
| 339 | * <p> |
| 340 | * New code should <em>strongly</em> avoid storing sensitive data in these |
| 341 | * device encrypted areas. |
| 342 | * |
| 343 | * @hide |
| 344 | */ |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 345 | public static File getDataSystemDeDirectory(int userId) { |
| 346 | return buildPath(getDataDirectory(), "system_de", String.valueOf(userId)); |
| 347 | } |
| 348 | |
| 349 | /** {@hide} */ |
| 350 | public static File getDataMiscDirectory() { |
| 351 | return new File(getDataDirectory(), "misc"); |
| 352 | } |
| 353 | |
| 354 | /** {@hide} */ |
Amith Yamasani | a280713 | 2017-01-26 12:35:14 -0800 | [diff] [blame] | 355 | public static File getDataMiscCeDirectory() { |
| 356 | return buildPath(getDataDirectory(), "misc_ce"); |
| 357 | } |
| 358 | |
| 359 | /** {@hide} */ |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 360 | public static File getDataMiscCeDirectory(int userId) { |
| 361 | return buildPath(getDataDirectory(), "misc_ce", String.valueOf(userId)); |
| 362 | } |
| 363 | |
| 364 | /** {@hide} */ |
| 365 | public static File getDataMiscDeDirectory(int userId) { |
| 366 | return buildPath(getDataDirectory(), "misc_de", String.valueOf(userId)); |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Calin Juravle | d479b52 | 2016-02-24 16:22:03 +0000 | [diff] [blame] | 369 | private static File getDataProfilesDeDirectory(int userId) { |
| 370 | return buildPath(getDataDirectory(), "misc", "profiles", "cur", String.valueOf(userId)); |
| 371 | } |
| 372 | |
| 373 | /** {@hide} */ |
Andreas Huber | 7fe2053 | 2018-01-22 11:26:44 -0800 | [diff] [blame] | 374 | public static File getDataVendorCeDirectory(int userId) { |
| 375 | return buildPath(getDataDirectory(), "vendor_ce", String.valueOf(userId)); |
| 376 | } |
| 377 | |
| 378 | /** {@hide} */ |
| 379 | public static File getDataVendorDeDirectory(int userId) { |
| 380 | return buildPath(getDataDirectory(), "vendor_de", String.valueOf(userId)); |
| 381 | } |
| 382 | |
| 383 | /** {@hide} */ |
Calin Juravle | 6ae39fc | 2018-01-19 20:32:47 -0800 | [diff] [blame] | 384 | public static File getDataRefProfilesDePackageDirectory(String packageName) { |
| 385 | return buildPath(getDataDirectory(), "misc", "profiles", "ref", packageName); |
Calin Juravle | 0bd7762 | 2016-07-12 15:56:41 +0100 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | /** {@hide} */ |
Calin Juravle | d479b52 | 2016-02-24 16:22:03 +0000 | [diff] [blame] | 389 | public static File getDataProfilesDePackageDirectory(int userId, String packageName) { |
| 390 | return buildPath(getDataProfilesDeDirectory(userId), packageName); |
| 391 | } |
| 392 | |
| 393 | /** {@hide} */ |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 394 | public static File getDataAppDirectory(String volumeUuid) { |
| 395 | return new File(getDataDirectory(volumeUuid), "app"); |
| 396 | } |
| 397 | |
| 398 | /** {@hide} */ |
Dario Freni | a8f4b13 | 2018-12-30 00:36:49 +0000 | [diff] [blame] | 399 | public static File getDataStagingDirectory(String volumeUuid) { |
Gavin Corkery | 296c8b9 | 2019-02-27 12:06:24 +0000 | [diff] [blame] | 400 | return new File(getDataDirectory(volumeUuid), "app-staging"); |
Dario Freni | a8f4b13 | 2018-12-30 00:36:49 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | /** {@hide} */ |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 404 | public static File getDataUserCeDirectory(String volumeUuid) { |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 405 | return new File(getDataDirectory(volumeUuid), "user"); |
| 406 | } |
| 407 | |
| 408 | /** {@hide} */ |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 409 | public static File getDataUserCeDirectory(String volumeUuid, int userId) { |
| 410 | return new File(getDataUserCeDirectory(volumeUuid), String.valueOf(userId)); |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | /** {@hide} */ |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 414 | public static File getDataUserCePackageDirectory(String volumeUuid, int userId, |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 415 | String packageName) { |
| 416 | // TODO: keep consistent with installd |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 417 | return new File(getDataUserCeDirectory(volumeUuid, userId), packageName); |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /** {@hide} */ |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 421 | public static File getDataUserDeDirectory(String volumeUuid) { |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 422 | return new File(getDataDirectory(volumeUuid), "user_de"); |
| 423 | } |
| 424 | |
| 425 | /** {@hide} */ |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 426 | public static File getDataUserDeDirectory(String volumeUuid, int userId) { |
| 427 | return new File(getDataUserDeDirectory(volumeUuid), String.valueOf(userId)); |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /** {@hide} */ |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 431 | public static File getDataUserDePackageDirectory(String volumeUuid, int userId, |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 432 | String packageName) { |
| 433 | // TODO: keep consistent with installd |
Jeff Sharkey | 8212ae0 | 2016-02-10 14:46:43 -0700 | [diff] [blame] | 434 | return new File(getDataUserDeDirectory(volumeUuid, userId), packageName); |
Jeff Sharkey | 6dce496 | 2015-07-03 18:08:41 -0700 | [diff] [blame] | 435 | } |
| 436 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | /** |
Fyodor Kupolov | 8825758 | 2016-05-24 16:06:56 -0700 | [diff] [blame] | 438 | * Return preloads directory. |
| 439 | * <p>This directory may contain pre-loaded content such as |
| 440 | * {@link #getDataPreloadsDemoDirectory() demo videos} and |
| 441 | * {@link #getDataPreloadsAppsDirectory() APK files} . |
| 442 | * {@hide} |
| 443 | */ |
| 444 | public static File getDataPreloadsDirectory() { |
| 445 | return new File(getDataDirectory(), "preloads"); |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * @see #getDataPreloadsDirectory() |
| 450 | * {@hide} |
| 451 | */ |
| 452 | public static File getDataPreloadsDemoDirectory() { |
| 453 | return new File(getDataPreloadsDirectory(), "demo"); |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * @see #getDataPreloadsDirectory() |
| 458 | * {@hide} |
| 459 | */ |
| 460 | public static File getDataPreloadsAppsDirectory() { |
| 461 | return new File(getDataPreloadsDirectory(), "apps"); |
| 462 | } |
| 463 | |
| 464 | /** |
Fyodor Kupolov | 19551a8 | 2016-08-22 14:46:08 -0700 | [diff] [blame] | 465 | * @see #getDataPreloadsDirectory() |
| 466 | * {@hide} |
| 467 | */ |
| 468 | public static File getDataPreloadsMediaDirectory() { |
| 469 | return new File(getDataPreloadsDirectory(), "media"); |
| 470 | } |
| 471 | |
| 472 | /** |
Fyodor Kupolov | 6e68706 | 2016-09-09 17:42:12 -0700 | [diff] [blame] | 473 | * Returns location of preloaded cache directory for package name |
| 474 | * @see #getDataPreloadsDirectory() |
| 475 | * {@hide} |
| 476 | */ |
| 477 | public static File getDataPreloadsFileCacheDirectory(String packageName) { |
| 478 | return new File(getDataPreloadsFileCacheDirectory(), packageName); |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Returns location of preloaded cache directory. |
| 483 | * @see #getDataPreloadsDirectory() |
| 484 | * {@hide} |
| 485 | */ |
| 486 | public static File getDataPreloadsFileCacheDirectory() { |
| 487 | return new File(getDataPreloadsDirectory(), "file_cache"); |
| 488 | } |
| 489 | |
| 490 | /** |
Sudheer Shanka | be0febe | 2018-11-07 18:24:37 -0800 | [diff] [blame] | 491 | * Returns location of packages cache directory. |
| 492 | * {@hide} |
| 493 | */ |
| 494 | public static File getPackageCacheDirectory() { |
| 495 | return new File(getDataSystemDirectory(), "package_cache"); |
| 496 | } |
| 497 | |
| 498 | /** |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 499 | * Return the primary shared/external storage directory. This directory may |
| 500 | * not currently be accessible if it has been mounted by the user on their |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 501 | * computer, has been removed from the device, or some other problem has |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 502 | * happened. You can determine its current state with |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 503 | * {@link #getExternalStorageState()}. |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 504 | * <p> |
| 505 | * <em>Note: don't be confused by the word "external" here. This directory |
| 506 | * can better be thought as media/shared storage. It is a filesystem that |
| 507 | * can hold a relatively large amount of data and that is shared across all |
| 508 | * applications (does not enforce permissions). Traditionally this is an SD |
| 509 | * card, but it may also be implemented as built-in storage in a device that |
| 510 | * is distinct from the protected internal storage and can be mounted as a |
| 511 | * filesystem on a computer.</em> |
| 512 | * <p> |
| 513 | * On devices with multiple users (as described by {@link UserManager}), |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 514 | * each user has their own isolated shared storage. Applications only have |
| 515 | * access to the shared storage for the user they're running as. |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 516 | * <p> |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 517 | * In devices with multiple shared/external storage directories, this |
| 518 | * directory represents the primary storage that the user will interact |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 519 | * with. Access to secondary storage is available through |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 520 | * {@link Context#getExternalFilesDirs(String)}, |
| 521 | * {@link Context#getExternalCacheDirs()}, and |
| 522 | * {@link Context#getExternalMediaDirs()}. |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 523 | * <p> |
| 524 | * Applications should not directly use this top-level directory, in order |
| 525 | * to avoid polluting the user's root namespace. Any files that are private |
| 526 | * to the application should be placed in a directory returned by |
| 527 | * {@link android.content.Context#getExternalFilesDir |
Dianne Hackborn | acaf028 | 2010-03-30 14:39:35 -0700 | [diff] [blame] | 528 | * Context.getExternalFilesDir}, which the system will take care of deleting |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 529 | * if the application is uninstalled. Other shared files should be placed in |
| 530 | * one of the directories returned by |
| 531 | * {@link #getExternalStoragePublicDirectory}. |
| 532 | * <p> |
| 533 | * Writing to this path requires the |
| 534 | * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permission, |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 535 | * and starting in {@link android.os.Build.VERSION_CODES#KITKAT}, read access requires the |
Jeff Sharkey | 8c16579 | 2012-10-22 14:08:29 -0700 | [diff] [blame] | 536 | * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} permission, |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 537 | * which is automatically granted if you hold the write permission. |
| 538 | * <p> |
Chet Haase | e8222dd | 2013-09-05 07:44:18 -0700 | [diff] [blame] | 539 | * Starting in {@link android.os.Build.VERSION_CODES#KITKAT}, if your |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 540 | * application only needs to store internal data, consider using |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 541 | * {@link Context#getExternalFilesDir(String)}, |
| 542 | * {@link Context#getExternalCacheDir()}, or |
| 543 | * {@link Context#getExternalMediaDirs()}, which require no permissions to |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 544 | * read or write. |
| 545 | * <p> |
| 546 | * This path may change between platform versions, so applications should |
| 547 | * only persist relative paths. |
| 548 | * <p> |
| 549 | * Here is an example of typical code to monitor the state of external |
| 550 | * storage: |
| 551 | * <p> |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 552 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 553 | * monitor_storage} |
Dianne Hackborn | 407f625 | 2010-10-04 11:31:17 -0700 | [diff] [blame] | 554 | * |
| 555 | * @see #getExternalStorageState() |
| 556 | * @see #isExternalStorageRemovable() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 557 | */ |
| 558 | public static File getExternalStorageDirectory() { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 559 | throwIfUserRequired(); |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 560 | return sCurrentUser.getExternalDirs()[0]; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | /** {@hide} */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 564 | @UnsupportedAppUsage |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 565 | public static File getLegacyExternalStorageDirectory() { |
| 566 | return new File(System.getenv(ENV_EXTERNAL_STORAGE)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 567 | } |
| 568 | |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 569 | /** {@hide} */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 570 | @UnsupportedAppUsage |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 571 | public static File getLegacyExternalStorageObbDirectory() { |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 572 | return buildPath(getLegacyExternalStorageDirectory(), DIR_ANDROID, DIR_OBB); |
Jeff Sharkey | 4fbbda4 | 2012-09-24 18:34:07 -0700 | [diff] [blame] | 573 | } |
| 574 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 575 | /** |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 576 | * Standard directory in which to place any audio files that should be |
| 577 | * in the regular list of music for the user. |
| 578 | * This may be combined with |
| 579 | * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, |
| 580 | * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series |
| 581 | * of directories to categories a particular audio file as more than one |
| 582 | * type. |
| 583 | */ |
| 584 | public static String DIRECTORY_MUSIC = "Music"; |
Felipe Leme | c7b1f89 | 2016-01-15 15:02:31 -0800 | [diff] [blame] | 585 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 586 | /** |
| 587 | * Standard directory in which to place any audio files that should be |
| 588 | * in the list of podcasts that the user can select (not as regular |
| 589 | * music). |
| 590 | * This may be combined with {@link #DIRECTORY_MUSIC}, |
| 591 | * {@link #DIRECTORY_NOTIFICATIONS}, |
| 592 | * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series |
| 593 | * of directories to categories a particular audio file as more than one |
| 594 | * type. |
| 595 | */ |
| 596 | public static String DIRECTORY_PODCASTS = "Podcasts"; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 597 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 598 | /** |
| 599 | * Standard directory in which to place any audio files that should be |
| 600 | * in the list of ringtones that the user can select (not as regular |
| 601 | * music). |
| 602 | * This may be combined with {@link #DIRECTORY_MUSIC}, |
| 603 | * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, and |
| 604 | * {@link #DIRECTORY_ALARMS} as a series |
| 605 | * of directories to categories a particular audio file as more than one |
| 606 | * type. |
| 607 | */ |
| 608 | public static String DIRECTORY_RINGTONES = "Ringtones"; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 609 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 610 | /** |
| 611 | * Standard directory in which to place any audio files that should be |
| 612 | * in the list of alarms that the user can select (not as regular |
| 613 | * music). |
| 614 | * This may be combined with {@link #DIRECTORY_MUSIC}, |
| 615 | * {@link #DIRECTORY_PODCASTS}, {@link #DIRECTORY_NOTIFICATIONS}, |
| 616 | * and {@link #DIRECTORY_RINGTONES} as a series |
| 617 | * of directories to categories a particular audio file as more than one |
| 618 | * type. |
| 619 | */ |
| 620 | public static String DIRECTORY_ALARMS = "Alarms"; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 621 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 622 | /** |
| 623 | * Standard directory in which to place any audio files that should be |
| 624 | * in the list of notifications that the user can select (not as regular |
| 625 | * music). |
| 626 | * This may be combined with {@link #DIRECTORY_MUSIC}, |
| 627 | * {@link #DIRECTORY_PODCASTS}, |
| 628 | * {@link #DIRECTORY_ALARMS}, and {@link #DIRECTORY_RINGTONES} as a series |
| 629 | * of directories to categories a particular audio file as more than one |
| 630 | * type. |
| 631 | */ |
| 632 | public static String DIRECTORY_NOTIFICATIONS = "Notifications"; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 633 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 634 | /** |
| 635 | * Standard directory in which to place pictures that are available to |
| 636 | * the user. Note that this is primarily a convention for the top-level |
| 637 | * public directory, as the media scanner will find and collect pictures |
| 638 | * in any directory. |
| 639 | */ |
| 640 | public static String DIRECTORY_PICTURES = "Pictures"; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 641 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 642 | /** |
| 643 | * Standard directory in which to place movies that are available to |
| 644 | * the user. Note that this is primarily a convention for the top-level |
| 645 | * public directory, as the media scanner will find and collect movies |
| 646 | * in any directory. |
| 647 | */ |
| 648 | public static String DIRECTORY_MOVIES = "Movies"; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 649 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 650 | /** |
| 651 | * Standard directory in which to place files that have been downloaded by |
| 652 | * the user. Note that this is primarily a convention for the top-level |
| 653 | * public directory, you are free to download files anywhere in your own |
Dianne Hackborn | ce59fb8 | 2010-04-07 17:19:04 -0700 | [diff] [blame] | 654 | * private directories. Also note that though the constant here is |
| 655 | * named DIRECTORY_DOWNLOADS (plural), the actual file name is non-plural for |
| 656 | * backwards compatibility reasons. |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 657 | */ |
Dianne Hackborn | ce59fb8 | 2010-04-07 17:19:04 -0700 | [diff] [blame] | 658 | public static String DIRECTORY_DOWNLOADS = "Download"; |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 659 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 660 | /** |
| 661 | * The traditional location for pictures and videos when mounting the |
| 662 | * device as a camera. Note that this is primarily a convention for the |
| 663 | * top-level public directory, as this convention makes no sense elsewhere. |
| 664 | */ |
| 665 | public static String DIRECTORY_DCIM = "DCIM"; |
Jeff Sharkey | 3e1189b | 2013-09-12 21:59:06 -0700 | [diff] [blame] | 666 | |
| 667 | /** |
| 668 | * Standard directory in which to place documents that have been created by |
| 669 | * the user. |
| 670 | */ |
| 671 | public static String DIRECTORY_DOCUMENTS = "Documents"; |
| 672 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 673 | /** |
Jeff Sharkey | c8e4924 | 2018-11-02 14:34:44 -0600 | [diff] [blame] | 674 | * Standard directory in which to place screenshots that have been taken by |
| 675 | * the user. Typically used as a secondary directory under |
| 676 | * {@link #DIRECTORY_PICTURES}. |
| 677 | */ |
| 678 | public static String DIRECTORY_SCREENSHOTS = "Screenshots"; |
| 679 | |
| 680 | /** |
Jeff Sharkey | 10887d5 | 2018-11-30 13:44:41 -0700 | [diff] [blame] | 681 | * Standard directory in which to place any audio files which are |
| 682 | * audiobooks. |
| 683 | */ |
| 684 | public static String DIRECTORY_AUDIOBOOKS = "Audiobooks"; |
| 685 | |
| 686 | /** |
Felipe Leme | c7b1f89 | 2016-01-15 15:02:31 -0800 | [diff] [blame] | 687 | * List of standard storage directories. |
| 688 | * <p> |
| 689 | * Each of its values have its own constant: |
| 690 | * <ul> |
| 691 | * <li>{@link #DIRECTORY_MUSIC} |
| 692 | * <li>{@link #DIRECTORY_PODCASTS} |
| 693 | * <li>{@link #DIRECTORY_ALARMS} |
| 694 | * <li>{@link #DIRECTORY_RINGTONES} |
| 695 | * <li>{@link #DIRECTORY_NOTIFICATIONS} |
| 696 | * <li>{@link #DIRECTORY_PICTURES} |
| 697 | * <li>{@link #DIRECTORY_MOVIES} |
| 698 | * <li>{@link #DIRECTORY_DOWNLOADS} |
| 699 | * <li>{@link #DIRECTORY_DCIM} |
| 700 | * <li>{@link #DIRECTORY_DOCUMENTS} |
Jeff Sharkey | 10887d5 | 2018-11-30 13:44:41 -0700 | [diff] [blame] | 701 | * <li>{@link #DIRECTORY_AUDIOBOOKS} |
Felipe Leme | c7b1f89 | 2016-01-15 15:02:31 -0800 | [diff] [blame] | 702 | * </ul> |
| 703 | * @hide |
| 704 | */ |
Felipe Leme | 3e166b2 | 2016-02-24 10:17:41 -0800 | [diff] [blame] | 705 | public static final String[] STANDARD_DIRECTORIES = { |
Felipe Leme | c7b1f89 | 2016-01-15 15:02:31 -0800 | [diff] [blame] | 706 | DIRECTORY_MUSIC, |
| 707 | DIRECTORY_PODCASTS, |
| 708 | DIRECTORY_RINGTONES, |
| 709 | DIRECTORY_ALARMS, |
| 710 | DIRECTORY_NOTIFICATIONS, |
| 711 | DIRECTORY_PICTURES, |
| 712 | DIRECTORY_MOVIES, |
| 713 | DIRECTORY_DOWNLOADS, |
| 714 | DIRECTORY_DCIM, |
Jeff Sharkey | 10887d5 | 2018-11-30 13:44:41 -0700 | [diff] [blame] | 715 | DIRECTORY_DOCUMENTS, |
| 716 | DIRECTORY_AUDIOBOOKS, |
Felipe Leme | c7b1f89 | 2016-01-15 15:02:31 -0800 | [diff] [blame] | 717 | }; |
| 718 | |
| 719 | /** |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 720 | * @hide |
| 721 | */ |
| 722 | public static boolean isStandardDirectory(String dir) { |
| 723 | for (String valid : STANDARD_DIRECTORIES) { |
| 724 | if (valid.equals(dir)) { |
| 725 | return true; |
| 726 | } |
| 727 | } |
| 728 | return false; |
| 729 | } |
| 730 | |
Jeff Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 731 | /** {@hide} */ public static final int HAS_MUSIC = 1 << 0; |
| 732 | /** {@hide} */ public static final int HAS_PODCASTS = 1 << 1; |
| 733 | /** {@hide} */ public static final int HAS_RINGTONES = 1 << 2; |
| 734 | /** {@hide} */ public static final int HAS_ALARMS = 1 << 3; |
| 735 | /** {@hide} */ public static final int HAS_NOTIFICATIONS = 1 << 4; |
| 736 | /** {@hide} */ public static final int HAS_PICTURES = 1 << 5; |
| 737 | /** {@hide} */ public static final int HAS_MOVIES = 1 << 6; |
| 738 | /** {@hide} */ public static final int HAS_DOWNLOADS = 1 << 7; |
| 739 | /** {@hide} */ public static final int HAS_DCIM = 1 << 8; |
| 740 | /** {@hide} */ public static final int HAS_DOCUMENTS = 1 << 9; |
Jeff Sharkey | 10887d5 | 2018-11-30 13:44:41 -0700 | [diff] [blame] | 741 | /** {@hide} */ public static final int HAS_AUDIOBOOKS = 1 << 10; |
Jeff Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 742 | |
| 743 | /** {@hide} */ public static final int HAS_ANDROID = 1 << 16; |
| 744 | /** {@hide} */ public static final int HAS_OTHER = 1 << 17; |
| 745 | |
| 746 | /** |
| 747 | * Classify the content types present on the given external storage device. |
| 748 | * <p> |
| 749 | * This is typically useful for deciding if an inserted SD card is empty, or |
| 750 | * if it contains content like photos that should be preserved. |
| 751 | * |
| 752 | * @hide |
| 753 | */ |
| 754 | public static int classifyExternalStorageDirectory(File dir) { |
| 755 | int res = 0; |
| 756 | for (File f : FileUtils.listFilesOrEmpty(dir)) { |
| 757 | if (f.isFile() && isInterestingFile(f)) { |
| 758 | res |= HAS_OTHER; |
| 759 | } else if (f.isDirectory() && hasInterestingFiles(f)) { |
| 760 | final String name = f.getName(); |
| 761 | if (DIRECTORY_MUSIC.equals(name)) res |= HAS_MUSIC; |
| 762 | else if (DIRECTORY_PODCASTS.equals(name)) res |= HAS_PODCASTS; |
| 763 | else if (DIRECTORY_RINGTONES.equals(name)) res |= HAS_RINGTONES; |
| 764 | else if (DIRECTORY_ALARMS.equals(name)) res |= HAS_ALARMS; |
| 765 | else if (DIRECTORY_NOTIFICATIONS.equals(name)) res |= HAS_NOTIFICATIONS; |
| 766 | else if (DIRECTORY_PICTURES.equals(name)) res |= HAS_PICTURES; |
| 767 | else if (DIRECTORY_MOVIES.equals(name)) res |= HAS_MOVIES; |
| 768 | else if (DIRECTORY_DOWNLOADS.equals(name)) res |= HAS_DOWNLOADS; |
| 769 | else if (DIRECTORY_DCIM.equals(name)) res |= HAS_DCIM; |
| 770 | else if (DIRECTORY_DOCUMENTS.equals(name)) res |= HAS_DOCUMENTS; |
Jeff Sharkey | 10887d5 | 2018-11-30 13:44:41 -0700 | [diff] [blame] | 771 | else if (DIRECTORY_AUDIOBOOKS.equals(name)) res |= HAS_AUDIOBOOKS; |
Jeff Sharkey | 2035614 | 2017-12-06 15:22:05 -0700 | [diff] [blame] | 772 | else if (DIRECTORY_ANDROID.equals(name)) res |= HAS_ANDROID; |
| 773 | else res |= HAS_OTHER; |
| 774 | } |
| 775 | } |
| 776 | return res; |
| 777 | } |
| 778 | |
| 779 | private static boolean hasInterestingFiles(File dir) { |
| 780 | final LinkedList<File> explore = new LinkedList<>(); |
| 781 | explore.add(dir); |
| 782 | while (!explore.isEmpty()) { |
| 783 | dir = explore.pop(); |
| 784 | for (File f : FileUtils.listFilesOrEmpty(dir)) { |
| 785 | if (isInterestingFile(f)) return true; |
| 786 | if (f.isDirectory()) explore.add(f); |
| 787 | } |
| 788 | } |
| 789 | return false; |
| 790 | } |
| 791 | |
| 792 | private static boolean isInterestingFile(File file) { |
| 793 | if (file.isFile()) { |
| 794 | final String name = file.getName().toLowerCase(); |
| 795 | if (name.endsWith(".exe") || name.equals("autorun.inf") |
| 796 | || name.equals("launchpad.zip") || name.equals(".nomedia")) { |
| 797 | return false; |
| 798 | } else { |
| 799 | return true; |
| 800 | } |
| 801 | } else { |
| 802 | return false; |
| 803 | } |
| 804 | } |
| 805 | |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 806 | /** |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 807 | * Get a top-level shared/external storage directory for placing files of a |
| 808 | * particular type. This is where the user will typically place and manage |
| 809 | * their own files, so you should be careful about what you put here to |
| 810 | * ensure you don't erase their files or get in the way of their own |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 811 | * organization. |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 812 | * <p> |
| 813 | * On devices with multiple users (as described by {@link UserManager}), |
| 814 | * each user has their own isolated shared storage. Applications only have |
| 815 | * access to the shared storage for the user they're running as. |
| 816 | * </p> |
| 817 | * <p> |
| 818 | * Here is an example of typical code to manipulate a picture on the public |
| 819 | * shared storage: |
| 820 | * </p> |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 821 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/content/ExternalStorage.java |
| 822 | * public_picture} |
Felipe Leme | c7b1f89 | 2016-01-15 15:02:31 -0800 | [diff] [blame] | 823 | * |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 824 | * @param type The type of storage directory to return. Should be one of |
| 825 | * {@link #DIRECTORY_MUSIC}, {@link #DIRECTORY_PODCASTS}, |
| 826 | * {@link #DIRECTORY_RINGTONES}, {@link #DIRECTORY_ALARMS}, |
| 827 | * {@link #DIRECTORY_NOTIFICATIONS}, {@link #DIRECTORY_PICTURES}, |
Felipe Leme | c7b1f89 | 2016-01-15 15:02:31 -0800 | [diff] [blame] | 828 | * {@link #DIRECTORY_MOVIES}, {@link #DIRECTORY_DOWNLOADS}, |
| 829 | * {@link #DIRECTORY_DCIM}, or {@link #DIRECTORY_DOCUMENTS}. May not be null. |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 830 | * @return Returns the File path for the directory. Note that this directory |
| 831 | * may not yet exist, so you must make sure it exists before using |
| 832 | * it such as with {@link File#mkdirs File.mkdirs()}. |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 833 | */ |
| 834 | public static File getExternalStoragePublicDirectory(String type) { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 835 | throwIfUserRequired(); |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 836 | return sCurrentUser.buildExternalStoragePublicDirs(type)[0]; |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | /** |
| 840 | * Returns the path for android-specific data on the SD card. |
| 841 | * @hide |
| 842 | */ |
Sudheer Shanka | e93db51 | 2019-01-11 16:05:28 -0800 | [diff] [blame] | 843 | public static File[] buildExternalStorageAndroidSandboxDirs() { |
| 844 | throwIfUserRequired(); |
| 845 | return sCurrentUser.buildExternalStorageAndroidSandboxDirs(); |
| 846 | } |
| 847 | |
| 848 | /** |
| 849 | * Returns the path for android-specific data on the SD card. |
| 850 | * @hide |
| 851 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 852 | @UnsupportedAppUsage |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 853 | public static File[] buildExternalStorageAndroidDataDirs() { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 854 | throwIfUserRequired(); |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 855 | return sCurrentUser.buildExternalStorageAndroidDataDirs(); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 856 | } |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 857 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 858 | /** |
| 859 | * Generates the raw path to an application's data |
| 860 | * @hide |
| 861 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 862 | @UnsupportedAppUsage |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 863 | public static File[] buildExternalStorageAppDataDirs(String packageName) { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 864 | throwIfUserRequired(); |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 865 | return sCurrentUser.buildExternalStorageAppDataDirs(packageName); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 866 | } |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 867 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 868 | /** |
| 869 | * Generates the raw path to an application's media |
| 870 | * @hide |
| 871 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 872 | @UnsupportedAppUsage |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 873 | public static File[] buildExternalStorageAppMediaDirs(String packageName) { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 874 | throwIfUserRequired(); |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 875 | return sCurrentUser.buildExternalStorageAppMediaDirs(packageName); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 876 | } |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 877 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 878 | /** |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 879 | * Generates the raw path to an application's OBB files |
| 880 | * @hide |
| 881 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 882 | @UnsupportedAppUsage |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 883 | public static File[] buildExternalStorageAppObbDirs(String packageName) { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 884 | throwIfUserRequired(); |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 885 | return sCurrentUser.buildExternalStorageAppObbDirs(packageName); |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 886 | } |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 887 | |
Dianne Hackborn | 805fd7e | 2011-01-16 18:30:29 -0800 | [diff] [blame] | 888 | /** |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 889 | * Generates the path to an application's files. |
| 890 | * @hide |
| 891 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 892 | @UnsupportedAppUsage |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 893 | public static File[] buildExternalStorageAppFilesDirs(String packageName) { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 894 | throwIfUserRequired(); |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 895 | return sCurrentUser.buildExternalStorageAppFilesDirs(packageName); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 896 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 897 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 898 | /** |
| 899 | * Generates the path to an application's cache. |
| 900 | * @hide |
| 901 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 902 | @UnsupportedAppUsage |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 903 | public static File[] buildExternalStorageAppCacheDirs(String packageName) { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 904 | throwIfUserRequired(); |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 905 | return sCurrentUser.buildExternalStorageAppCacheDirs(packageName); |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 906 | } |
Felipe Leme | b012f91 | 2016-01-22 16:49:55 -0800 | [diff] [blame] | 907 | |
Dianne Hackborn | e83cefce | 2010-02-04 17:38:14 -0800 | [diff] [blame] | 908 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 909 | * Return the download/cache content directory. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 910 | */ |
| 911 | public static File getDownloadCacheDirectory() { |
Jeff Sharkey | 1544779 | 2015-11-05 16:18:51 -0800 | [diff] [blame] | 912 | return DIR_DOWNLOAD_CACHE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 916 | * Unknown storage state, such as when a path isn't backed by known storage |
| 917 | * media. |
| 918 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 919 | * @see #getExternalStorageState(File) |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 920 | */ |
| 921 | public static final String MEDIA_UNKNOWN = "unknown"; |
| 922 | |
| 923 | /** |
| 924 | * Storage state if the media is not present. |
| 925 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 926 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 927 | */ |
| 928 | public static final String MEDIA_REMOVED = "removed"; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 929 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 930 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 931 | * Storage state if the media is present but not mounted. |
| 932 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 933 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 934 | */ |
| 935 | public static final String MEDIA_UNMOUNTED = "unmounted"; |
| 936 | |
| 937 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 938 | * Storage state if the media is present and being disk-checked. |
| 939 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 940 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 941 | */ |
| 942 | public static final String MEDIA_CHECKING = "checking"; |
| 943 | |
| 944 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 945 | * Storage state if the media is present but is blank or is using an |
| 946 | * unsupported filesystem. |
| 947 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 948 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 949 | */ |
| 950 | public static final String MEDIA_NOFS = "nofs"; |
| 951 | |
| 952 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 953 | * Storage state if the media is present and mounted at its mount point with |
| 954 | * read/write access. |
| 955 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 956 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 957 | */ |
| 958 | public static final String MEDIA_MOUNTED = "mounted"; |
| 959 | |
| 960 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 961 | * Storage state if the media is present and mounted at its mount point with |
| 962 | * read-only access. |
| 963 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 964 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 965 | */ |
| 966 | public static final String MEDIA_MOUNTED_READ_ONLY = "mounted_ro"; |
| 967 | |
| 968 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 969 | * Storage state if the media is present not mounted, and shared via USB |
| 970 | * mass storage. |
| 971 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 972 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 973 | */ |
| 974 | public static final String MEDIA_SHARED = "shared"; |
| 975 | |
| 976 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 977 | * Storage state if the media was removed before it was unmounted. |
| 978 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 979 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 980 | */ |
| 981 | public static final String MEDIA_BAD_REMOVAL = "bad_removal"; |
| 982 | |
| 983 | /** |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 984 | * Storage state if the media is present but cannot be mounted. Typically |
| 985 | * this happens if the file system on the media is corrupted. |
| 986 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 987 | * @see #getExternalStorageState(File) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 988 | */ |
| 989 | public static final String MEDIA_UNMOUNTABLE = "unmountable"; |
| 990 | |
| 991 | /** |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 992 | * Storage state if the media is in the process of being ejected. |
| 993 | * |
| 994 | * @see #getExternalStorageState(File) |
| 995 | */ |
| 996 | public static final String MEDIA_EJECTING = "ejecting"; |
| 997 | |
| 998 | /** |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 999 | * Returns the current state of the primary shared/external storage media. |
Felipe Leme | c7b1f89 | 2016-01-15 15:02:31 -0800 | [diff] [blame] | 1000 | * |
Jeff Sharkey | 8c16579 | 2012-10-22 14:08:29 -0700 | [diff] [blame] | 1001 | * @see #getExternalStorageDirectory() |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 1002 | * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED}, |
| 1003 | * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING}, |
| 1004 | * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED}, |
| 1005 | * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED}, |
| 1006 | * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1007 | */ |
| 1008 | public static String getExternalStorageState() { |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 1009 | final File externalDir = sCurrentUser.getExternalDirs()[0]; |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 1010 | return getExternalStorageState(externalDir); |
| 1011 | } |
| 1012 | |
| 1013 | /** |
| 1014 | * @deprecated use {@link #getExternalStorageState(File)} |
| 1015 | */ |
| 1016 | @Deprecated |
| 1017 | public static String getStorageState(File path) { |
| 1018 | return getExternalStorageState(path); |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | /** |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 1022 | * Returns the current state of the shared/external storage media at the |
| 1023 | * given path. |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 1024 | * |
| 1025 | * @return one of {@link #MEDIA_UNKNOWN}, {@link #MEDIA_REMOVED}, |
| 1026 | * {@link #MEDIA_UNMOUNTED}, {@link #MEDIA_CHECKING}, |
| 1027 | * {@link #MEDIA_NOFS}, {@link #MEDIA_MOUNTED}, |
| 1028 | * {@link #MEDIA_MOUNTED_READ_ONLY}, {@link #MEDIA_SHARED}, |
| 1029 | * {@link #MEDIA_BAD_REMOVAL}, or {@link #MEDIA_UNMOUNTABLE}. |
| 1030 | */ |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 1031 | public static String getExternalStorageState(File path) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1032 | final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId()); |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 1033 | if (volume != null) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1034 | return volume.getState(); |
| 1035 | } else { |
| 1036 | return MEDIA_UNKNOWN; |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 1037 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1038 | } |
| 1039 | |
Dianne Hackborn | 407f625 | 2010-10-04 11:31:17 -0700 | [diff] [blame] | 1040 | /** |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 1041 | * Returns whether the primary shared/external storage media is physically |
| 1042 | * removable. |
Dianne Hackborn | 407f625 | 2010-10-04 11:31:17 -0700 | [diff] [blame] | 1043 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 1044 | * @return true if the storage device can be removed (such as an SD card), |
| 1045 | * or false if the storage device is built in and cannot be |
| 1046 | * physically removed. |
Dianne Hackborn | 407f625 | 2010-10-04 11:31:17 -0700 | [diff] [blame] | 1047 | */ |
| 1048 | public static boolean isExternalStorageRemovable() { |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 1049 | final File externalDir = sCurrentUser.getExternalDirs()[0]; |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 1050 | return isExternalStorageRemovable(externalDir); |
Dianne Hackborn | 407f625 | 2010-10-04 11:31:17 -0700 | [diff] [blame] | 1051 | } |
| 1052 | |
Kenny Root | e1ff214 | 2010-10-12 11:20:01 -0700 | [diff] [blame] | 1053 | /** |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 1054 | * Returns whether the shared/external storage media at the given path is |
| 1055 | * physically removable. |
Andy Stadler | 50c294f | 2011-03-07 19:13:42 -0800 | [diff] [blame] | 1056 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 1057 | * @return true if the storage device can be removed (such as an SD card), |
| 1058 | * or false if the storage device is built in and cannot be |
| 1059 | * physically removed. |
| 1060 | * @throws IllegalArgumentException if the path is not a valid storage |
| 1061 | * device. |
| 1062 | */ |
| 1063 | public static boolean isExternalStorageRemovable(File path) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1064 | final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId()); |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 1065 | if (volume != null) { |
| 1066 | return volume.isRemovable(); |
| 1067 | } else { |
| 1068 | throw new IllegalArgumentException("Failed to find storage device at " + path); |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | /** |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 1073 | * Returns whether the primary shared/external storage media is emulated. |
| 1074 | * <p> |
| 1075 | * The contents of emulated storage devices are backed by a private user |
| 1076 | * data partition, which means there is little benefit to apps storing data |
| 1077 | * here instead of the private directories returned by |
| 1078 | * {@link Context#getFilesDir()}, etc. |
| 1079 | * <p> |
| 1080 | * This returns true when emulated storage is backed by either internal |
| 1081 | * storage or an adopted storage device. |
Andy Stadler | 50c294f | 2011-03-07 19:13:42 -0800 | [diff] [blame] | 1082 | * |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 1083 | * @see DevicePolicyManager#setStorageEncryption(android.content.ComponentName, |
| 1084 | * boolean) |
Kenny Root | e1ff214 | 2010-10-12 11:20:01 -0700 | [diff] [blame] | 1085 | */ |
| 1086 | public static boolean isExternalStorageEmulated() { |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 1087 | final File externalDir = sCurrentUser.getExternalDirs()[0]; |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 1088 | return isExternalStorageEmulated(externalDir); |
| 1089 | } |
| 1090 | |
| 1091 | /** |
Jeff Sharkey | 59d28dc8 | 2015-10-14 13:56:23 -0700 | [diff] [blame] | 1092 | * Returns whether the shared/external storage media at the given path is |
| 1093 | * emulated. |
| 1094 | * <p> |
| 1095 | * The contents of emulated storage devices are backed by a private user |
| 1096 | * data partition, which means there is little benefit to apps storing data |
| 1097 | * here instead of the private directories returned by |
| 1098 | * {@link Context#getFilesDir()}, etc. |
| 1099 | * <p> |
| 1100 | * This returns true when emulated storage is backed by either internal |
| 1101 | * storage or an adopted storage device. |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 1102 | * |
| 1103 | * @throws IllegalArgumentException if the path is not a valid storage |
| 1104 | * device. |
| 1105 | */ |
| 1106 | public static boolean isExternalStorageEmulated(File path) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 1107 | final StorageVolume volume = StorageManager.getStorageVolume(path, UserHandle.myUserId()); |
Jeff Sharkey | 4ca728c | 2014-01-10 16:27:19 -0800 | [diff] [blame] | 1108 | if (volume != null) { |
| 1109 | return volume.isEmulated(); |
| 1110 | } else { |
| 1111 | throw new IllegalArgumentException("Failed to find storage device at " + path); |
| 1112 | } |
Kenny Root | e1ff214 | 2010-10-12 11:20:01 -0700 | [diff] [blame] | 1113 | } |
| 1114 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1115 | static File getDirectory(String variableName, String defaultPath) { |
| 1116 | String path = System.getenv(variableName); |
| 1117 | return path == null ? new File(defaultPath) : new File(path); |
| 1118 | } |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1119 | |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 1120 | /** {@hide} */ |
| 1121 | public static void setUserRequired(boolean userRequired) { |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 1122 | sUserRequired = userRequired; |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | private static void throwIfUserRequired() { |
Andreas Gampe | d281b42 | 2016-07-08 03:50:27 +0000 | [diff] [blame] | 1126 | if (sUserRequired) { |
Jeff Sharkey | 48749fc | 2013-04-19 13:25:04 -0700 | [diff] [blame] | 1127 | Log.wtf(TAG, "Path requests must specify a user by using UserEnvironment", |
| 1128 | new Throwable()); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1129 | } |
| 1130 | } |
| 1131 | |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 1132 | /** |
| 1133 | * Append path segments to each given base path, returning result. |
| 1134 | * |
| 1135 | * @hide |
| 1136 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 1137 | @UnsupportedAppUsage |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 1138 | public static File[] buildPaths(File[] base, String... segments) { |
| 1139 | File[] result = new File[base.length]; |
| 1140 | for (int i = 0; i < base.length; i++) { |
| 1141 | result[i] = buildPath(base[i], segments); |
| 1142 | } |
| 1143 | return result; |
| 1144 | } |
| 1145 | |
| 1146 | /** |
| 1147 | * Append path segments to given base path, returning result. |
| 1148 | * |
| 1149 | * @hide |
| 1150 | */ |
Philip P. Moltmann | f80809f | 2018-04-04 11:20:44 -0700 | [diff] [blame] | 1151 | @TestApi |
Jeff Sharkey | 1abdb71 | 2013-08-11 16:28:14 -0700 | [diff] [blame] | 1152 | public static File buildPath(File base, String... segments) { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 1153 | File cur = base; |
| 1154 | for (String segment : segments) { |
| 1155 | if (cur == null) { |
| 1156 | cur = new File(segment); |
| 1157 | } else { |
| 1158 | cur = new File(cur, segment); |
| 1159 | } |
| 1160 | } |
| 1161 | return cur; |
| 1162 | } |
Jeff Sharkey | 63d0a06 | 2013-03-01 16:12:55 -0800 | [diff] [blame] | 1163 | |
| 1164 | /** |
| 1165 | * If the given path exists on emulated external storage, return the |
| 1166 | * translated backing path hosted on internal storage. This bypasses any |
| 1167 | * emulation later, improving performance. This is <em>only</em> suitable |
| 1168 | * for read-only access. |
| 1169 | * <p> |
| 1170 | * Returns original path if given path doesn't meet these criteria. Callers |
| 1171 | * must hold {@link android.Manifest.permission#WRITE_MEDIA_STORAGE} |
| 1172 | * permission. |
| 1173 | * |
Jeff Sharkey | 70eb34a | 2018-09-13 11:57:03 -0600 | [diff] [blame] | 1174 | * @deprecated disabled now that FUSE has been replaced by sdcardfs |
Jeff Sharkey | 63d0a06 | 2013-03-01 16:12:55 -0800 | [diff] [blame] | 1175 | * @hide |
| 1176 | */ |
Andrei Onea | 24ec321 | 2019-03-15 17:35:05 +0000 | [diff] [blame^] | 1177 | @UnsupportedAppUsage |
Jeff Sharkey | 70eb34a | 2018-09-13 11:57:03 -0600 | [diff] [blame] | 1178 | @Deprecated |
Jeff Sharkey | 63d0a06 | 2013-03-01 16:12:55 -0800 | [diff] [blame] | 1179 | public static File maybeTranslateEmulatedPathToInternal(File path) { |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 1180 | return StorageManager.maybeTranslateEmulatedPathToInternal(path); |
Jeff Sharkey | 63d0a06 | 2013-03-01 16:12:55 -0800 | [diff] [blame] | 1181 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1182 | } |