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 public class Stopwatches {
     26     // Private actions processed by the receiver
     27     public static final String START_STOPWATCH = "start_stopwatch";
     28     public static final String LAP_STOPWATCH = "lap_stopwatch";
     29     public static final String STOP_STOPWATCH = "stop_stopwatch";
     30     public static final String RESET_STOPWATCH = "reset_stopwatch";
     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     private static String[] mFormats = null;
     55 
     56     public static String getShareTitle(Context context) {
     57         String [] mLabels = context.getResources().getStringArray(R.array.sw_share_strings);
     58         return mLabels[(int)(Math.random() * mLabels.length)];
     59     }
     60 
     61     public static String buildShareResults(Context context, String time, long[] laps) {
     62         StringBuilder b = new StringBuilder (context.getString(R.string.sw_share_main, time));
     63         b.append("\n");
     64 
     65         int lapsNum = laps == null? 0 : laps.length;
     66         if (lapsNum == 0) {
     67             return b.toString();
     68         }
     69 
     70         b.append(context.getString(R.string.sw_share_laps));
     71         b.append("\n");
     72         for (int i = 1; i <= lapsNum; i ++) {
     73             b.append(getTimeText(context, laps[lapsNum-i], i));
     74             b.append("\n");
     75         }
     76         return b.toString();
     77     }
     78 
     79     public static String buildShareResults(Context context, long time, long[] laps) {
     80         return buildShareResults(context, getTimeText(context, time, NO_LAP_NUMBER), laps);
     81     }
     82 
     83     /***
     84      * Sets the string of the time running on the stopwatch up to hundred of a second accuracy
     85      * @param time - in hundreds of a second since the stopwatch started
     86      */
     87     public static String getTimeText(Context context, long time, int lap) {
     88         if (time < 0) {
     89             time = 0;
     90         }
     91         if (lap != NO_LAP_NUMBER) {
     92             mFormats = context.getResources().getStringArray(R.array.shared_laps_format_set);
     93         } else {
     94             mFormats = context.getResources().getStringArray(R.array.stopwatch_format_set);
     95         }
     96         char decimalSeparator = DecimalFormatSymbols.getInstance().getDecimalSeparator();
     97         int formatIndex = 0;
     98 
     99         long hundreds, seconds, minutes, hours;
    100         seconds = time / 1000;
    101         hundreds = (time - seconds * 1000) / 10;
    102         minutes = seconds / 60;
    103         seconds = seconds - minutes * 60;
    104         hours = minutes / 60;
    105         minutes = minutes - hours * 60;
    106         if (hours >= 100) {
    107           formatIndex = 4;
    108         } else if (hours >= 10) {
    109             formatIndex = 3;
    110         } else if (hours > 0) {
    111           formatIndex = 2;
    112         } else if (minutes >= 10) {
    113           formatIndex = 1;
    114         } else {
    115           formatIndex = 0;
    116         }
    117         return String.format(mFormats[formatIndex], hours, minutes,
    118                 seconds, hundreds, decimalSeparator, lap);
    119     }
    120 
    121     /***
    122      * Sets the string of the time running on the stopwatch up to hundred of a second accuracy
    123      * @param time - in hundreds of a second since the stopwatch started
    124      */
    125     public static String formatTimeText(long time, final String format) {
    126         if (time < 0) {
    127             time = 0;
    128         }
    129         long hundreds, seconds, minutes, hours;
    130         seconds = time / 1000;
    131         hundreds = (time - seconds * 1000) / 10;
    132         minutes = seconds / 60;
    133         seconds = seconds - minutes * 60;
    134         hours = minutes / 60;
    135         minutes = minutes - hours * 60;
    136         char decimalSeparator = DecimalFormatSymbols.getInstance().getDecimalSeparator();
    137         String timeStr = String.format(format, hours, minutes, seconds, hundreds, decimalSeparator);
    138         return timeStr;
    139     }
    140 }
    141