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.support.v7.widget.RecyclerView; 21 import android.support.v7.widget.SwitchCompat; 22 import android.view.View; 23 import android.widget.CompoundButton; 24 import android.widget.TextView; 25 26 import com.android.deskclock.AlarmUtils; 27 import com.android.deskclock.R; 28 import com.android.deskclock.alarms.AlarmTimeClickHandler; 29 import com.android.deskclock.provider.Alarm; 30 import com.android.deskclock.provider.AlarmInstance; 31 import com.android.deskclock.widget.TextTime; 32 33 /** 34 * Abstract ViewHolder for alarm time items. 35 */ 36 public abstract class AlarmTimeViewHolder extends RecyclerView.ViewHolder { 37 38 private static final float CLOCK_ENABLED_ALPHA = 1f; 39 private static final float CLOCK_DISABLED_ALPHA = 0.69f; 40 41 public final TextTime clock; 42 public final CompoundButton onoff; 43 public final View arrow; 44 public final View preemptiveDismissContainer; 45 public final TextView preemptiveDismissButton; 46 47 protected Alarm mAlarm; 48 protected AlarmInstance mAlarmInstance; 49 50 private final AlarmTimeClickHandler mAlarmTimeClickHandler; 51 AlarmTimeViewHolder(View itemView, AlarmTimeClickHandler alarmTimeClickHandler)52 public AlarmTimeViewHolder(View itemView, AlarmTimeClickHandler alarmTimeClickHandler) { 53 super(itemView); 54 mAlarmTimeClickHandler = alarmTimeClickHandler; 55 clock = (TextTime) itemView.findViewById(R.id.digital_clock); 56 onoff = (CompoundButton) itemView.findViewById(R.id.onoff); 57 arrow = itemView.findViewById(R.id.arrow); 58 preemptiveDismissContainer = itemView.findViewById(R.id.preemptive_dismiss_container); 59 preemptiveDismissButton = 60 (TextView) itemView.findViewById(R.id.preemptive_dismiss_button); 61 preemptiveDismissButton.setOnClickListener(new View.OnClickListener() { 62 @Override 63 public void onClick(View v) { 64 mAlarmTimeClickHandler.dismissAlarmInstance(mAlarmInstance); 65 } 66 }); 67 onoff.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 68 @Override 69 public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { 70 mAlarmTimeClickHandler.setAlarmEnabled(mAlarm, checked); 71 } 72 }); 73 } 74 setData(Alarm alarm, AlarmInstance alarmInstance)75 public void setData(Alarm alarm, AlarmInstance alarmInstance) { 76 mAlarmInstance = alarmInstance; 77 mAlarm = alarm; 78 } 79 clearData()80 public void clearData() { 81 mAlarmInstance = null; 82 mAlarm = null; 83 } 84 85 /** 86 * Binds the view with {@link Alarm} data. 87 */ bindAlarm(Context context, Alarm alarm, AlarmInstance alarmInstance)88 public abstract void bindAlarm(Context context, Alarm alarm, AlarmInstance alarmInstance); 89 bindOnOffSwitch(Context context, Alarm alarm)90 protected void bindOnOffSwitch(Context context, Alarm alarm) { 91 onoff.setChecked(alarm.enabled); 92 ((SwitchCompat) onoff).setTextOn(context.getString(R.string.on_switch)); 93 ((SwitchCompat) onoff).setTextOff(context.getString(R.string.off_switch)); 94 } 95 bindClock(Context context, Alarm alarm)96 protected void bindClock(Context context, Alarm alarm) { 97 clock.setAlpha(alarm.enabled ? CLOCK_ENABLED_ALPHA : CLOCK_DISABLED_ALPHA); 98 clock.setFormat(context); 99 clock.setTime(alarm.hour, alarm.minutes); 100 } 101 bindPreemptiveDismissButton(Context context, Alarm alarm, AlarmInstance alarmInstance)102 protected boolean bindPreemptiveDismissButton(Context context, Alarm alarm, 103 AlarmInstance alarmInstance) { 104 boolean canBind = alarm.canPreemptivelyDismiss() && alarmInstance != null; 105 if (canBind) { 106 preemptiveDismissContainer.setVisibility(View.VISIBLE); 107 final String dismissText = alarm.instanceState == AlarmInstance.SNOOZE_STATE 108 ? context.getString(R.string.alarm_alert_snooze_until, 109 AlarmUtils.getAlarmText(context, alarmInstance, false)) 110 : context.getString(R.string.alarm_alert_dismiss_now_text); 111 preemptiveDismissButton.setText(dismissText); 112 } else { 113 preemptiveDismissContainer.setVisibility(View.GONE); 114 } 115 return canBind; 116 } 117 } 118