Tony Mantler | b859235 | 2017-02-17 14:04:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 com.android.settingslib.inputmethod; |
| 18 | |
| 19 | import android.content.Context; |
Aurimas Liutikas | a14377a | 2018-04-17 09:50:46 -0700 | [diff] [blame] | 20 | import androidx.preference.Preference; |
Tony Mantler | b859235 | 2017-02-17 14:04:00 -0800 | [diff] [blame] | 21 | import android.text.TextUtils; |
| 22 | import android.view.inputmethod.InputMethodInfo; |
| 23 | import android.view.inputmethod.InputMethodSubtype; |
| 24 | |
Tadashi G. Takaoka | 6504675 | 2017-08-01 13:02:59 +0900 | [diff] [blame] | 25 | import com.android.internal.annotations.VisibleForTesting; |
Tony Mantler | b859235 | 2017-02-17 14:04:00 -0800 | [diff] [blame] | 26 | import com.android.internal.inputmethod.InputMethodUtils; |
| 27 | |
| 28 | import java.text.Collator; |
| 29 | import java.util.Locale; |
| 30 | |
| 31 | /** |
| 32 | * Input method subtype preference. |
| 33 | * |
| 34 | * This preference represents a subtype of an IME. It is used to enable or disable the subtype. |
| 35 | */ |
| 36 | public class InputMethodSubtypePreference extends SwitchWithNoTextPreference { |
| 37 | private final boolean mIsSystemLocale; |
| 38 | private final boolean mIsSystemLanguage; |
| 39 | |
| 40 | public InputMethodSubtypePreference(final Context context, final InputMethodSubtype subtype, |
| 41 | final InputMethodInfo imi) { |
Tadashi G. Takaoka | 6504675 | 2017-08-01 13:02:59 +0900 | [diff] [blame] | 42 | this(context, |
| 43 | imi.getId() + subtype.hashCode(), |
| 44 | InputMethodAndSubtypeUtil.getSubtypeLocaleNameAsSentence(subtype, context, imi), |
| 45 | subtype.getLocale(), |
| 46 | context.getResources().getConfiguration().locale); |
| 47 | } |
| 48 | |
| 49 | @VisibleForTesting |
| 50 | InputMethodSubtypePreference( |
| 51 | final Context context, |
| 52 | final String prefKey, |
| 53 | final CharSequence title, |
| 54 | final String subtypeLocaleString, |
| 55 | final Locale systemLocale) { |
Tony Mantler | b859235 | 2017-02-17 14:04:00 -0800 | [diff] [blame] | 56 | super(context); |
| 57 | setPersistent(false); |
Tadashi G. Takaoka | 6504675 | 2017-08-01 13:02:59 +0900 | [diff] [blame] | 58 | setKey(prefKey); |
| 59 | setTitle(title); |
Tony Mantler | b859235 | 2017-02-17 14:04:00 -0800 | [diff] [blame] | 60 | if (TextUtils.isEmpty(subtypeLocaleString)) { |
| 61 | mIsSystemLocale = false; |
| 62 | mIsSystemLanguage = false; |
| 63 | } else { |
Tony Mantler | b859235 | 2017-02-17 14:04:00 -0800 | [diff] [blame] | 64 | mIsSystemLocale = subtypeLocaleString.equals(systemLocale.toString()); |
| 65 | mIsSystemLanguage = mIsSystemLocale |
| 66 | || InputMethodUtils.getLanguageFromLocaleString(subtypeLocaleString) |
| 67 | .equals(systemLocale.getLanguage()); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | public int compareTo(final Preference rhs, final Collator collator) { |
| 72 | if (this == rhs) { |
| 73 | return 0; |
| 74 | } |
| 75 | if (rhs instanceof InputMethodSubtypePreference) { |
| 76 | final InputMethodSubtypePreference rhsPref = (InputMethodSubtypePreference) rhs; |
| 77 | if (mIsSystemLocale && !rhsPref.mIsSystemLocale) { |
| 78 | return -1; |
| 79 | } |
| 80 | if (!mIsSystemLocale && rhsPref.mIsSystemLocale) { |
| 81 | return 1; |
| 82 | } |
| 83 | if (mIsSystemLanguage && !rhsPref.mIsSystemLanguage) { |
| 84 | return -1; |
| 85 | } |
| 86 | if (!mIsSystemLanguage && rhsPref.mIsSystemLanguage) { |
| 87 | return 1; |
| 88 | } |
Tadashi G. Takaoka | 6504675 | 2017-08-01 13:02:59 +0900 | [diff] [blame] | 89 | final CharSequence title = getTitle(); |
| 90 | final CharSequence rhsTitle = rhs.getTitle(); |
| 91 | final boolean emptyTitle = TextUtils.isEmpty(title); |
| 92 | final boolean rhsEmptyTitle = TextUtils.isEmpty(rhsTitle); |
| 93 | if (!emptyTitle && !rhsEmptyTitle) { |
| 94 | return collator.compare(title.toString(), rhsTitle.toString()); |
Tony Mantler | b859235 | 2017-02-17 14:04:00 -0800 | [diff] [blame] | 95 | } |
Tadashi G. Takaoka | 6504675 | 2017-08-01 13:02:59 +0900 | [diff] [blame] | 96 | // For historical reasons, an empty text needs to be put at the first. |
| 97 | return (emptyTitle ? -1 : 0) - (rhsEmptyTitle ? -1 : 0); |
Tony Mantler | b859235 | 2017-02-17 14:04:00 -0800 | [diff] [blame] | 98 | } |
| 99 | return super.compareTo(rhs); |
| 100 | } |
| 101 | } |