Home | History | Annotate | Download | only in datetimepicker
      1 /*
      2  * Copyright (C) 2013 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 com.android.datetimepicker;
     18 
     19 import android.animation.Keyframe;
     20 import android.animation.ObjectAnimator;
     21 import android.animation.PropertyValuesHolder;
     22 import android.annotation.SuppressLint;
     23 import android.os.Build;
     24 import android.text.format.Time;
     25 import android.view.View;
     26 
     27 import java.util.Calendar;
     28 
     29 /**
     30  * Utility helper functions for time and date pickers.
     31  *
     32  * @deprecated This module is deprecated. Do not use this class.
     33  */
     34 @Deprecated
     35 public class Utils {
     36 
     37     public static final int MONDAY_BEFORE_JULIAN_EPOCH = Time.EPOCH_JULIAN_DAY - 3;
     38     public static final int PULSE_ANIMATOR_DURATION = 544;
     39 
     40     // Alpha level for time picker selection.
     41     public static final int SELECTED_ALPHA = 51;
     42     public static final int SELECTED_ALPHA_THEME_DARK = 102;
     43     // Alpha level for fully opaque.
     44     public static final int FULL_ALPHA = 255;
     45 
     46 
     47     static final String SHARED_PREFS_NAME = "com.android.calendar_preferences";
     48 
     49     public static boolean isJellybeanOrLater() {
     50       return Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
     51     }
     52 
     53     /**
     54      * Try to speak the specified text, for accessibility. Only available on JB or later.
     55      * @param text Text to announce.
     56      */
     57     @SuppressLint("NewApi")
     58     public static void tryAccessibilityAnnounce(View view, CharSequence text) {
     59         if (isJellybeanOrLater() && view != null && text != null) {
     60             view.announceForAccessibility(text);
     61         }
     62     }
     63 
     64     public static int getDaysInMonth(int month, int year) {
     65         switch (month) {
     66             case Calendar.JANUARY:
     67             case Calendar.MARCH:
     68             case Calendar.MAY:
     69             case Calendar.JULY:
     70             case Calendar.AUGUST:
     71             case Calendar.OCTOBER:
     72             case Calendar.DECEMBER:
     73                 return 31;
     74             case Calendar.APRIL:
     75             case Calendar.JUNE:
     76             case Calendar.SEPTEMBER:
     77             case Calendar.NOVEMBER:
     78                 return 30;
     79             case Calendar.FEBRUARY:
     80                 return (year % 4 == 0) ? 29 : 28;
     81             default:
     82                 throw new IllegalArgumentException("Invalid Month");
     83         }
     84     }
     85 
     86     /**
     87      * Takes a number of weeks since the epoch and calculates the Julian day of
     88      * the Monday for that week.
     89      *
     90      * This assumes that the week containing the {@link Time#EPOCH_JULIAN_DAY}
     91      * is considered week 0. It returns the Julian day for the Monday
     92      * {@code week} weeks after the Monday of the week containing the epoch.
     93      *
     94      * @param week Number of weeks since the epoch
     95      * @return The julian day for the Monday of the given week since the epoch
     96      */
     97     public static int getJulianMondayFromWeeksSinceEpoch(int week) {
     98         return MONDAY_BEFORE_JULIAN_EPOCH + week * 7;
     99     }
    100 
    101     /**
    102      * Returns the week since {@link Time#EPOCH_JULIAN_DAY} (Jan 1, 1970)
    103      * adjusted for first day of week.
    104      *
    105      * This takes a julian day and the week start day and calculates which
    106      * week since {@link Time#EPOCH_JULIAN_DAY} that day occurs in, starting
    107      * at 0. *Do not* use this to compute the ISO week number for the year.
    108      *
    109      * @param julianDay The julian day to calculate the week number for
    110      * @param firstDayOfWeek Which week day is the first day of the week,
    111      *          see {@link Time#SUNDAY}
    112      * @return Weeks since the epoch
    113      */
    114     public static int getWeeksSinceEpochFromJulianDay(int julianDay, int firstDayOfWeek) {
    115         int diff = Time.THURSDAY - firstDayOfWeek;
    116         if (diff < 0) {
    117             diff += 7;
    118         }
    119         int refDay = Time.EPOCH_JULIAN_DAY - diff;
    120         return (julianDay - refDay) / 7;
    121     }
    122 
    123     /**
    124      * Render an animator to pulsate a view in place.
    125      * @param labelToAnimate the view to pulsate.
    126      * @return The animator object. Use .start() to begin.
    127      */
    128     public static ObjectAnimator getPulseAnimator(View labelToAnimate, float decreaseRatio,
    129             float increaseRatio) {
    130         Keyframe k0 = Keyframe.ofFloat(0f, 1f);
    131         Keyframe k1 = Keyframe.ofFloat(0.275f, decreaseRatio);
    132         Keyframe k2 = Keyframe.ofFloat(0.69f, increaseRatio);
    133         Keyframe k3 = Keyframe.ofFloat(1f, 1f);
    134 
    135         PropertyValuesHolder scaleX = PropertyValuesHolder.ofKeyframe("scaleX", k0, k1, k2, k3);
    136         PropertyValuesHolder scaleY = PropertyValuesHolder.ofKeyframe("scaleY", k0, k1, k2, k3);
    137         ObjectAnimator pulseAnimator =
    138                 ObjectAnimator.ofPropertyValuesHolder(labelToAnimate, scaleX, scaleY);
    139         pulseAnimator.setDuration(PULSE_ANIMATOR_DURATION);
    140 
    141         return pulseAnimator;
    142     }
    143 }
    144