DiskInfo.java revision 56bd3129138b525b0f2eba52bd4fa140f23e792c
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.content.res.Resources;
20import android.os.Parcel;
21import android.os.Parcelable;
22import android.util.DebugUtils;
23
24import com.android.internal.util.IndentingPrintWriter;
25import com.android.internal.util.Preconditions;
26
27import java.io.CharArrayWriter;
28
29/**
30 * Information about a physical disk which may contain one or more
31 * {@link VolumeInfo}.
32 *
33 * @hide
34 */
35public class DiskInfo implements Parcelable {
36    public static final String EXTRA_DISK_ID = "android.os.storage.extra.DISK_ID";
37
38    public static final int FLAG_ADOPTABLE = 1 << 0;
39    public static final int FLAG_DEFAULT_PRIMARY = 1 << 1;
40    public static final int FLAG_SD = 1 << 2;
41    public static final int FLAG_USB = 1 << 3;
42
43    public final String id;
44    public final int flags;
45    public long size;
46    public String label;
47    public String[] volumeIds;
48
49    public DiskInfo(String id, int flags) {
50        this.id = Preconditions.checkNotNull(id);
51        this.flags = flags;
52    }
53
54    public DiskInfo(Parcel parcel) {
55        id = parcel.readString();
56        flags = parcel.readInt();
57        size = parcel.readLong();
58        label = parcel.readString();
59        volumeIds = parcel.readStringArray();
60    }
61
62    public String getDescription() {
63        // TODO: splice vendor label into these strings
64        if ((flags & FLAG_SD) != 0) {
65            return Resources.getSystem().getString(com.android.internal.R.string.storage_sd_card);
66        } else if ((flags & FLAG_USB) != 0) {
67            return Resources.getSystem().getString(com.android.internal.R.string.storage_usb);
68        } else {
69            return null;
70        }
71    }
72
73    public boolean isSd() {
74        return (flags & FLAG_SD) != 0;
75    }
76
77    public boolean isUsb() {
78        return (flags & FLAG_USB) != 0;
79    }
80
81    public boolean isAdoptable() {
82        return (flags & FLAG_ADOPTABLE) != 0;
83    }
84
85    @Override
86    public String toString() {
87        final CharArrayWriter writer = new CharArrayWriter();
88        dump(new IndentingPrintWriter(writer, "    ", 80));
89        return writer.toString();
90    }
91
92    public void dump(IndentingPrintWriter pw) {
93        pw.println("DiskInfo:");
94        pw.increaseIndent();
95        pw.printPair("id", id);
96        pw.printPair("flags", DebugUtils.flagsToString(getClass(), "FLAG_", flags));
97        pw.printPair("size", size);
98        pw.printPair("label", label);
99        pw.printPair("volumeIds", volumeIds);
100        pw.decreaseIndent();
101        pw.println();
102    }
103
104    @Override
105    public DiskInfo clone() {
106        final Parcel temp = Parcel.obtain();
107        try {
108            writeToParcel(temp, 0);
109            temp.setDataPosition(0);
110            return CREATOR.createFromParcel(temp);
111        } finally {
112            temp.recycle();
113        }
114    }
115
116    public static final Creator<DiskInfo> CREATOR = new Creator<DiskInfo>() {
117        @Override
118        public DiskInfo createFromParcel(Parcel in) {
119            return new DiskInfo(in);
120        }
121
122        @Override
123        public DiskInfo[] newArray(int size) {
124            return new DiskInfo[size];
125        }
126    };
127
128    @Override
129    public int describeContents() {
130        return 0;
131    }
132
133    @Override
134    public void writeToParcel(Parcel parcel, int flags) {
135        parcel.writeString(id);
136        parcel.writeInt(this.flags);
137        parcel.writeLong(size);
138        parcel.writeString(label);
139        parcel.writeStringArray(volumeIds);
140    }
141}
142