StorageVolume.java revision 1b8ef7e3165ff9aa52a4905dafc8d0f83e7403f9
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.net.TrafficStats;
21import android.os.Parcel;
22import android.os.Parcelable;
23import android.os.UserHandle;
24
25import com.android.internal.util.IndentingPrintWriter;
26import com.android.internal.util.Preconditions;
27
28import java.io.CharArrayWriter;
29import java.io.File;
30
31/**
32 * Information about a storage volume that may be mounted. This is a legacy
33 * specialization of {@link VolumeInfo} which describes the volume for a
34 * specific user.
35 * <p>
36 * This class may be deprecated in the future.
37 *
38 * @hide
39 */
40public class StorageVolume implements Parcelable {
41
42    private final String mId;
43    private final int mStorageId;
44    private final File mPath;
45    private final String mDescription;
46    private final boolean mPrimary;
47    private final boolean mRemovable;
48    private final boolean mEmulated;
49    private final long mMtpReserveSize;
50    private final boolean mAllowMassStorage;
51    private final long mMaxFileSize;
52    private final UserHandle mOwner;
53    private final String mFsUuid;
54    private final String mState;
55
56    // StorageVolume extra for ACTION_MEDIA_REMOVED, ACTION_MEDIA_UNMOUNTED, ACTION_MEDIA_CHECKING,
57    // ACTION_MEDIA_NOFS, ACTION_MEDIA_MOUNTED, ACTION_MEDIA_SHARED, ACTION_MEDIA_UNSHARED,
58    // ACTION_MEDIA_BAD_REMOVAL, ACTION_MEDIA_UNMOUNTABLE and ACTION_MEDIA_EJECT broadcasts.
59    public static final String EXTRA_STORAGE_VOLUME = "storage_volume";
60
61    public StorageVolume(String id, int storageId, File path, String description, boolean primary,
62            boolean removable, boolean emulated, long mtpReserveSize, boolean allowMassStorage,
63            long maxFileSize, UserHandle owner, String fsUuid, String state) {
64        mId = Preconditions.checkNotNull(id);
65        mStorageId = storageId;
66        mPath = Preconditions.checkNotNull(path);
67        mDescription = Preconditions.checkNotNull(description);
68        mPrimary = primary;
69        mRemovable = removable;
70        mEmulated = emulated;
71        mMtpReserveSize = mtpReserveSize;
72        mAllowMassStorage = allowMassStorage;
73        mMaxFileSize = maxFileSize;
74        mOwner = Preconditions.checkNotNull(owner);
75        mFsUuid = fsUuid;
76        mState = Preconditions.checkNotNull(state);
77    }
78
79    private StorageVolume(Parcel in) {
80        mId = in.readString();
81        mStorageId = in.readInt();
82        mPath = new File(in.readString());
83        mDescription = in.readString();
84        mPrimary = in.readInt() != 0;
85        mRemovable = in.readInt() != 0;
86        mEmulated = in.readInt() != 0;
87        mMtpReserveSize = in.readLong();
88        mAllowMassStorage = in.readInt() != 0;
89        mMaxFileSize = in.readLong();
90        mOwner = in.readParcelable(null);
91        mFsUuid = in.readString();
92        mState = in.readString();
93    }
94
95    public String getId() {
96        return mId;
97    }
98
99    /**
100     * Returns the mount path for the volume.
101     *
102     * @return the mount path
103     */
104    public String getPath() {
105        return mPath.toString();
106    }
107
108    public File getPathFile() {
109        return mPath;
110    }
111
112    /**
113     * Returns a user visible description of the volume.
114     *
115     * @return the volume description
116     */
117    public String getDescription(Context context) {
118        return mDescription;
119    }
120
121    public boolean isPrimary() {
122        return mPrimary;
123    }
124
125    /**
126     * Returns true if the volume is removable.
127     *
128     * @return is removable
129     */
130    public boolean isRemovable() {
131        return mRemovable;
132    }
133
134    /**
135     * Returns true if the volume is emulated.
136     *
137     * @return is removable
138     */
139    public boolean isEmulated() {
140        return mEmulated;
141    }
142
143    /**
144     * Returns the MTP storage ID for the volume.
145     * this is also used for the storage_id column in the media provider.
146     *
147     * @return MTP storage ID
148     */
149    public int getStorageId() {
150        return mStorageId;
151    }
152
153    /**
154     * Number of megabytes of space to leave unallocated by MTP.
155     * MTP will subtract this value from the free space it reports back
156     * to the host via GetStorageInfo, and will not allow new files to
157     * be added via MTP if there is less than this amount left free in the storage.
158     * If MTP has dedicated storage this value should be zero, but if MTP is
159     * sharing storage with the rest of the system, set this to a positive value
160     * to ensure that MTP activity does not result in the storage being
161     * too close to full.
162     *
163     * @return MTP reserve space
164     */
165    public int getMtpReserveSpace() {
166        return (int) (mMtpReserveSize / TrafficStats.MB_IN_BYTES);
167    }
168
169    /**
170     * Returns true if this volume can be shared via USB mass storage.
171     *
172     * @return whether mass storage is allowed
173     */
174    public boolean allowMassStorage() {
175        return mAllowMassStorage;
176    }
177
178    /**
179     * Returns maximum file size for the volume, or zero if it is unbounded.
180     *
181     * @return maximum file size
182     */
183    public long getMaxFileSize() {
184        return mMaxFileSize;
185    }
186
187    public UserHandle getOwner() {
188        return mOwner;
189    }
190
191    public String getUuid() {
192        return mFsUuid;
193    }
194
195    /**
196     * Parse and return volume UUID as FAT volume ID, or return -1 if unable to
197     * parse or UUID is unknown.
198     */
199    public int getFatVolumeId() {
200        if (mFsUuid == null || mFsUuid.length() != 9) {
201            return -1;
202        }
203        try {
204            return (int) Long.parseLong(mFsUuid.replace("-", ""), 16);
205        } catch (NumberFormatException e) {
206            return -1;
207        }
208    }
209
210    public String getUserLabel() {
211        return mDescription;
212    }
213
214    public String getState() {
215        return mState;
216    }
217
218    @Override
219    public boolean equals(Object obj) {
220        if (obj instanceof StorageVolume && mPath != null) {
221            StorageVolume volume = (StorageVolume)obj;
222            return (mPath.equals(volume.mPath));
223        }
224        return false;
225    }
226
227    @Override
228    public int hashCode() {
229        return mPath.hashCode();
230    }
231
232    @Override
233    public String toString() {
234        final CharArrayWriter writer = new CharArrayWriter();
235        dump(new IndentingPrintWriter(writer, "    ", 80));
236        return writer.toString();
237    }
238
239    public void dump(IndentingPrintWriter pw) {
240        pw.println("StorageVolume:");
241        pw.increaseIndent();
242        pw.printPair("mId", mId);
243        pw.printPair("mStorageId", mStorageId);
244        pw.printPair("mPath", mPath);
245        pw.printPair("mDescription", mDescription);
246        pw.printPair("mPrimary", mPrimary);
247        pw.printPair("mRemovable", mRemovable);
248        pw.printPair("mEmulated", mEmulated);
249        pw.printPair("mMtpReserveSize", mMtpReserveSize);
250        pw.printPair("mAllowMassStorage", mAllowMassStorage);
251        pw.printPair("mMaxFileSize", mMaxFileSize);
252        pw.printPair("mOwner", mOwner);
253        pw.printPair("mFsUuid", mFsUuid);
254        pw.printPair("mState", mState);
255        pw.decreaseIndent();
256    }
257
258    public static final Creator<StorageVolume> CREATOR = new Creator<StorageVolume>() {
259        @Override
260        public StorageVolume createFromParcel(Parcel in) {
261            return new StorageVolume(in);
262        }
263
264        @Override
265        public StorageVolume[] newArray(int size) {
266            return new StorageVolume[size];
267        }
268    };
269
270    @Override
271    public int describeContents() {
272        return 0;
273    }
274
275    @Override
276    public void writeToParcel(Parcel parcel, int flags) {
277        parcel.writeString(mId);
278        parcel.writeInt(mStorageId);
279        parcel.writeString(mPath.toString());
280        parcel.writeString(mDescription);
281        parcel.writeInt(mPrimary ? 1 : 0);
282        parcel.writeInt(mRemovable ? 1 : 0);
283        parcel.writeInt(mEmulated ? 1 : 0);
284        parcel.writeLong(mMtpReserveSize);
285        parcel.writeInt(mAllowMassStorage ? 1 : 0);
286        parcel.writeLong(mMaxFileSize);
287        parcel.writeParcelable(mOwner, flags);
288        parcel.writeString(mFsUuid);
289        parcel.writeString(mState);
290    }
291}
292