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.date; 18 19 import android.content.Context; 20 import android.content.res.Resources; 21 import android.graphics.drawable.StateListDrawable; 22 import android.util.Log; 23 import android.view.View; 24 import android.view.ViewGroup; 25 import android.view.accessibility.AccessibilityEvent; 26 import android.widget.AdapterView; 27 import android.widget.AdapterView.OnItemClickListener; 28 import android.widget.ArrayAdapter; 29 import android.widget.ListView; 30 import android.widget.TextView; 31 32 import com.android.datetimepicker.R; 33 import com.android.datetimepicker.Utils; 34 import com.android.datetimepicker.date.DatePickerDialog.OnDateChangedListener; 35 36 import java.util.ArrayList; 37 import java.util.List; 38 39 /** 40 * Displays a selectable list of years. 41 */ 42 public class YearPickerView extends ListView implements OnItemClickListener, OnDateChangedListener { 43 private static final String TAG = "YearPickerView"; 44 45 private final DatePickerController mController; 46 private YearAdapter mAdapter; 47 private int mViewSize; 48 private int mChildSize; 49 private TextViewWithCircularIndicator mSelectedView; 50 51 /** 52 * @param context 53 */ 54 public YearPickerView(Context context, DatePickerController controller) { 55 super(context); 56 mController = controller; 57 mController.registerOnDateChangedListener(this); 58 ViewGroup.LayoutParams frame = new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, 59 LayoutParams.WRAP_CONTENT); 60 setLayoutParams(frame); 61 Resources res = context.getResources(); 62 mViewSize = res.getDimensionPixelOffset(R.dimen.date_picker_view_animator_height); 63 mChildSize = res.getDimensionPixelOffset(R.dimen.year_label_height); 64 setVerticalFadingEdgeEnabled(true); 65 setFadingEdgeLength(mChildSize / 3); 66 init(context); 67 setOnItemClickListener(this); 68 setSelector(new StateListDrawable()); 69 setDividerHeight(0); 70 onDateChanged(); 71 } 72 73 private void init(Context context) { 74 ArrayList<String> years = new ArrayList<String>(); 75 for (int year = mController.getMinYear(); year <= mController.getMaxYear(); year++) { 76 years.add(String.format("%d", year)); 77 } 78 mAdapter = new YearAdapter(context, R.layout.year_label_text_view, years); 79 setAdapter(mAdapter); 80 } 81 82 @Override 83 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 84 mController.tryVibrate(); 85 TextViewWithCircularIndicator clickedView = (TextViewWithCircularIndicator) view; 86 if (clickedView != null) { 87 if (clickedView != mSelectedView) { 88 if (mSelectedView != null) { 89 mSelectedView.drawIndicator(false); 90 mSelectedView.requestLayout(); 91 } 92 clickedView.drawIndicator(true); 93 clickedView.requestLayout(); 94 mSelectedView = clickedView; 95 } 96 mController.onYearSelected(getYearFromTextView(clickedView)); 97 mAdapter.notifyDataSetChanged(); 98 } 99 } 100 101 private int getYearFromTextView(TextView view) { 102 return Integer.valueOf(view.getText().toString()); 103 } 104 105 private class YearAdapter extends ArrayAdapter<String> { 106 107 public YearAdapter(Context context, int resource, List<String> objects) { 108 super(context, resource, objects); 109 } 110 111 @Override 112 public View getView(int position, View convertView, ViewGroup parent) { 113 TextViewWithCircularIndicator v = (TextViewWithCircularIndicator) 114 super.getView(position, convertView, parent); 115 v.requestLayout(); 116 int year = getYearFromTextView(v); 117 boolean selected = mController.getSelectedDay().year == year; 118 v.drawIndicator(selected); 119 if (selected) { 120 mSelectedView = v; 121 } 122 return v; 123 } 124 } 125 126 public void postSetSelectionCentered(final int position) { 127 postSetSelectionFromTop(position, mViewSize / 2 - mChildSize / 2); 128 } 129 130 public void postSetSelectionFromTop(final int position, final int offset) { 131 post(new Runnable() { 132 133 @Override 134 public void run() { 135 setSelectionFromTop(position, offset); 136 requestLayout(); 137 } 138 }); 139 } 140 141 public int getFirstPositionOffset() { 142 final View firstChild = getChildAt(0); 143 if (firstChild == null) { 144 return 0; 145 } 146 return firstChild.getTop(); 147 } 148 149 @Override 150 public void onDateChanged() { 151 mAdapter.notifyDataSetChanged(); 152 postSetSelectionCentered(mController.getSelectedDay().year - mController.getMinYear()); 153 } 154 155 @Override 156 public void onInitializeAccessibilityEvent(AccessibilityEvent event) { 157 super.onInitializeAccessibilityEvent(event); 158 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_SCROLLED) { 159 event.setFromIndex(0); 160 event.setToIndex(0); 161 } 162 } 163 } 164