blob: a241728557f5be67391a74612c2254ffc763668e [file] [log] [blame]
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -07001/*
2 * Copyright (C) 2015 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
Jeff Sharkey7151a9a2015-04-04 15:22:37 -070019import android.annotation.NonNull;
20import android.annotation.Nullable;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070021import android.content.Context;
22import android.content.Intent;
Jeff Sharkey59d577a2015-04-11 21:27:21 -070023import android.content.res.Resources;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070024import android.mtp.MtpStorage;
Jeff Sharkey56bd3122015-04-14 10:30:34 -070025import android.net.Uri;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070026import android.os.Environment;
27import android.os.Parcel;
28import android.os.Parcelable;
29import android.os.UserHandle;
Jeff Sharkey56bd3122015-04-14 10:30:34 -070030import android.provider.DocumentsContract;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070031import android.text.TextUtils;
32import android.util.ArrayMap;
33import android.util.DebugUtils;
34import android.util.SparseArray;
35
36import com.android.internal.util.IndentingPrintWriter;
37import com.android.internal.util.Preconditions;
38
Jeff Sharkey7151a9a2015-04-04 15:22:37 -070039import java.io.CharArrayWriter;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070040import java.io.File;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -070041import java.util.Comparator;
42import java.util.Objects;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070043
44/**
45 * Information about a storage volume that may be mounted. A volume may be a
46 * partition on a physical {@link DiskInfo}, an emulated volume above some other
47 * storage medium, or a standalone container like an ASEC or OBB.
48 *
49 * @hide
50 */
51public class VolumeInfo implements Parcelable {
Jeff Sharkey56bd3122015-04-14 10:30:34 -070052 public static final String EXTRA_VOLUME_ID = "android.os.storage.extra.VOLUME_ID";
53
Jeff Sharkey59d577a2015-04-11 21:27:21 -070054 /** Stub volume representing internal private storage */
55 public static final String ID_PRIVATE_INTERNAL = "private";
Jeff Sharkeyb2b9ab82015-04-05 21:10:42 -070056 /** Real volume representing internal emulated storage */
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070057 public static final String ID_EMULATED_INTERNAL = "emulated";
58
59 public static final int TYPE_PUBLIC = 0;
60 public static final int TYPE_PRIVATE = 1;
61 public static final int TYPE_EMULATED = 2;
62 public static final int TYPE_ASEC = 3;
63 public static final int TYPE_OBB = 4;
64
65 public static final int STATE_UNMOUNTED = 0;
66 public static final int STATE_MOUNTING = 1;
67 public static final int STATE_MOUNTED = 2;
68 public static final int STATE_FORMATTING = 3;
69 public static final int STATE_UNMOUNTING = 4;
Jeff Sharkey16c9c242015-04-04 21:37:20 -070070 public static final int STATE_UNMOUNTABLE = 5;
Jeff Sharkey59d577a2015-04-11 21:27:21 -070071 public static final int STATE_REMOVED = 6;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070072
73 public static final int FLAG_PRIMARY = 1 << 0;
74 public static final int FLAG_VISIBLE = 1 << 1;
75
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070076 public static final int USER_FLAG_INITED = 1 << 0;
77 public static final int USER_FLAG_SNOOZED = 1 << 1;
78
Jeff Sharkey7151a9a2015-04-04 15:22:37 -070079 private static SparseArray<String> sStateToEnvironment = new SparseArray<>();
80 private static ArrayMap<String, String> sEnvironmentToBroadcast = new ArrayMap<>();
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070081
Jeff Sharkeye2d45be2015-04-15 17:14:12 -070082 private static final Comparator<VolumeInfo>
83 sDescriptionComparator = new Comparator<VolumeInfo>() {
84 @Override
85 public int compare(VolumeInfo lhs, VolumeInfo rhs) {
86 if (VolumeInfo.ID_PRIVATE_INTERNAL.equals(lhs.getId())) {
87 return -1;
88 } else if (lhs.getDescription() == null) {
89 return 1;
90 } else if (rhs.getDescription() == null) {
91 return -1;
92 } else {
93 return lhs.getDescription().compareTo(rhs.getDescription());
94 }
95 }
96 };
97
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070098 static {
99 sStateToEnvironment.put(VolumeInfo.STATE_UNMOUNTED, Environment.MEDIA_UNMOUNTED);
100 sStateToEnvironment.put(VolumeInfo.STATE_MOUNTING, Environment.MEDIA_CHECKING);
101 sStateToEnvironment.put(VolumeInfo.STATE_MOUNTED, Environment.MEDIA_MOUNTED);
102 sStateToEnvironment.put(VolumeInfo.STATE_FORMATTING, Environment.MEDIA_UNMOUNTED);
103 sStateToEnvironment.put(VolumeInfo.STATE_UNMOUNTING, Environment.MEDIA_EJECTING);
Jeff Sharkey16c9c242015-04-04 21:37:20 -0700104 sStateToEnvironment.put(VolumeInfo.STATE_UNMOUNTABLE, Environment.MEDIA_UNMOUNTABLE);
Jeff Sharkey59d577a2015-04-11 21:27:21 -0700105 sStateToEnvironment.put(VolumeInfo.STATE_REMOVED, Environment.MEDIA_REMOVED);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700106
107 sEnvironmentToBroadcast.put(Environment.MEDIA_UNMOUNTED, Intent.ACTION_MEDIA_UNMOUNTED);
108 sEnvironmentToBroadcast.put(Environment.MEDIA_CHECKING, Intent.ACTION_MEDIA_CHECKING);
109 sEnvironmentToBroadcast.put(Environment.MEDIA_MOUNTED, Intent.ACTION_MEDIA_MOUNTED);
110 sEnvironmentToBroadcast.put(Environment.MEDIA_EJECTING, Intent.ACTION_MEDIA_EJECT);
Jeff Sharkey16c9c242015-04-04 21:37:20 -0700111 sEnvironmentToBroadcast.put(Environment.MEDIA_UNMOUNTABLE, Intent.ACTION_MEDIA_UNMOUNTABLE);
Jeff Sharkey59d577a2015-04-11 21:27:21 -0700112 sEnvironmentToBroadcast.put(Environment.MEDIA_REMOVED, Intent.ACTION_MEDIA_REMOVED);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700113 }
114
115 /** vold state */
116 public final String id;
117 public final int type;
118 public int flags = 0;
119 public int userId = -1;
120 public int state = STATE_UNMOUNTED;
121 public String fsType;
122 public String fsUuid;
123 public String fsLabel;
124 public String path;
125
126 /** Framework state */
127 public final int mtpIndex;
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700128 public String diskId;
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700129 public String nickname;
130 public int userFlags = 0;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700131
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700132 public VolumeInfo(String id, int type, int mtpIndex) {
133 this.id = Preconditions.checkNotNull(id);
134 this.type = type;
135 this.mtpIndex = mtpIndex;
136 }
137
138 public VolumeInfo(Parcel parcel) {
139 id = parcel.readString();
140 type = parcel.readInt();
141 flags = parcel.readInt();
142 userId = parcel.readInt();
143 state = parcel.readInt();
144 fsType = parcel.readString();
145 fsUuid = parcel.readString();
146 fsLabel = parcel.readString();
147 path = parcel.readString();
148 mtpIndex = parcel.readInt();
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700149 diskId = parcel.readString();
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700150 nickname = parcel.readString();
151 userFlags = parcel.readInt();
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700152 }
153
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700154 public static @NonNull String getEnvironmentForState(int state) {
155 final String envState = sStateToEnvironment.get(state);
156 if (envState != null) {
157 return envState;
158 } else {
159 return Environment.MEDIA_UNKNOWN;
160 }
161 }
162
163 public static @Nullable String getBroadcastForEnvironment(String envState) {
164 return sEnvironmentToBroadcast.get(envState);
165 }
166
167 public static @Nullable String getBroadcastForState(int state) {
168 return getBroadcastForEnvironment(getEnvironmentForState(state));
169 }
170
Jeff Sharkeye2d45be2015-04-15 17:14:12 -0700171 public static @NonNull Comparator<VolumeInfo> getDescriptionComparator() {
172 return sDescriptionComparator;
173 }
174
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700175 public @NonNull String getId() {
176 return id;
177 }
178
179 public @Nullable String getDiskId() {
180 return diskId;
181 }
182
183 public int getType() {
184 return type;
185 }
186
187 public int getState() {
188 return state;
189 }
190
191 public @Nullable String getFsUuid() {
192 return fsUuid;
193 }
194
195 public @Nullable String getNickname() {
196 return nickname;
197 }
198
Jeff Sharkey59d577a2015-04-11 21:27:21 -0700199 public @Nullable String getDescription() {
200 if (ID_PRIVATE_INTERNAL.equals(id)) {
201 return Resources.getSystem().getString(com.android.internal.R.string.storage_internal);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700202 } else if (!TextUtils.isEmpty(nickname)) {
203 return nickname;
204 } else if (!TextUtils.isEmpty(fsLabel)) {
205 return fsLabel;
206 } else {
207 return null;
208 }
209 }
210
211 public boolean isPrimary() {
212 return (flags & FLAG_PRIMARY) != 0;
213 }
214
215 public boolean isVisible() {
216 return (flags & FLAG_VISIBLE) != 0;
217 }
218
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700219 public boolean isInited() {
220 return (userFlags & USER_FLAG_INITED) != 0;
221 }
222
223 public boolean isSnoozed() {
224 return (userFlags & USER_FLAG_SNOOZED) != 0;
225 }
226
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700227 public boolean isVisibleToUser(int userId) {
228 if (type == TYPE_PUBLIC && userId == this.userId) {
229 return isVisible();
230 } else if (type == TYPE_EMULATED) {
231 return isVisible();
232 } else {
233 return false;
234 }
235 }
236
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700237 public File getPath() {
238 return new File(path);
239 }
240
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700241 public File getPathForUser(int userId) {
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700242 if (path == null) {
243 return null;
244 } else if (type == TYPE_PUBLIC && userId == this.userId) {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700245 return new File(path);
246 } else if (type == TYPE_EMULATED) {
247 return new File(path, Integer.toString(userId));
248 } else {
249 return null;
250 }
251 }
252
253 public StorageVolume buildStorageVolume(Context context, int userId) {
254 final boolean removable;
255 final boolean emulated;
256 final boolean allowMassStorage = false;
257 final int mtpStorageId = MtpStorage.getStorageIdForIndex(mtpIndex);
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700258 final String envState = getEnvironmentForState(state);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700259
260 File userPath = getPathForUser(userId);
261 if (userPath == null) {
262 userPath = new File("/dev/null");
263 }
264
Jeff Sharkey59d577a2015-04-11 21:27:21 -0700265 String description = getDescription();
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700266 if (description == null) {
267 description = context.getString(android.R.string.unknownName);
268 }
269
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700270 long mtpReserveSize = 0;
271 long maxFileSize = 0;
272
273 if (type == TYPE_EMULATED) {
274 emulated = true;
275 mtpReserveSize = StorageManager.from(context).getStorageLowBytes(userPath);
276
277 if (ID_EMULATED_INTERNAL.equals(id)) {
278 removable = false;
279 } else {
280 removable = true;
281 }
282
283 } else if (type == TYPE_PUBLIC) {
284 emulated = false;
285 removable = true;
286
287 if ("vfat".equals(fsType)) {
288 maxFileSize = 4294967295L;
289 }
290
291 } else {
292 throw new IllegalStateException("Unexpected volume type " + type);
293 }
294
295 return new StorageVolume(id, mtpStorageId, userPath, description, isPrimary(), removable,
296 emulated, mtpReserveSize, allowMassStorage, maxFileSize, new UserHandle(userId),
297 fsUuid, envState);
298 }
299
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700300 // TODO: avoid this layering violation
301 private static final String DOCUMENT_AUTHORITY = "com.android.externalstorage.documents";
302 private static final String DOCUMENT_ROOT_PRIMARY_EMULATED = "primary";
303
304 /**
305 * Build an intent to browse the contents of this volume. Only valid for
306 * {@link #TYPE_EMULATED} or {@link #TYPE_PUBLIC}.
307 */
308 public Intent buildBrowseIntent() {
309 final Uri uri;
310 if (type == VolumeInfo.TYPE_PUBLIC) {
311 uri = DocumentsContract.buildRootUri(DOCUMENT_AUTHORITY, fsUuid);
312 } else if (VolumeInfo.ID_EMULATED_INTERNAL.equals(id)) {
313 uri = DocumentsContract.buildRootUri(DOCUMENT_AUTHORITY,
314 DOCUMENT_ROOT_PRIMARY_EMULATED);
315 } else if (type == VolumeInfo.TYPE_EMULATED) {
316 // TODO: build intent once supported
317 uri = null;
318 } else {
319 throw new IllegalArgumentException();
320 }
321
322 final Intent intent = new Intent(DocumentsContract.ACTION_BROWSE_DOCUMENT_ROOT);
323 intent.addCategory(Intent.CATEGORY_DEFAULT);
324 intent.setData(uri);
325 return intent;
326 }
327
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700328 @Override
329 public String toString() {
330 final CharArrayWriter writer = new CharArrayWriter();
331 dump(new IndentingPrintWriter(writer, " ", 80));
332 return writer.toString();
333 }
334
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700335 public void dump(IndentingPrintWriter pw) {
336 pw.println("VolumeInfo:");
337 pw.increaseIndent();
338 pw.printPair("id", id);
339 pw.printPair("type", DebugUtils.valueToString(getClass(), "TYPE_", type));
340 pw.printPair("flags", DebugUtils.flagsToString(getClass(), "FLAG_", flags));
341 pw.printPair("userId", userId);
342 pw.printPair("state", DebugUtils.valueToString(getClass(), "STATE_", state));
343 pw.println();
344 pw.printPair("fsType", fsType);
345 pw.printPair("fsUuid", fsUuid);
346 pw.printPair("fsLabel", fsLabel);
347 pw.println();
348 pw.printPair("path", path);
349 pw.printPair("mtpIndex", mtpIndex);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700350 pw.printPair("diskId", diskId);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700351 pw.printPair("nickname", nickname);
352 pw.printPair("userFlags", DebugUtils.flagsToString(getClass(), "USER_FLAG_", userFlags));
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700353 pw.decreaseIndent();
354 pw.println();
355 }
356
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700357 @Override
358 public VolumeInfo clone() {
359 final Parcel temp = Parcel.obtain();
360 try {
361 writeToParcel(temp, 0);
362 temp.setDataPosition(0);
363 return CREATOR.createFromParcel(temp);
364 } finally {
365 temp.recycle();
366 }
367 }
368
Jeff Sharkeye2d45be2015-04-15 17:14:12 -0700369 @Override
370 public boolean equals(Object o) {
371 if (o instanceof VolumeInfo) {
372 return Objects.equals(id, ((VolumeInfo) o).id);
373 } else {
374 return false;
375 }
376 }
377
378 @Override
379 public int hashCode() {
380 return id.hashCode();
381 }
382
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700383 public static final Creator<VolumeInfo> CREATOR = new Creator<VolumeInfo>() {
384 @Override
385 public VolumeInfo createFromParcel(Parcel in) {
386 return new VolumeInfo(in);
387 }
388
389 @Override
390 public VolumeInfo[] newArray(int size) {
391 return new VolumeInfo[size];
392 }
393 };
394
395 @Override
396 public int describeContents() {
397 return 0;
398 }
399
400 @Override
401 public void writeToParcel(Parcel parcel, int flags) {
402 parcel.writeString(id);
403 parcel.writeInt(type);
Jeff Sharkey59d577a2015-04-11 21:27:21 -0700404 parcel.writeInt(this.flags);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700405 parcel.writeInt(userId);
406 parcel.writeInt(state);
407 parcel.writeString(fsType);
408 parcel.writeString(fsUuid);
409 parcel.writeString(fsLabel);
410 parcel.writeString(path);
411 parcel.writeInt(mtpIndex);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700412 parcel.writeString(diskId);
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -0700413 parcel.writeString(nickname);
414 parcel.writeInt(userFlags);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700415 }
416}