DiskInfo.java revision 7151a9a887051542c6da9f380376f3b306184e5c
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.Context; 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 int FLAG_ADOPTABLE = 1 << 0; 37 public static final int FLAG_DEFAULT_PRIMARY = 1 << 1; 38 public static final int FLAG_SD = 1 << 2; 39 public static final int FLAG_USB = 1 << 3; 40 41 public final String id; 42 public final int flags; 43 public long size; 44 public String label; 45 public String[] volumes; 46 47 public DiskInfo(String id, int flags) { 48 this.id = Preconditions.checkNotNull(id); 49 this.flags = flags; 50 } 51 52 public DiskInfo(Parcel parcel) { 53 id = parcel.readString(); 54 flags = parcel.readInt(); 55 size = parcel.readLong(); 56 label = parcel.readString(); 57 volumes = parcel.readStringArray(); 58 } 59 60 public String getDescription(Context context) { 61 // TODO: splice vendor label into these strings 62 if ((flags & FLAG_SD) != 0) { 63 return context.getString(com.android.internal.R.string.storage_sd_card); 64 } else if ((flags & FLAG_USB) != 0) { 65 return context.getString(com.android.internal.R.string.storage_usb); 66 } else { 67 return null; 68 } 69 } 70 71 @Override 72 public String toString() { 73 final CharArrayWriter writer = new CharArrayWriter(); 74 dump(new IndentingPrintWriter(writer, " ", 80)); 75 return writer.toString(); 76 } 77 78 public void dump(IndentingPrintWriter pw) { 79 pw.println("DiskInfo:"); 80 pw.increaseIndent(); 81 pw.printPair("id", id); 82 pw.printPair("flags", DebugUtils.flagsToString(getClass(), "FLAG_", flags)); 83 pw.printPair("size", size); 84 pw.printPair("label", label); 85 pw.printPair("volumes", volumes); 86 pw.decreaseIndent(); 87 pw.println(); 88 } 89 90 @Override 91 public DiskInfo clone() { 92 final Parcel temp = Parcel.obtain(); 93 try { 94 writeToParcel(temp, 0); 95 temp.setDataPosition(0); 96 return CREATOR.createFromParcel(temp); 97 } finally { 98 temp.recycle(); 99 } 100 } 101 102 public static final Creator<DiskInfo> CREATOR = new Creator<DiskInfo>() { 103 @Override 104 public DiskInfo createFromParcel(Parcel in) { 105 return new DiskInfo(in); 106 } 107 108 @Override 109 public DiskInfo[] newArray(int size) { 110 return new DiskInfo[size]; 111 } 112 }; 113 114 @Override 115 public int describeContents() { 116 return 0; 117 } 118 119 @Override 120 public void writeToParcel(Parcel parcel, int flags) { 121 parcel.writeString(id); 122 parcel.writeInt(flags); 123 parcel.writeLong(size); 124 parcel.writeString(label); 125 parcel.writeStringArray(volumes); 126 } 127} 128