DiskInfo.java revision e6c04f9417cc4bff0f5f9e72f0d6d66d2aab6e80
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 private boolean isInteresting(String label) { 68 if (TextUtils.isEmpty(label)) { 69 return false; 70 } 71 if (label.toLowerCase().contains("generic")) { 72 return false; 73 } 74 return true; 75 } 76 77 public String getDescription() { 78 final Resources res = Resources.getSystem(); 79 if ((flags & FLAG_SD) != 0) { 80 if (isInteresting(label)) { 81 return res.getString(com.android.internal.R.string.storage_sd_card_label, label); 82 } else { 83 return res.getString(com.android.internal.R.string.storage_sd_card); 84 } 85 } else if ((flags & FLAG_USB) != 0) { 86 if (isInteresting(label)) { 87 return res.getString(com.android.internal.R.string.storage_usb_drive_label, label); 88 } else { 89 return res.getString(com.android.internal.R.string.storage_usb_drive); 90 } 91 } else { 92 return null; 93 } 94 } 95 96 public boolean isSd() { 97 return (flags & FLAG_SD) != 0; 98 } 99 100 public boolean isUsb() { 101 return (flags & FLAG_USB) != 0; 102 } 103 104 public boolean isAdoptable() { 105 return (flags & FLAG_ADOPTABLE) != 0; 106 } 107 108 @Override 109 public String toString() { 110 final CharArrayWriter writer = new CharArrayWriter(); 111 dump(new IndentingPrintWriter(writer, " ", 80)); 112 return writer.toString(); 113 } 114 115 public void dump(IndentingPrintWriter pw) { 116 pw.println("DiskInfo{" + id + "}:"); 117 pw.increaseIndent(); 118 pw.printPair("flags", DebugUtils.flagsToString(getClass(), "FLAG_", flags)); 119 pw.printPair("size", size); 120 pw.printPair("label", label); 121 pw.decreaseIndent(); 122 pw.println(); 123 } 124 125 @Override 126 public DiskInfo clone() { 127 final Parcel temp = Parcel.obtain(); 128 try { 129 writeToParcel(temp, 0); 130 temp.setDataPosition(0); 131 return CREATOR.createFromParcel(temp); 132 } finally { 133 temp.recycle(); 134 } 135 } 136 137 @Override 138 public boolean equals(Object o) { 139 if (o instanceof DiskInfo) { 140 return Objects.equals(id, ((DiskInfo) o).id); 141 } else { 142 return false; 143 } 144 } 145 146 @Override 147 public int hashCode() { 148 return id.hashCode(); 149 } 150 151 public static final Creator<DiskInfo> CREATOR = new Creator<DiskInfo>() { 152 @Override 153 public DiskInfo createFromParcel(Parcel in) { 154 return new DiskInfo(in); 155 } 156 157 @Override 158 public DiskInfo[] newArray(int size) { 159 return new DiskInfo[size]; 160 } 161 }; 162 163 @Override 164 public int describeContents() { 165 return 0; 166 } 167 168 @Override 169 public void writeToParcel(Parcel parcel, int flags) { 170 parcel.writeString(id); 171 parcel.writeInt(this.flags); 172 parcel.writeLong(size); 173 parcel.writeString(label); 174 } 175} 176