1e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin/*
2e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin * Copyright (C) 2011 The Android Open Source Project
3e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin *
4e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin * Licensed under the Apache License, Version 2.0 (the "License");
5e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin * you may not use this file except in compliance with the License.
6e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin * You may obtain a copy of the License at
7e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin *
8e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin *      http://www.apache.org/licenses/LICENSE-2.0
9e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin *
10e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin * Unless required by applicable law or agreed to in writing, software
11e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin * distributed under the License is distributed on an "AS IS" BASIS,
12e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin * See the License for the specific language governing permissions and
14e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin * limitations under the License.
15e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin */
16e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin
17e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolinpackage android.text.style;
18e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin
191b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolinimport android.app.PendingIntent;
20e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolinimport android.os.Parcel;
21e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolinimport android.text.ParcelableSpan;
22e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolinimport android.text.TextUtils;
23e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolinimport android.widget.TextView;
24e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin
25e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin/**
26e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin * Provides an easy way to edit a portion of text.
27e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin * <p>
28e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin * The {@link TextView} uses this span to allow the user to delete a chuck of text in one click.
291b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin * <p>
301b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin * {@link TextView} removes the span when the user deletes the whole text or modifies it.
311b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin * <p>
321b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin * This span can be also used to receive notification when the user deletes or modifies the text;
33e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin */
34e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolinpublic class EasyEditSpan implements ParcelableSpan {
35e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin
361b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin    /**
371b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     * The extra key field in the pending intent that describes how the text changed.
381b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     *
391b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     * @see #TEXT_DELETED
401b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     * @see #TEXT_MODIFIED
411b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     * @see #getPendingIntent()
421b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     */
431b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin    public static final String EXTRA_TEXT_CHANGED_TYPE =
441b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin            "android.text.style.EXTRA_TEXT_CHANGED_TYPE";
451b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin
461b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin    /**
471b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     * The value of {@link #EXTRA_TEXT_CHANGED_TYPE} when the text wrapped by this span is deleted.
481b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     */
491b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin    public static final int TEXT_DELETED = 1;
501b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin
511b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin    /**
521b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     * The value of {@link #EXTRA_TEXT_CHANGED_TYPE} when the text wrapped by this span is modified.
531b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     */
541b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin    public static final int TEXT_MODIFIED = 2;
551b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin
561b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin    private final PendingIntent mPendingIntent;
571b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin
581b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin    private boolean mDeleteEnabled;
591b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin
601b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin    /**
611b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     * Creates the span. No intent is sent when the wrapped text is modified or
621b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     * deleted.
631b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     */
64e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin    public EasyEditSpan() {
651b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin        mPendingIntent = null;
661b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin        mDeleteEnabled = true;
671b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin    }
681b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin
691b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin    /**
701b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     * @param pendingIntent The intent will be sent when the wrapped text is deleted or modified.
711b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     *                      When the pending intent is sent, {@link #EXTRA_TEXT_CHANGED_TYPE} is
721b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     *                      added in the intent to describe how the text changed.
731b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     */
741b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin    public EasyEditSpan(PendingIntent pendingIntent) {
751b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin        mPendingIntent = pendingIntent;
761b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin        mDeleteEnabled = true;
771b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin    }
781b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin
791b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin    /**
801b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     * Constructor called from {@link TextUtils} to restore the span.
811b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     */
821b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin    public EasyEditSpan(Parcel source) {
831b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin        mPendingIntent = source.readParcelable(null);
841b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin        mDeleteEnabled = (source.readByte() == 1);
85e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin    }
86e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin
87e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin    @Override
88e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin    public int describeContents() {
89e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin        return 0;
90e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin    }
91e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin
92e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin    @Override
93e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin    public void writeToParcel(Parcel dest, int flags) {
941b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin        dest.writeParcelable(mPendingIntent, 0);
951b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin        dest.writeByte((byte) (mDeleteEnabled ? 1 : 0));
96e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin    }
97e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin
98e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin    @Override
99e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin    public int getSpanTypeId() {
100e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin        return TextUtils.EASY_EDIT_SPAN;
101e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin    }
1021b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin
1031b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin    /**
1041b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     * @return True if the {@link TextView} should offer the ability to delete the text.
1051b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     *
1061b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     * @hide
1071b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     */
1081b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin    public boolean isDeleteEnabled() {
1091b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin        return mDeleteEnabled;
1101b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin    }
1111b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin
1121b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin    /**
1131b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     * Enables or disables the deletion of the text.
1141b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     *
1151b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     * @hide
1161b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     */
1171b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin    public void setDeleteEnabled(boolean value) {
1181b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin        mDeleteEnabled = value;
1191b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin    }
1201b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin
1211b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin    /**
1221b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     * @return the pending intent to send when the wrapped text is deleted or modified.
1231b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     *
1241b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     * @hide
1251b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin     */
1261b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin    public PendingIntent getPendingIntent() {
1271b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin        return mPendingIntent;
1281b15ba5d194c1db71d0a34ee110bd1ab169c8a29Luca Zanolin    }
129e6d368218918f911b1954296dab25bf84147b4c6Luca Zanolin}
130