1 /*
2  * Copyright (C) 2015 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.deskclock.alarms.dataadapter;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.content.res.TypedArray;
22 import android.graphics.drawable.Drawable;
23 import android.graphics.drawable.LayerDrawable;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.widget.Button;
27 import android.widget.CheckBox;
28 import android.widget.CompoundButton;
29 import android.widget.LinearLayout;
30 import android.widget.TextView;
31 
32 import com.android.deskclock.R;
33 import com.android.deskclock.Utils;
34 import com.android.deskclock.alarms.AlarmTimeClickHandler;
35 import com.android.deskclock.alarms.utils.DayOrderUtils;
36 import com.android.deskclock.data.DataModel;
37 import com.android.deskclock.provider.Alarm;
38 import com.android.deskclock.provider.AlarmInstance;
39 import com.android.deskclock.provider.DaysOfWeek;
40 
41 import java.util.HashSet;
42 
43 /**
44  * A ViewHolder containing views for an alarm item in expanded stated.
45  */
46 public final class ExpandedAlarmViewHolder extends AlarmTimeViewHolder {
47 
48     public final CheckBox repeat;
49     public final TextView editLabel;
50     public final LinearLayout repeatDays;
51     public final CompoundButton[] dayButtons = new CompoundButton[7];
52     public final CheckBox vibrate;
53     public final TextView ringtone;
54     public final Button delete;
55     public final View preemptiveDismissContainer;
56     public final TextView preemptiveDismissButton;
57 
58     private final boolean mHasVibrator;
59     private final int[] mDayOrder;
60 
ExpandedAlarmViewHolder(View itemView, final boolean hasVibrator, final AlarmTimeClickHandler alarmTimeClickHandler, final AlarmTimeAdapter alarmTimeAdapter)61     public ExpandedAlarmViewHolder(View itemView,
62             final boolean hasVibrator,
63             final AlarmTimeClickHandler alarmTimeClickHandler,
64             final AlarmTimeAdapter alarmTimeAdapter) {
65         super(itemView, alarmTimeClickHandler);
66         final Context context = itemView.getContext();
67         mHasVibrator = hasVibrator;
68         mDayOrder = DayOrderUtils.getDayOrder(context);
69         final Resources.Theme theme = context.getTheme();
70         int[] attrs = new int[] { android.R.attr.selectableItemBackground };
71 
72         final TypedArray typedArray = theme.obtainStyledAttributes(attrs);
73         final LayerDrawable background = new LayerDrawable(new Drawable[] {
74                 context.getResources().getDrawable(R.drawable.alarm_background_expanded),
75                 typedArray.getDrawable(0) });
76         itemView.setBackground(background);
77         typedArray.recycle();
78 
79         final int firstDay = Utils.getZeroIndexedFirstDayOfWeek(context);
80 
81         delete = (Button) itemView.findViewById(R.id.delete);
82 
83         repeat = (CheckBox) itemView.findViewById(R.id.repeat_onoff);
84         vibrate = (CheckBox) itemView.findViewById(R.id.vibrate_onoff);
85         ringtone = (TextView) itemView.findViewById(R.id.choose_ringtone);
86         editLabel = (TextView) itemView.findViewById(R.id.edit_label);
87         repeatDays = (LinearLayout) itemView.findViewById(R.id.repeat_days);
88 
89         // Build button for each day.
90         LayoutInflater mInflater = LayoutInflater.from(context);
91         for (int i = 0; i < DaysOfWeek.DAYS_IN_A_WEEK; i++) {
92             final CompoundButton dayButton = (CompoundButton) mInflater.inflate(
93                     R.layout.day_button, repeatDays, false /* attachToRoot */);
94             dayButton.setText(Utils.getShortWeekday(i, firstDay));
95             dayButton.setContentDescription(Utils.getLongWeekday(i, firstDay));
96             repeatDays.addView(dayButton);
97             dayButtons[i] = dayButton;
98         }
99 
100         preemptiveDismissContainer = itemView.findViewById(R.id.preemptive_dismiss_container);
101         preemptiveDismissButton =
102                 (TextView) itemView.findViewById(R.id.preemptive_dismiss_button);
103 
104         // Collapse handler
105         itemView.setOnClickListener(new View.OnClickListener() {
106             @Override
107             public void onClick(View v) {
108                 alarmTimeAdapter.collapse(getAdapterPosition());
109             }
110         });
111         arrow.setOnClickListener(new View.OnClickListener() {
112             @Override
113             public void onClick(View v) {
114                 alarmTimeAdapter.collapse(getAdapterPosition());
115             }
116         });
117         // Edit time handler
118         clock.setOnClickListener(new View.OnClickListener() {
119             @Override
120             public void onClick(View v) {
121                 alarmTimeClickHandler.onClockClicked(mAlarm);
122             }
123         });
124         // Edit label handler
125         editLabel.setOnClickListener(new View.OnClickListener() {
126             @Override
127             public void onClick(View view) {
128                 alarmTimeClickHandler.onEditLabelClicked(mAlarm);
129             }
130         });
131         // Vibrator checkbox handler
132         vibrate.setOnClickListener(new View.OnClickListener() {
133             @Override
134             public void onClick(View v) {
135                 alarmTimeClickHandler.setAlarmVibrationEnabled(mAlarm, ((CheckBox) v).isChecked());
136             }
137         });
138         // Ringtone editor handler
139         ringtone.setOnClickListener(new View.OnClickListener() {
140             @Override
141             public void onClick(View view) {
142                 alarmTimeClickHandler.onRingtoneClicked(mAlarm);
143             }
144         });
145         // Delete alarm handler
146         delete.setOnClickListener(new View.OnClickListener() {
147             @Override
148             public void onClick(View v) {
149                 alarmTimeClickHandler.onDeleteClicked(mAlarm);
150                 v.announceForAccessibility(context.getString(R.string.alarm_deleted));
151             }
152         });
153         // Repeat checkbox handler
154         repeat.setOnClickListener(new View.OnClickListener() {
155             @Override
156             public void onClick(View view) {
157                 final boolean checked = ((CheckBox) view).isChecked();
158                 alarmTimeClickHandler.setAlarmRepeatEnabled(mAlarm, checked);
159             }
160         });
161         // Day buttons handler
162         for (int i = 0; i < DaysOfWeek.DAYS_IN_A_WEEK; i++) {
163             final int buttonIndex = i;
164             dayButtons[i].setOnClickListener(new View.OnClickListener() {
165                 @Override
166                 public void onClick(View view) {
167                     final boolean isChecked = ((CompoundButton) view).isChecked();
168                     alarmTimeClickHandler.setDayOfWeekEnabled(mAlarm, isChecked, buttonIndex);
169                 }
170             });
171         }
172     }
173 
174     @Override
bindAlarm(Context context, Alarm alarm, AlarmInstance alarmInstance)175     public void bindAlarm(Context context, Alarm alarm, AlarmInstance alarmInstance) {
176         setData(alarm, alarmInstance);
177         bindOnOffSwitch(context, alarm);
178         bindClock(context, alarm);
179         bindEditLabel(alarm);
180         bindDaysOfWeekButtons(alarm);
181         bindVibrator(alarm);
182         bindRingtoneTitle(context, alarm);
183         bindPreemptiveDismissButton(context, alarm, alarmInstance);
184     }
185 
bindRingtoneTitle(Context context, Alarm alarm)186     private void bindRingtoneTitle(Context context, Alarm alarm) {
187         final String title = DataModel.getDataModel().getAlarmRingtoneTitle(alarm.alert);
188         final String description = context.getString(R.string.ringtone_description);
189 
190         ringtone.setText(title);
191         ringtone.setContentDescription(description + " " + title);
192     }
193 
bindDaysOfWeekButtons(Alarm alarm)194     private void bindDaysOfWeekButtons(Alarm alarm) {
195         HashSet<Integer> setDays = alarm.daysOfWeek.getSetDays();
196         for (int i = 0; i < DaysOfWeek.DAYS_IN_A_WEEK; i++) {
197             final CompoundButton dayButton = dayButtons[i];
198             if (setDays.contains(mDayOrder[i])) {
199                 dayButton.setChecked(true);
200                 dayButton.setTextColor(Utils.getCurrentHourColor());
201             } else {
202                 dayButton.setChecked(false);
203                 dayButton.setTextColor(itemView.getContext().getResources().getColor(R.color
204                         .white));
205             }
206         }
207         if (alarm.daysOfWeek.isRepeating()) {
208             repeat.setChecked(true);
209             repeatDays.setVisibility(View.VISIBLE);
210         } else {
211             repeat.setChecked(false);
212             repeatDays.setVisibility(View.GONE);
213         }
214     }
215 
bindEditLabel(Alarm alarm)216     private void bindEditLabel(Alarm alarm) {
217         if (alarm.label != null && alarm.label.length() > 0) {
218             editLabel.setText(alarm.label);
219         } else {
220             editLabel.setText(R.string.label);
221         }
222     }
223 
bindVibrator(Alarm alarm)224     private void bindVibrator(Alarm alarm) {
225         if (!mHasVibrator) {
226             vibrate.setVisibility(View.INVISIBLE);
227         } else {
228             vibrate.setVisibility(View.VISIBLE);
229             vibrate.setChecked(alarm.vibrate);
230         }
231     }
232 }
233