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.os.Parcel; 22 import android.text.ParcelableSpan; 23 import android.text.TextUtils; 24 import android.widget.TextView; 25 26 /** 27 * Provides an easy way to edit a portion of text. 28 * <p> 29 * The {@link TextView} uses this span to allow the user to delete a chuck of text in one click. 30 * <p> 31 * {@link TextView} removes the span when the user deletes the whole text or modifies it. 32 * <p> 33 * This span can be also used to receive notification when the user deletes or modifies the text; 34 */ 35 public class EasyEditSpan implements ParcelableSpan { 36 37 /** 38 * The extra key field in the pending intent that describes how the text changed. 39 * 40 * @see #TEXT_DELETED 41 * @see #TEXT_MODIFIED 42 * @see #getPendingIntent() 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 */ isDeleteEnabled()119 public boolean isDeleteEnabled() { 120 return mDeleteEnabled; 121 } 122 123 /** 124 * Enables or disables the deletion of the text. 125 * 126 * @hide 127 */ setDeleteEnabled(boolean value)128 public void setDeleteEnabled(boolean value) { 129 mDeleteEnabled = value; 130 } 131 132 /** 133 * @return the pending intent to send when the wrapped text is deleted or modified. 134 * 135 * @hide 136 */ getPendingIntent()137 public PendingIntent getPendingIntent() { 138 return mPendingIntent; 139 } 140 } 141