Skip to content

Most visited

Recently visited

navigation
Timer / src / com.example.android.wearable.timer / util /

TimerFormat.java

1
/*
2
 * Copyright (C) 2014 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.example.android.wearable.timer.util;
18
 
19
/** Helper class to format the timer. Based on com.android.deskclock.timer.CountingTimerView. */
20
public final class TimerFormat {
21
 
22
    private static final String TWO_DIGITS = "%02d";
23
 
24
    private static final String ONE_DIGIT = "%01d";
25
 
26
    private static final String NEG_TWO_DIGITS = "-%02d";
27
 
28
    private static final String NEG_ONE_DIGIT = "-%01d";
29
 
30
    private static String mHours;
31
 
32
    private static String mMinutes;
33
 
34
    private static String mSeconds;
35
 
36
    private TimerFormat() {
37
 
38
    }
39
 
40
    /**
41
     * Update the time to display. Separates that time into the hours, minutes, seconds.
42
     * Copied and shortened from com.android.deskclock.timer.CountingTimerView.
43
     *
44
     * @param time new time to display - in milliseconds
45
     */
46
    private static void setTime(long time) {
47
        boolean neg = false;
48
        boolean showNeg = false;
49
        String format;
50
        if (time < 0) {
51
            time = -time;
52
            neg = showNeg = true;
53
        }
54
        long seconds = time / 1000;
55
        long hundreds = (time - seconds * 1000) / 10;
56
        long minutes = seconds / 60;
57
        seconds = seconds - minutes * 60;
58
        long hours = minutes / 60;
59
        minutes = minutes - hours * 60;
60
        if (hours > 999) {
61
            hours = 0;
62
        }
63
        // The time  can be between 0 and -1 seconds, but the "truncated" equivalent time of hours
64
        // and minutes and seconds could be zero, so since we do not show fractions of seconds
65
        // when counting down, do not show the minus sign.
66
        if (hours == 0 && minutes == 0 && seconds == 0) {
67
            showNeg = false;
68
        }
69
 
70
        // Normalize and check if it is 'time' to invalidate
71
        if (!neg && hundreds != 0) {
72
            seconds++;
73
            if (seconds == 60) {
74
                seconds = 0;
75
                minutes++;
76
                if (minutes == 60) {
77
                    minutes = 0;
78
                    hours++;
79
                }
80
            }
81
        }
82
 
83
        // Hours may be empty
84
        if (hours >= 10) {
85
            format = showNeg ? NEG_TWO_DIGITS : TWO_DIGITS;
86
            mHours = String.format(format, hours);
87
        } else if (hours > 0) {
88
            format = showNeg ? NEG_ONE_DIGIT : ONE_DIGIT;
89
            mHours = String.format(format, hours);
90
        } else {
91
            mHours = null;
92
        }
93
 
94
        // Minutes are never empty and when hours are non-empty, must be two digits
95
        if (minutes >= 10 || hours > 0) {
96
            format = (showNeg && hours == 0) ? NEG_TWO_DIGITS : TWO_DIGITS;
97
            mMinutes = String.format(format, minutes);
98
        } else {
99
            format = (showNeg && hours == 0) ? NEG_ONE_DIGIT : ONE_DIGIT;
100
            mMinutes = String.format(format, minutes);
101
        }
102
 
103
        // Seconds are always two digits
104
        mSeconds = String.format(TWO_DIGITS, seconds);
105
    }
106
 
107
    /**
108
     * Based on com.android.deskclock.timer.CountingTimerView.
109
     *
110
     * @param time the time to format.
111
     * @return nicely formatted time.
112
     */
113
    public static String getTimeString(long time) {
114
        setTime(time);
115
        if (mHours == null) {
116
            return String.format("%s:%s", mMinutes, mSeconds);
117
        }
118
        return String.format("%s:%s:%s", mHours, mMinutes, mSeconds);
119
 
120
    }
121
}
This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.