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