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