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