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 import java.text.DecimalFormatSymbols; 24 25 /** 26 * Stopwatch utility class providing access to stopwatch resources and data formatting strings of 27 * stopwatch data. 28 */ 29 public class Stopwatches { 30 // Actions processed by stopwatch receiver 31 public static final String START_STOPWATCH = "start_stopwatch"; 32 public static final String LAP_STOPWATCH = "lap_stopwatch"; 33 public static final String STOP_STOPWATCH = "stop_stopwatch"; 34 public static final String RESET_STOPWATCH = "reset_stopwatch"; 35 public static final String SHARE_STOPWATCH = "share_stopwatch"; 36 public static final String RESET_AND_LAUNCH_STOPWATCH = "reset_and_launch_stopwatch"; 37 public static final String MESSAGE_TIME = "message_time"; 38 public static final String SHOW_NOTIF = "show_notification"; 39 public static final String KILL_NOTIF = "kill_notification"; 40 public static final String PREF_START_TIME = "sw_start_time"; 41 public static final String PREF_ACCUM_TIME = "sw_accum_time"; 42 public static final String PREF_STATE = "sw_state"; 43 public static final String PREF_LAP_NUM = "sw_lap_num"; 44 public static final String PREF_LAP_TIME = "sw_lap_time_"; 45 public static final String PREF_UPDATE_CIRCLE = "sw_update_circle"; 46 public static final String NOTIF_CLOCK_BASE = "notif_clock_base"; 47 public static final String NOTIF_CLOCK_ELAPSED = "notif_clock_elapsed"; 48 public static final String NOTIF_CLOCK_RUNNING = "notif_clock_running"; 49 public static final String KEY = "sw"; 50 51 public static final int STOPWATCH_RESET = 0; 52 public static final int STOPWATCH_RUNNING = 1; 53 public static final int STOPWATCH_STOPPED = 2; 54 55 public static final int MAX_LAPS = 99; 56 public static final int NO_LAP_NUMBER = -1; 57 58 /** 59 * Pull a random jocular title 60 * @param context context with resources 61 * @return the random title 62 */ getShareTitle(Context context)63 public static String getShareTitle(Context context) { 64 String [] mLabels = context.getResources().getStringArray(R.array.sw_share_strings); 65 return mLabels[(int)(Math.random() * mLabels.length)]; 66 } 67 68 /** 69 * Create a multi-line text with the stopwatch lap data 70 * @param context context with resources 71 * @param time total elapsed time 72 * @param laps array of times 73 * @return formatted text 74 */ buildShareResults(Context context, String time, long[] laps)75 public static String buildShareResults(Context context, String time, long[] laps) { 76 StringBuilder b = new StringBuilder (context.getString(R.string.sw_share_main, time)); 77 b.append("\n"); 78 79 int lapsNum = laps == null? 0 : laps.length; 80 if (lapsNum == 0) { 81 return b.toString(); 82 } 83 84 b.append(context.getString(R.string.sw_share_laps)); 85 b.append("\n"); 86 for (int i = 1; i <= lapsNum; i ++) { 87 b.append(getTimeText(context, laps[lapsNum-i], i)); 88 b.append("\n"); 89 } 90 return b.toString(); 91 } 92 93 /** 94 * Create a multi-line text with the stopwatch lap data 95 * @param context context with resources 96 * @param time total elapsed time 97 * @param laps array of times 98 * @return formatted text 99 */ buildShareResults(Context context, long time, long[] laps)100 public static String buildShareResults(Context context, long time, long[] laps) { 101 return buildShareResults(context, getTimeText(context, time, NO_LAP_NUMBER), laps); 102 } 103 104 /*** 105 * Format the string of the time running on the stopwatch up to hundred of a second accuracy 106 * @param context context with resources 107 * @param time - in hundreds of a second since the stopwatch started 108 * @param lap lap number 109 * @return formatted text 110 */ getTimeText(Context context, long time, int lap)111 public static String getTimeText(Context context, long time, int lap) { 112 if (time < 0) { 113 time = 0; 114 } 115 String[] formats; 116 if (lap != NO_LAP_NUMBER) { 117 formats = context.getResources().getStringArray(R.array.shared_laps_format_set); 118 } else { 119 formats = context.getResources().getStringArray(R.array.stopwatch_format_set); 120 } 121 char decimalSeparator = DecimalFormatSymbols.getInstance().getDecimalSeparator(); 122 int formatIndex; 123 124 long hundreds, seconds, minutes, hours; 125 seconds = time / 1000; 126 hundreds = (time - seconds * 1000) / 10; 127 minutes = seconds / 60; 128 seconds = seconds - minutes * 60; 129 hours = minutes / 60; 130 minutes = minutes - hours * 60; 131 if (hours >= 100) { 132 formatIndex = 4; 133 } else if (hours >= 10) { 134 formatIndex = 3; 135 } else if (hours > 0) { 136 formatIndex = 2; 137 } else if (minutes >= 10) { 138 formatIndex = 1; 139 } else { 140 formatIndex = 0; 141 } 142 return String.format(formats[formatIndex], hours, minutes, 143 seconds, hundreds, decimalSeparator, lap); 144 } 145 146 /*** 147 * Sets the string of the time running on the stopwatch up to hundred of a second accuracy 148 * @param time - in hundreds of a second since the stopwatch started 149 */ formatTimeText(long time, final String format)150 public static String formatTimeText(long time, final String format) { 151 if (time < 0) { 152 time = 0; 153 } 154 long hundreds, seconds, minutes, hours; 155 seconds = time / 1000; 156 hundreds = (time - seconds * 1000) / 10; 157 minutes = seconds / 60; 158 seconds = seconds - minutes * 60; 159 hours = minutes / 60; 160 minutes = minutes - hours * 60; 161 char decimalSeparator = DecimalFormatSymbols.getInstance().getDecimalSeparator(); 162 return String.format(format, hours, minutes, seconds, hundreds, decimalSeparator); 163 } 164 } 165