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