• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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.stopwatch;
18 
19 import android.content.Context;
20 
21 import com.android.deskclock.R;
22 
23 public class Stopwatches {
24     // Private actions processed by the receiver
25     public static final String START_STOPWATCH = "start_stopwatch";
26     public static final String LAP_STOPWATCH = "lap_stopwatch";
27     public static final String STOP_STOPWATCH = "stop_stopwatch";
28     public static final String RESET_STOPWATCH = "reset_stopwatch";
29     public static final String SHARE_STOPWATCH = "share_stopwatch";
30     public static final String RESET_AND_LAUNCH_STOPWATCH = "reset_and_launch_stopwatch";
31     public static final String MESSAGE_TIME = "message_time";
32     public static final String SHOW_NOTIF = "show_notification";
33     public static final String KILL_NOTIF = "kill_notification";
34     public static final String PREF_START_TIME  = "sw_start_time";
35     public static final String PREF_ACCUM_TIME = "sw_accum_time";
36     public static final String PREF_STATE = "sw_state";
37     public static final String PREF_LAP_NUM = "sw_lap_num";
38     public static final String PREF_LAP_TIME = "sw_lap_time_";
39     public static final String PREF_UPDATE_CIRCLE = "sw_update_circle";
40     public static final String NOTIF_CLOCK_BASE = "notif_clock_base";
41     public static final String NOTIF_CLOCK_ELAPSED = "notif_clock_elapsed";
42     public static final String NOTIF_CLOCK_RUNNING = "notif_clock_running";
43     public static final String KEY = "sw";
44 
45     public static final int STOPWATCH_RESET = 0;
46     public static final int STOPWATCH_RUNNING = 1;
47     public static final int STOPWATCH_STOPPED = 2;
48 
49     public static final int MAX_LAPS = 99;
50 
getShareTitle(Context context)51     public static String getShareTitle(Context context) {
52         String [] mLabels = context.getResources().getStringArray(R.array.sw_share_strings);
53         return mLabels[(int)(Math.random() * mLabels.length)];
54     }
55 
buildShareResults(Context context, String time, long[] laps)56     public static String buildShareResults(Context context, String time, long[] laps) {
57         String results = context.getString(R.string.sw_share_main, time + "\n");
58         int lapsNum = laps == null? 0 : laps.length;
59         if (lapsNum == 0) {
60             return results;
61         }
62         results += context.getString(R.string.sw_share_laps) + "\n";
63         for (int i = 1; i <= lapsNum; i ++) {
64             results += String.format("%d. %s\n", i, getTimeText(laps[lapsNum-i]));
65         }
66         return results;
67     }
68 
buildShareResults(Context context, long time, long[] laps)69     public static String buildShareResults(Context context, long time, long[] laps) {
70         return buildShareResults(context, getTimeText(time), laps);
71     }
72 
73     /***
74      * Sets the string of the time running on the stopwatch up to hundred of a second accuracy
75      * @param time - in hundreds of a second since the stopwatch started
76      */
getTimeText(long time)77     public static String getTimeText(long time) {
78         if (time < 0) {
79             time = 0;
80         }
81         long hundreds, seconds, minutes, hours;
82         seconds = time / 1000;
83         hundreds = (time - seconds * 1000) / 10;
84         minutes = seconds / 60;
85         seconds = seconds - minutes * 60;
86         hours = minutes / 60;
87         minutes = minutes - hours * 60;
88         if (hours > 99) {
89             hours = 0;
90         }
91         // TODO: must build to account for localization
92         String timeStr;
93         if (hours >= 10) {
94             timeStr = String.format("%02dh %02dm %02ds .%02d", hours, minutes,
95                     seconds, hundreds);
96         } else if (hours > 0) {
97             timeStr = String.format("%01dh %02dm %02ds .%02d", hours, minutes,
98                     seconds, hundreds);
99         } else if (minutes >= 10) {
100             timeStr = String.format("%02dm %02ds .%02d", minutes, seconds,
101                     hundreds);
102         } else {
103             timeStr = String.format("%02dm %02ds .%02d", minutes, seconds,
104                     hundreds);
105         }
106         return timeStr;
107     }
108 
109     /***
110      * Sets the string of the time running on the stopwatch up to hundred of a second accuracy
111      * @param time - in hundreds of a second since the stopwatch started
112      */
formatTimeText(long time, final String format)113     public static String formatTimeText(long time, final String format) {
114         if (time < 0) {
115             time = 0;
116         }
117         long hundreds, seconds, minutes, hours;
118         seconds = time / 1000;
119         hundreds = (time - seconds * 1000) / 10;
120         minutes = seconds / 60;
121         seconds = seconds - minutes * 60;
122         hours = minutes / 60;
123         minutes = minutes - hours * 60;
124         String timeStr = String.format(format, hours, minutes, seconds, hundreds);
125         return timeStr;
126     }
127 
128 }
129