1 /* 2 * Copyright (C) 2021 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 android.app.smartspace.uitemplatedata; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.SystemApi; 22 import android.app.smartspace.SmartspaceUtils; 23 import android.os.Parcel; 24 import android.os.Parcelable; 25 import android.text.TextUtils; 26 27 import java.util.Objects; 28 29 /** 30 * Holds the information for a Smartspace-card icon. Including the icon image itself, and an 31 * optional content description as the icon's accessibility description. 32 * 33 * @hide 34 */ 35 @SystemApi 36 public final class Icon implements Parcelable { 37 38 @NonNull 39 private final android.graphics.drawable.Icon mIcon; 40 41 @Nullable 42 private final CharSequence mContentDescription; 43 44 private final boolean mShouldTint; 45 Icon(@onNull Parcel in)46 Icon(@NonNull Parcel in) { 47 mIcon = in.readTypedObject(android.graphics.drawable.Icon.CREATOR); 48 mContentDescription = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); 49 mShouldTint = in.readBoolean(); 50 } 51 Icon(@onNull android.graphics.drawable.Icon icon, @Nullable CharSequence contentDescription, boolean shouldTint)52 private Icon(@NonNull android.graphics.drawable.Icon icon, 53 @Nullable CharSequence contentDescription, 54 boolean shouldTint) { 55 mIcon = icon; 56 mContentDescription = contentDescription; 57 mShouldTint = shouldTint; 58 } 59 60 /** Returns the icon image. */ 61 @NonNull getIcon()62 public android.graphics.drawable.Icon getIcon() { 63 return mIcon; 64 } 65 66 /** Returns the content description of the icon image. */ 67 @Nullable getContentDescription()68 public CharSequence getContentDescription() { 69 return mContentDescription; 70 } 71 72 /** 73 * Return shouldTint value, which means whether should tint the icon with the system's theme 74 * color. The default value is true. 75 */ shouldTint()76 public boolean shouldTint() { 77 return mShouldTint; 78 } 79 80 @NonNull 81 public static final Creator<Icon> CREATOR = new Creator<Icon>() { 82 @Override 83 public Icon createFromParcel(Parcel in) { 84 return new Icon(in); 85 } 86 87 @Override 88 public Icon[] newArray(int size) { 89 return new Icon[size]; 90 } 91 }; 92 93 @Override equals(Object o)94 public boolean equals(Object o) { 95 if (this == o) return true; 96 if (!(o instanceof Icon)) return false; 97 Icon that = (Icon) o; 98 return mIcon.toString().equals(that.mIcon.toString()) && SmartspaceUtils.isEqual( 99 mContentDescription, 100 that.mContentDescription) && mShouldTint == that.mShouldTint; 101 } 102 103 @Override hashCode()104 public int hashCode() { 105 return Objects.hash(mIcon.toString(), mContentDescription, mShouldTint); 106 } 107 108 @Override describeContents()109 public int describeContents() { 110 return 0; 111 } 112 113 @Override writeToParcel(@onNull Parcel out, int flags)114 public void writeToParcel(@NonNull Parcel out, int flags) { 115 out.writeTypedObject(mIcon, flags); 116 TextUtils.writeToParcel(mContentDescription, out, flags); 117 out.writeBoolean(mShouldTint); 118 } 119 120 @Override toString()121 public String toString() { 122 return "SmartspaceIcon{" 123 + "mIcon=" + mIcon 124 + ", mContentDescription=" + mContentDescription 125 + ", mShouldTint=" + mShouldTint 126 + '}'; 127 } 128 129 /** 130 * A builder for {@link Icon} object. 131 * 132 * @hide 133 */ 134 @SystemApi 135 public static final class Builder { 136 137 private android.graphics.drawable.Icon mIcon; 138 private CharSequence mContentDescription; 139 private boolean mShouldTint; 140 141 /** 142 * A builder for {@link Icon}, which sets shouldTint to true by default. 143 * 144 * @param icon the icon image of this {@link Icon} instance. 145 */ Builder(@onNull android.graphics.drawable.Icon icon)146 public Builder(@NonNull android.graphics.drawable.Icon icon) { 147 mIcon = Objects.requireNonNull(icon); 148 mShouldTint = true; 149 } 150 151 /** 152 * Sets the icon's content description. 153 */ 154 @NonNull setContentDescription(@onNull CharSequence contentDescription)155 public Builder setContentDescription(@NonNull CharSequence contentDescription) { 156 mContentDescription = contentDescription; 157 return this; 158 } 159 160 /** 161 * Sets should tint icon with the system's theme color. 162 */ 163 @NonNull setShouldTint(boolean shouldTint)164 public Builder setShouldTint(boolean shouldTint) { 165 mShouldTint = shouldTint; 166 return this; 167 } 168 169 /** 170 * Builds a new SmartspaceIcon instance. 171 */ 172 @NonNull build()173 public Icon build() { 174 mIcon.convertToAshmem(); 175 return new Icon(mIcon, mContentDescription, mShouldTint); 176 } 177 } 178 } 179