1733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa/*
2733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa * Copyright (C) 2014 The Android Open Source Project
3733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa *
4733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa * Licensed under the Apache License, Version 2.0 (the "License");
5733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa * you may not use this file except in compliance with the License.
6733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa * You may obtain a copy of the License at
7733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa *
8733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa *      http://www.apache.org/licenses/LICENSE-2.0
9733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa *
10733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa * Unless required by applicable law or agreed to in writing, software
11733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa * distributed under the License is distributed on an "AS IS" BASIS,
12733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa * See the License for the specific language governing permissions and
14733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa * limitations under the License.
15733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa */
16733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa
17733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawapackage com.android.inputmethod.compat;
18733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa
19733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawaimport android.view.textservice.TextInfo;
20733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa
21733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawaimport com.android.inputmethod.annotations.UsedForTesting;
22733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa
23733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawaimport java.lang.reflect.Constructor;
24733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawaimport java.lang.reflect.Method;
25733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa
26733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa@UsedForTesting
27733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawapublic final class TextInfoCompatUtils {
28733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    // Note that TextInfo.getCharSequence() is supposed to be available in API level 21 and later.
29733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    private static final Method TEXT_INFO_GET_CHAR_SEQUENCE =
30733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa            CompatUtils.getMethod(TextInfo.class, "getCharSequence");
31733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    private static final Constructor<?> TEXT_INFO_CONSTRUCTOR_FOR_CHAR_SEQUENCE =
32733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa            CompatUtils.getConstructor(TextInfo.class, CharSequence.class, int.class, int.class,
33733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa                    int.class, int.class);
34733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa
35733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    @UsedForTesting
36733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    public static boolean isCharSequenceSupported() {
37733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa        return TEXT_INFO_GET_CHAR_SEQUENCE != null &&
38733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa                TEXT_INFO_CONSTRUCTOR_FOR_CHAR_SEQUENCE != null;
39733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    }
40733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa
41733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    @UsedForTesting
42733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    public static TextInfo newInstance(CharSequence charSequence, int start, int end, int cookie,
43733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa            int sequenceNumber) {
44733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa        if (TEXT_INFO_CONSTRUCTOR_FOR_CHAR_SEQUENCE != null) {
45733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa            return (TextInfo) CompatUtils.newInstance(TEXT_INFO_CONSTRUCTOR_FOR_CHAR_SEQUENCE,
46733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa                    charSequence, start, end, cookie, sequenceNumber);
47733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa        }
48733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa        return new TextInfo(charSequence.subSequence(start, end).toString(), cookie,
49733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa                sequenceNumber);
50733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    }
51733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa
52fc137f35c8d7b03676475fbdeabdf82f89782419Yohei Yukawa    /**
53fc137f35c8d7b03676475fbdeabdf82f89782419Yohei Yukawa     * Returns the result of {@link TextInfo#getCharSequence()} when available. Otherwise returns
54fc137f35c8d7b03676475fbdeabdf82f89782419Yohei Yukawa     * the result of {@link TextInfo#getText()} as fall back.
55fc137f35c8d7b03676475fbdeabdf82f89782419Yohei Yukawa     * @param textInfo the instance for which {@link TextInfo#getCharSequence()} or
56fc137f35c8d7b03676475fbdeabdf82f89782419Yohei Yukawa     * {@link TextInfo#getText()} is called.
57fc137f35c8d7b03676475fbdeabdf82f89782419Yohei Yukawa     * @return the result of {@link TextInfo#getCharSequence()} when available. Otherwise returns
58fc137f35c8d7b03676475fbdeabdf82f89782419Yohei Yukawa     * the result of {@link TextInfo#getText()} as fall back. If {@code textInfo} is {@code null},
59fc137f35c8d7b03676475fbdeabdf82f89782419Yohei Yukawa     * returns {@code null}.
60fc137f35c8d7b03676475fbdeabdf82f89782419Yohei Yukawa     */
61733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    @UsedForTesting
62fc137f35c8d7b03676475fbdeabdf82f89782419Yohei Yukawa    public static CharSequence getCharSequenceOrString(final TextInfo textInfo) {
63fc137f35c8d7b03676475fbdeabdf82f89782419Yohei Yukawa        final CharSequence defaultValue = (textInfo == null ? null : textInfo.getText());
64733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa        return (CharSequence) CompatUtils.invoke(textInfo, defaultValue,
65733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa                TEXT_INFO_GET_CHAR_SEQUENCE);
66733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    }
67733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa}
68