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

Commit 3484ba8f authored by Roozbeh Pournader's avatar Roozbeh Pournader
Browse files

Internationalize subclasses of NumberKeyListener

The previous implementation assumed ASCII digits, and a fixed set of
separators and signs.

The following classes have been internationalized: DigitsKeyListener,
DateKeyListener, TimeKeyListener, and DateTimeKeyListener.
DialerKeyListener is not modified yet, due to concerns about the
handling of non-ASCII digits by dialing-related parts of Android.

Even with this CL, characters outside BMP and multi-character signs
and decimal separators are still not supported, due to limitations in
android.view.KeyEvent and android.view.KeyCharacterMap.

Test: cts-tradefed run cts-dev --module CtsTextTestCases --test android.text.method.cts.*KeyListenerTest
Bug: https://code.google.com/p/android/issues/detail?id=2626
Bug: https://code.google.com/p/android/issues/detail?id=82993
Bug: 8319249
Bug: 33276673
Bug: 33643035
Bug: 34394455
Change-Id: I1cf87d0d9d1b383f5265c07ecd63b5767f9a68ca
parent b0ac86b7
Loading
Loading
Loading
Loading
+23 −13
Original line number Diff line number Diff line
@@ -40008,19 +40008,23 @@ package android.text.method {
  }
  public class DateKeyListener extends android.text.method.NumberKeyListener {
    ctor public DateKeyListener();
    ctor public deprecated DateKeyListener();
    ctor public DateKeyListener(java.util.Locale);
    method protected char[] getAcceptedChars();
    method public int getInputType();
    method public static android.text.method.DateKeyListener getInstance();
    field public static final char[] CHARACTERS;
    method public static deprecated android.text.method.DateKeyListener getInstance();
    method public static android.text.method.DateKeyListener getInstance(java.util.Locale);
    field public static final deprecated char[] CHARACTERS;
  }
  public class DateTimeKeyListener extends android.text.method.NumberKeyListener {
    ctor public DateTimeKeyListener();
    ctor public deprecated DateTimeKeyListener();
    ctor public DateTimeKeyListener(java.util.Locale);
    method protected char[] getAcceptedChars();
    method public int getInputType();
    method public static android.text.method.DateTimeKeyListener getInstance();
    field public static final char[] CHARACTERS;
    method public static deprecated android.text.method.DateTimeKeyListener getInstance();
    method public static android.text.method.DateTimeKeyListener getInstance(java.util.Locale);
    field public static final deprecated char[] CHARACTERS;
  }
  public class DialerKeyListener extends android.text.method.NumberKeyListener {
@@ -40032,12 +40036,16 @@ package android.text.method {
  }
  public class DigitsKeyListener extends android.text.method.NumberKeyListener {
    ctor public DigitsKeyListener();
    ctor public DigitsKeyListener(boolean, boolean);
    ctor public deprecated DigitsKeyListener();
    ctor public deprecated DigitsKeyListener(boolean, boolean);
    ctor public DigitsKeyListener(java.util.Locale);
    ctor public DigitsKeyListener(java.util.Locale, boolean, boolean);
    method protected char[] getAcceptedChars();
    method public int getInputType();
    method public static android.text.method.DigitsKeyListener getInstance();
    method public static android.text.method.DigitsKeyListener getInstance(boolean, boolean);
    method public static deprecated android.text.method.DigitsKeyListener getInstance();
    method public static deprecated android.text.method.DigitsKeyListener getInstance(boolean, boolean);
    method public static android.text.method.DigitsKeyListener getInstance(java.util.Locale);
    method public static android.text.method.DigitsKeyListener getInstance(java.util.Locale, boolean, boolean);
    method public static android.text.method.DigitsKeyListener getInstance(java.lang.String);
  }
@@ -40181,11 +40189,13 @@ package android.text.method {
  }
  public class TimeKeyListener extends android.text.method.NumberKeyListener {
    ctor public TimeKeyListener();
    ctor public deprecated TimeKeyListener();
    ctor public TimeKeyListener(java.util.Locale);
    method protected char[] getAcceptedChars();
    method public int getInputType();
    method public static android.text.method.TimeKeyListener getInstance();
    field public static final char[] CHARACTERS;
    method public static deprecated android.text.method.TimeKeyListener getInstance();
    method public static android.text.method.TimeKeyListener getInstance(java.util.Locale);
    field public static final deprecated char[] CHARACTERS;
  }
  public class Touch {
+23 −13
Original line number Diff line number Diff line
@@ -43264,19 +43264,23 @@ package android.text.method {
  }
  public class DateKeyListener extends android.text.method.NumberKeyListener {
    ctor public DateKeyListener();
    ctor public deprecated DateKeyListener();
    ctor public DateKeyListener(java.util.Locale);
    method protected char[] getAcceptedChars();
    method public int getInputType();
    method public static android.text.method.DateKeyListener getInstance();
    field public static final char[] CHARACTERS;
    method public static deprecated android.text.method.DateKeyListener getInstance();
    method public static android.text.method.DateKeyListener getInstance(java.util.Locale);
    field public static final deprecated char[] CHARACTERS;
  }
  public class DateTimeKeyListener extends android.text.method.NumberKeyListener {
    ctor public DateTimeKeyListener();
    ctor public deprecated DateTimeKeyListener();
    ctor public DateTimeKeyListener(java.util.Locale);
    method protected char[] getAcceptedChars();
    method public int getInputType();
    method public static android.text.method.DateTimeKeyListener getInstance();
    field public static final char[] CHARACTERS;
    method public static deprecated android.text.method.DateTimeKeyListener getInstance();
    method public static android.text.method.DateTimeKeyListener getInstance(java.util.Locale);
    field public static final deprecated char[] CHARACTERS;
  }
  public class DialerKeyListener extends android.text.method.NumberKeyListener {
@@ -43288,12 +43292,16 @@ package android.text.method {
  }
  public class DigitsKeyListener extends android.text.method.NumberKeyListener {
    ctor public DigitsKeyListener();
    ctor public DigitsKeyListener(boolean, boolean);
    ctor public deprecated DigitsKeyListener();
    ctor public deprecated DigitsKeyListener(boolean, boolean);
    ctor public DigitsKeyListener(java.util.Locale);
    ctor public DigitsKeyListener(java.util.Locale, boolean, boolean);
    method protected char[] getAcceptedChars();
    method public int getInputType();
    method public static android.text.method.DigitsKeyListener getInstance();
    method public static android.text.method.DigitsKeyListener getInstance(boolean, boolean);
    method public static deprecated android.text.method.DigitsKeyListener getInstance();
    method public static deprecated android.text.method.DigitsKeyListener getInstance(boolean, boolean);
    method public static android.text.method.DigitsKeyListener getInstance(java.util.Locale);
    method public static android.text.method.DigitsKeyListener getInstance(java.util.Locale, boolean, boolean);
    method public static android.text.method.DigitsKeyListener getInstance(java.lang.String);
  }
@@ -43437,11 +43445,13 @@ package android.text.method {
  }
  public class TimeKeyListener extends android.text.method.NumberKeyListener {
    ctor public TimeKeyListener();
    ctor public deprecated TimeKeyListener();
    ctor public TimeKeyListener(java.util.Locale);
    method protected char[] getAcceptedChars();
    method public int getInputType();
    method public static android.text.method.TimeKeyListener getInstance();
    field public static final char[] CHARACTERS;
    method public static deprecated android.text.method.TimeKeyListener getInstance();
    method public static android.text.method.TimeKeyListener getInstance(java.util.Locale);
    field public static final deprecated char[] CHARACTERS;
  }
  public class Touch {
+23 −13
Original line number Diff line number Diff line
@@ -40130,19 +40130,23 @@ package android.text.method {
  }
  public class DateKeyListener extends android.text.method.NumberKeyListener {
    ctor public DateKeyListener();
    ctor public deprecated DateKeyListener();
    ctor public DateKeyListener(java.util.Locale);
    method protected char[] getAcceptedChars();
    method public int getInputType();
    method public static android.text.method.DateKeyListener getInstance();
    field public static final char[] CHARACTERS;
    method public static deprecated android.text.method.DateKeyListener getInstance();
    method public static android.text.method.DateKeyListener getInstance(java.util.Locale);
    field public static final deprecated char[] CHARACTERS;
  }
  public class DateTimeKeyListener extends android.text.method.NumberKeyListener {
    ctor public DateTimeKeyListener();
    ctor public deprecated DateTimeKeyListener();
    ctor public DateTimeKeyListener(java.util.Locale);
    method protected char[] getAcceptedChars();
    method public int getInputType();
    method public static android.text.method.DateTimeKeyListener getInstance();
    field public static final char[] CHARACTERS;
    method public static deprecated android.text.method.DateTimeKeyListener getInstance();
    method public static android.text.method.DateTimeKeyListener getInstance(java.util.Locale);
    field public static final deprecated char[] CHARACTERS;
  }
  public class DialerKeyListener extends android.text.method.NumberKeyListener {
@@ -40154,12 +40158,16 @@ package android.text.method {
  }
  public class DigitsKeyListener extends android.text.method.NumberKeyListener {
    ctor public DigitsKeyListener();
    ctor public DigitsKeyListener(boolean, boolean);
    ctor public deprecated DigitsKeyListener();
    ctor public deprecated DigitsKeyListener(boolean, boolean);
    ctor public DigitsKeyListener(java.util.Locale);
    ctor public DigitsKeyListener(java.util.Locale, boolean, boolean);
    method protected char[] getAcceptedChars();
    method public int getInputType();
    method public static android.text.method.DigitsKeyListener getInstance();
    method public static android.text.method.DigitsKeyListener getInstance(boolean, boolean);
    method public static deprecated android.text.method.DigitsKeyListener getInstance();
    method public static deprecated android.text.method.DigitsKeyListener getInstance(boolean, boolean);
    method public static android.text.method.DigitsKeyListener getInstance(java.util.Locale);
    method public static android.text.method.DigitsKeyListener getInstance(java.util.Locale, boolean, boolean);
    method public static android.text.method.DigitsKeyListener getInstance(java.lang.String);
  }
@@ -40303,11 +40311,13 @@ package android.text.method {
  }
  public class TimeKeyListener extends android.text.method.NumberKeyListener {
    ctor public TimeKeyListener();
    ctor public deprecated TimeKeyListener();
    ctor public TimeKeyListener(java.util.Locale);
    method protected char[] getAcceptedChars();
    method public int getInputType();
    method public static android.text.method.TimeKeyListener getInstance();
    field public static final char[] CHARACTERS;
    method public static deprecated android.text.method.TimeKeyListener getInstance();
    method public static android.text.method.TimeKeyListener getInstance(java.util.Locale);
    field public static final deprecated char[] CHARACTERS;
  }
  public class Touch {
+64 −9
Original line number Diff line number Diff line
@@ -16,9 +16,17 @@

package android.text.method;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.text.InputType;
import android.view.KeyEvent;

import com.android.internal.annotations.GuardedBy;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Locale;

/**
 * For entering dates in a text field.
 * <p></p>
@@ -34,29 +42,76 @@ public class DateKeyListener extends NumberKeyListener
    }

    @Override
    protected char[] getAcceptedChars()
    {
        return CHARACTERS;
    @NonNull
    protected char[] getAcceptedChars() {
        return mCharacters;
    }

    /**
     * @deprecated Use {@link #DateKeyListener(Locale)} instead.
     */
    @Deprecated
    public DateKeyListener() {
        this(null);
    }

    private static final String SYMBOLS_TO_IGNORE = "yMLd";
    private static final String[] SKELETONS = {"yMd", "yM", "Md"};

    public DateKeyListener(@Nullable Locale locale) {
        final LinkedHashSet<Character> chars = new LinkedHashSet<>();
        // First add the digits, then add all the non-pattern characters seen in the pattern for
        // "yMd", which is supposed to only have numerical fields.
        final boolean success = NumberKeyListener.addDigits(chars, locale)
                                && NumberKeyListener.addFormatCharsFromSkeletons(
                                        chars, locale, SKELETONS, SYMBOLS_TO_IGNORE);
        mCharacters = success ? NumberKeyListener.collectionToArray(chars) : CHARACTERS;
    }

    /**
     * @deprecated Use {@link #getInstance(Locale)} instead.
     */
    @Deprecated
    @NonNull
    public static DateKeyListener getInstance() {
        if (sInstance != null)
            return sInstance;
        return getInstance(null);
    }

        sInstance = new DateKeyListener();
        return sInstance;
    /**
     * Returns an instance of DateKeyListener appropriate for the given locale.
     */
    @NonNull
    public static DateKeyListener getInstance(@Nullable Locale locale) {
        DateKeyListener instance;
        synchronized (sLock) {
            instance = sInstanceCache.get(locale);
            if (instance == null) {
                instance = new DateKeyListener(locale);
                sInstanceCache.put(locale, instance);
            }
        }
        return instance;
    }

    /**
     * The characters that are used.
     * This field used to list the characters that were used. But is now a fixed data
     * field that is the list of code units used for the deprecated case where the class
     * is instantiated with null or no input parameter.
     *
     * @see KeyEvent#getMatch
     * @see #getAcceptedChars
     *
     * @deprecated Use {@link #getAcceptedChars()} instead.
     */
    @Deprecated
    public static final char[] CHARACTERS = new char[] {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            '/', '-', '.'
        };

    private static DateKeyListener sInstance;
    private final char[] mCharacters;

    private static final Object sLock = new Object();
    @GuardedBy("sLock")
    private static final HashMap<Locale, DateKeyListener> sInstanceCache = new HashMap<>();
}
+66 −7
Original line number Diff line number Diff line
@@ -16,9 +16,17 @@

package android.text.method;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.text.InputType;
import android.view.KeyEvent;

import com.android.internal.annotations.GuardedBy;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.Locale;

/**
 * For entering dates and times in the same text field.
 * <p></p>
@@ -34,29 +42,80 @@ public class DateTimeKeyListener extends NumberKeyListener
    }
    
    @Override
    @NonNull
    protected char[] getAcceptedChars()
    {
        return CHARACTERS;
        return mCharacters;
    }

    /**
     * @deprecated Use {@link #DateTimeKeyListener(Locale)} instead.
     */
    @Deprecated
    public DateTimeKeyListener() {
        this(null);
    }

    private static final String SYMBOLS_TO_IGNORE = "yMLdahHKkms";
    private static final String SKELETON_12HOUR = "yMdhms";
    private static final String SKELETON_24HOUR = "yMdHms";

    public DateTimeKeyListener(@Nullable Locale locale) {
        final LinkedHashSet<Character> chars = new LinkedHashSet<>();
        // First add the digits. Then, add all the character in AM and PM markers. Finally, add all
        // the non-pattern characters seen in the patterns for "yMdhms" and "yMdHms".
        boolean success = NumberKeyListener.addDigits(chars, locale)
                          && NumberKeyListener.addAmPmChars(chars, locale)
                          && NumberKeyListener.addFormatCharsFromSkeleton(
                              chars, locale, SKELETON_12HOUR, SYMBOLS_TO_IGNORE)
                          && NumberKeyListener.addFormatCharsFromSkeleton(
                              chars, locale, SKELETON_24HOUR, SYMBOLS_TO_IGNORE);
        mCharacters = success ? NumberKeyListener.collectionToArray(chars) : CHARACTERS;
    }

    /**
     * @deprecated Use {@link #getInstance(Locale)} instead.
     */
    @Deprecated
    @NonNull
    public static DateTimeKeyListener getInstance() {
        if (sInstance != null)
            return sInstance;
        return getInstance(null);
    }

        sInstance = new DateTimeKeyListener();
        return sInstance;
    /**
     * Returns an instance of DateTimeKeyListener appropriate for the given locale.
     */
    @NonNull
    public static DateTimeKeyListener getInstance(@Nullable Locale locale) {
        DateTimeKeyListener instance;
        synchronized (sLock) {
            instance = sInstanceCache.get(locale);
            if (instance == null) {
                instance = new DateTimeKeyListener(locale);
                sInstanceCache.put(locale, instance);
            }
        }
        return instance;
    }

    /**
     * The characters that are used.
     * This field used to list the characters that were used. But is now a fixed data
     * field that is the list of code units used for the deprecated case where the class
     * is instantiated with null or no input parameter.
     *
     * @see KeyEvent#getMatch
     * @see #getAcceptedChars
     *
     * @deprecated Use {@link #getAcceptedChars()} instead.
     */
    public static final char[] CHARACTERS = new char[] {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'm',
            'p', ':', '/', '-', ' '
        };

    private static DateTimeKeyListener sInstance;
    private final char[] mCharacters;

    private static final Object sLock = new Object();
    @GuardedBy("sLock")
    private static final HashMap<Locale, DateTimeKeyListener> sInstanceCache = new HashMap<>();
}
Loading