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