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