Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit adb43583 authored by satok's avatar satok
Browse files

Add CorrectionSpan and APIs to pass a secure CorrectionSpan to TextView

- CorrectionSpan is a span which has suggestions made by IME.
This has a function to change the current IME to other IME specified
in this span. For security reasons, only the current IME
is allowed to use this function through InputConnection.
(IME token is used for checking the validity of it.).

- CorrectionSpan stores following information:

flags, subtype Id, InputMethodInfo Id, suggests, locale, original string

Change-Id: Id3abc9ea4d11753cdc4f483a2bb3128f49ba198a
parent fd865d0f
Loading
Loading
Loading
Loading
+264 −0
Original line number Diff line number Diff line
@@ -201843,6 +201843,186 @@
</parameter>
</method>
</class>
<class name="CorrectionSpan"
 extends="java.lang.Object"
 abstract="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<implements name="android.text.ParcelableSpan">
</implements>
<constructor name="CorrectionSpan"
 type="android.text.style.CorrectionSpan"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="context" type="android.content.Context">
</parameter>
<parameter name="suggests" type="java.util.List&lt;java.lang.CharSequence&gt;">
</parameter>
<parameter name="flags" type="int">
</parameter>
</constructor>
<constructor name="CorrectionSpan"
 type="android.text.style.CorrectionSpan"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="locale" type="java.util.Locale">
</parameter>
<parameter name="suggests" type="java.util.List&lt;java.lang.CharSequence&gt;">
</parameter>
<parameter name="flags" type="int">
</parameter>
</constructor>
<constructor name="CorrectionSpan"
 type="android.text.style.CorrectionSpan"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="context" type="android.content.Context">
</parameter>
<parameter name="locale" type="java.util.Locale">
</parameter>
<parameter name="suggests" type="java.util.List&lt;java.lang.CharSequence&gt;">
</parameter>
<parameter name="flags" type="int">
</parameter>
<parameter name="originalString" type="java.lang.String">
</parameter>
</constructor>
<constructor name="CorrectionSpan"
 type="android.text.style.CorrectionSpan"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="src" type="android.os.Parcel">
</parameter>
</constructor>
<method name="describeContents"
 return="int"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
</method>
<method name="getFlags"
 return="int"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
</method>
<method name="getLocale"
 return="java.lang.String"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
</method>
<method name="getOriginalString"
 return="java.lang.String"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
</method>
<method name="getSpanTypeId"
 return="int"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
</method>
<method name="getSuggests"
 return="java.util.List&lt;java.lang.CharSequence&gt;"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
</method>
<method name="writeToParcel"
 return="void"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="dest" type="android.os.Parcel">
</parameter>
<parameter name="flags" type="int">
</parameter>
</method>
<field name="CREATOR"
 type="android.os.Parcelable.Creator"
 transient="false"
 volatile="false"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
<field name="FLAG_DEFAULT"
 type="int"
 transient="false"
 volatile="false"
 value="0"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
<field name="FLAG_VERBATIM"
 type="int"
 transient="false"
 volatile="false"
 value="1"
 static="true"
 final="true"
 deprecated="not deprecated"
 visibility="public"
>
</field>
</class>
<class name="DrawableMarginSpan"
 extends="java.lang.Object"
 abstract="false"
@@ -236544,6 +236724,27 @@
<parameter name="newCursorPosition" type="int">
</parameter>
</method>
<method name="setCorrectionSpan"
 return="boolean"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="token" type="android.os.IBinder">
</parameter>
<parameter name="correctionSpan" type="android.text.style.CorrectionSpan">
</parameter>
<parameter name="start" type="int">
</parameter>
<parameter name="end" type="int">
</parameter>
<parameter name="flags" type="int">
</parameter>
</method>
<method name="setSelection"
 return="boolean"
 abstract="false"
@@ -237814,6 +238015,27 @@
<parameter name="newCursorPosition" type="int">
</parameter>
</method>
<method name="setCorrectionSpan"
 return="boolean"
 abstract="true"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="token" type="android.os.IBinder">
</parameter>
<parameter name="correctionSpan" type="android.text.style.CorrectionSpan">
</parameter>
<parameter name="start" type="int">
</parameter>
<parameter name="end" type="int">
</parameter>
<parameter name="flags" type="int">
</parameter>
</method>
<method name="setSelection"
 return="boolean"
 abstract="true"
@@ -238144,6 +238366,27 @@
<parameter name="newCursorPosition" type="int">
</parameter>
</method>
<method name="setCorrectionSpan"
 return="boolean"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="token" type="android.os.IBinder">
</parameter>
<parameter name="correctionSpan" type="android.text.style.CorrectionSpan">
</parameter>
<parameter name="start" type="int">
</parameter>
<parameter name="end" type="int">
</parameter>
<parameter name="flags" type="int">
</parameter>
</method>
<method name="setSelection"
 return="boolean"
 abstract="false"
@@ -264470,6 +264713,27 @@
<parameter name="bottom" type="android.graphics.drawable.Drawable">
</parameter>
</method>
<method name="setCorrectionSpan"
 return="boolean"
 abstract="false"
 native="false"
 synchronized="false"
 static="false"
 final="false"
 deprecated="not deprecated"
 visibility="public"
>
<parameter name="token" type="android.os.IBinder">
</parameter>
<parameter name="span" type="android.text.style.CorrectionSpan">
</parameter>
<parameter name="start" type="int">
</parameter>
<parameter name="end" type="int">
</parameter>
<parameter name="flags" type="int">
</parameter>
</method>
<method name="setCursorVisible"
 return="void"
 abstract="false"
+2 −0
Original line number Diff line number Diff line
@@ -565,6 +565,8 @@ public class TextUtils {
    public static final int TEXT_APPEARANCE_SPAN = 17;
    /** @hide */
    public static final int ANNOTATION = 18;
    /** @hide */
    public static final int CORRECTION_SPAN = 19;

    /**
     * Flatten a CharSequence and whatever styles can be copied across processes
+19 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.text.style;

parcelable CorrectionSpan;
+161 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.text.style;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.ParcelableSpan;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class CorrectionSpan implements ParcelableSpan {

    /**
     * Flag for the default value.
     */
    public static final int FLAG_DEFAULT = 0x0000;
    /**
     * Flag for indicating that the input is verbatim. TextView refers to this flag to determine
     * how it displays a word with CorrectionSpan.
     */
    public static final int FLAG_VERBATIM = 0x0001;

    private static final int SUGGESTS_MAX_SIZE = 5;

    /*
     * TODO: Needs to check the validity and add a feature that TextView will change
     * the current IME to the other IME which is specified in CorrectionSpan.
     * An IME needs to set the span by specifying the target IME and Subtype of CorrectionSpan.
     * And the current IME might want to specify any IME as the target IME including other IMEs.
     */

    private final int mFlags;
    private final List<CharSequence> mSuggests = new ArrayList<CharSequence>();
    private final String mLocaleString;
    private final String mOriginalString;
    /*
     * TODO: If switching IME is required, needs to add parameters for ids of InputMethodInfo
     * and InputMethodSubtype.
     */

    /**
     * @param context Context for the application
     * @param suggests Suggests for the string under the span
     * @param flags Additional flags indicating how this span is handled in TextView
     */
    public CorrectionSpan(Context context, List<CharSequence> suggests, int flags) {
        this(context, null, suggests, flags, null);
    }

    /**
     * @param locale Locale of the suggestions
     * @param suggests Suggests for the string under the span
     * @param flags Additional flags indicating how this span is handled in TextView
     */
    public CorrectionSpan(Locale locale, List<CharSequence> suggests, int flags) {
        this(null, locale, suggests, flags, null);
    }

    /**
     * @param context Context for the application
     * @param locale locale Locale of the suggestions
     * @param suggests suggests Suggests for the string under the span
     * @param flags Additional flags indicating how this span is handled in TextView
     * @param originalString originalString for suggests
     */
    public CorrectionSpan(Context context, Locale locale, List<CharSequence> suggests, int flags,
            String originalString) {
        final int N = Math.min(SUGGESTS_MAX_SIZE, suggests.size());
        for (int i = 0; i < N; ++i) {
            mSuggests.add(suggests.get(i));
        }
        mFlags = flags;
        if (context != null && locale == null) {
            mLocaleString = context.getResources().getConfiguration().locale.toString();
        } else {
            mLocaleString = locale.toString();
        }
        mOriginalString = originalString;
    }

    public CorrectionSpan(Parcel src) {
        src.readList(mSuggests, null);
        mFlags = src.readInt();
        mLocaleString = src.readString();
        mOriginalString = src.readString();
    }

    /**
     * @return suggestions
     */
    public List<CharSequence> getSuggests() {
        return new ArrayList<CharSequence>(mSuggests);
    }

    /**
     * @return locale of suggestions
     */
    public String getLocale() {
        return mLocaleString;
    }

    /**
     * @return original string of suggestions
     */
    public String getOriginalString() {
        return mOriginalString;
    }

    public int getFlags() {
        return mFlags;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeList(mSuggests);
        dest.writeInt(mFlags);
        dest.writeString(mLocaleString);
        dest.writeString(mOriginalString);
    }

    @Override
    public int getSpanTypeId() {
        return TextUtils.CORRECTION_SPAN;
    }

    public static final Parcelable.Creator<CorrectionSpan> CREATOR =
            new Parcelable.Creator<CorrectionSpan>() {
        @Override
        public CorrectionSpan createFromParcel(Parcel source) {
            return new CorrectionSpan(source);
        }

        @Override
        public CorrectionSpan[] newArray(int size) {
            return new CorrectionSpan[size];
        }
    };
}
+11 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.SystemClock;
import android.text.Editable;
import android.text.NoCopySpan;
@@ -29,6 +30,7 @@ import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.MetaKeyKeyListener;
import android.text.style.CorrectionSpan;
import android.util.Log;
import android.util.LogPrinter;
import android.view.KeyCharacterMap;
@@ -189,6 +191,15 @@ public class BaseInputConnection implements InputConnection {
        return true;
    }

    /**
     * Default implementation does nothing and returns false.
     */
    @Override
    public boolean setCorrectionSpan(IBinder token, CorrectionSpan correctionSpan, int start,
            int end, int flags) {
        return false;
    }

    /**
     * The default implementation performs the deletion around the current
     * selection position of the editable text.
Loading