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