Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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.storage; |
| 18 | |
Amin Shaikh | 305e87e | 2018-11-19 12:33:27 -0500 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 20 | import android.annotation.Nullable; |
Jeff Sharkey | c609116 | 2018-06-29 17:15:40 -0600 | [diff] [blame] | 21 | import android.annotation.TestApi; |
Mathew Inwood | 98e9ad1 | 2018-08-30 13:11:50 +0100 | [diff] [blame] | 22 | import android.annotation.UnsupportedAppUsage; |
Fabrice Di Meglio | 13fe2a5 | 2012-05-18 18:08:58 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 24 | import android.content.Intent; |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 25 | import android.net.Uri; |
Mathew Inwood | 8c854f8 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 26 | import android.os.Build; |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 27 | import android.os.Environment; |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 28 | import android.os.Parcel; |
| 29 | import android.os.Parcelable; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 30 | import android.os.UserHandle; |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 31 | import android.provider.DocumentsContract; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 32 | |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 33 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 34 | import com.android.internal.util.Preconditions; |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 35 | |
| 36 | import java.io.CharArrayWriter; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 37 | import java.io.File; |
Jeff Sharkey | 3f64ec5 | 2019-01-22 13:19:40 -0700 | [diff] [blame] | 38 | import java.util.Locale; |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 41 | * Information about a shared/external storage volume for a specific user. |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 42 | * |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 43 | * <p> |
| 44 | * A device always has one (and one only) primary storage volume, but it could have extra volumes, |
| 45 | * like SD cards and USB drives. This object represents the logical view of a storage |
| 46 | * volume for a specific user: different users might have different views for the same physical |
| 47 | * volume (for example, if the volume is a built-in emulated storage). |
| 48 | * |
| 49 | * <p> |
| 50 | * The storage volume is not necessarily mounted, applications should use {@link #getState()} to |
| 51 | * verify its state. |
| 52 | * |
| 53 | * <p> |
| 54 | * Applications willing to read or write to this storage volume needs to get a permission from the |
| 55 | * user first, which can be achieved in the following ways: |
| 56 | * |
| 57 | * <ul> |
| 58 | * <li>To get access to standard directories (like the {@link Environment#DIRECTORY_PICTURES}), they |
| 59 | * can use the {@link #createAccessIntent(String)}. This is the recommend way, since it provides a |
| 60 | * simpler API and narrows the access to the given directory (and its descendants). |
Felipe Leme | 53fcc75 | 2016-02-17 14:45:52 -0800 | [diff] [blame] | 61 | * <li>To get access to any directory (and its descendants), they can use the Storage Acess |
| 62 | * Framework APIs (such as {@link Intent#ACTION_OPEN_DOCUMENT} and |
| 63 | * {@link Intent#ACTION_OPEN_DOCUMENT_TREE}, although these APIs do not guarantee the user will |
| 64 | * select this specific volume. |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 65 | * <li>To get read and write access to the primary storage volume, applications can declare the |
| 66 | * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} and |
| 67 | * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permissions respectively, with the |
| 68 | * latter including the former. This approach is discouraged, since users may be hesitant to grant |
| 69 | * broad access to all files contained on a storage device. |
| 70 | * </ul> |
| 71 | * |
Jeff Sharkey | c02bfae | 2016-03-27 15:06:53 -0600 | [diff] [blame] | 72 | * <p>It can be obtained through {@link StorageManager#getStorageVolumes()} and |
| 73 | * {@link StorageManager#getPrimaryStorageVolume()} and also as an extra in some broadcasts |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 74 | * (see {@link #EXTRA_STORAGE_VOLUME}). |
| 75 | * |
| 76 | * <p> |
| 77 | * See {@link Environment#getExternalStorageDirectory()} for more info about shared/external |
| 78 | * storage semantics. |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 79 | */ |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 80 | // NOTE: This is a legacy specialization of VolumeInfo which describes the volume for a specific |
| 81 | // user, but is now part of the public API. |
Jeff Sharkey | 50d1c04 | 2016-02-29 16:34:46 -0700 | [diff] [blame] | 82 | public final class StorageVolume implements Parcelable { |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 83 | |
Mathew Inwood | 98e9ad1 | 2018-08-30 13:11:50 +0100 | [diff] [blame] | 84 | @UnsupportedAppUsage |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 85 | private final String mId; |
Mathew Inwood | 98e9ad1 | 2018-08-30 13:11:50 +0100 | [diff] [blame] | 86 | @UnsupportedAppUsage |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 87 | private final File mPath; |
Jerry Zhang | 71938e1 | 2018-05-10 18:28:29 -0700 | [diff] [blame] | 88 | private final File mInternalPath; |
Mathew Inwood | 98e9ad1 | 2018-08-30 13:11:50 +0100 | [diff] [blame] | 89 | @UnsupportedAppUsage |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 90 | private final String mDescription; |
Mathew Inwood | 98e9ad1 | 2018-08-30 13:11:50 +0100 | [diff] [blame] | 91 | @UnsupportedAppUsage |
Jeff Sharkey | 9545dc02 | 2012-09-06 22:46:30 -0700 | [diff] [blame] | 92 | private final boolean mPrimary; |
Mathew Inwood | 98e9ad1 | 2018-08-30 13:11:50 +0100 | [diff] [blame] | 93 | @UnsupportedAppUsage |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 94 | private final boolean mRemovable; |
| 95 | private final boolean mEmulated; |
Mike Lockwood | 8e8b280 | 2011-06-07 08:03:33 -0700 | [diff] [blame] | 96 | private final boolean mAllowMassStorage; |
Mike Lockwood | 7a59dd2 | 2011-07-11 09:18:03 -0400 | [diff] [blame] | 97 | private final long mMaxFileSize; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 98 | private final UserHandle mOwner; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 99 | private final String mFsUuid; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 100 | private final String mState; |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 101 | |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 102 | /** |
| 103 | * Name of the {@link Parcelable} extra in the {@link Intent#ACTION_MEDIA_REMOVED}, |
| 104 | * {@link Intent#ACTION_MEDIA_UNMOUNTED}, {@link Intent#ACTION_MEDIA_CHECKING}, |
| 105 | * {@link Intent#ACTION_MEDIA_NOFS}, {@link Intent#ACTION_MEDIA_MOUNTED}, |
| 106 | * {@link Intent#ACTION_MEDIA_SHARED}, {@link Intent#ACTION_MEDIA_BAD_REMOVAL}, |
| 107 | * {@link Intent#ACTION_MEDIA_UNMOUNTABLE}, and {@link Intent#ACTION_MEDIA_EJECT} broadcast that |
| 108 | * contains a {@link StorageVolume}. |
| 109 | */ |
| 110 | // Also sent on ACTION_MEDIA_UNSHARED, which is @hide |
| 111 | public static final String EXTRA_STORAGE_VOLUME = "android.os.storage.extra.STORAGE_VOLUME"; |
Mike Lockwood | a5250c9 | 2011-05-23 13:44:04 -0400 | [diff] [blame] | 112 | |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 113 | /** |
| 114 | * Name of the String extra used by {@link #createAccessIntent(String) createAccessIntent}. |
| 115 | * |
| 116 | * @hide |
| 117 | */ |
| 118 | public static final String EXTRA_DIRECTORY_NAME = "android.os.storage.extra.DIRECTORY_NAME"; |
| 119 | |
| 120 | /** |
| 121 | * Name of the intent used by {@link #createAccessIntent(String) createAccessIntent}. |
| 122 | */ |
| 123 | private static final String ACTION_OPEN_EXTERNAL_DIRECTORY = |
| 124 | "android.os.storage.action.OPEN_EXTERNAL_DIRECTORY"; |
| 125 | |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 126 | /** {@hide} */ |
Jeff Sharkey | 5af1835d | 2015-07-07 17:26:59 -0700 | [diff] [blame] | 127 | public static final int STORAGE_ID_INVALID = 0x00000000; |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 128 | /** {@hide} */ |
Jeff Sharkey | 5af1835d | 2015-07-07 17:26:59 -0700 | [diff] [blame] | 129 | public static final int STORAGE_ID_PRIMARY = 0x00010001; |
| 130 | |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 131 | /** {@hide} */ |
Jerry Zhang | 71938e1 | 2018-05-10 18:28:29 -0700 | [diff] [blame] | 132 | public StorageVolume(String id, File path, File internalPath, String description, |
| 133 | boolean primary, boolean removable, boolean emulated, boolean allowMassStorage, |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 134 | long maxFileSize, UserHandle owner, String fsUuid, String state) { |
| 135 | mId = Preconditions.checkNotNull(id); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 136 | mPath = Preconditions.checkNotNull(path); |
Jerry Zhang | 71938e1 | 2018-05-10 18:28:29 -0700 | [diff] [blame] | 137 | mInternalPath = Preconditions.checkNotNull(internalPath); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 138 | mDescription = Preconditions.checkNotNull(description); |
Jeff Sharkey | 9545dc02 | 2012-09-06 22:46:30 -0700 | [diff] [blame] | 139 | mPrimary = primary; |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 140 | mRemovable = removable; |
| 141 | mEmulated = emulated; |
Mike Lockwood | 8e8b280 | 2011-06-07 08:03:33 -0700 | [diff] [blame] | 142 | mAllowMassStorage = allowMassStorage; |
Mike Lockwood | 7a59dd2 | 2011-07-11 09:18:03 -0400 | [diff] [blame] | 143 | mMaxFileSize = maxFileSize; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 144 | mOwner = Preconditions.checkNotNull(owner); |
| 145 | mFsUuid = fsUuid; |
| 146 | mState = Preconditions.checkNotNull(state); |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Jeff Sharkey | 9545dc02 | 2012-09-06 22:46:30 -0700 | [diff] [blame] | 149 | private StorageVolume(Parcel in) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 150 | mId = in.readString(); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 151 | mPath = new File(in.readString()); |
Jerry Zhang | 71938e1 | 2018-05-10 18:28:29 -0700 | [diff] [blame] | 152 | mInternalPath = new File(in.readString()); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 153 | mDescription = in.readString(); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 154 | mPrimary = in.readInt() != 0; |
| 155 | mRemovable = in.readInt() != 0; |
| 156 | mEmulated = in.readInt() != 0; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 157 | mAllowMassStorage = in.readInt() != 0; |
Jeff Sharkey | 9545dc02 | 2012-09-06 22:46:30 -0700 | [diff] [blame] | 158 | mMaxFileSize = in.readLong(); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 159 | mOwner = in.readParcelable(null); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 160 | mFsUuid = in.readString(); |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 161 | mState = in.readString(); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 164 | /** {@hide} */ |
Mathew Inwood | 98e9ad1 | 2018-08-30 13:11:50 +0100 | [diff] [blame] | 165 | @UnsupportedAppUsage |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 166 | public String getId() { |
| 167 | return mId; |
Mike Lockwood | fbfe555 | 2011-05-17 17:19:37 -0400 | [diff] [blame] | 168 | } |
| 169 | |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 170 | /** |
| 171 | * Returns the mount path for the volume. |
| 172 | * |
| 173 | * @return the mount path |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 174 | * @hide |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 175 | */ |
Jeff Sharkey | c609116 | 2018-06-29 17:15:40 -0600 | [diff] [blame] | 176 | @TestApi |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 177 | public String getPath() { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 178 | return mPath.toString(); |
| 179 | } |
| 180 | |
Jerry Zhang | 71938e1 | 2018-05-10 18:28:29 -0700 | [diff] [blame] | 181 | /** |
| 182 | * Returns the path of the underlying filesystem. |
| 183 | * |
| 184 | * @return the internal path |
| 185 | * @hide |
| 186 | */ |
| 187 | public String getInternalPath() { |
| 188 | return mInternalPath.toString(); |
| 189 | } |
| 190 | |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 191 | /** {@hide} */ |
Mathew Inwood | 98e9ad1 | 2018-08-30 13:11:50 +0100 | [diff] [blame] | 192 | @UnsupportedAppUsage |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 193 | public File getPathFile() { |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 194 | return mPath; |
| 195 | } |
| 196 | |
| 197 | /** |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 198 | * Returns a user-visible description of the volume. |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 199 | * |
| 200 | * @return the volume description |
| 201 | */ |
Fabrice Di Meglio | 13fe2a5 | 2012-05-18 18:08:58 -0700 | [diff] [blame] | 202 | public String getDescription(Context context) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 203 | return mDescription; |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 206 | /** |
| 207 | * Returns true if the volume is the primary shared/external storage, which is the volume |
| 208 | * backed by {@link Environment#getExternalStorageDirectory()}. |
| 209 | */ |
Jeff Sharkey | 9545dc02 | 2012-09-06 22:46:30 -0700 | [diff] [blame] | 210 | public boolean isPrimary() { |
| 211 | return mPrimary; |
| 212 | } |
| 213 | |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 214 | /** |
| 215 | * Returns true if the volume is removable. |
| 216 | * |
| 217 | * @return is removable |
| 218 | */ |
| 219 | public boolean isRemovable() { |
| 220 | return mRemovable; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Returns true if the volume is emulated. |
| 225 | * |
| 226 | * @return is removable |
| 227 | */ |
| 228 | public boolean isEmulated() { |
| 229 | return mEmulated; |
| 230 | } |
| 231 | |
| 232 | /** |
Mike Lockwood | 8e8b280 | 2011-06-07 08:03:33 -0700 | [diff] [blame] | 233 | * Returns true if this volume can be shared via USB mass storage. |
| 234 | * |
| 235 | * @return whether mass storage is allowed |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 236 | * @hide |
Mike Lockwood | 8e8b280 | 2011-06-07 08:03:33 -0700 | [diff] [blame] | 237 | */ |
Mathew Inwood | 98e9ad1 | 2018-08-30 13:11:50 +0100 | [diff] [blame] | 238 | @UnsupportedAppUsage |
Mike Lockwood | 8e8b280 | 2011-06-07 08:03:33 -0700 | [diff] [blame] | 239 | public boolean allowMassStorage() { |
| 240 | return mAllowMassStorage; |
| 241 | } |
| 242 | |
Mike Lockwood | 7a59dd2 | 2011-07-11 09:18:03 -0400 | [diff] [blame] | 243 | /** |
| 244 | * Returns maximum file size for the volume, or zero if it is unbounded. |
| 245 | * |
| 246 | * @return maximum file size |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 247 | * @hide |
Mike Lockwood | 7a59dd2 | 2011-07-11 09:18:03 -0400 | [diff] [blame] | 248 | */ |
Mathew Inwood | 98e9ad1 | 2018-08-30 13:11:50 +0100 | [diff] [blame] | 249 | @UnsupportedAppUsage |
Mike Lockwood | 7a59dd2 | 2011-07-11 09:18:03 -0400 | [diff] [blame] | 250 | public long getMaxFileSize() { |
| 251 | return mMaxFileSize; |
| 252 | } |
| 253 | |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 254 | /** {@hide} */ |
Mathew Inwood | 8c854f8 | 2018-09-14 12:35:36 +0100 | [diff] [blame] | 255 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 256 | public UserHandle getOwner() { |
| 257 | return mOwner; |
| 258 | } |
| 259 | |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 260 | /** |
| 261 | * Gets the volume UUID, if any. |
| 262 | */ |
| 263 | public @Nullable String getUuid() { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 264 | return mFsUuid; |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Jeff Sharkey | 3f64ec5 | 2019-01-22 13:19:40 -0700 | [diff] [blame] | 267 | /** {@hide} */ |
| 268 | public @Nullable String getNormalizedUuid() { |
| 269 | return mFsUuid != null ? mFsUuid.toLowerCase(Locale.US) : null; |
| 270 | } |
| 271 | |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 272 | /** |
| 273 | * Parse and return volume UUID as FAT volume ID, or return -1 if unable to |
| 274 | * parse or UUID is unknown. |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 275 | * @hide |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 276 | */ |
Mathew Inwood | 98e9ad1 | 2018-08-30 13:11:50 +0100 | [diff] [blame] | 277 | @UnsupportedAppUsage |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 278 | public int getFatVolumeId() { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 279 | if (mFsUuid == null || mFsUuid.length() != 9) { |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 280 | return -1; |
| 281 | } |
| 282 | try { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 283 | return (int) Long.parseLong(mFsUuid.replace("-", ""), 16); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 284 | } catch (NumberFormatException e) { |
| 285 | return -1; |
| 286 | } |
| 287 | } |
| 288 | |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 289 | /** {@hide} */ |
Mathew Inwood | 98e9ad1 | 2018-08-30 13:11:50 +0100 | [diff] [blame] | 290 | @UnsupportedAppUsage |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 291 | public String getUserLabel() { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 292 | return mDescription; |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 295 | /** |
| 296 | * Returns the current state of the volume. |
| 297 | * |
| 298 | * @return one of {@link Environment#MEDIA_UNKNOWN}, {@link Environment#MEDIA_REMOVED}, |
| 299 | * {@link Environment#MEDIA_UNMOUNTED}, {@link Environment#MEDIA_CHECKING}, |
| 300 | * {@link Environment#MEDIA_NOFS}, {@link Environment#MEDIA_MOUNTED}, |
| 301 | * {@link Environment#MEDIA_MOUNTED_READ_ONLY}, {@link Environment#MEDIA_SHARED}, |
| 302 | * {@link Environment#MEDIA_BAD_REMOVAL}, or {@link Environment#MEDIA_UNMOUNTABLE}. |
| 303 | */ |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 304 | public String getState() { |
| 305 | return mState; |
| 306 | } |
| 307 | |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 308 | /** |
Felipe Leme | db892b8 | 2016-03-17 18:56:20 -0700 | [diff] [blame] | 309 | * Builds an intent to give access to a standard storage directory or entire volume after |
| 310 | * obtaining the user's approval. |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 311 | * <p> |
| 312 | * When invoked, the system will ask the user to grant access to the requested directory (and |
| 313 | * its descendants). The result of the request will be returned to the activity through the |
| 314 | * {@code onActivityResult} method. |
| 315 | * <p> |
| 316 | * To gain access to descendants (child, grandchild, etc) documents, use |
| 317 | * {@link DocumentsContract#buildDocumentUriUsingTree(Uri, String)}, or |
| 318 | * {@link DocumentsContract#buildChildDocumentsUriUsingTree(Uri, String)} with the returned URI. |
Felipe Leme | 2ac8769 | 2016-03-29 19:06:02 -0700 | [diff] [blame] | 319 | * <p> |
| 320 | * If your application only needs to store internal data, consider using |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 321 | * {@link Context#getExternalFilesDirs(String) Context.getExternalFilesDirs}, |
Felipe Leme | 2ac8769 | 2016-03-29 19:06:02 -0700 | [diff] [blame] | 322 | * {@link Context#getExternalCacheDirs()}, or {@link Context#getExternalMediaDirs()}, which |
| 323 | * require no permissions to read or write. |
| 324 | * <p> |
| 325 | * Access to the entire volume is only available for non-primary volumes (for the primary |
| 326 | * volume, apps can use the {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} and |
| 327 | * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permissions) and should be used |
| 328 | * with caution, since users are more likely to deny access when asked for entire volume access |
| 329 | * rather than specific directories. |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 330 | * |
Felipe Leme | 2ac8769 | 2016-03-29 19:06:02 -0700 | [diff] [blame] | 331 | * @param directoryName must be one of {@link Environment#DIRECTORY_MUSIC}, |
| 332 | * {@link Environment#DIRECTORY_PODCASTS}, {@link Environment#DIRECTORY_RINGTONES}, |
| 333 | * {@link Environment#DIRECTORY_ALARMS}, {@link Environment#DIRECTORY_NOTIFICATIONS}, |
| 334 | * {@link Environment#DIRECTORY_PICTURES}, {@link Environment#DIRECTORY_MOVIES}, |
| 335 | * {@link Environment#DIRECTORY_DOWNLOADS}, {@link Environment#DIRECTORY_DCIM}, or |
Garfield Tan | 92b96ba | 2016-11-01 14:33:48 -0700 | [diff] [blame] | 336 | * {@link Environment#DIRECTORY_DOCUMENTS}, or {@code null} to request access to the |
Felipe Leme | 2ac8769 | 2016-03-29 19:06:02 -0700 | [diff] [blame] | 337 | * entire volume. |
| 338 | * @return intent to request access, or {@code null} if the requested directory is invalid for |
| 339 | * that volume. |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 340 | * @see DocumentsContract |
Amin Shaikh | b49cedf | 2018-10-23 13:37:19 -0400 | [diff] [blame] | 341 | * @deprecated Callers should migrate to using {@link Intent#ACTION_OPEN_DOCUMENT_TREE} instead. |
| 342 | * Launching this {@link Intent} on devices running |
| 343 | * {@link android.os.Build.VERSION_CODES#Q} or higher, will immediately finish |
| 344 | * with a result code of {@link android.app.Activity#RESULT_CANCELED}. |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 345 | */ |
Amin Shaikh | b49cedf | 2018-10-23 13:37:19 -0400 | [diff] [blame] | 346 | @Deprecated |
Felipe Leme | 2ac8769 | 2016-03-29 19:06:02 -0700 | [diff] [blame] | 347 | public @Nullable Intent createAccessIntent(String directoryName) { |
| 348 | if ((isPrimary() && directoryName == null) || |
| 349 | (directoryName != null && !Environment.isStandardDirectory(directoryName))) { |
| 350 | return null; |
| 351 | } |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 352 | final Intent intent = new Intent(ACTION_OPEN_EXTERNAL_DIRECTORY); |
| 353 | intent.putExtra(EXTRA_STORAGE_VOLUME, this); |
| 354 | intent.putExtra(EXTRA_DIRECTORY_NAME, directoryName); |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 355 | return intent; |
| 356 | } |
| 357 | |
Amin Shaikh | 305e87e | 2018-11-19 12:33:27 -0500 | [diff] [blame] | 358 | /** |
| 359 | * Builds an {@link Intent#ACTION_OPEN_DOCUMENT_TREE} to allow the user to grant access to any |
| 360 | * directory subtree (or entire volume) from the {@link android.provider.DocumentsProvider}s |
| 361 | * available on the device. The initial location of the document navigation will be the root of |
| 362 | * this {@link StorageVolume}. |
| 363 | * |
| 364 | * Note that the returned {@link Intent} simply suggests that the user picks this {@link |
| 365 | * StorageVolume} by default, but the user may select a different location. Callers must respect |
| 366 | * the user's chosen location, even if it is different from the originally requested location. |
| 367 | * |
| 368 | * @return intent to {@link Intent#ACTION_OPEN_DOCUMENT_TREE} initially showing the contents |
| 369 | * of this {@link StorageVolume} |
| 370 | * @see Intent#ACTION_OPEN_DOCUMENT_TREE |
| 371 | */ |
| 372 | @NonNull public Intent createOpenDocumentTreeIntent() { |
| 373 | final String rootId = isEmulated() |
| 374 | ? DocumentsContract.EXTERNAL_STORAGE_PRIMARY_EMULATED_ROOT_ID |
| 375 | : mFsUuid; |
| 376 | final Uri rootUri = DocumentsContract.buildRootUri( |
| 377 | DocumentsContract.EXTERNAL_STORAGE_PROVIDER_AUTHORITY, rootId); |
| 378 | final Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) |
| 379 | .putExtra(DocumentsContract.EXTRA_INITIAL_URI, rootUri) |
| 380 | .putExtra(DocumentsContract.EXTRA_SHOW_ADVANCED, true); |
| 381 | return intent; |
| 382 | } |
| 383 | |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 384 | @Override |
| 385 | public boolean equals(Object obj) { |
| 386 | if (obj instanceof StorageVolume && mPath != null) { |
| 387 | StorageVolume volume = (StorageVolume)obj; |
| 388 | return (mPath.equals(volume.mPath)); |
| 389 | } |
| 390 | return false; |
| 391 | } |
| 392 | |
| 393 | @Override |
| 394 | public int hashCode() { |
| 395 | return mPath.hashCode(); |
| 396 | } |
| 397 | |
| 398 | @Override |
| 399 | public String toString() { |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 400 | final StringBuilder buffer = new StringBuilder("StorageVolume: ").append(mDescription); |
| 401 | if (mFsUuid != null) { |
| 402 | buffer.append(" (").append(mFsUuid).append(")"); |
| 403 | } |
| 404 | return buffer.toString(); |
| 405 | } |
| 406 | |
| 407 | /** {@hide} */ |
Felipe Leme | 104b932 | 2017-03-14 08:40:06 -0700 | [diff] [blame] | 408 | // TODO: find out where toString() is called internally and replace these calls by dump(). |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 409 | public String dump() { |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 410 | final CharArrayWriter writer = new CharArrayWriter(); |
| 411 | dump(new IndentingPrintWriter(writer, " ", 80)); |
| 412 | return writer.toString(); |
| 413 | } |
| 414 | |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 415 | /** {@hide} */ |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 416 | public void dump(IndentingPrintWriter pw) { |
| 417 | pw.println("StorageVolume:"); |
| 418 | pw.increaseIndent(); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 419 | pw.printPair("mId", mId); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 420 | pw.printPair("mPath", mPath); |
Jerry Zhang | 71938e1 | 2018-05-10 18:28:29 -0700 | [diff] [blame] | 421 | pw.printPair("mInternalPath", mInternalPath); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 422 | pw.printPair("mDescription", mDescription); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 423 | pw.printPair("mPrimary", mPrimary); |
| 424 | pw.printPair("mRemovable", mRemovable); |
| 425 | pw.printPair("mEmulated", mEmulated); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 426 | pw.printPair("mAllowMassStorage", mAllowMassStorage); |
| 427 | pw.printPair("mMaxFileSize", mMaxFileSize); |
| 428 | pw.printPair("mOwner", mOwner); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 429 | pw.printPair("mFsUuid", mFsUuid); |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 430 | pw.printPair("mState", mState); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 431 | pw.decreaseIndent(); |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 434 | public static final @android.annotation.NonNull Creator<StorageVolume> CREATOR = new Creator<StorageVolume>() { |
Jeff Sharkey | 9545dc02 | 2012-09-06 22:46:30 -0700 | [diff] [blame] | 435 | @Override |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 436 | public StorageVolume createFromParcel(Parcel in) { |
Jeff Sharkey | 9545dc02 | 2012-09-06 22:46:30 -0700 | [diff] [blame] | 437 | return new StorageVolume(in); |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Jeff Sharkey | 9545dc02 | 2012-09-06 22:46:30 -0700 | [diff] [blame] | 440 | @Override |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 441 | public StorageVolume[] newArray(int size) { |
| 442 | return new StorageVolume[size]; |
| 443 | } |
| 444 | }; |
| 445 | |
Jeff Sharkey | 9545dc02 | 2012-09-06 22:46:30 -0700 | [diff] [blame] | 446 | @Override |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 447 | public int describeContents() { |
| 448 | return 0; |
| 449 | } |
| 450 | |
Jeff Sharkey | 9545dc02 | 2012-09-06 22:46:30 -0700 | [diff] [blame] | 451 | @Override |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 452 | public void writeToParcel(Parcel parcel, int flags) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 453 | parcel.writeString(mId); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 454 | parcel.writeString(mPath.toString()); |
Jerry Zhang | 71938e1 | 2018-05-10 18:28:29 -0700 | [diff] [blame] | 455 | parcel.writeString(mInternalPath.toString()); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 456 | parcel.writeString(mDescription); |
Jeff Sharkey | 9545dc02 | 2012-09-06 22:46:30 -0700 | [diff] [blame] | 457 | parcel.writeInt(mPrimary ? 1 : 0); |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 458 | parcel.writeInt(mRemovable ? 1 : 0); |
| 459 | parcel.writeInt(mEmulated ? 1 : 0); |
Mike Lockwood | 8e8b280 | 2011-06-07 08:03:33 -0700 | [diff] [blame] | 460 | parcel.writeInt(mAllowMassStorage ? 1 : 0); |
Mike Lockwood | 7a59dd2 | 2011-07-11 09:18:03 -0400 | [diff] [blame] | 461 | parcel.writeLong(mMaxFileSize); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 462 | parcel.writeParcelable(mOwner, flags); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 463 | parcel.writeString(mFsUuid); |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 464 | parcel.writeString(mState); |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 465 | } |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 466 | } |