1 /* 2 * Copyright (C) 2006 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 android.text.method; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.text.InputType; 22 import android.view.KeyEvent; 23 24 import com.android.internal.annotations.GuardedBy; 25 import com.android.internal.util.ArrayUtils; 26 27 import java.util.HashMap; 28 import java.util.LinkedHashSet; 29 import java.util.Locale; 30 31 /** 32 * For entering dates and times in the same text field. 33 * <p></p> 34 * As for all implementations of {@link KeyListener}, this class is only concerned 35 * with hardware keyboards. Software input methods have no obligation to trigger 36 * the methods in this class. 37 */ 38 @android.ravenwood.annotation.RavenwoodKeepWholeClass 39 public class DateTimeKeyListener extends NumberKeyListener 40 { getInputType()41 public int getInputType() { 42 if (mNeedsAdvancedInput) { 43 return InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL; 44 } else { 45 return InputType.TYPE_CLASS_DATETIME | InputType.TYPE_DATETIME_VARIATION_NORMAL; 46 } 47 } 48 49 @Override 50 @NonNull getAcceptedChars()51 protected char[] getAcceptedChars() 52 { 53 return mCharacters; 54 } 55 56 /** 57 * @deprecated Use {@link #DateTimeKeyListener(Locale)} instead. 58 */ 59 @Deprecated DateTimeKeyListener()60 public DateTimeKeyListener() { 61 this(null); 62 } 63 64 private static final String SYMBOLS_TO_IGNORE = "yMLdahHKkms"; 65 private static final String SKELETON_12HOUR = "yMdhms"; 66 private static final String SKELETON_24HOUR = "yMdHms"; 67 DateTimeKeyListener(@ullable Locale locale)68 public DateTimeKeyListener(@Nullable Locale locale) { 69 final LinkedHashSet<Character> chars = new LinkedHashSet<>(); 70 // First add the digits. Then, add all the character in AM and PM markers. Finally, add all 71 // the non-pattern characters seen in the patterns for "yMdhms" and "yMdHms". 72 final boolean success = NumberKeyListener.addDigits(chars, locale) 73 && NumberKeyListener.addAmPmChars(chars, locale) 74 && NumberKeyListener.addFormatCharsFromSkeleton( 75 chars, locale, SKELETON_12HOUR, SYMBOLS_TO_IGNORE) 76 && NumberKeyListener.addFormatCharsFromSkeleton( 77 chars, locale, SKELETON_24HOUR, SYMBOLS_TO_IGNORE); 78 if (success) { 79 mCharacters = NumberKeyListener.collectionToArray(chars); 80 if (locale != null && "en".equals(locale.getLanguage())) { 81 // For backward compatibility reasons, assume we don't need advanced input for 82 // English locales, although English locales literally also need a comma and perhaps 83 // uppercase letters for AM and PM. 84 mNeedsAdvancedInput = false; 85 } else { 86 mNeedsAdvancedInput = !ArrayUtils.containsAll(CHARACTERS, mCharacters); 87 } 88 } else { 89 mCharacters = CHARACTERS; 90 mNeedsAdvancedInput = false; 91 } 92 } 93 94 /** 95 * @deprecated Use {@link #getInstance(Locale)} instead. 96 */ 97 @Deprecated 98 @NonNull getInstance()99 public static DateTimeKeyListener getInstance() { 100 return getInstance(null); 101 } 102 103 /** 104 * Returns an instance of DateTimeKeyListener appropriate for the given locale. 105 */ 106 @NonNull getInstance(@ullable Locale locale)107 public static DateTimeKeyListener getInstance(@Nullable Locale locale) { 108 DateTimeKeyListener instance; 109 synchronized (sLock) { 110 instance = sInstanceCache.get(locale); 111 if (instance == null) { 112 instance = new DateTimeKeyListener(locale); 113 sInstanceCache.put(locale, instance); 114 } 115 } 116 return instance; 117 } 118 119 /** 120 * This field used to list the characters that were used. But is now a fixed data 121 * field that is the list of code units used for the deprecated case where the class 122 * is instantiated with null or no input parameter. 123 * 124 * @see KeyEvent#getMatch 125 * @see #getAcceptedChars 126 * 127 * @deprecated Use {@link #getAcceptedChars()} instead. 128 */ 129 public static final char[] CHARACTERS = new char[] { 130 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'm', 131 'p', ':', '/', '-', ' ' 132 }; 133 134 private final char[] mCharacters; 135 private final boolean mNeedsAdvancedInput; 136 137 private static final Object sLock = new Object(); 138 @GuardedBy("sLock") 139 private static final HashMap<Locale, DateTimeKeyListener> sInstanceCache = new HashMap<>(); 140 } 141