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