blob: 828f1c304ccbf15ed472da067b5f9ddbb1926571 [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 Sharkeyd95d3bf2015-04-14 21:39:44 -070019import android.annotation.NonNull;
Jeff Sharkey46270712018-04-02 09:53:21 -060020import android.annotation.Nullable;
Mathew Inwood98e9ad12018-08-30 13:11:50 +010021import android.annotation.UnsupportedAppUsage;
Jeff Sharkey59d577a2015-04-11 21:27:21 -070022import android.content.res.Resources;
Mathew Inwood31755f92018-12-20 13:53:36 +000023import android.os.Build;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070024import android.os.Parcel;
25import android.os.Parcelable;
Jeff Sharkey7e92ef32015-04-17 17:35:07 -070026import android.text.TextUtils;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070027import android.util.DebugUtils;
28
29import com.android.internal.util.IndentingPrintWriter;
30import com.android.internal.util.Preconditions;
31
Jeff Sharkey7151a9a2015-04-04 15:22:37 -070032import java.io.CharArrayWriter;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -070033import java.util.Objects;
Jeff Sharkey7151a9a2015-04-04 15:22:37 -070034
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070035/**
36 * Information about a physical disk which may contain one or more
37 * {@link VolumeInfo}.
38 *
39 * @hide
40 */
41public class DiskInfo implements Parcelable {
Jeff Sharkey620b32b2015-04-23 19:36:02 -070042 public static final String ACTION_DISK_SCANNED =
43 "android.os.storage.action.DISK_SCANNED";
44 public static final String EXTRA_DISK_ID =
45 "android.os.storage.extra.DISK_ID";
Jeff Sharkeyc7acac62015-06-12 16:16:56 -070046 public static final String EXTRA_VOLUME_COUNT =
47 "android.os.storage.extra.VOLUME_COUNT";
Jeff Sharkey56bd3122015-04-14 10:30:34 -070048
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070049 public static final int FLAG_ADOPTABLE = 1 << 0;
50 public static final int FLAG_DEFAULT_PRIMARY = 1 << 1;
51 public static final int FLAG_SD = 1 << 2;
52 public static final int FLAG_USB = 1 << 3;
53
54 public final String id;
Mathew Inwood98e9ad12018-08-30 13:11:50 +010055 @UnsupportedAppUsage
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070056 public final int flags;
Mathew Inwood98e9ad12018-08-30 13:11:50 +010057 @UnsupportedAppUsage
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070058 public long size;
Mathew Inwood98e9ad12018-08-30 13:11:50 +010059 @UnsupportedAppUsage
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070060 public String label;
Jeff Sharkeyf5a6bd72015-05-19 14:42:38 -070061 /** Hacky; don't rely on this count */
62 public int volumeCount;
Jeff Sharkeye8a4b662015-06-27 15:43:45 -070063 public String sysPath;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070064
65 public DiskInfo(String id, int flags) {
66 this.id = Preconditions.checkNotNull(id);
67 this.flags = flags;
68 }
69
Mathew Inwood31755f92018-12-20 13:53:36 +000070 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070071 public DiskInfo(Parcel parcel) {
72 id = parcel.readString();
73 flags = parcel.readInt();
74 size = parcel.readLong();
75 label = parcel.readString();
Jeff Sharkeyf5a6bd72015-05-19 14:42:38 -070076 volumeCount = parcel.readInt();
Jeff Sharkeye8a4b662015-06-27 15:43:45 -070077 sysPath = parcel.readString();
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070078 }
79
Mathew Inwood98e9ad12018-08-30 13:11:50 +010080 @UnsupportedAppUsage
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070081 public @NonNull String getId() {
82 return id;
83 }
84
Jeff Sharkeye6c04f92015-04-18 21:38:05 -070085 private boolean isInteresting(String label) {
86 if (TextUtils.isEmpty(label)) {
87 return false;
88 }
Jeff Sharkey74acbbb2015-04-21 12:14:03 -070089 if (label.equalsIgnoreCase("ata")) {
90 return false;
91 }
Jeff Sharkeye6c04f92015-04-18 21:38:05 -070092 if (label.toLowerCase().contains("generic")) {
93 return false;
94 }
Jeff Sharkey47b872d2015-06-11 12:47:24 -070095 if (label.toLowerCase().startsWith("usb")) {
96 return false;
97 }
98 if (label.toLowerCase().startsWith("multiple")) {
99 return false;
100 }
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700101 return true;
102 }
103
Mathew Inwood98e9ad12018-08-30 13:11:50 +0100104 @UnsupportedAppUsage
Jeff Sharkey46270712018-04-02 09:53:21 -0600105 public @Nullable String getDescription() {
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700106 final Resources res = Resources.getSystem();
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700107 if ((flags & FLAG_SD) != 0) {
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700108 if (isInteresting(label)) {
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700109 return res.getString(com.android.internal.R.string.storage_sd_card_label, label);
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700110 } else {
111 return res.getString(com.android.internal.R.string.storage_sd_card);
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700112 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700113 } else if ((flags & FLAG_USB) != 0) {
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700114 if (isInteresting(label)) {
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700115 return res.getString(com.android.internal.R.string.storage_usb_drive_label, label);
Jeff Sharkeye6c04f92015-04-18 21:38:05 -0700116 } else {
117 return res.getString(com.android.internal.R.string.storage_usb_drive);
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700118 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700119 } else {
120 return null;
121 }
122 }
123
Jeff Sharkey46270712018-04-02 09:53:21 -0600124 public @Nullable String getShortDescription() {
125 final Resources res = Resources.getSystem();
126 if (isSd()) {
127 return res.getString(com.android.internal.R.string.storage_sd_card);
128 } else if (isUsb()) {
129 return res.getString(com.android.internal.R.string.storage_usb_drive);
130 } else {
131 return null;
132 }
133 }
134
Mathew Inwood98e9ad12018-08-30 13:11:50 +0100135 @UnsupportedAppUsage
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700136 public boolean isAdoptable() {
137 return (flags & FLAG_ADOPTABLE) != 0;
138 }
139
Mathew Inwood98e9ad12018-08-30 13:11:50 +0100140 @UnsupportedAppUsage
Jeff Sharkey620b32b2015-04-23 19:36:02 -0700141 public boolean isDefaultPrimary() {
142 return (flags & FLAG_DEFAULT_PRIMARY) != 0;
143 }
144
Mathew Inwood98e9ad12018-08-30 13:11:50 +0100145 @UnsupportedAppUsage
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700146 public boolean isSd() {
147 return (flags & FLAG_SD) != 0;
148 }
149
Mathew Inwood98e9ad12018-08-30 13:11:50 +0100150 @UnsupportedAppUsage
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700151 public boolean isUsb() {
152 return (flags & FLAG_USB) != 0;
153 }
154
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700155 @Override
156 public String toString() {
157 final CharArrayWriter writer = new CharArrayWriter();
158 dump(new IndentingPrintWriter(writer, " ", 80));
159 return writer.toString();
160 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700161
162 public void dump(IndentingPrintWriter pw) {
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700163 pw.println("DiskInfo{" + id + "}:");
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700164 pw.increaseIndent();
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700165 pw.printPair("flags", DebugUtils.flagsToString(getClass(), "FLAG_", flags));
166 pw.printPair("size", size);
167 pw.printPair("label", label);
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700168 pw.println();
169 pw.printPair("sysPath", sysPath);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700170 pw.decreaseIndent();
171 pw.println();
172 }
173
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700174 @Override
175 public DiskInfo clone() {
176 final Parcel temp = Parcel.obtain();
177 try {
178 writeToParcel(temp, 0);
179 temp.setDataPosition(0);
180 return CREATOR.createFromParcel(temp);
181 } finally {
182 temp.recycle();
183 }
184 }
185
Jeff Sharkeye2d45be2015-04-15 17:14:12 -0700186 @Override
187 public boolean equals(Object o) {
188 if (o instanceof DiskInfo) {
189 return Objects.equals(id, ((DiskInfo) o).id);
190 } else {
191 return false;
192 }
193 }
194
195 @Override
196 public int hashCode() {
197 return id.hashCode();
198 }
199
Mathew Inwood31755f92018-12-20 13:53:36 +0000200 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700201 public static final Creator<DiskInfo> CREATOR = new Creator<DiskInfo>() {
202 @Override
203 public DiskInfo createFromParcel(Parcel in) {
204 return new DiskInfo(in);
205 }
206
207 @Override
208 public DiskInfo[] newArray(int size) {
209 return new DiskInfo[size];
210 }
211 };
212
213 @Override
214 public int describeContents() {
215 return 0;
216 }
217
218 @Override
219 public void writeToParcel(Parcel parcel, int flags) {
220 parcel.writeString(id);
Jeff Sharkey59d577a2015-04-11 21:27:21 -0700221 parcel.writeInt(this.flags);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700222 parcel.writeLong(size);
223 parcel.writeString(label);
Jeff Sharkeyf5a6bd72015-05-19 14:42:38 -0700224 parcel.writeInt(volumeCount);
Jeff Sharkeye8a4b662015-06-27 15:43:45 -0700225 parcel.writeString(sysPath);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700226 }
227}