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
17package android.text.style;
18
19import android.app.PendingIntent;
20import android.os.Parcel;
21import android.text.ParcelableSpan;
22import android.text.TextUtils;
23import 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 */
34public 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        writeToParcelInternal(dest, flags);
95    }
96
97    /** @hide */
98    public void writeToParcelInternal(Parcel dest, int flags) {
99        dest.writeParcelable(mPendingIntent, 0);
100        dest.writeByte((byte) (mDeleteEnabled ? 1 : 0));
101    }
102
103    @Override
104    public int getSpanTypeId() {
105        return getSpanTypeIdInternal();
106    }
107
108    /** @hide */
109    public int getSpanTypeIdInternal() {
110        return TextUtils.EASY_EDIT_SPAN;
111    }
112
113    /**
114     * @return True if the {@link TextView} should offer the ability to delete the text.
115     *
116     * @hide
117     */
118    public boolean isDeleteEnabled() {
119        return mDeleteEnabled;
120    }
121
122    /**
123     * Enables or disables the deletion of the text.
124     *
125     * @hide
126     */
127    public void setDeleteEnabled(boolean value) {
128        mDeleteEnabled = value;
129    }
130
131    /**
132     * @return the pending intent to send when the wrapped text is deleted or modified.
133     *
134     * @hide
135     */
136    public PendingIntent getPendingIntent() {
137        return mPendingIntent;
138    }
139}
140