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