Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 23 | import android.content.res.Resources; |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 24 | import android.net.Uri; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 25 | import android.os.Environment; |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 26 | import android.os.IVold; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 27 | import android.os.Parcel; |
| 28 | import android.os.Parcelable; |
| 29 | import android.os.UserHandle; |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 30 | import android.provider.DocumentsContract; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 31 | import android.text.TextUtils; |
| 32 | import android.util.ArrayMap; |
| 33 | import android.util.DebugUtils; |
| 34 | import android.util.SparseArray; |
Jeff Sharkey | 5fc2473 | 2015-06-10 14:21:27 -0700 | [diff] [blame] | 35 | import android.util.SparseIntArray; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 36 | |
Jeff Sharkey | 5fc2473 | 2015-06-10 14:21:27 -0700 | [diff] [blame] | 37 | import com.android.internal.R; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 38 | import com.android.internal.util.IndentingPrintWriter; |
| 39 | import com.android.internal.util.Preconditions; |
| 40 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 41 | import java.io.CharArrayWriter; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 42 | import java.io.File; |
Jeff Sharkey | e2d45be | 2015-04-15 17:14:12 -0700 | [diff] [blame] | 43 | import java.util.Comparator; |
| 44 | import java.util.Objects; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Information about a storage volume that may be mounted. A volume may be a |
| 48 | * partition on a physical {@link DiskInfo}, an emulated volume above some other |
| 49 | * storage medium, or a standalone container like an ASEC or OBB. |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 50 | * <p> |
| 51 | * Volumes may be mounted with various flags: |
| 52 | * <ul> |
| 53 | * <li>{@link #MOUNT_FLAG_PRIMARY} means the volume provides primary external |
| 54 | * storage, historically found at {@code /sdcard}. |
| 55 | * <li>{@link #MOUNT_FLAG_VISIBLE} means the volume is visible to third-party |
| 56 | * apps for direct filesystem access. The system should send out relevant |
| 57 | * storage broadcasts and index any media on visible volumes. Visible volumes |
| 58 | * are considered a more stable part of the device, which is why we take the |
| 59 | * time to index them. In particular, transient volumes like USB OTG devices |
| 60 | * <em>should not</em> be marked as visible; their contents should be surfaced |
| 61 | * to apps through the Storage Access Framework. |
| 62 | * </ul> |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 63 | * |
| 64 | * @hide |
| 65 | */ |
| 66 | public class VolumeInfo implements Parcelable { |
Jeff Sharkey | e6c04f9 | 2015-04-18 21:38:05 -0700 | [diff] [blame] | 67 | public static final String ACTION_VOLUME_STATE_CHANGED = |
| 68 | "android.os.storage.action.VOLUME_STATE_CHANGED"; |
| 69 | public static final String EXTRA_VOLUME_ID = |
| 70 | "android.os.storage.extra.VOLUME_ID"; |
Jeff Sharkey | c7acac6 | 2015-06-12 16:16:56 -0700 | [diff] [blame] | 71 | public static final String EXTRA_VOLUME_STATE = |
| 72 | "android.os.storage.extra.VOLUME_STATE"; |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 73 | |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 74 | /** Stub volume representing internal private storage */ |
| 75 | public static final String ID_PRIVATE_INTERNAL = "private"; |
Jeff Sharkey | b2b9ab8 | 2015-04-05 21:10:42 -0700 | [diff] [blame] | 76 | /** Real volume representing internal emulated storage */ |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 77 | public static final String ID_EMULATED_INTERNAL = "emulated"; |
| 78 | |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 79 | public static final int TYPE_PUBLIC = IVold.VOLUME_TYPE_PUBLIC; |
| 80 | public static final int TYPE_PRIVATE = IVold.VOLUME_TYPE_PRIVATE; |
| 81 | public static final int TYPE_EMULATED = IVold.VOLUME_TYPE_EMULATED; |
| 82 | public static final int TYPE_ASEC = IVold.VOLUME_TYPE_ASEC; |
| 83 | public static final int TYPE_OBB = IVold.VOLUME_TYPE_OBB; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 84 | |
Jeff Sharkey | 8058fe6 | 2017-09-13 11:50:33 -0600 | [diff] [blame] | 85 | public static final int STATE_UNMOUNTED = IVold.VOLUME_STATE_UNMOUNTED; |
| 86 | public static final int STATE_CHECKING = IVold.VOLUME_STATE_CHECKING; |
| 87 | public static final int STATE_MOUNTED = IVold.VOLUME_STATE_MOUNTED; |
| 88 | public static final int STATE_MOUNTED_READ_ONLY = IVold.VOLUME_STATE_MOUNTED_READ_ONLY; |
| 89 | public static final int STATE_FORMATTING = IVold.VOLUME_STATE_FORMATTING; |
| 90 | public static final int STATE_EJECTING = IVold.VOLUME_STATE_EJECTING; |
| 91 | public static final int STATE_UNMOUNTABLE = IVold.VOLUME_STATE_UNMOUNTABLE; |
| 92 | public static final int STATE_REMOVED = IVold.VOLUME_STATE_REMOVED; |
| 93 | public static final int STATE_BAD_REMOVAL = IVold.VOLUME_STATE_BAD_REMOVAL; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 94 | |
Jeff Sharkey | ace874b | 2017-09-07 15:27:33 -0600 | [diff] [blame] | 95 | public static final int MOUNT_FLAG_PRIMARY = IVold.MOUNT_FLAG_PRIMARY; |
| 96 | public static final int MOUNT_FLAG_VISIBLE = IVold.MOUNT_FLAG_VISIBLE; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 97 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 98 | private static SparseArray<String> sStateToEnvironment = new SparseArray<>(); |
| 99 | private static ArrayMap<String, String> sEnvironmentToBroadcast = new ArrayMap<>(); |
Jeff Sharkey | 5fc2473 | 2015-06-10 14:21:27 -0700 | [diff] [blame] | 100 | private static SparseIntArray sStateToDescrip = new SparseIntArray(); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 101 | |
Jeff Sharkey | e2d45be | 2015-04-15 17:14:12 -0700 | [diff] [blame] | 102 | private static final Comparator<VolumeInfo> |
| 103 | sDescriptionComparator = new Comparator<VolumeInfo>() { |
| 104 | @Override |
| 105 | public int compare(VolumeInfo lhs, VolumeInfo rhs) { |
| 106 | if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(lhs.getId())) { |
| 107 | return -1; |
| 108 | } else if (lhs.getDescription() == null) { |
| 109 | return 1; |
| 110 | } else if (rhs.getDescription() == null) { |
| 111 | return -1; |
| 112 | } else { |
| 113 | return lhs.getDescription().compareTo(rhs.getDescription()); |
| 114 | } |
| 115 | } |
| 116 | }; |
| 117 | |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 118 | static { |
| 119 | sStateToEnvironment.put(VolumeInfo.STATE_UNMOUNTED, Environment.MEDIA_UNMOUNTED); |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 120 | sStateToEnvironment.put(VolumeInfo.STATE_CHECKING, Environment.MEDIA_CHECKING); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 121 | sStateToEnvironment.put(VolumeInfo.STATE_MOUNTED, Environment.MEDIA_MOUNTED); |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 122 | sStateToEnvironment.put(VolumeInfo.STATE_MOUNTED_READ_ONLY, Environment.MEDIA_MOUNTED_READ_ONLY); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 123 | sStateToEnvironment.put(VolumeInfo.STATE_FORMATTING, Environment.MEDIA_UNMOUNTED); |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 124 | sStateToEnvironment.put(VolumeInfo.STATE_EJECTING, Environment.MEDIA_EJECTING); |
Jeff Sharkey | 16c9c24 | 2015-04-04 21:37:20 -0700 | [diff] [blame] | 125 | sStateToEnvironment.put(VolumeInfo.STATE_UNMOUNTABLE, Environment.MEDIA_UNMOUNTABLE); |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 126 | sStateToEnvironment.put(VolumeInfo.STATE_REMOVED, Environment.MEDIA_REMOVED); |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 127 | sStateToEnvironment.put(VolumeInfo.STATE_BAD_REMOVAL, Environment.MEDIA_BAD_REMOVAL); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 128 | |
| 129 | sEnvironmentToBroadcast.put(Environment.MEDIA_UNMOUNTED, Intent.ACTION_MEDIA_UNMOUNTED); |
| 130 | sEnvironmentToBroadcast.put(Environment.MEDIA_CHECKING, Intent.ACTION_MEDIA_CHECKING); |
| 131 | sEnvironmentToBroadcast.put(Environment.MEDIA_MOUNTED, Intent.ACTION_MEDIA_MOUNTED); |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 132 | sEnvironmentToBroadcast.put(Environment.MEDIA_MOUNTED_READ_ONLY, Intent.ACTION_MEDIA_MOUNTED); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 133 | sEnvironmentToBroadcast.put(Environment.MEDIA_EJECTING, Intent.ACTION_MEDIA_EJECT); |
Jeff Sharkey | 16c9c24 | 2015-04-04 21:37:20 -0700 | [diff] [blame] | 134 | sEnvironmentToBroadcast.put(Environment.MEDIA_UNMOUNTABLE, Intent.ACTION_MEDIA_UNMOUNTABLE); |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 135 | sEnvironmentToBroadcast.put(Environment.MEDIA_REMOVED, Intent.ACTION_MEDIA_REMOVED); |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 136 | sEnvironmentToBroadcast.put(Environment.MEDIA_BAD_REMOVAL, Intent.ACTION_MEDIA_BAD_REMOVAL); |
Jeff Sharkey | 5fc2473 | 2015-06-10 14:21:27 -0700 | [diff] [blame] | 137 | |
| 138 | sStateToDescrip.put(VolumeInfo.STATE_UNMOUNTED, R.string.ext_media_status_unmounted); |
| 139 | sStateToDescrip.put(VolumeInfo.STATE_CHECKING, R.string.ext_media_status_checking); |
| 140 | sStateToDescrip.put(VolumeInfo.STATE_MOUNTED, R.string.ext_media_status_mounted); |
| 141 | sStateToDescrip.put(VolumeInfo.STATE_MOUNTED_READ_ONLY, R.string.ext_media_status_mounted_ro); |
| 142 | sStateToDescrip.put(VolumeInfo.STATE_FORMATTING, R.string.ext_media_status_formatting); |
| 143 | sStateToDescrip.put(VolumeInfo.STATE_EJECTING, R.string.ext_media_status_ejecting); |
| 144 | sStateToDescrip.put(VolumeInfo.STATE_UNMOUNTABLE, R.string.ext_media_status_unmountable); |
| 145 | sStateToDescrip.put(VolumeInfo.STATE_REMOVED, R.string.ext_media_status_removed); |
| 146 | sStateToDescrip.put(VolumeInfo.STATE_BAD_REMOVAL, R.string.ext_media_status_bad_removal); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /** vold state */ |
| 150 | public final String id; |
| 151 | public final int type; |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 152 | public final DiskInfo disk; |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 153 | public final String partGuid; |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 154 | public int mountFlags = 0; |
| 155 | public int mountUserId = -1; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 156 | public int state = STATE_UNMOUNTED; |
| 157 | public String fsType; |
| 158 | public String fsUuid; |
| 159 | public String fsLabel; |
| 160 | public String path; |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 161 | public String internalPath; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 162 | |
Jeff Sharkey | 5af1835d | 2015-07-07 17:26:59 -0700 | [diff] [blame] | 163 | public VolumeInfo(String id, int type, DiskInfo disk, String partGuid) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 164 | this.id = Preconditions.checkNotNull(id); |
| 165 | this.type = type; |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 166 | this.disk = disk; |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 167 | this.partGuid = partGuid; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | public VolumeInfo(Parcel parcel) { |
| 171 | id = parcel.readString(); |
| 172 | type = parcel.readInt(); |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 173 | if (parcel.readInt() != 0) { |
| 174 | disk = DiskInfo.CREATOR.createFromParcel(parcel); |
| 175 | } else { |
| 176 | disk = null; |
| 177 | } |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 178 | partGuid = parcel.readString(); |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 179 | mountFlags = parcel.readInt(); |
| 180 | mountUserId = parcel.readInt(); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 181 | state = parcel.readInt(); |
| 182 | fsType = parcel.readString(); |
| 183 | fsUuid = parcel.readString(); |
| 184 | fsLabel = parcel.readString(); |
| 185 | path = parcel.readString(); |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 186 | internalPath = parcel.readString(); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 189 | public static @NonNull String getEnvironmentForState(int state) { |
| 190 | final String envState = sStateToEnvironment.get(state); |
| 191 | if (envState != null) { |
| 192 | return envState; |
| 193 | } else { |
| 194 | return Environment.MEDIA_UNKNOWN; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | public static @Nullable String getBroadcastForEnvironment(String envState) { |
| 199 | return sEnvironmentToBroadcast.get(envState); |
| 200 | } |
| 201 | |
| 202 | public static @Nullable String getBroadcastForState(int state) { |
| 203 | return getBroadcastForEnvironment(getEnvironmentForState(state)); |
| 204 | } |
| 205 | |
Jeff Sharkey | e2d45be | 2015-04-15 17:14:12 -0700 | [diff] [blame] | 206 | public static @NonNull Comparator<VolumeInfo> getDescriptionComparator() { |
| 207 | return sDescriptionComparator; |
| 208 | } |
| 209 | |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 210 | public @NonNull String getId() { |
| 211 | return id; |
| 212 | } |
| 213 | |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 214 | public @Nullable DiskInfo getDisk() { |
| 215 | return disk; |
| 216 | } |
| 217 | |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 218 | public @Nullable String getDiskId() { |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 219 | return (disk != null) ? disk.id : null; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | public int getType() { |
| 223 | return type; |
| 224 | } |
| 225 | |
| 226 | public int getState() { |
| 227 | return state; |
| 228 | } |
| 229 | |
Jeff Sharkey | 5fc2473 | 2015-06-10 14:21:27 -0700 | [diff] [blame] | 230 | public int getStateDescription() { |
| 231 | return sStateToDescrip.get(state, 0); |
| 232 | } |
| 233 | |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 234 | public @Nullable String getFsUuid() { |
| 235 | return fsUuid; |
| 236 | } |
| 237 | |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 238 | public int getMountUserId() { |
| 239 | return mountUserId; |
| 240 | } |
| 241 | |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 242 | public @Nullable String getDescription() { |
Jeff Sharkey | 7a788a8 | 2015-07-07 14:33:55 -0700 | [diff] [blame] | 243 | if (ID_PRIVATE_INTERNAL.equals(id) || ID_EMULATED_INTERNAL.equals(id)) { |
Jeff Sharkey | 59d577a | 2015-04-11 21:27:21 -0700 | [diff] [blame] | 244 | return Resources.getSystem().getString(com.android.internal.R.string.storage_internal); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 245 | } else if (!TextUtils.isEmpty(fsLabel)) { |
| 246 | return fsLabel; |
| 247 | } else { |
| 248 | return null; |
| 249 | } |
| 250 | } |
| 251 | |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 252 | public boolean isMountedReadable() { |
| 253 | return state == STATE_MOUNTED || state == STATE_MOUNTED_READ_ONLY; |
| 254 | } |
| 255 | |
| 256 | public boolean isMountedWritable() { |
| 257 | return state == STATE_MOUNTED; |
| 258 | } |
| 259 | |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 260 | public boolean isPrimary() { |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 261 | return (mountFlags & MOUNT_FLAG_PRIMARY) != 0; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Jeff Sharkey | 620b32b | 2015-04-23 19:36:02 -0700 | [diff] [blame] | 264 | public boolean isPrimaryPhysical() { |
| 265 | return isPrimary() && (getType() == TYPE_PUBLIC); |
| 266 | } |
| 267 | |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 268 | public boolean isVisible() { |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 269 | return (mountFlags & MOUNT_FLAG_VISIBLE) != 0; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 272 | public boolean isVisibleForRead(int userId) { |
| 273 | if (type == TYPE_PUBLIC) { |
| 274 | if (isPrimary() && mountUserId != userId) { |
| 275 | // Primary physical is only visible to single user |
| 276 | return false; |
| 277 | } else { |
| 278 | return isVisible(); |
| 279 | } |
| 280 | } else if (type == TYPE_EMULATED) { |
| 281 | return isVisible(); |
| 282 | } else { |
| 283 | return false; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | public boolean isVisibleForWrite(int userId) { |
| 288 | if (type == TYPE_PUBLIC && mountUserId == userId) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 289 | return isVisible(); |
| 290 | } else if (type == TYPE_EMULATED) { |
| 291 | return isVisible(); |
| 292 | } else { |
| 293 | return false; |
| 294 | } |
| 295 | } |
| 296 | |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 297 | public File getPath() { |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 298 | return (path != null) ? new File(path) : null; |
| 299 | } |
| 300 | |
| 301 | public File getInternalPath() { |
| 302 | return (internalPath != null) ? new File(internalPath) : null; |
Jeff Sharkey | d95d3bf | 2015-04-14 21:39:44 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 305 | public File getPathForUser(int userId) { |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 306 | if (path == null) { |
| 307 | return null; |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 308 | } else if (type == TYPE_PUBLIC) { |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 309 | return new File(path); |
| 310 | } else if (type == TYPE_EMULATED) { |
| 311 | return new File(path, Integer.toString(userId)); |
| 312 | } else { |
| 313 | return null; |
| 314 | } |
| 315 | } |
| 316 | |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 317 | /** |
| 318 | * Path which is accessible to apps holding |
| 319 | * {@link android.Manifest.permission#WRITE_MEDIA_STORAGE}. |
| 320 | */ |
| 321 | public File getInternalPathForUser(int userId) { |
| 322 | if (type == TYPE_PUBLIC) { |
| 323 | // TODO: plumb through cleaner path from vold |
| 324 | return new File(path.replace("/storage/", "/mnt/media_rw/")); |
| 325 | } else { |
| 326 | return getPathForUser(userId); |
| 327 | } |
| 328 | } |
| 329 | |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 330 | public StorageVolume buildStorageVolume(Context context, int userId, boolean reportUnmounted) { |
Jeff Sharkey | a83bf19 | 2015-07-08 09:18:20 -0700 | [diff] [blame] | 331 | final StorageManager storage = context.getSystemService(StorageManager.class); |
| 332 | |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 333 | final boolean removable; |
| 334 | final boolean emulated; |
| 335 | final boolean allowMassStorage = false; |
Svet Ganov | 6ee871e | 2015-07-10 14:29:33 -0700 | [diff] [blame] | 336 | final String envState = reportUnmounted |
| 337 | ? Environment.MEDIA_UNMOUNTED : getEnvironmentForState(state); |
Jeff Sharkey | 4634987 | 2015-07-28 10:49:47 -0700 | [diff] [blame] | 338 | |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 339 | File userPath = getPathForUser(userId); |
| 340 | if (userPath == null) { |
| 341 | userPath = new File("/dev/null"); |
| 342 | } |
| 343 | |
Jeff Sharkey | a83bf19 | 2015-07-08 09:18:20 -0700 | [diff] [blame] | 344 | String description = null; |
Jeff Sharkey | 8e2ea2a | 2015-08-19 14:15:33 -0700 | [diff] [blame] | 345 | String derivedFsUuid = fsUuid; |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 346 | long maxFileSize = 0; |
| 347 | |
| 348 | if (type == TYPE_EMULATED) { |
| 349 | emulated = true; |
Jeff Sharkey | 5af1835d | 2015-07-07 17:26:59 -0700 | [diff] [blame] | 350 | |
Jeff Sharkey | a83bf19 | 2015-07-08 09:18:20 -0700 | [diff] [blame] | 351 | final VolumeInfo privateVol = storage.findPrivateForEmulated(this); |
| 352 | if (privateVol != null) { |
| 353 | description = storage.getBestVolumeDescription(privateVol); |
Jeff Sharkey | 8e2ea2a | 2015-08-19 14:15:33 -0700 | [diff] [blame] | 354 | derivedFsUuid = privateVol.fsUuid; |
Jeff Sharkey | a83bf19 | 2015-07-08 09:18:20 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 357 | if (ID_EMULATED_INTERNAL.equals(id)) { |
| 358 | removable = false; |
| 359 | } else { |
| 360 | removable = true; |
| 361 | } |
| 362 | |
| 363 | } else if (type == TYPE_PUBLIC) { |
| 364 | emulated = false; |
| 365 | removable = true; |
| 366 | |
Jeff Sharkey | a83bf19 | 2015-07-08 09:18:20 -0700 | [diff] [blame] | 367 | description = storage.getBestVolumeDescription(this); |
| 368 | |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 369 | if ("vfat".equals(fsType)) { |
| 370 | maxFileSize = 4294967295L; |
| 371 | } |
| 372 | |
| 373 | } else { |
| 374 | throw new IllegalStateException("Unexpected volume type " + type); |
| 375 | } |
| 376 | |
Jeff Sharkey | a83bf19 | 2015-07-08 09:18:20 -0700 | [diff] [blame] | 377 | if (description == null) { |
| 378 | description = context.getString(android.R.string.unknownName); |
| 379 | } |
| 380 | |
Jerry Zhang | f9c5c25 | 2017-08-16 18:07:51 -0700 | [diff] [blame^] | 381 | return new StorageVolume(id, userPath, description, isPrimary(), removable, |
| 382 | emulated, allowMassStorage, maxFileSize, new UserHandle(userId), |
Jeff Sharkey | 8e2ea2a | 2015-08-19 14:15:33 -0700 | [diff] [blame] | 383 | derivedFsUuid, envState); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Jeff Sharkey | 5af1835d | 2015-07-07 17:26:59 -0700 | [diff] [blame] | 386 | public static int buildStableMtpStorageId(String fsUuid) { |
| 387 | if (TextUtils.isEmpty(fsUuid)) { |
| 388 | return StorageVolume.STORAGE_ID_INVALID; |
| 389 | } else { |
| 390 | int hash = 0; |
| 391 | for (int i = 0; i < fsUuid.length(); ++i) { |
| 392 | hash = 31 * hash + fsUuid.charAt(i); |
| 393 | } |
| 394 | hash = (hash ^ (hash << 16)) & 0xffff0000; |
| 395 | // Work around values that the spec doesn't allow, or that we've |
| 396 | // reserved for primary |
| 397 | if (hash == 0x00000000) hash = 0x00020000; |
| 398 | if (hash == 0x00010000) hash = 0x00020000; |
| 399 | if (hash == 0xffff0000) hash = 0xfffe0000; |
| 400 | return hash | 0x0001; |
| 401 | } |
| 402 | } |
| 403 | |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 404 | // TODO: avoid this layering violation |
| 405 | private static final String DOCUMENT_AUTHORITY = "com.android.externalstorage.documents"; |
| 406 | private static final String DOCUMENT_ROOT_PRIMARY_EMULATED = "primary"; |
| 407 | |
| 408 | /** |
| 409 | * Build an intent to browse the contents of this volume. Only valid for |
| 410 | * {@link #TYPE_EMULATED} or {@link #TYPE_PUBLIC}. |
| 411 | */ |
| 412 | public Intent buildBrowseIntent() { |
| 413 | final Uri uri; |
| 414 | if (type == VolumeInfo.TYPE_PUBLIC) { |
| 415 | uri = DocumentsContract.buildRootUri(DOCUMENT_AUTHORITY, fsUuid); |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 416 | } else if (type == VolumeInfo.TYPE_EMULATED && isPrimary()) { |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 417 | uri = DocumentsContract.buildRootUri(DOCUMENT_AUTHORITY, |
| 418 | DOCUMENT_ROOT_PRIMARY_EMULATED); |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 419 | } else { |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 420 | return null; |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Garfield Tan | 5d3b37b | 2017-03-01 11:01:05 -0800 | [diff] [blame] | 423 | final Intent intent = new Intent(Intent.ACTION_VIEW); |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 424 | intent.addCategory(Intent.CATEGORY_DEFAULT); |
Jeff Sharkey | 42a4aaa | 2016-10-10 15:26:14 -0600 | [diff] [blame] | 425 | intent.setDataAndType(uri, DocumentsContract.Root.MIME_TYPE_ITEM); |
Aga Wronska | 1719b35 | 2016-03-21 11:28:03 -0700 | [diff] [blame] | 426 | |
| 427 | // note that docsui treats this as *force* show advanced. So sending |
| 428 | // false permits advanced to be shown based on user preferences. |
| 429 | intent.putExtra(DocumentsContract.EXTRA_SHOW_ADVANCED, isPrimary()); |
Jeff Sharkey | 56bd312 | 2015-04-14 10:30:34 -0700 | [diff] [blame] | 430 | return intent; |
| 431 | } |
| 432 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 433 | @Override |
| 434 | public String toString() { |
| 435 | final CharArrayWriter writer = new CharArrayWriter(); |
| 436 | dump(new IndentingPrintWriter(writer, " ", 80)); |
| 437 | return writer.toString(); |
| 438 | } |
| 439 | |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 440 | public void dump(IndentingPrintWriter pw) { |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 441 | pw.println("VolumeInfo{" + id + "}:"); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 442 | pw.increaseIndent(); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 443 | pw.printPair("type", DebugUtils.valueToString(getClass(), "TYPE_", type)); |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 444 | pw.printPair("diskId", getDiskId()); |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 445 | pw.printPair("partGuid", partGuid); |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 446 | pw.printPair("mountFlags", DebugUtils.flagsToString(getClass(), "MOUNT_FLAG_", mountFlags)); |
| 447 | pw.printPair("mountUserId", mountUserId); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 448 | pw.printPair("state", DebugUtils.valueToString(getClass(), "STATE_", state)); |
| 449 | pw.println(); |
| 450 | pw.printPair("fsType", fsType); |
| 451 | pw.printPair("fsUuid", fsUuid); |
| 452 | pw.printPair("fsLabel", fsLabel); |
| 453 | pw.println(); |
| 454 | pw.printPair("path", path); |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 455 | pw.printPair("internalPath", internalPath); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 456 | pw.decreaseIndent(); |
| 457 | pw.println(); |
| 458 | } |
| 459 | |
Jeff Sharkey | 7151a9a | 2015-04-04 15:22:37 -0700 | [diff] [blame] | 460 | @Override |
| 461 | public VolumeInfo clone() { |
| 462 | final Parcel temp = Parcel.obtain(); |
| 463 | try { |
| 464 | writeToParcel(temp, 0); |
| 465 | temp.setDataPosition(0); |
| 466 | return CREATOR.createFromParcel(temp); |
| 467 | } finally { |
| 468 | temp.recycle(); |
| 469 | } |
| 470 | } |
| 471 | |
Jeff Sharkey | e2d45be | 2015-04-15 17:14:12 -0700 | [diff] [blame] | 472 | @Override |
| 473 | public boolean equals(Object o) { |
| 474 | if (o instanceof VolumeInfo) { |
| 475 | return Objects.equals(id, ((VolumeInfo) o).id); |
| 476 | } else { |
| 477 | return false; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | @Override |
| 482 | public int hashCode() { |
| 483 | return id.hashCode(); |
| 484 | } |
| 485 | |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 486 | public static final Creator<VolumeInfo> CREATOR = new Creator<VolumeInfo>() { |
| 487 | @Override |
| 488 | public VolumeInfo createFromParcel(Parcel in) { |
| 489 | return new VolumeInfo(in); |
| 490 | } |
| 491 | |
| 492 | @Override |
| 493 | public VolumeInfo[] newArray(int size) { |
| 494 | return new VolumeInfo[size]; |
| 495 | } |
| 496 | }; |
| 497 | |
| 498 | @Override |
| 499 | public int describeContents() { |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | @Override |
| 504 | public void writeToParcel(Parcel parcel, int flags) { |
| 505 | parcel.writeString(id); |
| 506 | parcel.writeInt(type); |
Jeff Sharkey | 27de30d | 2015-04-18 16:20:27 -0700 | [diff] [blame] | 507 | if (disk != null) { |
| 508 | parcel.writeInt(1); |
| 509 | disk.writeToParcel(parcel, flags); |
| 510 | } else { |
| 511 | parcel.writeInt(0); |
| 512 | } |
Jeff Sharkey | 5cc0df2 | 2015-06-17 19:44:05 -0700 | [diff] [blame] | 513 | parcel.writeString(partGuid); |
Jeff Sharkey | 7e92ef3 | 2015-04-17 17:35:07 -0700 | [diff] [blame] | 514 | parcel.writeInt(mountFlags); |
| 515 | parcel.writeInt(mountUserId); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 516 | parcel.writeInt(state); |
| 517 | parcel.writeString(fsType); |
| 518 | parcel.writeString(fsUuid); |
| 519 | parcel.writeString(fsLabel); |
| 520 | parcel.writeString(path); |
Jeff Sharkey | 50a0545 | 2015-04-29 11:24:52 -0700 | [diff] [blame] | 521 | parcel.writeString(internalPath); |
Jeff Sharkey | 1b8ef7e | 2015-04-03 17:14:45 -0700 | [diff] [blame] | 522 | } |
| 523 | } |