165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/*
265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Copyright (C) 2014 The Android Open Source Project
365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Licensed under the Apache License, Version 2.0 (the "License");
565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * you may not use this file except in compliance with the License.
665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * You may obtain a copy of the License at
765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *      http://www.apache.org/licenses/LICENSE-2.0
965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane *
1065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Unless required by applicable law or agreed to in writing, software
1165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * distributed under the License is distributed on an "AS IS" BASIS,
1265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * See the License for the specific language governing permissions and
1465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * limitations under the License.
1565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
1665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepackage com.android.tv.settings.widget.picker;
1865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
1965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.content.res.Resources;
2065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.R;
2265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.text.DateFormatSymbols;
2465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
2565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/**
2665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Picker related constants
2765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */
2865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepublic class PickerConstant {
2965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static PickerConstant sInst;
3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private static Object sInstLock = new Object();
3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public final String[] months;
3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public final String[] days31;
3465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public final String[] days30;
3565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public final String[] days29;
3665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public final String[] days28;
3765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public final String[] hours12;
3865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public final String[] hours24;
3965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public final String[] minutes;
4065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public final String[] ampm;
4165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public final String dateSeparator;
4265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    public final String timeSeparator;
4365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
4465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private PickerConstant(Resources resources) {
4565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        // TODO re-init months and ampm if the locale changes
4665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        months = new DateFormatSymbols().getShortMonths();
4765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        days28 = createStringIntArrays(28, false, 2);
4865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        days29 = createStringIntArrays(29, false, 2);
4965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        days30 = createStringIntArrays(30, false, 2);
5065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        days31 = createStringIntArrays(31, false, 2);
5165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        hours12 = createStringIntArrays(12, false, 2);
5265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        hours24 = createStringIntArrays(23, true, 2);
5365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        minutes = createStringIntArrays(59, true, 2);
5465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        ampm = resources.getStringArray(R.array.ampm);
5565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        dateSeparator = resources.getString(R.string.date_separator);
5665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        timeSeparator = resources.getString(R.string.time_separator);
5765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
5865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
5965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
6065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    private String[] createStringIntArrays(int lastNumber, boolean startAtZero, int minLen) {
6165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        int range = startAtZero ? (lastNumber + 1) : lastNumber;
6265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        String format = "%0" + minLen + "d";
6365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        String[] array = new String[range];
6465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        for (int i = 0; i < range; i++) {
6565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (minLen > 0) {
6665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                array[i] = String.format(format, startAtZero ? i : (i + 1));
6765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            } else {
6865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                array[i] = String.valueOf(startAtZero ? i : (i + 1));
6965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
7065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
7165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return array;
7265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
7365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane
7465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    static public PickerConstant getInstance(Resources resources) {
7565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        synchronized (sInstLock) {
7665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            if (sInst == null) {
7765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane                sInst = new PickerConstant(resources);
7865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane            }
7965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        }
8065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane        return sInst;
8165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane    }
8265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane}
83