Home | History | Annotate | Download | only in inputmethod
      1 /*
      2  * Copyright (C) 2014 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.settings.inputmethod;
     18 
     19 import android.content.Context;
     20 import android.preference.Preference;
     21 import android.text.TextUtils;
     22 import android.view.inputmethod.InputMethodInfo;
     23 import android.view.inputmethod.InputMethodSubtype;
     24 
     25 import com.android.internal.inputmethod.InputMethodUtils;
     26 
     27 import java.text.Collator;
     28 import java.util.Locale;
     29 
     30 /**
     31  * Input method subtype preference.
     32  *
     33  * This preference represents a subtype of an IME. It is used to enable or disable the subtype.
     34  */
     35 class InputMethodSubtypePreference extends SwitchWithNoTextPreference {
     36     private final boolean mIsSystemLocale;
     37     private final boolean mIsSystemLanguage;
     38 
     39     InputMethodSubtypePreference(final Context context, final InputMethodSubtype subtype,
     40             final InputMethodInfo imi) {
     41         super(context);
     42         setPersistent(false);
     43         setKey(imi.getId() + subtype.hashCode());
     44         final CharSequence subtypeLabel = subtype.getDisplayName(context,
     45                 imi.getPackageName(), imi.getServiceInfo().applicationInfo);
     46         setTitle(subtypeLabel);
     47         final String subtypeLocaleString = subtype.getLocale();
     48         if (TextUtils.isEmpty(subtypeLocaleString)) {
     49             mIsSystemLocale = false;
     50             mIsSystemLanguage = false;
     51         } else {
     52             final Locale systemLocale = context.getResources().getConfiguration().locale;
     53             mIsSystemLocale = subtypeLocaleString.equals(systemLocale.toString());
     54             mIsSystemLanguage = mIsSystemLocale
     55                     || InputMethodUtils.getLanguageFromLocaleString(subtypeLocaleString)
     56                             .equals(systemLocale.getLanguage());
     57         }
     58     }
     59 
     60     int compareTo(final Preference rhs, final Collator collator) {
     61         if (this == rhs) {
     62             return 0;
     63         }
     64         if (rhs instanceof InputMethodSubtypePreference) {
     65             final InputMethodSubtypePreference pref = (InputMethodSubtypePreference) rhs;
     66             final CharSequence t0 = getTitle();
     67             final CharSequence t1 = rhs.getTitle();
     68             if (TextUtils.equals(t0, t1)) {
     69                 return 0;
     70             }
     71             if (mIsSystemLocale) {
     72                 return -1;
     73             }
     74             if (pref.mIsSystemLocale) {
     75                 return 1;
     76             }
     77             if (mIsSystemLanguage) {
     78                 return -1;
     79             }
     80             if (pref.mIsSystemLanguage) {
     81                 return 1;
     82             }
     83             if (TextUtils.isEmpty(t0)) {
     84                 return 1;
     85             }
     86             if (TextUtils.isEmpty(t1)) {
     87                 return -1;
     88             }
     89             return collator.compare(t0.toString(), t1.toString());
     90         }
     91         return super.compareTo(rhs);
     92     }
     93 }
     94