1 /*
2  * Copyright (C) 2011 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.text.style;
18 
19 import android.annotation.NonNull;
20 import android.os.Parcel;
21 import android.os.Parcelable;
22 import android.text.ParcelableSpan;
23 import android.text.TextPaint;
24 import android.text.TextUtils;
25 
26 /**
27  * A SuggestionRangeSpan is used to show which part of an EditText is affected by a suggestion
28  * popup window.
29  */
30 public final class SuggestionRangeSpan extends CharacterStyle implements ParcelableSpan {
31     private int mBackgroundColor;
32 
SuggestionRangeSpan()33     public SuggestionRangeSpan() {
34         // 0 is a fully transparent black. Has to be set using #setBackgroundColor
35         mBackgroundColor = 0;
36     }
37 
38     /** @hide */
SuggestionRangeSpan(@onNull Parcel src)39     public SuggestionRangeSpan(@NonNull Parcel src) {
40         mBackgroundColor = src.readInt();
41     }
42 
43     public static final @NonNull Parcelable.Creator<SuggestionRangeSpan>
44             CREATOR = new Parcelable.Creator<SuggestionRangeSpan>() {
45                 @Override
46                 public SuggestionRangeSpan createFromParcel(Parcel source) {
47                     return new SuggestionRangeSpan(source);
48                 }
49 
50                 @Override
51                 public SuggestionRangeSpan[] newArray(int size) {
52                     return new SuggestionRangeSpan[size];
53                 }
54             };
55 
56     @Override
describeContents()57     public int describeContents() {
58         return 0;
59     }
60 
61     @Override
writeToParcel(@onNull Parcel dest, int flags)62     public void writeToParcel(@NonNull Parcel dest, int flags) {
63         writeToParcelInternal(dest, flags);
64     }
65 
66     /** @hide */
writeToParcelInternal(Parcel dest, int flags)67     public void writeToParcelInternal(Parcel dest, int flags) {
68         dest.writeInt(mBackgroundColor);
69     }
70 
71     @Override
getSpanTypeId()72     public int getSpanTypeId() {
73         return getSpanTypeIdInternal();
74     }
75 
76     /** @hide */
getSpanTypeIdInternal()77     public int getSpanTypeIdInternal() {
78         return TextUtils.SUGGESTION_RANGE_SPAN;
79     }
80 
setBackgroundColor(int backgroundColor)81     public void setBackgroundColor(int backgroundColor) {
82         mBackgroundColor = backgroundColor;
83     }
84 
getBackgroundColor()85     public int getBackgroundColor() {
86         return mBackgroundColor;
87     }
88 
89     @Override
updateDrawState(@onNull TextPaint tp)90     public void updateDrawState(@NonNull TextPaint tp) {
91         tp.bgColor = mBackgroundColor;
92     }
93 }
94