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