1/*
2 * Copyright (C) 2014 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 com.android.tv.settings.widget.picker;
18
19import android.content.res.Resources;
20
21import com.android.tv.settings.R;
22
23import java.text.DateFormatSymbols;
24
25/**
26 * Picker related constants
27 */
28public class PickerConstant {
29    private static PickerConstant sInst;
30    private static Object sInstLock = new Object();
31
32    public final String[] months;
33    public final String[] days31;
34    public final String[] days30;
35    public final String[] days29;
36    public final String[] days28;
37    public final String[] hours12;
38    public final String[] hours24;
39    public final String[] minutes;
40    public final String[] ampm;
41    public final String dateSeparator;
42    public final String timeSeparator;
43
44    private PickerConstant(Resources resources) {
45        // TODO re-init months and ampm if the locale changes
46        months = new DateFormatSymbols().getShortMonths();
47        days28 = createStringIntArrays(28, false, 2);
48        days29 = createStringIntArrays(29, false, 2);
49        days30 = createStringIntArrays(30, false, 2);
50        days31 = createStringIntArrays(31, false, 2);
51        hours12 = createStringIntArrays(12, false, 2);
52        hours24 = createStringIntArrays(23, true, 2);
53        minutes = createStringIntArrays(59, true, 2);
54        ampm = resources.getStringArray(R.array.ampm);
55        dateSeparator = resources.getString(R.string.date_separator);
56        timeSeparator = resources.getString(R.string.time_separator);
57    }
58
59
60    private String[] createStringIntArrays(int lastNumber, boolean startAtZero, int minLen) {
61        int range = startAtZero ? (lastNumber + 1) : lastNumber;
62        String format = "%0" + minLen + "d";
63        String[] array = new String[range];
64        for (int i = 0; i < range; i++) {
65            if (minLen > 0) {
66                array[i] = String.format(format, startAtZero ? i : (i + 1));
67            } else {
68                array[i] = String.valueOf(startAtZero ? i : (i + 1));
69            }
70        }
71        return array;
72    }
73
74    static public PickerConstant getInstance(Resources resources) {
75        synchronized (sInstLock) {
76            if (sInst == null) {
77                sInst = new PickerConstant(resources);
78            }
79        }
80        return sInst;
81    }
82}
83