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