Fix formatting of shared laps info

Bug: 8555992
Change-Id: I1d211ace1db81f32e42dfe1e25637dc27251a9fa
diff --git a/res/values/strings.xml b/res/values/strings.xml
index cbaedb8..a068bbc 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -364,8 +364,7 @@
     <string name="sw_share_laps">Lap times:</string>
     <!-- Label to enumerate the number of laps in the notification the user has counted -->
     <string name="sw_notification_lap_number">Lap <xliff:g id="number">%d</xliff:g></string>
-    <!-- Formatted stopwatch time
-        [CHAR LIMIT=1] for each abbreviation as in hours_label, etc. -->
+    <!-- Formatted stopwatch time -->
     <string-array name="stopwatch_format_set" translatable="false">
         <item> <!-- <10 minutes - example "1 02.03" -->
             <xliff:g id="M">%2$d</xliff:g> &#x200E;<xliff:g id="S">%3$02d</xliff:g><xliff:g id="decimal_separator">%5$c</xliff:g><xliff:g id="D">%4$02d</xliff:g>
@@ -382,7 +381,27 @@
         <item> <!-- <1000 hours - example "123 04 05.06" -->
             <xliff:g id="H">%1$03d</xliff:g> &#x200E;<xliff:g id="M">%2$02d</xliff:g> &#x200E;<xliff:g id="S">%3$02d</xliff:g><xliff:g id="decimal_separator">%5$c</xliff:g><xliff:g id="D">%4$02d</xliff:g>
         </item>
-        </string-array>
+    </string-array>
+
+    <!-- Formatted stopwatch time for laps shared using the share button -->
+    <string-array name="shared_laps_format_set" translatable="false">
+        <item> <!-- <10 minutes - example "1 02.03" -->
+            <xliff:g id="lap">%6$d</xliff:g><xliff:g id="decimal_separator">%5$c</xliff:g> &#x200E;<xliff:g id="M">%2$d</xliff:g> &#x200E;<xliff:g id="S">%3$02d</xliff:g><xliff:g id="decimal_separator">%5$c</xliff:g><xliff:g id="D">%4$02d</xliff:g>
+        </item>
+        <item> <!-- <1 hour - example "12 03.04" -->
+            <xliff:g id="lap">%6$d</xliff:g><xliff:g id="decimal_separator">%5$c</xliff:g> &#x200E;<xliff:g id="M">%2$02d</xliff:g> &#x200E;<xliff:g id="S">%3$02d</xliff:g><xliff:g id="decimal_separator">%5$c</xliff:g><xliff:g id="D">%4$02d</xliff:g>
+        </item>
+        <item> <!-- <10 hours - example "1 02 03.04" -->
+            <xliff:g id="lap">%6$d</xliff:g><xliff:g id="decimal_separator">%5$c</xliff:g> &#x200E;<xliff:g id="H">%1$d</xliff:g> &#x200E;<xliff:g id="M">%2$02d</xliff:g> &#x200E;<xliff:g id="S">%3$02d</xliff:g><xliff:g id="decimal_separator">%5$c</xliff:g><xliff:g id="D">%4$02d</xliff:g>
+        </item>
+        <item> <!-- <100 hours - example "12 03 04.05" -->
+            <xliff:g id="lap">%6$d</xliff:g><xliff:g id="decimal_separator">%5$c</xliff:g> &#x200E;<xliff:g id="H">%1$02d</xliff:g> &#x200E;<xliff:g id="M">%2$02d</xliff:g> &#x200E;<xliff:g id="S">%3$02d</xliff:g><xliff:g id="decimal_separator">%5$c</xliff:g><xliff:g id="D">%4$02d</xliff:g>
+        </item>
+        <item> <!-- <1000 hours - example "123 04 05.06" -->
+            <xliff:g id="lap">%6$d</xliff:g><xliff:g id="decimal_separator">%5$c</xliff:g> &#x200E;<xliff:g id="H">%1$03d</xliff:g> &#x200E;<xliff:g id="M">%2$02d</xliff:g> &#x200E;<xliff:g id="S">%3$02d</xliff:g><xliff:g id="decimal_separator">%5$c</xliff:g><xliff:g id="D">%4$02d</xliff:g>
+        </item>
+    </string-array>
+
     <!-- Label to enumerate the number of laps the user has counted -->
     <string-array name="sw_lap_number_set" translatable="false">
         <item># <xliff:g id="number">%d</xliff:g></item>
diff --git a/src/com/android/deskclock/stopwatch/Stopwatches.java b/src/com/android/deskclock/stopwatch/Stopwatches.java
index 70a37ae..a7d55d1 100644
--- a/src/com/android/deskclock/stopwatch/Stopwatches.java
+++ b/src/com/android/deskclock/stopwatch/Stopwatches.java
@@ -49,6 +49,9 @@
     public static final int STOPWATCH_STOPPED = 2;
 
     public static final int MAX_LAPS = 99;
+    public static final int NO_LAP_NUMBER = -1;
+
+    private static String[] mFormats = null;
 
     public static String getShareTitle(Context context) {
         String [] mLabels = context.getResources().getStringArray(R.array.sw_share_strings);
@@ -56,30 +59,43 @@
     }
 
     public static String buildShareResults(Context context, String time, long[] laps) {
-        String results = context.getString(R.string.sw_share_main, time + "\n");
+        StringBuilder b = new StringBuilder (context.getString(R.string.sw_share_main, time));
+        b.append("\n");
+
         int lapsNum = laps == null? 0 : laps.length;
         if (lapsNum == 0) {
-            return results;
+            return b.toString();
         }
-        results += context.getString(R.string.sw_share_laps) + "\n";
+
+        b.append(context.getString(R.string.sw_share_laps));
+        b.append("\n");
         for (int i = 1; i <= lapsNum; i ++) {
-            results += String.format("%d. %s\n", i, getTimeText(laps[lapsNum-i]));
+            b.append(getTimeText(context, laps[lapsNum-i], i));
+            b.append("\n");
         }
-        return results;
+        return b.toString();
     }
 
     public static String buildShareResults(Context context, long time, long[] laps) {
-        return buildShareResults(context, getTimeText(time), laps);
+        return buildShareResults(context, getTimeText(context, time, NO_LAP_NUMBER), laps);
     }
 
     /***
      * Sets the string of the time running on the stopwatch up to hundred of a second accuracy
      * @param time - in hundreds of a second since the stopwatch started
      */
-    public static String getTimeText(long time) {
+    public static String getTimeText(Context context, long time, int lap) {
         if (time < 0) {
             time = 0;
         }
+        if (lap != NO_LAP_NUMBER) {
+            mFormats = context.getResources().getStringArray(R.array.shared_laps_format_set);
+        } else {
+            mFormats = context.getResources().getStringArray(R.array.stopwatch_format_set);
+        }
+        char decimalSeparator = DecimalFormatSymbols.getInstance().getDecimalSeparator();
+        int formatIndex = 0;
+
         long hundreds, seconds, minutes, hours;
         seconds = time / 1000;
         hundreds = (time - seconds * 1000) / 10;
@@ -87,25 +103,19 @@
         seconds = seconds - minutes * 60;
         hours = minutes / 60;
         minutes = minutes - hours * 60;
-        if (hours > 99) {
-            hours = 0;
-        }
-        // TODO: must build to account for localization
-        String timeStr;
-        if (hours >= 10) {
-            timeStr = String.format("%02dh %02dm %02ds .%02d", hours, minutes,
-                    seconds, hundreds);
+        if (hours >= 100) {
+          formatIndex = 4;
+        } else if (hours >= 10) {
+            formatIndex = 3;
         } else if (hours > 0) {
-            timeStr = String.format("%01dh %02dm %02ds .%02d", hours, minutes,
-                    seconds, hundreds);
+          formatIndex = 2;
         } else if (minutes >= 10) {
-            timeStr = String.format("%02dm %02ds .%02d", minutes, seconds,
-                    hundreds);
+          formatIndex = 1;
         } else {
-            timeStr = String.format("%02dm %02ds .%02d", minutes, seconds,
-                    hundreds);
+          formatIndex = 0;
         }
-        return timeStr;
+        return String.format(mFormats[formatIndex], hours, minutes,
+                seconds, hundreds, decimalSeparator, lap);
     }
 
     /***
@@ -127,5 +137,4 @@
         String timeStr = String.format(format, hours, minutes, seconds, hundreds, decimalSeparator);
         return timeStr;
     }
-
 }