1 /* 2 * 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.timer 18 19 import android.os.Bundle 20 import android.view.LayoutInflater 21 import android.view.View 22 import android.view.ViewGroup 23 import androidx.fragment.app.Fragment 24 25 import com.android.deskclock.LabelDialogFragment 26 import com.android.deskclock.R 27 import com.android.deskclock.data.DataModel 28 import com.android.deskclock.data.Timer 29 import com.android.deskclock.data.TimerStringFormatter 30 import com.android.deskclock.events.Events 31 32 /** The public no-arg constructor required by all fragments. */ 33 class TimerItemFragment : Fragment() { 34 var timerId = 0 35 private set 36 onCreatenull37 override fun onCreate(savedInstanceState: Bundle?) { 38 super.onCreate(savedInstanceState) 39 40 timerId = requireArguments().getInt(KEY_TIMER_ID) 41 } 42 onCreateViewnull43 override fun onCreateView( 44 inflater: LayoutInflater, 45 container: ViewGroup?, 46 savedInstanceState: Bundle? 47 ): View? { 48 val timer = timer ?: return null 49 50 val view = inflater.inflate(R.layout.timer_item, container, false) as TimerItem 51 view.findViewById<View>(R.id.reset_add).setOnClickListener(ResetAddListener()) 52 view.findViewById<View>(R.id.timer_label).setOnClickListener(EditLabelListener()) 53 view.findViewById<View>(R.id.timer_time_text).setOnClickListener(TimeTextListener()) 54 view.update(timer) 55 56 return view 57 } 58 59 /** 60 * @return `true` iff the timer is in a state that requires continuous updates 61 */ updateTimenull62 fun updateTime(): Boolean { 63 val view = view as TimerItem? 64 if (view != null) { 65 val timer = timer!! 66 view.update(timer) 67 return !timer.isReset 68 } 69 70 return false 71 } 72 73 val timer: Timer? 74 get() = DataModel.dataModel.getTimer(timerId) 75 76 private inner class ResetAddListener : View.OnClickListener { onClicknull77 override fun onClick(v: View) { 78 val timer = timer!! 79 if (timer.isPaused) { 80 DataModel.dataModel.resetOrDeleteTimer(timer, R.string.label_deskclock) 81 } else if (timer.isRunning || timer.isExpired || timer.isMissed) { 82 DataModel.dataModel.addTimerMinute(timer) 83 Events.sendTimerEvent(R.string.action_add_minute, R.string.label_deskclock) 84 85 val context = v.context 86 // Must re-retrieve timer because old timer is no longer accurate. 87 val currentTime: Long = this@TimerItemFragment.timer!!.remainingTime 88 if (currentTime > 0) { 89 v.announceForAccessibility(TimerStringFormatter.formatString( 90 context, R.string.timer_accessibility_one_minute_added, currentTime, 91 true)) 92 } 93 } 94 } 95 } 96 97 private inner class EditLabelListener : View.OnClickListener { onClicknull98 override fun onClick(v: View) { 99 val fragment = LabelDialogFragment.newInstance(timer!!) 100 LabelDialogFragment.show(parentFragmentManager, fragment) 101 } 102 } 103 104 private inner class TimeTextListener : View.OnClickListener { onClicknull105 override fun onClick(view: View) { 106 val clickedTimer = timer!! 107 if (clickedTimer.isPaused || clickedTimer.isReset) { 108 DataModel.dataModel.startTimer(clickedTimer) 109 } else if (clickedTimer.isRunning) { 110 DataModel.dataModel.pauseTimer(clickedTimer) 111 } 112 } 113 } 114 115 companion object { 116 private const val KEY_TIMER_ID = "KEY_TIMER_ID" 117 newInstancenull118 fun newInstance(timer: Timer): TimerItemFragment { 119 val fragment = TimerItemFragment() 120 val args = Bundle() 121 args.putInt(KEY_TIMER_ID, timer.id) 122 fragment.arguments = args 123 return fragment 124 } 125 } 126 }