VolumeInfo.java revision e2d45be4dae116307f8edd85eaa61134221cb8f9
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 17package android.os.storage; 18 19import android.annotation.NonNull; 20import android.annotation.Nullable; 21import android.content.Context; 22import android.content.Intent; 23import android.content.res.Resources; 24import android.mtp.MtpStorage; 25import android.net.Uri; 26import android.os.Environment; 27import android.os.Parcel; 28import android.os.Parcelable; 29import android.os.UserHandle; 30import android.provider.DocumentsContract; 31import android.text.TextUtils; 32import android.util.ArrayMap; 33import android.util.DebugUtils; 34import android.util.SparseArray; 35 36import com.android.internal.util.IndentingPrintWriter; 37import com.android.internal.util.Preconditions; 38 39import java.io.CharArrayWriter; 40import java.io.File; 41import java.util.Comparator; 42import java.util.Objects; 43 44/** 45 * Information about a storage volume that may be mounted. A volume may be a 46 * partition on a physical {@link DiskInfo}, an emulated volume above some other 47 * storage medium, or a standalone container like an ASEC or OBB. 48 * 49 * @hide 50 */ 51public class VolumeInfo implements Parcelable { 52 public static final String EXTRA_VOLUME_ID = "android.os.storage.extra.VOLUME_ID"; 53 54 /** Stub volume representing internal private storage */ 55 public static final String ID_PRIVATE_INTERNAL = "private"; 56 /** Real volume representing internal emulated storage */ 57 public static final String ID_EMULATED_INTERNAL = "emulated"; 58 59 public static final int TYPE_PUBLIC = 0; 60 public static final int TYPE_PRIVATE = 1; 61 public static final int TYPE_EMULATED = 2; 62 public static final int TYPE_ASEC = 3; 63 public static final int TYPE_OBB = 4; 64 65 public static final int STATE_UNMOUNTED = 0; 66 public static final int STATE_MOUNTING = 1; 67 public static final int STATE_MOUNTED = 2; 68 public static final int STATE_FORMATTING = 3; 69 public static final int STATE_UNMOUNTING = 4; 70 public static final int STATE_UNMOUNTABLE = 5; 71 public static final int STATE_REMOVED = 6; 72 73 public static final int FLAG_PRIMARY = 1 << 0; 74 public static final int FLAG_VISIBLE = 1 << 1; 75 76 public static final int USER_FLAG_INITED = 1 << 0; 77 public static final int USER_FLAG_SNOOZED = 1 << 1; 78 79 private static SparseArray<String> sStateToEnvironment = new SparseArray<>(); 80 private static ArrayMap<String, String> sEnvironmentToBroadcast = new ArrayMap<>(); 81 82 private static final Comparator<VolumeInfo> 83 sDescriptionComparator = new Comparator<VolumeInfo>() { 84 @Override 85 public int compare(VolumeInfo lhs, VolumeInfo rhs) { 86 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(lhs.getId())) { 87 return -1; 88 } else if (lhs.getDescription() == null) { 89 return 1; 90 } else if (rhs.getDescription() == null) { 91 return -1; 92 } else { 93 return lhs.getDescription().compareTo(rhs.getDescription()); 94 } 95 } 96 }; 97 98 static { 99 sStateToEnvironment.put(VolumeInfo.STATE_UNMOUNTED, Environment.MEDIA_UNMOUNTED); 100 sStateToEnvironment.put(VolumeInfo.STATE_MOUNTING, Environment.MEDIA_CHECKING); 101 sStateToEnvironment.put(VolumeInfo.STATE_MOUNTED, Environment.MEDIA_MOUNTED); 102 sStateToEnvironment.put(VolumeInfo.STATE_FORMATTING, Environment.MEDIA_UNMOUNTED); 103 sStateToEnvironment.put(VolumeInfo.STATE_UNMOUNTING, Environment.MEDIA_EJECTING); 104 sStateToEnvironment.put(VolumeInfo.STATE_UNMOUNTABLE, Environment.MEDIA_UNMOUNTABLE); 105 sStateToEnvironment.put(VolumeInfo.STATE_REMOVED, Environment.MEDIA_REMOVED); 106 107 sEnvironmentToBroadcast.put(Environment.MEDIA_UNMOUNTED, Intent.ACTION_MEDIA_UNMOUNTED); 108 sEnvironmentToBroadcast.put(Environment.MEDIA_CHECKING, Intent.ACTION_MEDIA_CHECKING); 109 sEnvironmentToBroadcast.put(Environment.MEDIA_MOUNTED, Intent.ACTION_MEDIA_MOUNTED); 110 sEnvironmentToBroadcast.put(Environment.MEDIA_EJECTING, Intent.ACTION_MEDIA_EJECT); 111 sEnvironmentToBroadcast.put(Environment.MEDIA_UNMOUNTABLE, Intent.ACTION_MEDIA_UNMOUNTABLE); 112 sEnvironmentToBroadcast.put(Environment.MEDIA_REMOVED, Intent.ACTION_MEDIA_REMOVED); 113 } 114 115 /** vold state */ 116 public final String id; 117 public final int type; 118 public int flags = 0; 119 public int userId = -1; 120 public int state = STATE_UNMOUNTED; 121 public String fsType; 122 public String fsUuid; 123 public String fsLabel; 124 public String path; 125 126 /** Framework state */ 127 public final int mtpIndex; 128 public String diskId; 129 public String nickname; 130 public int userFlags = 0; 131 132 public VolumeInfo(String id, int type, int mtpIndex) { 133 this.id = Preconditions.checkNotNull(id); 134 this.type = type; 135 this.mtpIndex = mtpIndex; 136 } 137 138 public VolumeInfo(Parcel parcel) { 139 id = parcel.readString(); 140 type = parcel.readInt(); 141 flags = parcel.readInt(); 142 userId = parcel.readInt(); 143 state = parcel.readInt(); 144 fsType = parcel.readString(); 145 fsUuid = parcel.readString(); 146 fsLabel = parcel.readString(); 147 path = parcel.readString(); 148 mtpIndex = parcel.readInt(); 149 diskId = parcel.readString(); 150 nickname = parcel.readString(); 151 userFlags = parcel.readInt(); 152 } 153 154 public static @NonNull String getEnvironmentForState(int state) { 155 final String envState = sStateToEnvironment.get(state); 156 if (envState != null) { 157 return envState; 158 } else { 159 return Environment.MEDIA_UNKNOWN; 160 } 161 } 162 163 public static @Nullable String getBroadcastForEnvironment(String envState) { 164 return sEnvironmentToBroadcast.get(envState); 165 } 166 167 public static @Nullable String getBroadcastForState(int state) { 168 return getBroadcastForEnvironment(getEnvironmentForState(state)); 169 } 170 171 public static @NonNull Comparator<VolumeInfo> getDescriptionComparator() { 172 return sDescriptionComparator; 173 } 174 175 public @NonNull String getId() { 176 return id; 177 } 178 179 public @Nullable String getDiskId() { 180 return diskId; 181 } 182 183 public int getType() { 184 return type; 185 } 186 187 public int getState() { 188 return state; 189 } 190 191 public @Nullable String getFsUuid() { 192 return fsUuid; 193 } 194 195 public @Nullable String getNickname() { 196 return nickname; 197 } 198 199 public @Nullable String getDescription() { 200 if (ID_PRIVATE_INTERNAL.equals(id)) { 201 return Resources.getSystem().getString(com.android.internal.R.string.storage_internal); 202 } else if (!TextUtils.isEmpty(nickname)) { 203 return nickname; 204 } else if (!TextUtils.isEmpty(fsLabel)) { 205 return fsLabel; 206 } else { 207 return null; 208 } 209 } 210 211 public boolean isPrimary() { 212 return (flags & FLAG_PRIMARY) != 0; 213 } 214 215 public boolean isVisible() { 216 return (flags & FLAG_VISIBLE) != 0; 217 } 218 219 public boolean isInited() { 220 return (userFlags & USER_FLAG_INITED) != 0; 221 } 222 223 public boolean isSnoozed() { 224 return (userFlags & USER_FLAG_SNOOZED) != 0; 225 } 226 227 public boolean isVisibleToUser(int userId) { 228 if (type == TYPE_PUBLIC && userId == this.userId) { 229 return isVisible(); 230 } else if (type == TYPE_EMULATED) { 231 return isVisible(); 232 } else { 233 return false; 234 } 235 } 236 237 public File getPath() { 238 return new File(path); 239 } 240 241 public File getPathForUser(int userId) { 242 if (path == null) { 243 return null; 244 } else if (type == TYPE_PUBLIC && userId == this.userId) { 245 return new File(path); 246 } else if (type == TYPE_EMULATED) { 247 return new File(path, Integer.toString(userId)); 248 } else { 249 return null; 250 } 251 } 252 253 public StorageVolume buildStorageVolume(Context context, int userId) { 254 final boolean removable; 255 final boolean emulated; 256 final boolean allowMassStorage = false; 257 final int mtpStorageId = MtpStorage.getStorageIdForIndex(mtpIndex); 258 final String envState = getEnvironmentForState(state); 259 260 File userPath = getPathForUser(userId); 261 if (userPath == null) { 262 userPath = new File("/dev/null"); 263 } 264 265 String description = getDescription(); 266 if (description == null) { 267 description = context.getString(android.R.string.unknownName); 268 } 269 270 long mtpReserveSize = 0; 271 long maxFileSize = 0; 272 273 if (type == TYPE_EMULATED) { 274 emulated = true; 275 mtpReserveSize = StorageManager.from(context).getStorageLowBytes(userPath); 276 277 if (ID_EMULATED_INTERNAL.equals(id)) { 278 removable = false; 279 } else { 280 removable = true; 281 } 282 283 } else if (type == TYPE_PUBLIC) { 284 emulated = false; 285 removable = true; 286 287 if ("vfat".equals(fsType)) { 288 maxFileSize = 4294967295L; 289 } 290 291 } else { 292 throw new IllegalStateException("Unexpected volume type " + type); 293 } 294 295 return new StorageVolume(id, mtpStorageId, userPath, description, isPrimary(), removable, 296 emulated, mtpReserveSize, allowMassStorage, maxFileSize, new UserHandle(userId), 297 fsUuid, envState); 298 } 299 300 // TODO: avoid this layering violation 301 private static final String DOCUMENT_AUTHORITY = "com.android.externalstorage.documents"; 302 private static final String DOCUMENT_ROOT_PRIMARY_EMULATED = "primary"; 303 304 /** 305 * Build an intent to browse the contents of this volume. Only valid for 306 * {@link #TYPE_EMULATED} or {@link #TYPE_PUBLIC}. 307 */ 308 public Intent buildBrowseIntent() { 309 final Uri uri; 310 if (type == VolumeInfo.TYPE_PUBLIC) { 311 uri = DocumentsContract.buildRootUri(DOCUMENT_AUTHORITY, fsUuid); 312 } else if (VolumeInfo.ID_EMULATED_INTERNAL.equals(id)) { 313 uri = DocumentsContract.buildRootUri(DOCUMENT_AUTHORITY, 314 DOCUMENT_ROOT_PRIMARY_EMULATED); 315 } else if (type == VolumeInfo.TYPE_EMULATED) { 316 // TODO: build intent once supported 317 uri = null; 318 } else { 319 throw new IllegalArgumentException(); 320 } 321 322 final Intent intent = new Intent(DocumentsContract.ACTION_BROWSE_DOCUMENT_ROOT); 323 intent.addCategory(Intent.CATEGORY_DEFAULT); 324 intent.setData(uri); 325 return intent; 326 } 327 328 @Override 329 public String toString() { 330 final CharArrayWriter writer = new CharArrayWriter(); 331 dump(new IndentingPrintWriter(writer, " ", 80)); 332 return writer.toString(); 333 } 334 335 public void dump(IndentingPrintWriter pw) { 336 pw.println("VolumeInfo:"); 337 pw.increaseIndent(); 338 pw.printPair("id", id); 339 pw.printPair("type", DebugUtils.valueToString(getClass(), "TYPE_", type)); 340 pw.printPair("flags", DebugUtils.flagsToString(getClass(), "FLAG_", flags)); 341 pw.printPair("userId", userId); 342 pw.printPair("state", DebugUtils.valueToString(getClass(), "STATE_", state)); 343 pw.println(); 344 pw.printPair("fsType", fsType); 345 pw.printPair("fsUuid", fsUuid); 346 pw.printPair("fsLabel", fsLabel); 347 pw.println(); 348 pw.printPair("path", path); 349 pw.printPair("mtpIndex", mtpIndex); 350 pw.printPair("diskId", diskId); 351 pw.printPair("nickname", nickname); 352 pw.printPair("userFlags", DebugUtils.flagsToString(getClass(), "USER_FLAG_", userFlags)); 353 pw.decreaseIndent(); 354 pw.println(); 355 } 356 357 @Override 358 public VolumeInfo clone() { 359 final Parcel temp = Parcel.obtain(); 360 try { 361 writeToParcel(temp, 0); 362 temp.setDataPosition(0); 363 return CREATOR.createFromParcel(temp); 364 } finally { 365 temp.recycle(); 366 } 367 } 368 369 @Override 370 public boolean equals(Object o) { 371 if (o instanceof VolumeInfo) { 372 return Objects.equals(id, ((VolumeInfo) o).id); 373 } else { 374 return false; 375 } 376 } 377 378 @Override 379 public int hashCode() { 380 return id.hashCode(); 381 } 382 383 public static final Creator<VolumeInfo> CREATOR = new Creator<VolumeInfo>() { 384 @Override 385 public VolumeInfo createFromParcel(Parcel in) { 386 return new VolumeInfo(in); 387 } 388 389 @Override 390 public VolumeInfo[] newArray(int size) { 391 return new VolumeInfo[size]; 392 } 393 }; 394 395 @Override 396 public int describeContents() { 397 return 0; 398 } 399 400 @Override 401 public void writeToParcel(Parcel parcel, int flags) { 402 parcel.writeString(id); 403 parcel.writeInt(type); 404 parcel.writeInt(this.flags); 405 parcel.writeInt(userId); 406 parcel.writeInt(state); 407 parcel.writeString(fsType); 408 parcel.writeString(fsUuid); 409 parcel.writeString(fsLabel); 410 parcel.writeString(path); 411 parcel.writeInt(mtpIndex); 412 parcel.writeString(diskId); 413 parcel.writeString(nickname); 414 parcel.writeInt(userFlags); 415 } 416} 417