blob: 2c60d41acd94120e84be7ad16a965c9ae2d28524 [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;
23import android.util.DebugUtils;
24
25import com.android.internal.util.IndentingPrintWriter;
26import com.android.internal.util.Preconditions;
27
Jeff Sharkey7151a9a2015-04-04 15:22:37 -070028import java.io.CharArrayWriter;
Jeff Sharkeye2d45be2015-04-15 17:14:12 -070029import java.util.Objects;
Jeff Sharkey7151a9a2015-04-04 15:22:37 -070030
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070031/**
32 * Information about a physical disk which may contain one or more
33 * {@link VolumeInfo}.
34 *
35 * @hide
36 */
37public class DiskInfo implements Parcelable {
Jeff Sharkey56bd3122015-04-14 10:30:34 -070038 public static final String EXTRA_DISK_ID = "android.os.storage.extra.DISK_ID";
39
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070040 public static final int FLAG_ADOPTABLE = 1 << 0;
41 public static final int FLAG_DEFAULT_PRIMARY = 1 << 1;
42 public static final int FLAG_SD = 1 << 2;
43 public static final int FLAG_USB = 1 << 3;
44
45 public final String id;
46 public final int flags;
47 public long size;
48 public String label;
Jeff Sharkey56bd3122015-04-14 10:30:34 -070049 public String[] volumeIds;
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 Sharkey56bd3122015-04-14 10:30:34 -070061 volumeIds = parcel.readStringArray();
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070062 }
63
Jeff Sharkeyd95d3bf2015-04-14 21:39:44 -070064 public @NonNull String getId() {
65 return id;
66 }
67
Jeff Sharkey59d577a2015-04-11 21:27:21 -070068 public String getDescription() {
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070069 // TODO: splice vendor label into these strings
70 if ((flags & FLAG_SD) != 0) {
Jeff Sharkey59d577a2015-04-11 21:27:21 -070071 return Resources.getSystem().getString(com.android.internal.R.string.storage_sd_card);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070072 } else if ((flags & FLAG_USB) != 0) {
Jeff Sharkey59d577a2015-04-11 21:27:21 -070073 return Resources.getSystem().getString(com.android.internal.R.string.storage_usb);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070074 } else {
75 return null;
76 }
77 }
78
Jeff Sharkey56bd3122015-04-14 10:30:34 -070079 public boolean isSd() {
80 return (flags & FLAG_SD) != 0;
81 }
82
83 public boolean isUsb() {
84 return (flags & FLAG_USB) != 0;
85 }
86
87 public boolean isAdoptable() {
88 return (flags & FLAG_ADOPTABLE) != 0;
89 }
90
Jeff Sharkey7151a9a2015-04-04 15:22:37 -070091 @Override
92 public String toString() {
93 final CharArrayWriter writer = new CharArrayWriter();
94 dump(new IndentingPrintWriter(writer, " ", 80));
95 return writer.toString();
96 }
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -070097
98 public void dump(IndentingPrintWriter pw) {
99 pw.println("DiskInfo:");
100 pw.increaseIndent();
101 pw.printPair("id", id);
102 pw.printPair("flags", DebugUtils.flagsToString(getClass(), "FLAG_", flags));
103 pw.printPair("size", size);
104 pw.printPair("label", label);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700105 pw.printPair("volumeIds", volumeIds);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700106 pw.decreaseIndent();
107 pw.println();
108 }
109
Jeff Sharkey7151a9a2015-04-04 15:22:37 -0700110 @Override
111 public DiskInfo clone() {
112 final Parcel temp = Parcel.obtain();
113 try {
114 writeToParcel(temp, 0);
115 temp.setDataPosition(0);
116 return CREATOR.createFromParcel(temp);
117 } finally {
118 temp.recycle();
119 }
120 }
121
Jeff Sharkeye2d45be2015-04-15 17:14:12 -0700122 @Override
123 public boolean equals(Object o) {
124 if (o instanceof DiskInfo) {
125 return Objects.equals(id, ((DiskInfo) o).id);
126 } else {
127 return false;
128 }
129 }
130
131 @Override
132 public int hashCode() {
133 return id.hashCode();
134 }
135
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700136 public static final Creator<DiskInfo> CREATOR = new Creator<DiskInfo>() {
137 @Override
138 public DiskInfo createFromParcel(Parcel in) {
139 return new DiskInfo(in);
140 }
141
142 @Override
143 public DiskInfo[] newArray(int size) {
144 return new DiskInfo[size];
145 }
146 };
147
148 @Override
149 public int describeContents() {
150 return 0;
151 }
152
153 @Override
154 public void writeToParcel(Parcel parcel, int flags) {
155 parcel.writeString(id);
Jeff Sharkey59d577a2015-04-11 21:27:21 -0700156 parcel.writeInt(this.flags);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700157 parcel.writeLong(size);
158 parcel.writeString(label);
Jeff Sharkey56bd3122015-04-14 10:30:34 -0700159 parcel.writeStringArray(volumeIds);
Jeff Sharkey1b8ef7e2015-04-03 17:14:45 -0700160 }
161}