1 /* 2 * Copyright (C) 2022 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.SystemApi; 21 import android.app.smartspace.SmartspaceUtils; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 import android.text.TextUtils; 25 26 import java.util.Objects; 27 28 /** 29 * Holds the information for a Smartspace-card text: the text content and 30 * the truncate_at information. 31 * 32 * @hide 33 */ 34 @SystemApi 35 public final class Text implements Parcelable { 36 37 @NonNull 38 private final CharSequence mText; 39 40 private final TextUtils.TruncateAt mTruncateAtType; 41 42 private final int mMaxLines; 43 Text(Parcel in)44 Text(Parcel in) { 45 mText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); 46 mTruncateAtType = TextUtils.TruncateAt.valueOf(in.readString()); 47 mMaxLines = in.readInt(); 48 } 49 Text(@onNull CharSequence text, TextUtils.TruncateAt truncateAtType, int maxLines)50 private Text(@NonNull CharSequence text, TextUtils.TruncateAt truncateAtType, int maxLines) { 51 mText = text; 52 mTruncateAtType = truncateAtType; 53 mMaxLines = maxLines; 54 } 55 56 /** Returns the text content. */ 57 @NonNull getText()58 public CharSequence getText() { 59 return mText; 60 } 61 62 /** Returns the {@link TextUtils.TruncateAt} type of the text content. */ 63 @NonNull getTruncateAtType()64 public TextUtils.TruncateAt getTruncateAtType() { 65 return mTruncateAtType; 66 } 67 68 /** Returns the allowed max lines for presenting the text content. */ getMaxLines()69 public int getMaxLines() { 70 return mMaxLines; 71 } 72 73 @NonNull 74 public static final Creator<Text> CREATOR = new Creator<Text>() { 75 @Override 76 public Text createFromParcel(Parcel in) { 77 return new Text(in); 78 } 79 80 @Override 81 public Text[] newArray(int size) { 82 return new Text[size]; 83 } 84 }; 85 86 @Override describeContents()87 public int describeContents() { 88 return 0; 89 } 90 91 @Override equals(Object o)92 public boolean equals(Object o) { 93 if (this == o) return true; 94 if (!(o instanceof Text)) return false; 95 Text that = (Text) o; 96 return mTruncateAtType == that.mTruncateAtType && SmartspaceUtils.isEqual(mText, 97 that.mText) && mMaxLines == that.mMaxLines; 98 } 99 100 @Override hashCode()101 public int hashCode() { 102 return Objects.hash(mText, mTruncateAtType, mMaxLines); 103 } 104 105 @Override writeToParcel(@onNull Parcel out, int flags)106 public void writeToParcel(@NonNull Parcel out, int flags) { 107 TextUtils.writeToParcel(mText, out, flags); 108 out.writeString(mTruncateAtType.name()); 109 out.writeInt(mMaxLines); 110 } 111 112 @Override toString()113 public String toString() { 114 return "Text{" 115 + "mText=" + mText 116 + ", mTruncateAtType=" + mTruncateAtType 117 + ", mMaxLines=" + mMaxLines 118 + '}'; 119 } 120 121 /** 122 * A builder for {@link Text} object. 123 * 124 * @hide 125 */ 126 @SystemApi 127 public static final class Builder { 128 private final CharSequence mText; 129 private TextUtils.TruncateAt mTruncateAtType; 130 private int mMaxLines; 131 132 /** 133 * A builder for {@link Text}, which by default sets TruncateAtType to AT_END, and the max 134 * lines to 1. 135 */ Builder(@onNull CharSequence text)136 public Builder(@NonNull CharSequence text) { 137 mText = Objects.requireNonNull(text); 138 mTruncateAtType = TextUtils.TruncateAt.END; 139 mMaxLines = 1; 140 } 141 142 /** 143 * Sets truncateAtType, where the text content should be truncated if not all the content 144 * can be presented. 145 */ 146 @NonNull setTruncateAtType(@onNull TextUtils.TruncateAt truncateAtType)147 public Builder setTruncateAtType(@NonNull TextUtils.TruncateAt truncateAtType) { 148 mTruncateAtType = Objects.requireNonNull(truncateAtType); 149 return this; 150 } 151 152 /** 153 * Sets the allowed max lines for the text content. 154 */ 155 @NonNull setMaxLines(int maxLines)156 public Builder setMaxLines(int maxLines) { 157 mMaxLines = maxLines; 158 return this; 159 } 160 161 /** 162 * Builds a new SmartspaceText instance. 163 */ 164 @NonNull build()165 public Text build() { 166 return new Text(mText, mTruncateAtType, mMaxLines); 167 } 168 } 169 } 170