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 17package com.android.settingslib.inputmethod; 18 19import android.content.Context; 20import android.support.v7.preference.Preference; 21import android.text.TextUtils; 22import android.view.inputmethod.InputMethodInfo; 23import android.view.inputmethod.InputMethodSubtype; 24 25import com.android.internal.annotations.VisibleForTesting; 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 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) { 56 super(context); 57 setPersistent(false); 58 setKey(prefKey); 59 setTitle(title); 60 if (TextUtils.isEmpty(subtypeLocaleString)) { 61 mIsSystemLocale = false; 62 mIsSystemLanguage = false; 63 } else { 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 } 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()); 95 } 96 // For historical reasons, an empty text needs to be put at the first. 97 return (emptyTitle ? -1 : 0) - (rhsEmptyTitle ? -1 : 0); 98 } 99 return super.compareTo(rhs); 100 } 101} 102