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