1 /*
2  * Copyright (C) 2016 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.content.Context;
20 import android.widget.TextView;
21 
22 import com.android.deskclock.uidata.UiDataModel;
23 
24 import static android.text.format.DateUtils.HOUR_IN_MILLIS;
25 import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
26 import static android.text.format.DateUtils.SECOND_IN_MILLIS;
27 
28 /**
29  * A controller which will format a provided time in millis to display as a stopwatch.
30  */
31 public final class StopwatchTextController {
32 
33     private final TextView mMainTextView;
34     private final TextView mHundredthsTextView;
35 
36     private long mLastTime = Long.MIN_VALUE;
37 
StopwatchTextController(TextView mainTextView, TextView hundredthsTextView)38     public StopwatchTextController(TextView mainTextView, TextView hundredthsTextView) {
39         mMainTextView = mainTextView;
40         mHundredthsTextView = hundredthsTextView;
41     }
42 
setTimeString(long accumulatedTime)43     public void setTimeString(long accumulatedTime) {
44         // Since time is only displayed to centiseconds, if there is a change at the milliseconds
45         // level but not the centiseconds level, we can avoid unnecessary work.
46         if ((mLastTime / 10) == (accumulatedTime / 10)) {
47             return;
48         }
49 
50         final int hours = (int) (accumulatedTime / HOUR_IN_MILLIS);
51         int remainder = (int) (accumulatedTime % HOUR_IN_MILLIS);
52 
53         final int minutes = (int) (remainder / MINUTE_IN_MILLIS);
54         remainder = (int) (remainder % MINUTE_IN_MILLIS);
55 
56         final int seconds = (int) (remainder / SECOND_IN_MILLIS);
57         remainder = (int) (remainder % SECOND_IN_MILLIS);
58 
59         mHundredthsTextView.setText(UiDataModel.getUiDataModel().getFormattedNumber(
60                 remainder / 10, 2));
61 
62         // Avoid unnecessary computations and garbage creation if seconds have not changed since
63         // last layout pass.
64         if ((mLastTime / SECOND_IN_MILLIS) != (accumulatedTime / SECOND_IN_MILLIS)) {
65             final Context context = mMainTextView.getContext();
66             final String time = Utils.getTimeString(context, hours, minutes, seconds);
67             mMainTextView.setText(time);
68         }
69         mLastTime = accumulatedTime;
70     }
71 }
72