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