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