StorageVolume.java revision a5250c93928e256738125b265e10c96c3575597e
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 17package android.os.storage; 18 19import android.os.Bundle; 20import android.os.Parcel; 21import android.os.Parcelable; 22import android.util.Log; 23 24/** 25 * A class representing a storage volume 26 * @hide 27 */ 28public class StorageVolume implements Parcelable { 29 30 private static final String TAG = "StorageVolume"; 31 32 private final String mPath; 33 private final String mDescription; 34 private final boolean mRemovable; 35 private final boolean mEmulated; 36 private final int mMtpReserveSpace; 37 private int mStorageId; 38 39 // StorageVolume extra for ACTION_MEDIA_REMOVED, ACTION_MEDIA_UNMOUNTED, ACTION_MEDIA_CHECKING, 40 // ACTION_MEDIA_NOFS, ACTION_MEDIA_MOUNTED, ACTION_MEDIA_SHARED, ACTION_MEDIA_UNSHARED, 41 // ACTION_MEDIA_BAD_REMOVAL, ACTION_MEDIA_UNMOUNTABLE and ACTION_MEDIA_EJECT broadcasts. 42 public static final String EXTRA_STORAGE_VOLUME = "storage_volume"; 43 44 public StorageVolume(String path, String description, 45 boolean removable, boolean emulated, int mtpReserveSpace) { 46 mPath = path; 47 mDescription = description; 48 mRemovable = removable; 49 mEmulated = emulated; 50 mMtpReserveSpace = mtpReserveSpace; 51 } 52 53 // for parcelling only 54 private StorageVolume(String path, String description, 55 boolean removable, boolean emulated, int mtpReserveSpace, int storageId) { 56 mPath = path; 57 mDescription = description; 58 mRemovable = removable; 59 mEmulated = emulated; 60 mMtpReserveSpace = mtpReserveSpace; 61 mStorageId = storageId; 62 } 63 64 /** 65 * Returns the mount path for the volume. 66 * 67 * @return the mount path 68 */ 69 public String getPath() { 70 return mPath; 71 } 72 73 /** 74 * Returns a user visible description of the volume. 75 * 76 * @return the volume description 77 */ 78 public String getDescription() { 79 return mDescription; 80 } 81 82 /** 83 * Returns true if the volume is removable. 84 * 85 * @return is removable 86 */ 87 public boolean isRemovable() { 88 return mRemovable; 89 } 90 91 /** 92 * Returns true if the volume is emulated. 93 * 94 * @return is removable 95 */ 96 public boolean isEmulated() { 97 return mEmulated; 98 } 99 100 /** 101 * Returns the MTP storage ID for the volume. 102 * this is also used for the storage_id column in the media provider. 103 * 104 * @return MTP storage ID 105 */ 106 public int getStorageId() { 107 return mStorageId; 108 } 109 110 /** 111 * Do not call this unless you are MountService 112 */ 113 public void setStorageId(int index) { 114 // storage ID is 0x00010001 for primary storage, 115 // then 0x00020001, 0x00030001, etc. for secondary storages 116 mStorageId = ((index + 1) << 16) + 1; 117 } 118 119 /** 120 * Number of megabytes of space to leave unallocated by MTP. 121 * MTP will subtract this value from the free space it reports back 122 * to the host via GetStorageInfo, and will not allow new files to 123 * be added via MTP if there is less than this amount left free in the storage. 124 * If MTP has dedicated storage this value should be zero, but if MTP is 125 * sharing storage with the rest of the system, set this to a positive value 126 * to ensure that MTP activity does not result in the storage being 127 * too close to full. 128 * 129 * @return MTP reserve space 130 */ 131 public int getMtpReserveSpace() { 132 return mMtpReserveSpace; 133 } 134 135 @Override 136 public boolean equals(Object obj) { 137 if (obj instanceof StorageVolume && mPath != null) { 138 StorageVolume volume = (StorageVolume)obj; 139 return (mPath.equals(volume.mPath)); 140 } 141 return false; 142 } 143 144 @Override 145 public int hashCode() { 146 return mPath.hashCode(); 147 } 148 149 @Override 150 public String toString() { 151 return mPath; 152 } 153 154 public static final Parcelable.Creator<StorageVolume> CREATOR = 155 new Parcelable.Creator<StorageVolume>() { 156 public StorageVolume createFromParcel(Parcel in) { 157 String path = in.readString(); 158 String description = in.readString(); 159 int removable = in.readInt(); 160 int emulated = in.readInt(); 161 int storageId = in.readInt(); 162 int mtpReserveSpace = in.readInt(); 163 return new StorageVolume(path, description, 164 removable == 1, emulated == 1, 165 mtpReserveSpace, storageId); 166 } 167 168 public StorageVolume[] newArray(int size) { 169 return new StorageVolume[size]; 170 } 171 }; 172 173 public int describeContents() { 174 return 0; 175 } 176 177 public void writeToParcel(Parcel parcel, int flags) { 178 parcel.writeString(mPath); 179 parcel.writeString(mDescription); 180 parcel.writeInt(mRemovable ? 1 : 0); 181 parcel.writeInt(mEmulated ? 1 : 0); 182 parcel.writeInt(mStorageId); 183 parcel.writeInt(mMtpReserveSpace); 184 } 185} 186