• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.inputmethod.latin.utils;
18 
19 import android.view.inputmethod.InputMethodSubtype;
20 
21 import com.android.inputmethod.latin.RichInputMethodSubtype;
22 
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.Locale;
26 
27 import javax.annotation.Nonnull;
28 
29 /**
30  * This class determines that the language name on the spacebar should be displayed in what format.
31  */
32 public final class LanguageOnSpacebarUtils {
33     public static final int FORMAT_TYPE_NONE = 0;
34     public static final int FORMAT_TYPE_LANGUAGE_ONLY = 1;
35     public static final int FORMAT_TYPE_FULL_LOCALE = 2;
36 
37     private static List<InputMethodSubtype> sEnabledSubtypes = Collections.emptyList();
38     private static boolean sIsSystemLanguageSameAsInputLanguage;
39 
LanguageOnSpacebarUtils()40     private LanguageOnSpacebarUtils() {
41         // This utility class is not publicly instantiable.
42     }
43 
getLanguageOnSpacebarFormatType( @onnull final RichInputMethodSubtype subtype)44     public static int getLanguageOnSpacebarFormatType(
45             @Nonnull final RichInputMethodSubtype subtype) {
46         if (subtype.isNoLanguage()) {
47             return FORMAT_TYPE_FULL_LOCALE;
48         }
49         // Only this subtype is enabled and equals to the system locale.
50         if (sEnabledSubtypes.size() < 2 && sIsSystemLanguageSameAsInputLanguage) {
51             return FORMAT_TYPE_NONE;
52         }
53         final Locale locale = subtype.getLocale();
54         if (locale == null) {
55             return FORMAT_TYPE_NONE;
56         }
57         final String keyboardLanguage = locale.getLanguage();
58         final String keyboardLayout = subtype.getKeyboardLayoutSetName();
59         int sameLanguageAndLayoutCount = 0;
60         for (final InputMethodSubtype ims : sEnabledSubtypes) {
61             final String language = SubtypeLocaleUtils.getSubtypeLocale(ims).getLanguage();
62             if (keyboardLanguage.equals(language) && keyboardLayout.equals(
63                     SubtypeLocaleUtils.getKeyboardLayoutSetName(ims))) {
64                 sameLanguageAndLayoutCount++;
65             }
66         }
67         // Display full locale name only when there are multiple subtypes that have the same
68         // locale and keyboard layout. Otherwise displaying language name is enough.
69         return sameLanguageAndLayoutCount > 1 ? FORMAT_TYPE_FULL_LOCALE
70                 : FORMAT_TYPE_LANGUAGE_ONLY;
71     }
72 
setEnabledSubtypes(@onnull final List<InputMethodSubtype> enabledSubtypes)73     public static void setEnabledSubtypes(@Nonnull final List<InputMethodSubtype> enabledSubtypes) {
74         sEnabledSubtypes = enabledSubtypes;
75     }
76 
onSubtypeChanged(@onnull final RichInputMethodSubtype subtype, final boolean implicitlyEnabledSubtype, @Nonnull final Locale systemLocale)77     public static void onSubtypeChanged(@Nonnull final RichInputMethodSubtype subtype,
78            final boolean implicitlyEnabledSubtype, @Nonnull final Locale systemLocale) {
79         final Locale newLocale = subtype.getLocale();
80         if (systemLocale.equals(newLocale)) {
81             sIsSystemLanguageSameAsInputLanguage = true;
82             return;
83         }
84         if (!systemLocale.getLanguage().equals(newLocale.getLanguage())) {
85             sIsSystemLanguageSameAsInputLanguage = false;
86             return;
87         }
88         // If the subtype is enabled explicitly, the language name should be displayed even when
89         // the keyboard language and the system language are equal.
90         sIsSystemLanguageSameAsInputLanguage = implicitlyEnabledSubtype;
91     }
92 }
93