• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2020 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.ItemViewHolder
27 import com.android.deskclock.ItemAnimator.OnAnimateChangeListener
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.provider.ClockContract.InstancesColumns
32 import com.android.deskclock.widget.TextTime
33 
34 /**
35  * Abstract ViewHolder for alarm time items.
36  */
37 abstract class AlarmItemViewHolder(itemView: View)
38     : ItemViewHolder<AlarmItemHolder>(itemView), OnAnimateChangeListener {
39     val clock: TextTime = itemView.findViewById(R.id.digital_clock)
40     val onOff: CompoundButton = itemView.findViewById(R.id.onoff) as CompoundButton
41     val arrow: ImageView = itemView.findViewById(R.id.arrow) as ImageView
42     val preemptiveDismissButton: TextView =
43             itemView.findViewById(R.id.preemptive_dismiss_button) as TextView
44 
45     init {
46         preemptiveDismissButton.setOnClickListener { _ ->
47             val alarmInstance = itemHolder!!.alarmInstance
48             if (alarmInstance != null) {
49                 itemHolder!!.alarmTimeClickHandler.dismissAlarmInstance(alarmInstance)
50             }
51         }
52         onOff.setOnCheckedChangeListener { _, checked ->
53             itemHolder!!.alarmTimeClickHandler.setAlarmEnabled(itemHolder!!.item, checked)
54         }
55     }
56 
57     override fun onBindItemView(itemHolder: AlarmItemHolder) {
58         val alarm = itemHolder.item
59         bindOnOffSwitch(alarm)
60         bindClock(alarm)
61         val context: Context = itemView.getContext()
62         itemView.setContentDescription(clock.text.toString() + " " +
63                 alarm.getLabelOrDefault(context))
64     }
65 
66     protected fun bindOnOffSwitch(alarm: Alarm) {
67         if (onOff.isChecked() != alarm.enabled) {
68             onOff.isChecked = alarm.enabled
69         }
70     }
71 
72     protected fun bindClock(alarm: Alarm) {
73         clock.setTime(alarm.hour, alarm.minutes)
74         clock.alpha = if (alarm.enabled) CLOCK_ENABLED_ALPHA else CLOCK_DISABLED_ALPHA
75     }
76 
77     protected fun bindPreemptiveDismissButton(
78         context: Context,
79         alarm: Alarm,
80         alarmInstance: AlarmInstance?
81     ): Boolean {
82         val canBind = alarm.canPreemptivelyDismiss() && alarmInstance != null
83         if (canBind) {
84             preemptiveDismissButton.visibility = View.VISIBLE
85             val dismissText: String = if (alarm.instanceState == InstancesColumns.SNOOZE_STATE) {
86                 context.getString(R.string.alarm_alert_snooze_until,
87                         AlarmUtils.getAlarmText(context, alarmInstance!!, false))
88             } else {
89                 context.getString(R.string.alarm_alert_dismiss_text)
90             }
91             preemptiveDismissButton.text = dismissText
92             preemptiveDismissButton.isClickable = true
93         } else {
94             preemptiveDismissButton.visibility = View.GONE
95             preemptiveDismissButton.isClickable = false
96         }
97         return canBind
98     }
99 
100     companion object {
101         private const val CLOCK_ENABLED_ALPHA = 1f
102         private const val CLOCK_DISABLED_ALPHA = 0.69f
103 
104         const val ANIM_STANDARD_DELAY_MULTIPLIER = 1f / 6f
105         const val ANIM_LONG_DURATION_MULTIPLIER = 2f / 3f
106         const val ANIM_SHORT_DURATION_MULTIPLIER = 1f / 4f
107         const val ANIM_SHORT_DELAY_INCREMENT_MULTIPLIER =
108                 1f - ANIM_LONG_DURATION_MULTIPLIER - ANIM_SHORT_DURATION_MULTIPLIER
109         const val ANIM_LONG_DELAY_INCREMENT_MULTIPLIER =
110                 1f - ANIM_STANDARD_DELAY_MULTIPLIER - ANIM_SHORT_DURATION_MULTIPLIER
111         const val ANIMATE_REPEAT_DAYS = "ANIMATE_REPEAT_DAYS"
112     }
113 }