Home | History | Annotate | Download | only in widget
      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 
     17 package android.widget;
     18 
     19 import android.annotation.StyleRes;
     20 import android.content.Context;
     21 import android.icu.util.Calendar;
     22 import android.util.AttributeSet;
     23 import android.widget.DayPickerView.OnDaySelectedListener;
     24 
     25 class CalendarViewMaterialDelegate extends CalendarView.AbstractCalendarViewDelegate {
     26     private final DayPickerView mDayPickerView;
     27 
     28     private CalendarView.OnDateChangeListener mOnDateChangeListener;
     29 
     30     public CalendarViewMaterialDelegate(CalendarView delegator, Context context, AttributeSet attrs,
     31             int defStyleAttr, int defStyleRes) {
     32         super(delegator, context);
     33 
     34         mDayPickerView = new DayPickerView(context, attrs, defStyleAttr, defStyleRes);
     35         mDayPickerView.setOnDaySelectedListener(mOnDaySelectedListener);
     36 
     37         delegator.addView(mDayPickerView);
     38     }
     39 
     40     @Override
     41     public void setWeekDayTextAppearance(@StyleRes int resId) {
     42         mDayPickerView.setDayOfWeekTextAppearance(resId);
     43     }
     44 
     45     @StyleRes
     46     @Override
     47     public int getWeekDayTextAppearance() {
     48         return mDayPickerView.getDayOfWeekTextAppearance();
     49     }
     50 
     51     @Override
     52     public void setDateTextAppearance(@StyleRes int resId) {
     53         mDayPickerView.setDayTextAppearance(resId);
     54     }
     55 
     56     @StyleRes
     57     @Override
     58     public int getDateTextAppearance() {
     59         return mDayPickerView.getDayTextAppearance();
     60     }
     61 
     62     @Override
     63     public void setMinDate(long minDate) {
     64         mDayPickerView.setMinDate(minDate);
     65     }
     66 
     67     @Override
     68     public long getMinDate() {
     69         return mDayPickerView.getMinDate();
     70     }
     71 
     72     @Override
     73     public void setMaxDate(long maxDate) {
     74         mDayPickerView.setMaxDate(maxDate);
     75     }
     76 
     77     @Override
     78     public long getMaxDate() {
     79         return mDayPickerView.getMaxDate();
     80     }
     81 
     82     @Override
     83     public void setFirstDayOfWeek(int firstDayOfWeek) {
     84         mDayPickerView.setFirstDayOfWeek(firstDayOfWeek);
     85     }
     86 
     87     @Override
     88     public int getFirstDayOfWeek() {
     89         return mDayPickerView.getFirstDayOfWeek();
     90     }
     91 
     92     @Override
     93     public void setDate(long date) {
     94         mDayPickerView.setDate(date, true);
     95     }
     96 
     97     @Override
     98     public void setDate(long date, boolean animate, boolean center) {
     99         mDayPickerView.setDate(date, animate);
    100     }
    101 
    102     @Override
    103     public long getDate() {
    104         return mDayPickerView.getDate();
    105     }
    106 
    107     @Override
    108     public void setOnDateChangeListener(CalendarView.OnDateChangeListener listener) {
    109         mOnDateChangeListener = listener;
    110     }
    111 
    112     private final OnDaySelectedListener mOnDaySelectedListener = new OnDaySelectedListener() {
    113         @Override
    114         public void onDaySelected(DayPickerView view, Calendar day) {
    115             if (mOnDateChangeListener != null) {
    116                 final int year = day.get(Calendar.YEAR);
    117                 final int month = day.get(Calendar.MONTH);
    118                 final int dayOfMonth = day.get(Calendar.DAY_OF_MONTH);
    119                 mOnDateChangeListener.onSelectedDayChange(mDelegator, year, month, dayOfMonth);
    120             }
    121         }
    122     };
    123 }
    124