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