1 /* 2 * Copyright (C) 2010 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 17 package com.android.internal.statusbar; 18 19 import android.graphics.drawable.Icon; 20 import android.os.Parcel; 21 import android.os.Parcelable; 22 import android.os.UserHandle; 23 import android.text.TextUtils; 24 25 import androidx.annotation.NonNull; 26 27 public class StatusBarIcon implements Parcelable { 28 public enum Type { 29 // Notification: the sender avatar for important conversations 30 PeopleAvatar, 31 // Notification: the monochrome version of the app icon if available; otherwise fall back to 32 // the small icon 33 MaybeMonochromeAppIcon, 34 // Notification: the small icon from the notification 35 NotifSmallIcon, 36 // The wi-fi, cellular or battery icon. 37 SystemIcon 38 } 39 40 public UserHandle user; 41 public String pkg; 42 public Icon icon; 43 public int iconLevel; 44 public boolean visible = true; 45 public int number; 46 public CharSequence contentDescription; 47 public Type type; 48 StatusBarIcon(UserHandle user, String resPackage, Icon icon, int iconLevel, int number, CharSequence contentDescription, Type type)49 public StatusBarIcon(UserHandle user, String resPackage, Icon icon, int iconLevel, int number, 50 CharSequence contentDescription, Type type) { 51 if (icon.getType() == Icon.TYPE_RESOURCE 52 && TextUtils.isEmpty(icon.getResPackage())) { 53 // This is an odd situation where someone's managed to hand us an icon without a 54 // package inside, probably by mashing an int res into a Notification object. 55 // Now that we have the correct package name handy, let's fix it. 56 icon = Icon.createWithResource(resPackage, icon.getResId()); 57 } 58 this.pkg = resPackage; 59 this.user = user; 60 this.icon = icon; 61 this.iconLevel = iconLevel; 62 this.number = number; 63 this.contentDescription = contentDescription; 64 this.type = type; 65 } 66 StatusBarIcon(String iconPackage, UserHandle user, int iconId, int iconLevel, int number, CharSequence contentDescription, Type type)67 public StatusBarIcon(String iconPackage, UserHandle user, 68 int iconId, int iconLevel, int number, 69 CharSequence contentDescription, Type type) { 70 this(user, iconPackage, Icon.createWithResource(iconPackage, iconId), 71 iconLevel, number, contentDescription, type); 72 } 73 74 @NonNull 75 @Override toString()76 public String toString() { 77 return "StatusBarIcon(icon=" + icon 78 + ((iconLevel != 0)?(" level=" + iconLevel):"") 79 + (visible?" visible":"") 80 + " user=" + user.getIdentifier() 81 + ((number != 0)?(" num=" + number):"") 82 + " )"; 83 } 84 85 @NonNull 86 @Override clone()87 public StatusBarIcon clone() { 88 StatusBarIcon that = new StatusBarIcon(this.user, this.pkg, this.icon, 89 this.iconLevel, this.number, this.contentDescription, this.type); 90 that.visible = this.visible; 91 return that; 92 } 93 94 /** 95 * Unflatten the StatusBarIcon from a parcel. 96 */ StatusBarIcon(Parcel in)97 public StatusBarIcon(Parcel in) { 98 readFromParcel(in); 99 } 100 readFromParcel(Parcel in)101 public void readFromParcel(Parcel in) { 102 this.icon = (Icon) in.readParcelable(null, android.graphics.drawable.Icon.class); 103 this.pkg = in.readString(); 104 this.user = (UserHandle) in.readParcelable(null, android.os.UserHandle.class); 105 this.iconLevel = in.readInt(); 106 this.visible = in.readInt() != 0; 107 this.number = in.readInt(); 108 this.contentDescription = in.readCharSequence(); 109 this.type = Type.valueOf(in.readString()); 110 } 111 writeToParcel(Parcel out, int flags)112 public void writeToParcel(Parcel out, int flags) { 113 out.writeParcelable(this.icon, 0); 114 out.writeString(this.pkg); 115 out.writeParcelable(this.user, 0); 116 out.writeInt(this.iconLevel); 117 out.writeInt(this.visible ? 1 : 0); 118 out.writeInt(this.number); 119 out.writeCharSequence(this.contentDescription); 120 out.writeString(this.type.name()); 121 } 122 describeContents()123 public int describeContents() { 124 return 0; 125 } 126 127 /** 128 * Parcelable.Creator that instantiates StatusBarIcon objects 129 */ 130 public static final Parcelable.Creator<StatusBarIcon> CREATOR 131 = new Parcelable.Creator<StatusBarIcon>() 132 { 133 public StatusBarIcon createFromParcel(Parcel parcel) 134 { 135 return new StatusBarIcon(parcel); 136 } 137 138 public StatusBarIcon[] newArray(int size) 139 { 140 return new StatusBarIcon[size]; 141 } 142 }; 143 } 144 145