blob: 64f2a05b6720408e8dd909192e597c3120f12fac [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 Sharkey59d577a2015-04-11 21:27:21 -070020import android.content.res.Resources;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070021import android.os.Parcel;
22import android.os.Parcelable;
Jeff Sharkey7e92ef32015-04-17 17:35:07 -070023import android.text.TextUtils;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070024import android.util.DebugUtils;
25
26import com.android.internal.util.IndentingPrintWriter;
27import com.android.internal.util.Preconditions;
28
Jeff Sharkey7151a9a2015-04-04 15:22:37 -070029import java.io.CharArrayWriter;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -070030import java.util.Objects;
Jeff Sharkey7151a9a2015-04-04 15:22:37 -070031
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070032/**
33 * Information about a physical disk which may contain one or more
34 * {@link VolumeInfo}.
35 *
36 * @hide
37 */
38public class DiskInfo implements Parcelable {
Jeff Sharkey56bd3122015-04-14 10:30:34 -070039 public static final String EXTRA_DISK_ID = "android.os.storage.extra.DISK_ID";
40
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070041 public static final int FLAG_ADOPTABLE = 1 << 0;
42 public static final int FLAG_DEFAULT_PRIMARY = 1 << 1;
43 public static final int FLAG_SD = 1 << 2;
44 public static final int FLAG_USB = 1 << 3;
45
46 public final String id;
47 public final int flags;
48 public long size;
49 public String label;
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070050
51 public DiskInfo(String id, int flags) {
52 this.id = Preconditions.checkNotNull(id);
53 this.flags = flags;
54 }
55
56 public DiskInfo(Parcel parcel) {
57 id = parcel.readString();
58 flags = parcel.readInt();
59 size = parcel.readLong();
60 label = parcel.readString();
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070061 }
62
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070063 public @NonNull String getId() {
64 return id;
65 }
66
Jeff Sharkeye6c04f92015-04-18 21:38:05 -070067 private boolean isInteresting(String label) {
68 if (TextUtils.isEmpty(label)) {
69 return false;
70 }
Jeff Sharkey74acbbb2015-04-21 12:14:03 -070071 if (label.equalsIgnoreCase("ata")) {
72 return false;
73 }
Jeff Sharkeye6c04f92015-04-18 21:38:05 -070074 if (label.toLowerCase().contains("generic")) {
75 return false;
76 }
77 return true;
78 }
79
Jeff Sharkey59d577a2015-04-11 21:27:21 -070080 public String getDescription() {
Jeff Sharkey7e92ef32015-04-17 17:35:07 -070081 final Resources res = Resources.getSystem();
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070082 if ((flags & FLAG_SD) != 0) {
Jeff Sharkeye6c04f92015-04-18 21:38:05 -070083 if (isInteresting(label)) {
Jeff Sharkey7e92ef32015-04-17 17:35:07 -070084 return res.getString(com.android.internal.R.string.storage_sd_card_label, label);
Jeff Sharkeye6c04f92015-04-18 21:38:05 -070085 } else {
86 return res.getString(com.android.internal.R.string.storage_sd_card);
Jeff Sharkey7e92ef32015-04-17 17:35:07 -070087 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070088 } else if ((flags & FLAG_USB) != 0) {
Jeff Sharkeye6c04f92015-04-18 21:38:05 -070089 if (isInteresting(label)) {
Jeff Sharkey7e92ef32015-04-17 17:35:07 -070090 return res.getString(com.android.internal.R.string.storage_usb_drive_label, label);
Jeff Sharkeye6c04f92015-04-18 21:38:05 -070091 } else {
92 return res.getString(com.android.internal.R.string.storage_usb_drive);
Jeff Sharkey7e92ef32015-04-17 17:35:07 -070093 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070094 } else {
95 return null;
96 }
97 }
98
Jeff Sharkey56bd3122015-04-14 10:30:34 -070099 public boolean isSd() {
100 return (flags & FLAG_SD) != 0;
101 }
102
103 public boolean isUsb() {
104 return (flags & FLAG_USB) != 0;
105 }
106
107 public boolean isAdoptable() {
108 return (flags & FLAG_ADOPTABLE) != 0;
109 }
110
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700111 @Override
112 public String toString() {
113 final CharArrayWriter writer = new CharArrayWriter();
114 dump(new IndentingPrintWriter(writer, " ", 80));
115 return writer.toString();
116 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700117
118 public void dump(IndentingPrintWriter pw) {
Jeff Sharkey7e92ef32015-04-17 17:35:07 -0700119 pw.println("DiskInfo{" + id + "}:");
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700120 pw.increaseIndent();
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700121 pw.printPair("flags", DebugUtils.flagsToString(getClass(), "FLAG_", flags));
122 pw.printPair("size", size);
123 pw.printPair("label", label);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700124 pw.decreaseIndent();
125 pw.println();
126 }
127
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700128 @Override
129 public DiskInfo clone() {
130 final Parcel temp = Parcel.obtain();
131 try {
132 writeToParcel(temp, 0);
133 temp.setDataPosition(0);
134 return CREATOR.createFromParcel(temp);
135 } finally {
136 temp.recycle();
137 }
138 }
139
Jeff Sharkeye2d45be2015-04-15 17:14:12 -0700140 @Override
141 public boolean equals(Object o) {
142 if (o instanceof DiskInfo) {
143 return Objects.equals(id, ((DiskInfo) o).id);
144 } else {
145 return false;
146 }
147 }
148
149 @Override
150 public int hashCode() {
151 return id.hashCode();
152 }
153
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700154 public static final Creator<DiskInfo> CREATOR = new Creator<DiskInfo>() {
155 @Override
156 public DiskInfo createFromParcel(Parcel in) {
157 return new DiskInfo(in);
158 }
159
160 @Override
161 public DiskInfo[] newArray(int size) {
162 return new DiskInfo[size];
163 }
164 };
165
166 @Override
167 public int describeContents() {
168 return 0;
169 }
170
171 @Override
172 public void writeToParcel(Parcel parcel, int flags) {
173 parcel.writeString(id);
Jeff Sharkey59d577a2015-04-11 21:27:21 -0700174 parcel.writeInt(this.flags);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700175 parcel.writeLong(size);
176 parcel.writeString(label);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700177 }
178}