Home | History | Annotate | Download | only in deskclock
      1 package com.android.deskclock;
      2 
      3 import android.annotation.TargetApi;
      4 import android.content.Context;
      5 import android.content.res.TypedArray;
      6 import android.graphics.Color;
      7 import android.graphics.drawable.Drawable;
      8 import android.os.Build;
      9 import android.util.AttributeSet;
     10 import android.widget.NumberPicker;
     11 
     12 import java.lang.reflect.Field;
     13 
     14 /**
     15  * Subclass of NumberPicker that allows customizing divider color.
     16  */
     17 public class NumberPickerCompat extends NumberPicker {
     18 
     19     private static Field sSelectionDivider;
     20     private static boolean sTrySelectionDivider = true;
     21 
     22     public NumberPickerCompat(Context context) {
     23         this(context, null /* attrs */);
     24     }
     25 
     26     public NumberPickerCompat(Context context, AttributeSet attrs) {
     27         super(context, attrs);
     28         tintSelectionDivider(context);
     29     }
     30 
     31     public NumberPickerCompat(Context context, AttributeSet attrs, int defStyleAttr) {
     32         super(context, attrs, defStyleAttr);
     33         tintSelectionDivider(context);
     34     }
     35 
     36     @TargetApi(Build.VERSION_CODES.LOLLIPOP)
     37     private void tintSelectionDivider(Context context) {
     38         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP
     39                 || Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
     40             // Accent color in KK will stay system blue, so leave divider color matching.
     41             // The divider is correctly tinted to controlColorNormal in M.
     42             return;
     43         }
     44 
     45         if (sTrySelectionDivider) {
     46             final TypedArray a = context.obtainStyledAttributes(
     47                     new int[] { android.R.attr.colorControlNormal });
     48              // White is default color if colorControlNormal is not defined.
     49             final int color = a.getColor(0, Color.WHITE);
     50             a.recycle();
     51 
     52             try {
     53                 if (sSelectionDivider == null) {
     54                     sSelectionDivider = NumberPicker.class.getDeclaredField("mSelectionDivider");
     55                     sSelectionDivider.setAccessible(true);
     56                 }
     57                 final Drawable selectionDivider = (Drawable) sSelectionDivider.get(this);
     58                 if (selectionDivider != null) {
     59                     // setTint is API21+, but this will only be called in API21
     60                     selectionDivider.setTint(color);
     61                 }
     62             } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
     63                 LogUtils.e("Unable to set selection divider", e);
     64                 sTrySelectionDivider = false;
     65             }
     66         }
     67     }
     68 
     69 }
     70