Home | History | Annotate | Download | only in picker
      1 /*
      2  * Copyright (C) 2017 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 androidx.leanback.widget.picker;
     18 
     19 import android.content.res.Resources;
     20 import android.os.Build;
     21 
     22 import java.text.DateFormatSymbols;
     23 import java.util.Calendar;
     24 import java.util.Locale;
     25 
     26 /**
     27  * Utility class that provides Date/Time related constants as well as locale-specific calendar for
     28  * both {@link DatePicker} and {@link TimePicker}.
     29  */
     30 class PickerUtility {
     31 
     32     // Whether the API version supports the use of {@link DateFormat#getBestDateTimePattern()}
     33     static final boolean SUPPORTS_BEST_DATE_TIME_PATTERN =
     34             Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2;
     35     public static class DateConstant {
     36         public final Locale locale;
     37         public final String[] months;
     38         public final String[] days;
     39 
     40         private DateConstant(Locale locale, Resources resources) {
     41             this.locale = locale;
     42             DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
     43             months = symbols.getShortMonths();
     44             Calendar calendar = Calendar.getInstance(locale);
     45             days = createStringIntArrays(calendar.getMinimum(Calendar.DAY_OF_MONTH),
     46                     calendar.getMaximum(Calendar.DAY_OF_MONTH), "%02d");
     47         }
     48     }
     49 
     50     public static class TimeConstant {
     51         public final Locale locale;
     52         public final String[] hours12;
     53         public final String[] hours24;
     54         public final String[] minutes;
     55         public final String[] ampm;
     56 
     57         private TimeConstant(Locale locale, Resources resources) {
     58             this.locale = locale;
     59             DateFormatSymbols symbols = DateFormatSymbols.getInstance(locale);
     60             hours12 = createStringIntArrays(1, 12, "%02d");
     61             hours24 = createStringIntArrays(0, 23, "%02d");
     62             minutes = createStringIntArrays(0, 59, "%02d");
     63             ampm = symbols.getAmPmStrings();
     64         }
     65     }
     66 
     67     public static DateConstant getDateConstantInstance(Locale locale, Resources resources) {
     68         return new DateConstant(locale, resources);
     69     }
     70 
     71     public static TimeConstant getTimeConstantInstance(Locale locale, Resources resources) {
     72         return new TimeConstant(locale, resources);
     73     }
     74 
     75 
     76     public static String[] createStringIntArrays(int firstNumber, int lastNumber, String format) {
     77         String[] array = new String[lastNumber - firstNumber + 1];
     78         for (int i = firstNumber; i <= lastNumber; i++) {
     79             if (format != null) {
     80                 array[i - firstNumber] = String.format(format, i);
     81             } else {
     82                 array[i - firstNumber] = String.valueOf(i);
     83             }
     84         }
     85         return array;
     86     }
     87 
     88     public static Calendar getCalendarForLocale(Calendar oldCalendar, Locale locale) {
     89         if (oldCalendar == null) {
     90             return Calendar.getInstance(locale);
     91         } else {
     92             final long currentTimeMillis = oldCalendar.getTimeInMillis();
     93             Calendar newCalendar = Calendar.getInstance(locale);
     94             newCalendar.setTimeInMillis(currentTimeMillis);
     95             return newCalendar;
     96         }
     97     }
     98 
     99     private PickerUtility() {
    100     }
    101 }
    102