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;
51 public final String id;
52 public final int flags;
55 /** Hacky; don't rely on this count */
56 public int volumeCount;
57 public String sysPath;
59 public DiskInfo(String id, int flags) {
60 this.id = Preconditions.checkNotNull(id);
64 public DiskInfo(Parcel parcel) {
65 id = parcel.readString();
66 flags = parcel.readInt();
67 size = parcel.readLong();
68 label = parcel.readString();
69 volumeCount = parcel.readInt();
70 sysPath = parcel.readString();
73 public @NonNull String getId() {
77 private boolean isInteresting(String label) {
78 if (TextUtils.isEmpty(label)) {
81 if (label.equalsIgnoreCase("ata")) {
84 if (label.toLowerCase().contains("generic")) {
87 if (label.toLowerCase().startsWith("usb")) {
90 if (label.toLowerCase().startsWith("multiple")) {
96 public String getDescription() {
97 final Resources res = Resources.getSystem();
98 if ((flags & FLAG_SD) != 0) {
99 if (isInteresting(label)) {
100 return res.getString(com.android.internal.R.string.storage_sd_card_label, label);
102 return res.getString(com.android.internal.R.string.storage_sd_card);
104 } else if ((flags & FLAG_USB) != 0) {
105 if (isInteresting(label)) {
106 return res.getString(com.android.internal.R.string.storage_usb_drive_label, label);
108 return res.getString(com.android.internal.R.string.storage_usb_drive);
115 public boolean isAdoptable() {
116 return (flags & FLAG_ADOPTABLE) != 0;
119 public boolean isDefaultPrimary() {
120 return (flags & FLAG_DEFAULT_PRIMARY) != 0;
123 public boolean isSd() {
124 return (flags & FLAG_SD) != 0;
127 public boolean isUsb() {
128 return (flags & FLAG_USB) != 0;
132 public String toString() {
133 final CharArrayWriter writer = new CharArrayWriter();
134 dump(new IndentingPrintWriter(writer, " ", 80));
135 return writer.toString();
138 public void dump(IndentingPrintWriter pw) {
139 pw.println("DiskInfo{" + id + "}:");
141 pw.printPair("flags", DebugUtils.flagsToString(getClass(), "FLAG_", flags));
142 pw.printPair("size", size);
143 pw.printPair("label", label);
145 pw.printPair("sysPath", sysPath);
151 public DiskInfo clone() {
152 final Parcel temp = Parcel.obtain();
154 writeToParcel(temp, 0);
155 temp.setDataPosition(0);
156 return CREATOR.createFromParcel(temp);
163 public boolean equals(Object o) {
164 if (o instanceof DiskInfo) {
165 return Objects.equals(id, ((DiskInfo) o).id);
172 public int hashCode() {
173 return id.hashCode();
176 public static final Creator<DiskInfo> CREATOR = new Creator<DiskInfo>() {
178 public DiskInfo createFromParcel(Parcel in) {
179 return new DiskInfo(in);
183 public DiskInfo[] newArray(int size) {
184 return new DiskInfo[size];
189 public int describeContents() {
194 public void writeToParcel(Parcel parcel, int flags) {
195 parcel.writeString(id);
196 parcel.writeInt(this.flags);
197 parcel.writeLong(size);
198 parcel.writeString(label);
199 parcel.writeInt(volumeCount);
200 parcel.writeString(sysPath);