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