DiskInfo.java revision 47b872d9c36347382dd24ad5c3f70490d8fcbb23
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 ACTION_DISK_SCANNED = 40 "android.os.storage.action.DISK_SCANNED"; 41 public static final String EXTRA_DISK_ID = 42 "android.os.storage.extra.DISK_ID"; 43 44 public static final int FLAG_ADOPTABLE = 1 << 0; 45 public static final int FLAG_DEFAULT_PRIMARY = 1 << 1; 46 public static final int FLAG_SD = 1 << 2; 47 public static final int FLAG_USB = 1 << 3; 48 49 public final String id; 50 public final int flags; 51 public long size; 52 public String label; 53 /** Hacky; don't rely on this count */ 54 public int volumeCount; 55 56 public DiskInfo(String id, int flags) { 57 this.id = Preconditions.checkNotNull(id); 58 this.flags = flags; 59 } 60 61 public DiskInfo(Parcel parcel) { 62 id = parcel.readString(); 63 flags = parcel.readInt(); 64 size = parcel.readLong(); 65 label = parcel.readString(); 66 volumeCount = parcel.readInt(); 67 } 68 69 public @NonNull String getId() { 70 return id; 71 } 72 73 private boolean isInteresting(String label) { 74 if (TextUtils.isEmpty(label)) { 75 return false; 76 } 77 if (label.equalsIgnoreCase("ata")) { 78 return false; 79 } 80 if (label.toLowerCase().contains("generic")) { 81 return false; 82 } 83 if (label.toLowerCase().startsWith("usb")) { 84 return false; 85 } 86 if (label.toLowerCase().startsWith("multiple")) { 87 return false; 88 } 89 return true; 90 } 91 92 public String getDescription() { 93 final Resources res = Resources.getSystem(); 94 if ((flags & FLAG_SD) != 0) { 95 if (isInteresting(label)) { 96 return res.getString(com.android.internal.R.string.storage_sd_card_label, label); 97 } else { 98 return res.getString(com.android.internal.R.string.storage_sd_card); 99 } 100 } else if ((flags & FLAG_USB) != 0) { 101 if (isInteresting(label)) { 102 return res.getString(com.android.internal.R.string.storage_usb_drive_label, label); 103 } else { 104 return res.getString(com.android.internal.R.string.storage_usb_drive); 105 } 106 } else { 107 return null; 108 } 109 } 110 111 public boolean isAdoptable() { 112 return (flags & FLAG_ADOPTABLE) != 0; 113 } 114 115 public boolean isDefaultPrimary() { 116 return (flags & FLAG_DEFAULT_PRIMARY) != 0; 117 } 118 119 public boolean isSd() { 120 return (flags & FLAG_SD) != 0; 121 } 122 123 public boolean isUsb() { 124 return (flags & FLAG_USB) != 0; 125 } 126 127 @Override 128 public String toString() { 129 final CharArrayWriter writer = new CharArrayWriter(); 130 dump(new IndentingPrintWriter(writer, " ", 80)); 131 return writer.toString(); 132 } 133 134 public void dump(IndentingPrintWriter pw) { 135 pw.println("DiskInfo{" + id + "}:"); 136 pw.increaseIndent(); 137 pw.printPair("flags", DebugUtils.flagsToString(getClass(), "FLAG_", flags)); 138 pw.printPair("size", size); 139 pw.printPair("label", label); 140 pw.decreaseIndent(); 141 pw.println(); 142 } 143 144 @Override 145 public DiskInfo clone() { 146 final Parcel temp = Parcel.obtain(); 147 try { 148 writeToParcel(temp, 0); 149 temp.setDataPosition(0); 150 return CREATOR.createFromParcel(temp); 151 } finally { 152 temp.recycle(); 153 } 154 } 155 156 @Override 157 public boolean equals(Object o) { 158 if (o instanceof DiskInfo) { 159 return Objects.equals(id, ((DiskInfo) o).id); 160 } else { 161 return false; 162 } 163 } 164 165 @Override 166 public int hashCode() { 167 return id.hashCode(); 168 } 169 170 public static final Creator<DiskInfo> CREATOR = new Creator<DiskInfo>() { 171 @Override 172 public DiskInfo createFromParcel(Parcel in) { 173 return new DiskInfo(in); 174 } 175 176 @Override 177 public DiskInfo[] newArray(int size) { 178 return new DiskInfo[size]; 179 } 180 }; 181 182 @Override 183 public int describeContents() { 184 return 0; 185 } 186 187 @Override 188 public void writeToParcel(Parcel parcel, int flags) { 189 parcel.writeString(id); 190 parcel.writeInt(this.flags); 191 parcel.writeLong(size); 192 parcel.writeString(label); 193 parcel.writeInt(volumeCount); 194 } 195} 196