• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 in a 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 DateKeyListener 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_DATE;
46         }
47     }
48 
49     @Override
50     @NonNull
getAcceptedChars()51     protected char[] getAcceptedChars() {
52         return mCharacters;
53     }
54 
55     /**
56      * @deprecated Use {@link #DateKeyListener(Locale)} instead.
57      */
58     @Deprecated
DateKeyListener()59     public DateKeyListener() {
60         this(null);
61     }
62 
63     private static final String SYMBOLS_TO_IGNORE = "yMLd";
64     private static final String[] SKELETONS = {"yMd", "yM", "Md"};
65 
DateKeyListener(@ullable Locale locale)66     public DateKeyListener(@Nullable Locale locale) {
67         final LinkedHashSet<Character> chars = new LinkedHashSet<>();
68         // First add the digits, then add all the non-pattern characters seen in the pattern for
69         // "yMd", which is supposed to only have numerical fields.
70         final boolean success = NumberKeyListener.addDigits(chars, locale)
71                                 && NumberKeyListener.addFormatCharsFromSkeletons(
72                                         chars, locale, SKELETONS, SYMBOLS_TO_IGNORE);
73         if (success) {
74             mCharacters = NumberKeyListener.collectionToArray(chars);
75             mNeedsAdvancedInput = !ArrayUtils.containsAll(CHARACTERS, mCharacters);
76         } else {
77             mCharacters = CHARACTERS;
78             mNeedsAdvancedInput = false;
79         }
80     }
81 
82     /**
83      * @deprecated Use {@link #getInstance(Locale)} instead.
84      */
85     @Deprecated
86     @NonNull
getInstance()87     public static DateKeyListener getInstance() {
88         return getInstance(null);
89     }
90 
91     /**
92      * Returns an instance of DateKeyListener appropriate for the given locale.
93      */
94     @NonNull
getInstance(@ullable Locale locale)95     public static DateKeyListener getInstance(@Nullable Locale locale) {
96         DateKeyListener instance;
97         synchronized (sLock) {
98             instance = sInstanceCache.get(locale);
99             if (instance == null) {
100                 instance = new DateKeyListener(locale);
101                 sInstanceCache.put(locale, instance);
102             }
103         }
104         return instance;
105     }
106 
107     /**
108      * This field used to list the characters that were used. But is now a fixed data
109      * field that is the list of code units used for the deprecated case where the class
110      * is instantiated with null or no input parameter.
111      *
112      * @see KeyEvent#getMatch
113      * @see #getAcceptedChars
114      *
115      * @deprecated Use {@link #getAcceptedChars()} instead.
116      */
117     @Deprecated
118     public static final char[] CHARACTERS = new char[] {
119             '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
120             '/', '-', '.'
121         };
122 
123     private final char[] mCharacters;
124     private final boolean mNeedsAdvancedInput;
125 
126     private static final Object sLock = new Object();
127     @GuardedBy("sLock")
128     private static final HashMap<Locale, DateKeyListener> sInstanceCache = new HashMap<>();
129 }
130