blob: 5fdab2967d1e9591b651d006220e5e271211045e [file] [log] [blame]
Tony Mantlerb8592352017-02-17 14:04:00 -08001/*
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
17package com.android.settingslib.inputmethod;
18
19import android.content.Context;
20import android.support.v14.preference.SwitchPreference;
21import android.support.v7.preference.Preference;
22import android.text.TextUtils;
23import android.view.inputmethod.InputMethodInfo;
24import android.view.inputmethod.InputMethodSubtype;
25
26import com.android.internal.inputmethod.InputMethodUtils;
27
28import java.text.Collator;
29import 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 */
36public 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) {
42 super(context);
43 setPersistent(false);
44 setKey(imi.getId() + subtype.hashCode());
45 final CharSequence subtypeLabel =
46 InputMethodAndSubtypeUtil.getSubtypeLocaleNameAsSentence(subtype, context, imi);
47 setTitle(subtypeLabel);
48 final String subtypeLocaleString = subtype.getLocale();
49 if (TextUtils.isEmpty(subtypeLocaleString)) {
50 mIsSystemLocale = false;
51 mIsSystemLanguage = false;
52 } else {
53 final Locale systemLocale = context.getResources().getConfiguration().locale;
54 mIsSystemLocale = subtypeLocaleString.equals(systemLocale.toString());
55 mIsSystemLanguage = mIsSystemLocale
56 || InputMethodUtils.getLanguageFromLocaleString(subtypeLocaleString)
57 .equals(systemLocale.getLanguage());
58 }
59 }
60
61 public int compareTo(final Preference rhs, final Collator collator) {
62 if (this == rhs) {
63 return 0;
64 }
65 if (rhs instanceof InputMethodSubtypePreference) {
66 final InputMethodSubtypePreference rhsPref = (InputMethodSubtypePreference) rhs;
67 if (mIsSystemLocale && !rhsPref.mIsSystemLocale) {
68 return -1;
69 }
70 if (!mIsSystemLocale && rhsPref.mIsSystemLocale) {
71 return 1;
72 }
73 if (mIsSystemLanguage && !rhsPref.mIsSystemLanguage) {
74 return -1;
75 }
76 if (!mIsSystemLanguage && rhsPref.mIsSystemLanguage) {
77 return 1;
78 }
79 final CharSequence t0 = getTitle();
80 final CharSequence t1 = rhs.getTitle();
81 if (t0 == null && t1 == null) {
82 return Integer.compare(hashCode(), rhs.hashCode());
83 }
84 if (t0 != null && t1 != null) {
85 return collator.compare(t0.toString(), t1.toString());
86 }
87 return t0 == null ? -1 : 1;
88 }
89 return super.compareTo(rhs);
90 }
91}