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.app.PendingIntent;
21 import android.compat.annotation.UnsupportedAppUsage;
22 import android.os.Parcel;
23 import android.text.ParcelableSpan;
24 import android.text.TextUtils;
25 import android.widget.TextView;
26 
27 /**
28  * Provides an easy way to edit a portion of text.
29  * <p>
30  * The {@link TextView} uses this span to allow the user to delete a chuck of text in one click.
31  * <p>
32  * {@link TextView} removes the span when the user deletes the whole text or modifies it.
33  * <p>
34  * This span can be also used to receive notification when the user deletes or modifies the text;
35  */
36 public class EasyEditSpan implements ParcelableSpan {
37 
38     /**
39      * The extra key field in the pending intent that describes how the text changed.
40      *
41      * @see #TEXT_DELETED
42      * @see #TEXT_MODIFIED
43      */
44     public static final String EXTRA_TEXT_CHANGED_TYPE =
45             "android.text.style.EXTRA_TEXT_CHANGED_TYPE";
46 
47     /**
48      * The value of {@link #EXTRA_TEXT_CHANGED_TYPE} when the text wrapped by this span is deleted.
49      */
50     public static final int TEXT_DELETED = 1;
51 
52     /**
53      * The value of {@link #EXTRA_TEXT_CHANGED_TYPE} when the text wrapped by this span is modified.
54      */
55     public static final int TEXT_MODIFIED = 2;
56 
57     private final PendingIntent mPendingIntent;
58 
59     private boolean mDeleteEnabled;
60 
61     /**
62      * Creates the span. No intent is sent when the wrapped text is modified or
63      * deleted.
64      */
EasyEditSpan()65     public EasyEditSpan() {
66         mPendingIntent = null;
67         mDeleteEnabled = true;
68     }
69 
70     /**
71      * @param pendingIntent The intent will be sent when the wrapped text is deleted or modified.
72      *                      When the pending intent is sent, {@link #EXTRA_TEXT_CHANGED_TYPE} is
73      *                      added in the intent to describe how the text changed.
74      */
EasyEditSpan(PendingIntent pendingIntent)75     public EasyEditSpan(PendingIntent pendingIntent) {
76         mPendingIntent = pendingIntent;
77         mDeleteEnabled = true;
78     }
79 
80     /**
81      * Constructor called from {@link TextUtils} to restore the span.
82      */
EasyEditSpan(@onNull Parcel source)83     public EasyEditSpan(@NonNull Parcel source) {
84         mPendingIntent = source.readParcelable(null);
85         mDeleteEnabled = (source.readByte() == 1);
86     }
87 
88     @Override
describeContents()89     public int describeContents() {
90         return 0;
91     }
92 
93     @Override
writeToParcel(@onNull Parcel dest, int flags)94     public void writeToParcel(@NonNull Parcel dest, int flags) {
95         writeToParcelInternal(dest, flags);
96     }
97 
98     /** @hide */
writeToParcelInternal(@onNull Parcel dest, int flags)99     public void writeToParcelInternal(@NonNull Parcel dest, int flags) {
100         dest.writeParcelable(mPendingIntent, 0);
101         dest.writeByte((byte) (mDeleteEnabled ? 1 : 0));
102     }
103 
104     @Override
getSpanTypeId()105     public int getSpanTypeId() {
106         return getSpanTypeIdInternal();
107     }
108 
109     /** @hide */
getSpanTypeIdInternal()110     public int getSpanTypeIdInternal() {
111         return TextUtils.EASY_EDIT_SPAN;
112     }
113 
114     /**
115      * @return True if the {@link TextView} should offer the ability to delete the text.
116      *
117      * @hide
118      */
119     @UnsupportedAppUsage
isDeleteEnabled()120     public boolean isDeleteEnabled() {
121         return mDeleteEnabled;
122     }
123 
124     /**
125      * Enables or disables the deletion of the text.
126      *
127      * @hide
128      */
129     @UnsupportedAppUsage
setDeleteEnabled(boolean value)130     public void setDeleteEnabled(boolean value) {
131         mDeleteEnabled = value;
132     }
133 
134     /**
135      * @return the pending intent to send when the wrapped text is deleted or modified.
136      *
137      * @hide
138      */
139     @UnsupportedAppUsage
getPendingIntent()140     public PendingIntent getPendingIntent() {
141         return mPendingIntent;
142     }
143 }
144