2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package android.os.storage;
19 import android.annotation.NonNull;
20 import android.content.res.Resources;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 import android.text.TextUtils;
24 import android.util.DebugUtils;
26 import com.android.internal.util.IndentingPrintWriter;
27 import com.android.internal.util.Preconditions;
29 import java.io.CharArrayWriter;
30 import java.util.Objects;
33 * Information about a physical disk which may contain one or more
38 public class DiskInfo implements Parcelable {
39 public static final String ACTION_DISK_SCANNED =
40 "android.os.storage.action.DISK_SCANNED";
41 public static final String EXTRA_DISK_ID =
42 "android.os.storage.extra.DISK_ID";
43 public static final String EXTRA_VOLUME_COUNT =
44 "android.os.storage.extra.VOLUME_COUNT";
46 public static final int FLAG_ADOPTABLE = 1 << 0;
47 public static final int FLAG_DEFAULT_PRIMARY = 1 << 1;
48 public static final int FLAG_SD = 1 << 2;
49 public static final int FLAG_USB = 1 << 3;
50 public static final int FLAG_CDROM = 1 << 6;
52 public final String id;
53 public final int flags;
56 /** Hacky; don't rely on this count */
57 public int volumeCount;
58 public String sysPath;
60 public DiskInfo(String id, int flags) {
61 this.id = Preconditions.checkNotNull(id);
65 public DiskInfo(Parcel parcel) {
66 id = parcel.readString();
67 flags = parcel.readInt();
68 size = parcel.readLong();
69 label = parcel.readString();
70 volumeCount = parcel.readInt();
71 sysPath = parcel.readString();
74 public @NonNull String getId() {
78 private boolean isInteresting(String label) {
79 if (TextUtils.isEmpty(label)) {
82 if (label.equalsIgnoreCase("ata")) {
85 if (label.toLowerCase().contains("generic")) {
88 if (label.toLowerCase().startsWith("usb")) {
91 if (label.toLowerCase().startsWith("multiple")) {
97 public String getDescription() {
98 final Resources res = Resources.getSystem();
99 if ((flags & FLAG_SD) != 0) {
100 if (isInteresting(label)) {
101 return res.getString(com.android.internal.R.string.storage_sd_card_label, label);
103 return res.getString(com.android.internal.R.string.storage_sd_card);
105 } else if ((flags & FLAG_USB) != 0) {
106 if (isInteresting(label)) {
107 return res.getString(com.android.internal.R.string.storage_usb_drive_label, label);
109 return res.getString(com.android.internal.R.string.storage_usb_drive);
111 } else if ((flags & FLAG_CDROM) != 0) {
112 if (isInteresting(label)) {
113 return res.getString(com.android.internal.R.string.storage_cdrom_label, label);
115 return res.getString(com.android.internal.R.string.storage_cdrom);
122 public boolean isAdoptable() {
123 return (flags & FLAG_ADOPTABLE) != 0;
126 public boolean isDefaultPrimary() {
127 return (flags & FLAG_DEFAULT_PRIMARY) != 0;
130 public boolean isSd() {
131 return (flags & FLAG_SD) != 0;
134 public boolean isUsb() {
135 return (flags & FLAG_USB) != 0;
139 public String toString() {
140 final CharArrayWriter writer = new CharArrayWriter();
141 dump(new IndentingPrintWriter(writer, " ", 80));
142 return writer.toString();
145 public void dump(IndentingPrintWriter pw) {
146 pw.println("DiskInfo{" + id + "}:");
148 pw.printPair("flags", DebugUtils.flagsToString(getClass(), "FLAG_", flags));
149 pw.printPair("size", size);
150 pw.printPair("label", label);
152 pw.printPair("sysPath", sysPath);
158 public DiskInfo clone() {
159 final Parcel temp = Parcel.obtain();
161 writeToParcel(temp, 0);
162 temp.setDataPosition(0);
163 return CREATOR.createFromParcel(temp);
170 public boolean equals(Object o) {
171 if (o instanceof DiskInfo) {
172 return Objects.equals(id, ((DiskInfo) o).id);
179 public int hashCode() {
180 return id.hashCode();
183 public static final Creator<DiskInfo> CREATOR = new Creator<DiskInfo>() {
185 public DiskInfo createFromParcel(Parcel in) {
186 return new DiskInfo(in);
190 public DiskInfo[] newArray(int size) {
191 return new DiskInfo[size];
196 public int describeContents() {
201 public void writeToParcel(Parcel parcel, int flags) {
202 parcel.writeString(id);
203 parcel.writeInt(this.flags);
204 parcel.writeLong(size);
205 parcel.writeString(label);
206 parcel.writeInt(volumeCount);
207 parcel.writeString(sysPath);