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 | |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Fabrice Di Meglio | 13fe2a5 | 2012-05-18 18:08:58 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 22 | import android.content.Intent; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 23 | import android.net.TrafficStats; |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 24 | import android.net.Uri; |
| 25 | import android.os.Environment; |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 26 | import android.os.Parcel; |
| 27 | import android.os.Parcelable; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 28 | import android.os.UserHandle; |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 29 | import android.provider.DocumentsContract; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 30 | |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 31 | import com.android.internal.util.IndentingPrintWriter; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 32 | import com.android.internal.util.Preconditions; |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 33 | |
| 34 | import java.io.CharArrayWriter; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 35 | import java.io.File; |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 38 | * Information about a shared/external storage volume for a specific user. |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 39 | * |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 40 | * <p> |
| 41 | * A device always has one (and one only) primary storage volume, but it could have extra volumes, |
| 42 | * like SD cards and USB drives. This object represents the logical view of a storage |
| 43 | * volume for a specific user: different users might have different views for the same physical |
| 44 | * volume (for example, if the volume is a built-in emulated storage). |
| 45 | * |
| 46 | * <p> |
| 47 | * The storage volume is not necessarily mounted, applications should use {@link #getState()} to |
| 48 | * verify its state. |
| 49 | * |
| 50 | * <p> |
| 51 | * Applications willing to read or write to this storage volume needs to get a permission from the |
| 52 | * user first, which can be achieved in the following ways: |
| 53 | * |
| 54 | * <ul> |
| 55 | * <li>To get access to standard directories (like the {@link Environment#DIRECTORY_PICTURES}), they |
| 56 | * can use the {@link #createAccessIntent(String)}. This is the recommend way, since it provides a |
| 57 | * 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] | 58 | * <li>To get access to any directory (and its descendants), they can use the Storage Acess |
| 59 | * Framework APIs (such as {@link Intent#ACTION_OPEN_DOCUMENT} and |
| 60 | * {@link Intent#ACTION_OPEN_DOCUMENT_TREE}, although these APIs do not guarantee the user will |
| 61 | * select this specific volume. |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 62 | * <li>To get read and write access to the primary storage volume, applications can declare the |
| 63 | * {@link android.Manifest.permission#READ_EXTERNAL_STORAGE} and |
| 64 | * {@link android.Manifest.permission#WRITE_EXTERNAL_STORAGE} permissions respectively, with the |
| 65 | * latter including the former. This approach is discouraged, since users may be hesitant to grant |
| 66 | * broad access to all files contained on a storage device. |
| 67 | * </ul> |
| 68 | * |
| 69 | * <p>It can be obtained through {@link StorageManager#getVolumeList()} and |
| 70 | * {@link StorageManager#getPrimaryVolume()} and also as an extra in some broadcasts |
| 71 | * (see {@link #EXTRA_STORAGE_VOLUME}). |
| 72 | * |
| 73 | * <p> |
| 74 | * See {@link Environment#getExternalStorageDirectory()} for more info about shared/external |
| 75 | * storage semantics. |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 76 | */ |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 77 | // NOTE: This is a legacy specialization of VolumeInfo which describes the volume for a specific |
| 78 | // user, but is now part of the public API. |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 79 | public class StorageVolume implements Parcelable { |
| 80 | |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 81 | private final String mId; |
| 82 | private final int mStorageId; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 83 | private final File mPath; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 84 | private final String mDescription; |
Jeff Sharkey | 9545dc02 | 2012-09-06 22:46:30 -0700 | [diff] [blame] | 85 | private final boolean mPrimary; |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 86 | private final boolean mRemovable; |
| 87 | private final boolean mEmulated; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 88 | private final long mMtpReserveSize; |
Mike Lockwood | 8e8b280 | 2011-06-07 08:03:33 -0700 | [diff] [blame] | 89 | private final boolean mAllowMassStorage; |
Mike Lockwood | 7a59dd2 | 2011-07-11 09:18:03 -0400 | [diff] [blame] | 90 | private final long mMaxFileSize; |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 91 | private final UserHandle mOwner; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 92 | private final String mFsUuid; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 93 | private final String mState; |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 94 | |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 95 | /** |
| 96 | * Name of the {@link Parcelable} extra in the {@link Intent#ACTION_MEDIA_REMOVED}, |
| 97 | * {@link Intent#ACTION_MEDIA_UNMOUNTED}, {@link Intent#ACTION_MEDIA_CHECKING}, |
| 98 | * {@link Intent#ACTION_MEDIA_NOFS}, {@link Intent#ACTION_MEDIA_MOUNTED}, |
| 99 | * {@link Intent#ACTION_MEDIA_SHARED}, {@link Intent#ACTION_MEDIA_BAD_REMOVAL}, |
| 100 | * {@link Intent#ACTION_MEDIA_UNMOUNTABLE}, and {@link Intent#ACTION_MEDIA_EJECT} broadcast that |
| 101 | * contains a {@link StorageVolume}. |
| 102 | */ |
| 103 | // Also sent on ACTION_MEDIA_UNSHARED, which is @hide |
| 104 | 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] | 105 | |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 106 | /** |
| 107 | * Name of the String extra used by {@link #createAccessIntent(String) createAccessIntent}. |
| 108 | * |
| 109 | * @hide |
| 110 | */ |
| 111 | public static final String EXTRA_DIRECTORY_NAME = "android.os.storage.extra.DIRECTORY_NAME"; |
| 112 | |
| 113 | /** |
| 114 | * Name of the intent used by {@link #createAccessIntent(String) createAccessIntent}. |
| 115 | */ |
| 116 | private static final String ACTION_OPEN_EXTERNAL_DIRECTORY = |
| 117 | "android.os.storage.action.OPEN_EXTERNAL_DIRECTORY"; |
| 118 | |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 119 | /** {@hide} */ |
Jeff Sharkey | 5af1835d | 2015-07-07 17:26:59 -0700 | [diff] [blame] | 120 | public static final int STORAGE_ID_INVALID = 0x00000000; |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 121 | /** {@hide} */ |
Jeff Sharkey | 5af1835d | 2015-07-07 17:26:59 -0700 | [diff] [blame] | 122 | public static final int STORAGE_ID_PRIMARY = 0x00010001; |
| 123 | |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 124 | /** {@hide} */ |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 125 | public StorageVolume(String id, int storageId, File path, String description, boolean primary, |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 126 | boolean removable, boolean emulated, long mtpReserveSize, boolean allowMassStorage, |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 127 | long maxFileSize, UserHandle owner, String fsUuid, String state) { |
| 128 | mId = Preconditions.checkNotNull(id); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 129 | mStorageId = storageId; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 130 | mPath = Preconditions.checkNotNull(path); |
| 131 | mDescription = Preconditions.checkNotNull(description); |
Jeff Sharkey | 9545dc02 | 2012-09-06 22:46:30 -0700 | [diff] [blame] | 132 | mPrimary = primary; |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 133 | mRemovable = removable; |
| 134 | mEmulated = emulated; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 135 | mMtpReserveSize = mtpReserveSize; |
Mike Lockwood | 8e8b280 | 2011-06-07 08:03:33 -0700 | [diff] [blame] | 136 | mAllowMassStorage = allowMassStorage; |
Mike Lockwood | 7a59dd2 | 2011-07-11 09:18:03 -0400 | [diff] [blame] | 137 | mMaxFileSize = maxFileSize; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 138 | mOwner = Preconditions.checkNotNull(owner); |
| 139 | mFsUuid = fsUuid; |
| 140 | mState = Preconditions.checkNotNull(state); |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Jeff Sharkey | 9545dc02 | 2012-09-06 22:46:30 -0700 | [diff] [blame] | 143 | private StorageVolume(Parcel in) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 144 | mId = in.readString(); |
Jeff Sharkey | 9545dc02 | 2012-09-06 22:46:30 -0700 | [diff] [blame] | 145 | mStorageId = in.readInt(); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 146 | mPath = new File(in.readString()); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 147 | mDescription = in.readString(); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 148 | mPrimary = in.readInt() != 0; |
| 149 | mRemovable = in.readInt() != 0; |
| 150 | mEmulated = in.readInt() != 0; |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 151 | mMtpReserveSize = in.readLong(); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 152 | mAllowMassStorage = in.readInt() != 0; |
Jeff Sharkey | 9545dc02 | 2012-09-06 22:46:30 -0700 | [diff] [blame] | 153 | mMaxFileSize = in.readLong(); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 154 | mOwner = in.readParcelable(null); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 155 | mFsUuid = in.readString(); |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 156 | mState = in.readString(); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 159 | /** {@hide} */ |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 160 | public String getId() { |
| 161 | return mId; |
Mike Lockwood | fbfe555 | 2011-05-17 17:19:37 -0400 | [diff] [blame] | 162 | } |
| 163 | |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 164 | /** |
| 165 | * Returns the mount path for the volume. |
| 166 | * |
| 167 | * @return the mount path |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 168 | * @hide |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 169 | */ |
| 170 | public String getPath() { |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 171 | return mPath.toString(); |
| 172 | } |
| 173 | |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 174 | /** {@hide} */ |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 175 | public File getPathFile() { |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 176 | return mPath; |
| 177 | } |
| 178 | |
| 179 | /** |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 180 | * Returns a user-visible description of the volume. |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 181 | * |
| 182 | * @return the volume description |
| 183 | */ |
Fabrice Di Meglio | 13fe2a5 | 2012-05-18 18:08:58 -0700 | [diff] [blame] | 184 | public String getDescription(Context context) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 185 | return mDescription; |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 188 | /** |
| 189 | * Returns true if the volume is the primary shared/external storage, which is the volume |
| 190 | * backed by {@link Environment#getExternalStorageDirectory()}. |
| 191 | */ |
Jeff Sharkey | 9545dc02 | 2012-09-06 22:46:30 -0700 | [diff] [blame] | 192 | public boolean isPrimary() { |
| 193 | return mPrimary; |
| 194 | } |
| 195 | |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 196 | /** |
| 197 | * Returns true if the volume is removable. |
| 198 | * |
| 199 | * @return is removable |
| 200 | */ |
| 201 | public boolean isRemovable() { |
| 202 | return mRemovable; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Returns true if the volume is emulated. |
| 207 | * |
| 208 | * @return is removable |
| 209 | */ |
| 210 | public boolean isEmulated() { |
| 211 | return mEmulated; |
| 212 | } |
| 213 | |
| 214 | /** |
Mike Lockwood | fbfe555 | 2011-05-17 17:19:37 -0400 | [diff] [blame] | 215 | * Returns the MTP storage ID for the volume. |
| 216 | * this is also used for the storage_id column in the media provider. |
| 217 | * |
| 218 | * @return MTP storage ID |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 219 | * @hide |
Mike Lockwood | fbfe555 | 2011-05-17 17:19:37 -0400 | [diff] [blame] | 220 | */ |
| 221 | public int getStorageId() { |
| 222 | return mStorageId; |
| 223 | } |
| 224 | |
| 225 | /** |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 226 | * Number of megabytes of space to leave unallocated by MTP. |
| 227 | * MTP will subtract this value from the free space it reports back |
| 228 | * to the host via GetStorageInfo, and will not allow new files to |
| 229 | * be added via MTP if there is less than this amount left free in the storage. |
| 230 | * If MTP has dedicated storage this value should be zero, but if MTP is |
| 231 | * sharing storage with the rest of the system, set this to a positive value |
| 232 | * to ensure that MTP activity does not result in the storage being |
| 233 | * too close to full. |
| 234 | * |
| 235 | * @return MTP reserve space |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 236 | * @hide |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 237 | */ |
| 238 | public int getMtpReserveSpace() { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 239 | return (int) (mMtpReserveSize / TrafficStats.MB_IN_BYTES); |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Mike Lockwood | 8e8b280 | 2011-06-07 08:03:33 -0700 | [diff] [blame] | 242 | /** |
| 243 | * Returns true if this volume can be shared via USB mass storage. |
| 244 | * |
| 245 | * @return whether mass storage is allowed |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 246 | * @hide |
Mike Lockwood | 8e8b280 | 2011-06-07 08:03:33 -0700 | [diff] [blame] | 247 | */ |
| 248 | public boolean allowMassStorage() { |
| 249 | return mAllowMassStorage; |
| 250 | } |
| 251 | |
Mike Lockwood | 7a59dd2 | 2011-07-11 09:18:03 -0400 | [diff] [blame] | 252 | /** |
| 253 | * Returns maximum file size for the volume, or zero if it is unbounded. |
| 254 | * |
| 255 | * @return maximum file size |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 256 | * @hide |
Mike Lockwood | 7a59dd2 | 2011-07-11 09:18:03 -0400 | [diff] [blame] | 257 | */ |
| 258 | public long getMaxFileSize() { |
| 259 | return mMaxFileSize; |
| 260 | } |
| 261 | |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 262 | /** {@hide} */ |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 263 | public UserHandle getOwner() { |
| 264 | return mOwner; |
| 265 | } |
| 266 | |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 267 | /** |
| 268 | * Gets the volume UUID, if any. |
| 269 | */ |
| 270 | public @Nullable String getUuid() { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 271 | return mFsUuid; |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Parse and return volume UUID as FAT volume ID, or return -1 if unable to |
| 276 | * parse or UUID is unknown. |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 277 | * @hide |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 278 | */ |
| 279 | public int getFatVolumeId() { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 280 | if (mFsUuid == null || mFsUuid.length() != 9) { |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 281 | return -1; |
| 282 | } |
| 283 | try { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 284 | return (int) Long.parseLong(mFsUuid.replace("-", ""), 16); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 285 | } catch (NumberFormatException e) { |
| 286 | return -1; |
| 287 | } |
| 288 | } |
| 289 | |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 290 | /** {@hide} */ |
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 | /** |
| 309 | * Builds an intent to give access to a standard storage directory after obtaining the user's |
| 310 | * approval. |
| 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. |
| 319 | * |
| 320 | * <b>If your application only needs to store internal data, consider using |
| 321 | * {@link Context#getExternalFilesDirs(String) Context.getExternalFilesDirs}, |
| 322 | * {@link Context#getExternalCacheDirs()}, or |
| 323 | * {@link Context#getExternalMediaDirs()}, which require no permissions to read or write. |
| 324 | * |
| 325 | * @param directoryName must be one of |
| 326 | * {@link Environment#DIRECTORY_MUSIC}, {@link Environment#DIRECTORY_PODCASTS}, |
| 327 | * {@link Environment#DIRECTORY_RINGTONES}, {@link Environment#DIRECTORY_ALARMS}, |
| 328 | * {@link Environment#DIRECTORY_NOTIFICATIONS}, {@link Environment#DIRECTORY_PICTURES}, |
| 329 | * {@link Environment#DIRECTORY_MOVIES}, {@link Environment#DIRECTORY_DOWNLOADS}, |
| 330 | * {@link Environment#DIRECTORY_DCIM}, or {@link Environment#DIRECTORY_DOCUMENTS} |
| 331 | * |
| 332 | * @see DocumentsContract |
| 333 | */ |
| 334 | public Intent createAccessIntent(@NonNull String directoryName) { |
Felipe Leme | 34a9d52 | 2016-02-17 10:12:04 -0800 | [diff] [blame] | 335 | final Intent intent = new Intent(ACTION_OPEN_EXTERNAL_DIRECTORY); |
| 336 | intent.putExtra(EXTRA_STORAGE_VOLUME, this); |
| 337 | intent.putExtra(EXTRA_DIRECTORY_NAME, directoryName); |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 338 | return intent; |
| 339 | } |
| 340 | |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 341 | @Override |
| 342 | public boolean equals(Object obj) { |
| 343 | if (obj instanceof StorageVolume && mPath != null) { |
| 344 | StorageVolume volume = (StorageVolume)obj; |
| 345 | return (mPath.equals(volume.mPath)); |
| 346 | } |
| 347 | return false; |
| 348 | } |
| 349 | |
| 350 | @Override |
| 351 | public int hashCode() { |
| 352 | return mPath.hashCode(); |
| 353 | } |
| 354 | |
| 355 | @Override |
| 356 | public String toString() { |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 357 | final StringBuilder buffer = new StringBuilder("StorageVolume: ").append(mDescription); |
| 358 | if (mFsUuid != null) { |
| 359 | buffer.append(" (").append(mFsUuid).append(")"); |
| 360 | } |
| 361 | return buffer.toString(); |
| 362 | } |
| 363 | |
| 364 | /** {@hide} */ |
| 365 | // TODO(b/26742218): find out where toString() is called internally and replace these calls by |
| 366 | // dump(). |
| 367 | public String dump() { |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 368 | final CharArrayWriter writer = new CharArrayWriter(); |
| 369 | dump(new IndentingPrintWriter(writer, " ", 80)); |
| 370 | return writer.toString(); |
| 371 | } |
| 372 | |
Felipe Leme | 04a5d40 | 2016-02-08 16:44:06 -0800 | [diff] [blame] | 373 | /** {@hide} */ |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 374 | public void dump(IndentingPrintWriter pw) { |
| 375 | pw.println("StorageVolume:"); |
| 376 | pw.increaseIndent(); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 377 | pw.printPair("mId", mId); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 378 | pw.printPair("mStorageId", mStorageId); |
| 379 | pw.printPair("mPath", mPath); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 380 | pw.printPair("mDescription", mDescription); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 381 | pw.printPair("mPrimary", mPrimary); |
| 382 | pw.printPair("mRemovable", mRemovable); |
| 383 | pw.printPair("mEmulated", mEmulated); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 384 | pw.printPair("mMtpReserveSize", mMtpReserveSize); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 385 | pw.printPair("mAllowMassStorage", mAllowMassStorage); |
| 386 | pw.printPair("mMaxFileSize", mMaxFileSize); |
| 387 | pw.printPair("mOwner", mOwner); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 388 | pw.printPair("mFsUuid", mFsUuid); |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 389 | pw.printPair("mState", mState); |
Jeff Sharkey | 5aca2b8 | 2013-10-16 16:21:54 -0700 | [diff] [blame] | 390 | pw.decreaseIndent(); |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Jeff Sharkey | 9545dc02 | 2012-09-06 22:46:30 -0700 | [diff] [blame] | 393 | public static final Creator<StorageVolume> CREATOR = new Creator<StorageVolume>() { |
| 394 | @Override |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 395 | public StorageVolume createFromParcel(Parcel in) { |
Jeff Sharkey | 9545dc02 | 2012-09-06 22:46:30 -0700 | [diff] [blame] | 396 | return new StorageVolume(in); |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Jeff Sharkey | 9545dc02 | 2012-09-06 22:46:30 -0700 | [diff] [blame] | 399 | @Override |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 400 | public StorageVolume[] newArray(int size) { |
| 401 | return new StorageVolume[size]; |
| 402 | } |
| 403 | }; |
| 404 | |
Jeff Sharkey | 9545dc02 | 2012-09-06 22:46:30 -0700 | [diff] [blame] | 405 | @Override |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 406 | public int describeContents() { |
| 407 | return 0; |
| 408 | } |
| 409 | |
Jeff Sharkey | 9545dc02 | 2012-09-06 22:46:30 -0700 | [diff] [blame] | 410 | @Override |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 411 | public void writeToParcel(Parcel parcel, int flags) { |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 412 | parcel.writeString(mId); |
Jeff Sharkey | 9545dc02 | 2012-09-06 22:46:30 -0700 | [diff] [blame] | 413 | parcel.writeInt(mStorageId); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 414 | parcel.writeString(mPath.toString()); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 415 | parcel.writeString(mDescription); |
Jeff Sharkey | 9545dc02 | 2012-09-06 22:46:30 -0700 | [diff] [blame] | 416 | parcel.writeInt(mPrimary ? 1 : 0); |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 417 | parcel.writeInt(mRemovable ? 1 : 0); |
| 418 | parcel.writeInt(mEmulated ? 1 : 0); |
Jeff Sharkey | 4887789 | 2015-03-18 11:27:19 -0700 | [diff] [blame] | 419 | parcel.writeLong(mMtpReserveSize); |
Mike Lockwood | 8e8b280 | 2011-06-07 08:03:33 -0700 | [diff] [blame] | 420 | parcel.writeInt(mAllowMassStorage ? 1 : 0); |
Mike Lockwood | 7a59dd2 | 2011-07-11 09:18:03 -0400 | [diff] [blame] | 421 | parcel.writeLong(mMaxFileSize); |
Jeff Sharkey | b049e21 | 2012-09-07 23:16:01 -0700 | [diff] [blame] | 422 | parcel.writeParcelable(mOwner, flags); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 423 | parcel.writeString(mFsUuid); |
Jeff Sharkey | 1f706c6 | 2013-10-17 10:52:17 -0700 | [diff] [blame] | 424 | parcel.writeString(mState); |
Mike Lockwood | 2f6a388 | 2011-05-09 19:08:06 -0700 | [diff] [blame] | 425 | } |
| 426 | } |