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