DiskInfo.java revision 7e92ef3a1146102806fa0543ef12e09231c55639
1/*
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
19import android.annotation.NonNull;
20import android.content.res.Resources;
21import android.os.Parcel;
22import android.os.Parcelable;
23import android.text.TextUtils;
24import android.util.DebugUtils;
25
26import com.android.internal.util.IndentingPrintWriter;
27import com.android.internal.util.Preconditions;
28
29import java.io.CharArrayWriter;
30import java.util.Objects;
31
32/**
33 * Information about a physical disk which may contain one or more
34 * {@link VolumeInfo}.
35 *
36 * @hide
37 */
38public class DiskInfo implements Parcelable {
39    public static final String EXTRA_DISK_ID = "android.os.storage.extra.DISK_ID";
40
41    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;
50
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();
61    }
62
63    public @NonNull String getId() {
64        return id;
65    }
66
67    public String getDescription() {
68        final Resources res = Resources.getSystem();
69        if ((flags & FLAG_SD) != 0) {
70            if (TextUtils.isEmpty(label)) {
71                return res.getString(com.android.internal.R.string.storage_sd_card);
72            } else {
73                return res.getString(com.android.internal.R.string.storage_sd_card_label, label);
74            }
75        } else if ((flags & FLAG_USB) != 0) {
76            if (TextUtils.isEmpty(label)) {
77                return res.getString(com.android.internal.R.string.storage_usb_drive);
78            } else {
79                return res.getString(com.android.internal.R.string.storage_usb_drive_label, label);
80            }
81        } else {
82            return null;
83        }
84    }
85
86    public boolean isSd() {
87        return (flags & FLAG_SD) != 0;
88    }
89
90    public boolean isUsb() {
91        return (flags & FLAG_USB) != 0;
92    }
93
94    public boolean isAdoptable() {
95        return (flags & FLAG_ADOPTABLE) != 0;
96    }
97
98    @Override
99    public String toString() {
100        final CharArrayWriter writer = new CharArrayWriter();
101        dump(new IndentingPrintWriter(writer, "    ", 80));
102        return writer.toString();
103    }
104
105    public void dump(IndentingPrintWriter pw) {
106        pw.println("DiskInfo{" + id + "}:");
107        pw.increaseIndent();
108        pw.printPair("flags", DebugUtils.flagsToString(getClass(), "FLAG_", flags));
109        pw.printPair("size", size);
110        pw.printPair("label", label);
111        pw.decreaseIndent();
112        pw.println();
113    }
114
115    @Override
116    public DiskInfo clone() {
117        final Parcel temp = Parcel.obtain();
118        try {
119            writeToParcel(temp, 0);
120            temp.setDataPosition(0);
121            return CREATOR.createFromParcel(temp);
122        } finally {
123            temp.recycle();
124        }
125    }
126
127    @Override
128    public boolean equals(Object o) {
129        if (o instanceof DiskInfo) {
130            return Objects.equals(id, ((DiskInfo) o).id);
131        } else {
132            return false;
133        }
134    }
135
136    @Override
137    public int hashCode() {
138        return id.hashCode();
139    }
140
141    public static final Creator<DiskInfo> CREATOR = new Creator<DiskInfo>() {
142        @Override
143        public DiskInfo createFromParcel(Parcel in) {
144            return new DiskInfo(in);
145        }
146
147        @Override
148        public DiskInfo[] newArray(int size) {
149            return new DiskInfo[size];
150        }
151    };
152
153    @Override
154    public int describeContents() {
155        return 0;
156    }
157
158    @Override
159    public void writeToParcel(Parcel parcel, int flags) {
160        parcel.writeString(id);
161        parcel.writeInt(this.flags);
162        parcel.writeLong(size);
163        parcel.writeString(label);
164    }
165}
166