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