Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5660fad7 authored by Yohei Yukawa's avatar Yohei Yukawa
Browse files

Use LocaleList in KeyboardLayout.

As an exercise for a new API candidate LocaleList class, this CL does a
mechanical refactoring to replace Locale[] with LocaleList in
KeyboardLayout class.  Note that what changed in this CL is just an
implementation details that is never exposed to application developers.

One take-away from this exercise is that finding the best-match locale from
an ordered locale list is really a common pattern.  Perhaps we may want
to have a guideline for this kind of situation.

Change-Id: I142379afbaf24d524ff09cf6c7ee7720150f7489
parent e97a3cc4
Loading
Loading
Loading
Loading
+9 −21
Original line number Diff line number Diff line
@@ -16,8 +16,10 @@

package android.hardware.input;

import android.annotation.NonNull;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.LocaleList;

import java.util.Locale;

@@ -32,7 +34,8 @@ public final class KeyboardLayout implements Parcelable,
    private final String mLabel;
    private final String mCollection;
    private final int mPriority;
    private final Locale[] mLocales;
    @NonNull
    private final LocaleList mLocales;
    private final int mVendorId;
    private final int mProductId;

@@ -47,16 +50,12 @@ public final class KeyboardLayout implements Parcelable,
    };

    public KeyboardLayout(String descriptor, String label, String collection, int priority,
            Locale[] locales, int vid, int pid) {
            LocaleList locales, int vid, int pid) {
        mDescriptor = descriptor;
        mLabel = label;
        mCollection = collection;
        mPriority = priority;
        if (locales != null) {
        mLocales = locales;
        } else {
            mLocales = new Locale[0];
        }
        mVendorId = vid;
        mProductId = pid;
    }
@@ -66,11 +65,7 @@ public final class KeyboardLayout implements Parcelable,
        mLabel = source.readString();
        mCollection = source.readString();
        mPriority = source.readInt();
        int N = source.readInt();
        mLocales = new Locale[N];
        for (int i = 0; i < N; i++) {
            mLocales[i] = Locale.forLanguageTag(source.readString());
        }
        mLocales = LocaleList.CREATOR.createFromParcel(source);
        mVendorId = source.readInt();
        mProductId = source.readInt();
    }
@@ -108,7 +103,7 @@ public final class KeyboardLayout implements Parcelable,
     * This may be empty if a locale has not been assigned to this keyboard layout.
     * @return The keyboard layout's intended locale.
     */
    public Locale[] getLocales() {
    public LocaleList getLocales() {
        return mLocales;
    }

@@ -141,14 +136,7 @@ public final class KeyboardLayout implements Parcelable,
        dest.writeString(mLabel);
        dest.writeString(mCollection);
        dest.writeInt(mPriority);
        if (mLocales != null) {
            dest.writeInt(mLocales.length);
            for (Locale l : mLocales) {
                dest.writeString(l.toLanguageTag());
            }
        } else {
            dest.writeInt(0);
        }
        mLocales.writeToParcel(dest, 0);
        dest.writeInt(mVendorId);
        dest.writeInt(mProductId);
    }
+22 −16
Original line number Diff line number Diff line
@@ -16,7 +16,9 @@

package com.android.server.input;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.util.LocaleList;
import android.view.Display;
import com.android.internal.inputmethod.InputMethodSubtypeHandle;
import com.android.internal.os.SomeArgs;
@@ -780,8 +782,10 @@ public class InputManagerService extends IInputManager.Stub
                        || layout.getProductId() != d.getProductId()) {
                    return;
                }
                for (Locale l : layout.getLocales()) {
                    if (isCompatibleLocale(systemLocale, l)) {
                final LocaleList locales = layout.getLocales();
                final int numLocales = locales.size();
                for (int localeIndex = 0; localeIndex < numLocales; ++localeIndex) {
                    if (isCompatibleLocale(systemLocale, locales.get(localeIndex))) {
                        layouts.add(layout);
                        break;
                    }
@@ -799,9 +803,12 @@ public class InputManagerService extends IInputManager.Stub
        final int N = layouts.size();
        for (int i = 0; i < N; i++) {
            KeyboardLayout layout = layouts.get(i);
            for (Locale l : layout.getLocales()) {
                if (l.getCountry().equals(systemLocale.getCountry())
                        && l.getVariant().equals(systemLocale.getVariant())) {
            final LocaleList locales = layout.getLocales();
            final int numLocales = locales.size();
            for (int localeIndex = 0; localeIndex < numLocales; ++localeIndex) {
                final Locale locale = locales.get(localeIndex);
                if (locale.getCountry().equals(systemLocale.getCountry())
                        && locale.getVariant().equals(systemLocale.getVariant())) {
                    return layout.getDescriptor();
                }
            }
@@ -809,8 +816,11 @@ public class InputManagerService extends IInputManager.Stub
        // Then try an exact match of language and country
        for (int i = 0; i < N; i++) {
            KeyboardLayout layout = layouts.get(i);
            for (Locale l : layout.getLocales()) {
                if (l.getCountry().equals(systemLocale.getCountry())) {
            final LocaleList locales = layout.getLocales();
            final int numLocales = locales.size();
            for (int localeIndex = 0; localeIndex < numLocales; ++localeIndex) {
                final Locale locale = locales.get(localeIndex);
                if (locale.getCountry().equals(systemLocale.getCountry())) {
                    return layout.getDescriptor();
                }
            }
@@ -1170,7 +1180,7 @@ public class InputManagerService extends IInputManager.Stub
                                    0);
                            String languageTags = a.getString(
                                    com.android.internal.R.styleable.KeyboardLayout_locale);
                            Locale[] locales = getLocalesFromLanguageTags(languageTags);
                            LocaleList locales = getLocalesFromLanguageTags(languageTags);
                            int vid = a.getInt(
                                    com.android.internal.R.styleable.KeyboardLayout_vendorId, -1);
                            int pid = a.getInt(
@@ -1210,16 +1220,12 @@ public class InputManagerService extends IInputManager.Stub
        }
    }

    private static Locale[] getLocalesFromLanguageTags(String languageTags) {
    @NonNull
    private static LocaleList getLocalesFromLanguageTags(String languageTags) {
        if (TextUtils.isEmpty(languageTags)) {
            return new Locale[0];
            return LocaleList.getEmptyLocaleList();
        }
        String[] tags = languageTags.split("\\|");
        Locale[] locales = new Locale[tags.length];
        for (int i = 0; i < tags.length; i++) {
            locales[i] = Locale.forLanguageTag(tags[i]);
        }
        return locales;
        return LocaleList.forLanguageTags(languageTags.replace('|', ','));
    }

    /**