| 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.view.translation; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.SystemApi; |
| 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
| 23 | import android.widget.TextView; |
| 24 | |
| 25 | import com.android.internal.util.DataClass; |
| 26 | |
| 27 | /** |
| 28 | * Specifications for configuring UI translation. |
| 29 | * |
| 30 | * @hide |
| 31 | */ |
| 32 | @DataClass( |
| 33 | genBuilder = true, genEqualsHashCode = true, genHiddenConstDefs = true, genToString = true) |
| 34 | @DataClass.Suppress("isShouldPadContentForCompat") |
| 35 | @SystemApi |
| 36 | public final class UiTranslationSpec implements Parcelable { |
| 37 | |
| 38 | /** |
| 39 | * Whether the original content of the view should be made to appear as if it is the same size |
| 40 | * as the translated content. Defaults to {@code false}. |
| 41 | * <p> |
| 42 | * For {@link TextView}, the system does not directly modify the original text, rather |
| 43 | * changes the displayed content using a {@link android.text.method.TransformationMethod}. This |
| 44 | * can cause issues in apps that do not account for length-changing TransformationMethods. For |
| 45 | * example, an app using DynamicLayout may use the calculated line-offsets to operate on the |
| 46 | * original text, but this can cause crashes when the layout was calculated on translated text |
| 47 | * with a different length. |
| 48 | * <p> |
| 49 | * If this is {@code true}, for a TextView the default implementation appends spaces to the |
| 50 | * result of {@link TextView#getText()} to make the length the same as the translated text. |
| 51 | * <p> |
| 52 | * This only affects apps with target SDK R or lower. |
| 53 | */ |
| 54 | private boolean mShouldPadContentForCompat = false; |
| 55 | |
| 56 | /** |
| 57 | * Whether the original content of the view should be made to appear as if it is the same size |
| 58 | * as the translated content. |
| 59 | * <p> |
| 60 | * For {@link TextView}, the system does not directly modify the original text, rather |
| 61 | * changes the displayed content using a {@link android.text.method.TransformationMethod}. This |
| 62 | * can cause issues in apps that do not account for length-changing TransformationMethods. For |
| 63 | * example, an app using DynamicLayout may use the calculated line-offsets to operate on the |
| 64 | * original text, but this can cause crashes when the layout was calculated on translated text |
| 65 | * with a different length. |
| 66 | * <p> |
| 67 | * If this is {@code true}, for a TextView the default implementation appends spaces to the |
| 68 | * result of {@link TextView#getText()} to make the length the same as the translated text. |
| 69 | * <p> |
| 70 | * This only affects apps with target SDK R or lower. |
| 71 | */ |
| 72 | public boolean shouldPadContentForCompat() { |
| 73 | return mShouldPadContentForCompat; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | |
| 78 | // Code below generated by codegen v1.0.23. |
| 79 | // |
| 80 | // DO NOT MODIFY! |
| 81 | // CHECKSTYLE:OFF Generated code |
| 82 | // |
| 83 | // To regenerate run: |
| 84 | // $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/view/translation/UiTranslationSpec.java |
| 85 | // |
| 86 | // To exclude the generated code from IntelliJ auto-formatting enable (one-time): |
| 87 | // Settings > Editor > Code Style > Formatter Control |
| 88 | //@formatter:off |
| 89 | |
| 90 | |
| 91 | @DataClass.Generated.Member |
| 92 | /* package-private */ UiTranslationSpec( |
| 93 | boolean shouldPadContentForCompat) { |
| 94 | this.mShouldPadContentForCompat = shouldPadContentForCompat; |
| 95 | |
| 96 | // onConstructed(); // You can define this method to get a callback |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | @DataClass.Generated.Member |
| 101 | public String toString() { |
| 102 | // You can override field toString logic by defining methods like: |
| 103 | // String fieldNameToString() { ... } |
| 104 | |
| 105 | return "UiTranslationSpec { " + |
| 106 | "shouldPadContentForCompat = " + mShouldPadContentForCompat + |
| 107 | " }"; |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | @DataClass.Generated.Member |
| 112 | public boolean equals(@android.annotation.Nullable Object o) { |
| 113 | // You can override field equality logic by defining either of the methods like: |
| 114 | // boolean fieldNameEquals(UiTranslationSpec other) { ... } |
| 115 | // boolean fieldNameEquals(FieldType otherValue) { ... } |
| 116 | |
| 117 | if (this == o) return true; |
| 118 | if (o == null || getClass() != o.getClass()) return false; |
| 119 | @SuppressWarnings("unchecked") |
| 120 | UiTranslationSpec that = (UiTranslationSpec) o; |
| 121 | //noinspection PointlessBooleanExpression |
| 122 | return true |
| 123 | && mShouldPadContentForCompat == that.mShouldPadContentForCompat; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | @DataClass.Generated.Member |
| 128 | public int hashCode() { |
| 129 | // You can override field hashCode logic by defining methods like: |
| 130 | // int fieldNameHashCode() { ... } |
| 131 | |
| 132 | int _hash = 1; |
| 133 | _hash = 31 * _hash + Boolean.hashCode(mShouldPadContentForCompat); |
| 134 | return _hash; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | @DataClass.Generated.Member |
| 139 | public void writeToParcel(@NonNull Parcel dest, int flags) { |
| 140 | // You can override field parcelling by defining methods like: |
| 141 | // void parcelFieldName(Parcel dest, int flags) { ... } |
| 142 | |
| 143 | byte flg = 0; |
| 144 | if (mShouldPadContentForCompat) flg |= 0x1; |
| 145 | dest.writeByte(flg); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | @DataClass.Generated.Member |
| 150 | public int describeContents() { return 0; } |
| 151 | |
| 152 | /** @hide */ |
| 153 | @SuppressWarnings({"unchecked", "RedundantCast"}) |
| 154 | @DataClass.Generated.Member |
| 155 | /* package-private */ UiTranslationSpec(@NonNull Parcel in) { |
| 156 | // You can override field unparcelling by defining methods like: |
| 157 | // static FieldType unparcelFieldName(Parcel in) { ... } |
| 158 | |
| 159 | byte flg = in.readByte(); |
| 160 | boolean shouldPadContentForCompat = (flg & 0x1) != 0; |
| 161 | |
| 162 | this.mShouldPadContentForCompat = shouldPadContentForCompat; |
| 163 | |
| 164 | // onConstructed(); // You can define this method to get a callback |
| 165 | } |
| 166 | |
| 167 | @DataClass.Generated.Member |
| 168 | public static final @NonNull Parcelable.Creator<UiTranslationSpec> CREATOR |
| 169 | = new Parcelable.Creator<UiTranslationSpec>() { |
| 170 | @Override |
| 171 | public UiTranslationSpec[] newArray(int size) { |
| 172 | return new UiTranslationSpec[size]; |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public UiTranslationSpec createFromParcel(@NonNull Parcel in) { |
| 177 | return new UiTranslationSpec(in); |
| 178 | } |
| 179 | }; |
| 180 | |
| 181 | /** |
| 182 | * A builder for {@link UiTranslationSpec} |
| 183 | */ |
| 184 | @SuppressWarnings("WeakerAccess") |
| 185 | @DataClass.Generated.Member |
| 186 | public static final class Builder { |
| 187 | |
| 188 | private boolean mShouldPadContentForCompat; |
| 189 | |
| 190 | private long mBuilderFieldsSet = 0L; |
| 191 | |
| 192 | public Builder() { |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Whether the original content of the view should be made to appear as if it is the same size |
| 197 | * as the translated content. Defaults to {@code false}. |
| 198 | * <p> |
| 199 | * For {@link TextView}, the system does not directly modify the original text, rather |
| 200 | * changes the displayed content using a {@link android.text.method.TransformationMethod}. This |
| 201 | * can cause issues in apps that do not account for length-changing TransformationMethods. For |
| 202 | * example, an app using DynamicLayout may use the calculated line-offsets to operate on the |
| 203 | * original text, but this can cause crashes when the layout was calculated on translated text |
| 204 | * with a different length. |
| 205 | * <p> |
| 206 | * If this is {@code true}, for a TextView the default implementation appends spaces to the |
| 207 | * result of {@link TextView#getText()} to make the length the same as the translated text. |
| 208 | * <p> |
| 209 | * This only affects apps with target SDK R or lower. |
| 210 | */ |
| 211 | @DataClass.Generated.Member |
| 212 | public @NonNull Builder setShouldPadContentForCompat(boolean value) { |
| 213 | checkNotUsed(); |
| 214 | mBuilderFieldsSet |= 0x1; |
| 215 | mShouldPadContentForCompat = value; |
| 216 | return this; |
| 217 | } |
| 218 | |
| 219 | /** Builds the instance. This builder should not be touched after calling this! */ |
| 220 | public @NonNull UiTranslationSpec build() { |
| 221 | checkNotUsed(); |
| 222 | mBuilderFieldsSet |= 0x2; // Mark builder used |
| 223 | |
| 224 | if ((mBuilderFieldsSet & 0x1) == 0) { |
| 225 | mShouldPadContentForCompat = false; |
| 226 | } |
| 227 | UiTranslationSpec o = new UiTranslationSpec( |
| 228 | mShouldPadContentForCompat); |
| 229 | return o; |
| 230 | } |
| 231 | |
| 232 | private void checkNotUsed() { |
| 233 | if ((mBuilderFieldsSet & 0x2) != 0) { |
| 234 | throw new IllegalStateException( |
| 235 | "This Builder should not be reused. Use a new Builder instance instead"); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | @DataClass.Generated( |
| 241 | time = 1620790033058L, |
| 242 | codegenVersion = "1.0.23", |
| 243 | sourceFile = "frameworks/base/core/java/android/view/translation/UiTranslationSpec.java", |
| 244 | inputSignatures = "private boolean mShouldPadContentForCompat\npublic boolean shouldPadContentForCompat()\nclass UiTranslationSpec extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genBuilder=true, genEqualsHashCode=true, genHiddenConstDefs=true, genToString=true)") |
| 245 | @Deprecated |
| 246 | private void __metadata() {} |
| 247 | |
| 248 | |
| 249 | //@formatter:on |
| 250 | // End of generated code |
| 251 | |
| 252 | } |