Home | History | Annotate | Download | only in internal
      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.keyboard.internal;
     18 
     19 import android.view.inputmethod.InputMethodSubtype;
     20 
     21 import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
     22 
     23 import java.util.Collections;
     24 import java.util.List;
     25 
     26 /**
     27  * This class determines that the language name on the spacebar should be displayed in what format.
     28  */
     29 public final class LanguageOnSpacebarHelper {
     30     public static final int FORMAT_TYPE_NONE = 0;
     31     public static final int FORMAT_TYPE_LANGUAGE_ONLY = 1;
     32     public static final int FORMAT_TYPE_FULL_LOCALE = 2;
     33 
     34     private List<InputMethodSubtype> mEnabledSubtypes = Collections.emptyList();
     35     private boolean mIsSystemLanguageSameAsInputLanguage;
     36 
     37     public int getLanguageOnSpacebarFormatType(final InputMethodSubtype subtype) {
     38         if (SubtypeLocaleUtils.isNoLanguage(subtype)) {
     39             return FORMAT_TYPE_FULL_LOCALE;
     40         }
     41         // Only this subtype is enabled and equals to the system locale.
     42         if (mEnabledSubtypes.size() < 2 && mIsSystemLanguageSameAsInputLanguage) {
     43             return FORMAT_TYPE_NONE;
     44         }
     45         final String keyboardLanguage = SubtypeLocaleUtils.getSubtypeLocale(subtype).getLanguage();
     46         final String keyboardLayout = SubtypeLocaleUtils.getKeyboardLayoutSetName(subtype);
     47         int sameLanguageAndLayoutCount = 0;
     48         for (final InputMethodSubtype ims : mEnabledSubtypes) {
     49             final String language = SubtypeLocaleUtils.getSubtypeLocale(ims).getLanguage();
     50             if (keyboardLanguage.equals(language) && keyboardLayout.equals(
     51                     SubtypeLocaleUtils.getKeyboardLayoutSetName(ims))) {
     52                 sameLanguageAndLayoutCount++;
     53             }
     54         }
     55         // Display full locale name only when there are multiple subtypes that have the same
     56         // locale and keyboard layout. Otherwise displaying language name is enough.
     57         return sameLanguageAndLayoutCount > 1 ? FORMAT_TYPE_FULL_LOCALE
     58                 : FORMAT_TYPE_LANGUAGE_ONLY;
     59     }
     60 
     61     public void updateEnabledSubtypes(final List<InputMethodSubtype> enabledSubtypes) {
     62         mEnabledSubtypes = enabledSubtypes;
     63     }
     64 
     65     public void updateIsSystemLanguageSameAsInputLanguage(final boolean isSame) {
     66         mIsSystemLanguageSameAsInputLanguage = isSame;
     67     }
     68 }
     69