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
18 
19 import android.text.format.DateUtils
20 import android.widget.TextView
21 
22 /**
23  * A controller which will format a provided time in millis to display as a timer.
24  */
25 class TimerTextController(private val mTextView: TextView) {
setTimeStringnull26     fun setTimeString(remainingTime: Long) {
27         var variableRemainingTime = remainingTime
28         var isNegative = false
29         if (variableRemainingTime < 0) {
30             variableRemainingTime = -variableRemainingTime
31             isNegative = true
32         }
33 
34         var hours = (variableRemainingTime / DateUtils.HOUR_IN_MILLIS).toInt()
35         var remainder = (variableRemainingTime % DateUtils.HOUR_IN_MILLIS).toInt()
36 
37         var minutes = (remainder / DateUtils.MINUTE_IN_MILLIS).toInt()
38         remainder = (remainder % DateUtils.MINUTE_IN_MILLIS).toInt()
39 
40         var seconds = (remainder / DateUtils.SECOND_IN_MILLIS).toInt()
41         remainder = (remainder % DateUtils.SECOND_IN_MILLIS).toInt()
42 
43         // Round up to the next second
44         if (!isNegative && remainder != 0) {
45             seconds++
46             if (seconds == 60) {
47                 seconds = 0
48                 minutes++
49                 if (minutes == 60) {
50                     minutes = 0
51                     hours++
52                 }
53             }
54         }
55 
56         var time = Utils.getTimeString(mTextView.context, hours, minutes, seconds)
57         if (isNegative && !(hours == 0 && minutes == 0 && seconds == 0)) {
58             time = "\u2212" + time
59         }
60 
61         mTextView.text = time
62     }
63 }