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.view.View;
21 import android.widget.CompoundButton;
22 import android.widget.ImageView;
23 import android.widget.TextView;
24 
25 import com.android.deskclock.AlarmUtils;
26 import com.android.deskclock.ItemAdapter;
27 import com.android.deskclock.ItemAnimator;
28 import com.android.deskclock.R;
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 AlarmItemViewHolder extends ItemAdapter.ItemViewHolder<AlarmItemHolder>
37         implements ItemAnimator.OnAnimateChangeListener {
38 
39     private static final float CLOCK_ENABLED_ALPHA = 1f;
40     private static final float CLOCK_DISABLED_ALPHA = 0.69f;
41 
42     public static final float ANIM_STANDARD_DELAY_MULTIPLIER = 1f / 6f;
43     public static final float ANIM_LONG_DURATION_MULTIPLIER = 2f / 3f;
44     public static final float ANIM_SHORT_DURATION_MULTIPLIER = 1f / 4f;
45     public static final float ANIM_SHORT_DELAY_INCREMENT_MULTIPLIER =
46             1f - ANIM_LONG_DURATION_MULTIPLIER - ANIM_SHORT_DURATION_MULTIPLIER;
47     public static final float ANIM_LONG_DELAY_INCREMENT_MULTIPLIER =
48             1f - ANIM_STANDARD_DELAY_MULTIPLIER - ANIM_SHORT_DURATION_MULTIPLIER;
49 
50     public static final String ANIMATE_REPEAT_DAYS = "ANIMATE_REPEAT_DAYS";
51 
52     public final TextTime clock;
53     public final CompoundButton onOff;
54     public final ImageView arrow;
55     public final TextView preemptiveDismissButton;
56 
AlarmItemViewHolder(View itemView)57     public AlarmItemViewHolder(View itemView) {
58         super(itemView);
59 
60         clock = (TextTime) itemView.findViewById(R.id.digital_clock);
61         onOff = (CompoundButton) itemView.findViewById(R.id.onoff);
62         arrow = (ImageView) itemView.findViewById(R.id.arrow);
63         preemptiveDismissButton =
64                 (TextView) itemView.findViewById(R.id.preemptive_dismiss_button);
65         preemptiveDismissButton.setOnClickListener(new View.OnClickListener() {
66             @Override
67             public void onClick(View v) {
68                 final AlarmInstance alarmInstance = getItemHolder().getAlarmInstance();
69                 if (alarmInstance != null) {
70                     getItemHolder().getAlarmTimeClickHandler().dismissAlarmInstance(alarmInstance);
71                 }
72             }
73         });
74         onOff.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
75             @Override
76             public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
77                 getItemHolder().getAlarmTimeClickHandler().setAlarmEnabled(
78                         getItemHolder().item, checked);
79             }
80         });
81     }
82 
83     @Override
onBindItemView(final AlarmItemHolder itemHolder)84     protected void onBindItemView(final AlarmItemHolder itemHolder) {
85         final Alarm alarm = itemHolder.item;
86         bindOnOffSwitch(alarm);
87         bindClock(alarm);
88         final Context context = itemView.getContext();
89         itemView.setContentDescription(clock.getText() + " " + alarm.getLabelOrDefault(context));
90     }
91 
bindOnOffSwitch(Alarm alarm)92     protected void bindOnOffSwitch(Alarm alarm) {
93         if (onOff.isChecked() != alarm.enabled) {
94             onOff.setChecked(alarm.enabled);
95         }
96     }
97 
bindClock(Alarm alarm)98     protected void bindClock(Alarm alarm) {
99         clock.setTime(alarm.hour, alarm.minutes);
100         clock.setAlpha(alarm.enabled ? CLOCK_ENABLED_ALPHA : CLOCK_DISABLED_ALPHA);
101     }
102 
bindPreemptiveDismissButton(Context context, Alarm alarm, AlarmInstance alarmInstance)103     protected boolean bindPreemptiveDismissButton(Context context, Alarm alarm,
104             AlarmInstance alarmInstance) {
105         final boolean canBind = alarm.canPreemptivelyDismiss() && alarmInstance != null;
106         if (canBind) {
107             preemptiveDismissButton.setVisibility(View.VISIBLE);
108             final String dismissText = alarm.instanceState == AlarmInstance.SNOOZE_STATE
109                     ? context.getString(R.string.alarm_alert_snooze_until,
110                             AlarmUtils.getAlarmText(context, alarmInstance, false))
111                     : context.getString(R.string.alarm_alert_dismiss_text);
112             preemptiveDismissButton.setText(dismissText);
113             preemptiveDismissButton.setClickable(true);
114         } else {
115             preemptiveDismissButton.setVisibility(View.GONE);
116             preemptiveDismissButton.setClickable(false);
117         }
118         return canBind;
119     }
120 }