Merge "Embedded country names in the app for ones not in framework" into jb-mr2-dev
diff --git a/res/values-v16/strings.xml b/res/values-v16/strings.xml
new file mode 100644
index 0000000..1c16be9
--- /dev/null
+++ b/res/values-v16/strings.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+     Copyright (C) 2013 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
+    <!-- Legend for the * symbol to mean that the time zone has
+     a future day light savings time [CHAR LIMIT=30] -->
+    <string name="dst_note">\u2600 Observes daylight saving time</string>
+</resources>
\ No newline at end of file
diff --git a/res/values/arrays.xml b/res/values/arrays.xml
index affcce1..5b1e2b5 100644
--- a/res/values/arrays.xml
+++ b/res/values/arrays.xml
@@ -70,7 +70,13 @@
         <item>"Europe/Minsk"</item>
         <!-- Only had GMT as the display string -->
         <item>"Europe/Kaliningrad"</item>
-    </string-array>
+
+        <!-- Brazil -->
+        <!-- Non DST version -->
+        <item>"America/Porto_Velho"</item>
+        <!-- Non DST version -->
+        <item>"America/Belem"</item>
+</string-array>
 
     <!--
         Replacement display strings for time zones. The order should match the entries in
@@ -110,7 +116,13 @@
         <item>"Atlantic Standard Time"</item>
         <item>"Eastern European Time"</item>
         <item>"Kaliningrad Time"</item>
-    </string-array>
+
+        <!-- Brazil -->
+        <!-- Non DST version of Amazon Standard Time -->
+        <item>"Amazon - Rondônia"</item>
+        <!-- Non DST version of Brasilia Standard Time -->
+        <item>"Brasilia - Pará"</item>
+        </string-array>
 
     <!--
         Country codes
@@ -137,4 +149,4 @@
         <item>"Curaçao"</item>
         <item>"South Sudan"</item>
     </string-array>
-</resources>
\ No newline at end of file
+</resources>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 7a6f8d2..395458a 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -53,7 +53,7 @@
 
     <!-- Legend for the * symbol to mean that the time zone has
      a future day light savings time [CHAR LIMIT=30] -->
-    <string name="dst_note">\u2600 Observes daylight saving time</string>
+    <string name="dst_note">\u002A Observes daylight saving time</string>
 
     <!-- Description of clear icon in the search bar, for accessibility. [CHAR LIMIT=NONE]-->
     <string name="searchview_description_clear">Clear query</string>
diff --git a/src/com/android/timezonepicker/TimeZoneData.java b/src/com/android/timezonepicker/TimeZoneData.java
index 1105169..c6eeeaa 100644
--- a/src/com/android/timezonepicker/TimeZoneData.java
+++ b/src/com/android/timezonepicker/TimeZoneData.java
@@ -123,10 +123,9 @@
             }
 
             /*
-             * Dropping non-GMT tzs without a country code. They are not
-             * really needed and they are dups but missing proper
-             * country codes. e.g. WET CET MST7MDT PST8PDT Asia/Khandyga
-             * Asia/Ust-Nera EST
+             * Dropping non-GMT tzs without a country code. They are not really
+             * needed and they are dups but missing proper country codes. e.g.
+             * WET CET MST7MDT PST8PDT Asia/Khandyga Asia/Ust-Nera EST
              */
             if (!tzId.startsWith("Etc/GMT")) {
                 continue;
@@ -170,32 +169,6 @@
 
         // Don't change the order of mTimeZones after this sort
         Collections.sort(mTimeZones);
-        // TimeZoneInfo last = null;
-        // boolean first = true;
-        // for (TimeZoneInfo tz : mTimeZones) {
-        // // All
-        // Log.e("ALL", tz.toString());
-        //
-        // // GMT
-        // String name = tz.mTz.getDisplayName();
-        // if (name.startsWith("GMT") && !tz.mTzId.startsWith("Etc/GMT")) {
-        // Log.e("GMT", tz.toString());
-        // }
-        //
-        // // Dups
-        // if (last != null) {
-        // if (last.compareTo(tz) == 0) {
-        // if (first) {
-        // Log.e("SAME", last.toString());
-        // first = false;
-        // }
-        // Log.e("SAME", tz.toString());
-        // } else {
-        // first = true;
-        // }
-        // }
-        // last = tz;
-        // }
 
         mTimeZonesByCountry = new LinkedHashMap<String, ArrayList<Integer>>();
         mTimeZonesByOffsets = new SparseArray<ArrayList<Integer>>(mHasTimeZonesInHrOffset.length);
@@ -243,6 +216,42 @@
 
             idx++;
         }
+
+        // printTimeZones();
+    }
+
+    private void printTimeZones() {
+        TimeZoneInfo last = null;
+        boolean first = true;
+        for (TimeZoneInfo tz : mTimeZones) {
+            // All
+            if (false) {
+                Log.e("ALL", tz.toString());
+            }
+
+            // GMT
+            if (true) {
+                String name = tz.mTz.getDisplayName();
+                if (name.startsWith("GMT") && !tz.mTzId.startsWith("Etc/GMT")) {
+                    Log.e("GMT", tz.toString());
+                }
+            }
+
+            // Dups
+            if (true && last != null) {
+                if (last.compareTo(tz) == 0) {
+                    if (first) {
+                        Log.e("SAME", last.toString());
+                        first = false;
+                    }
+                    Log.e("SAME", tz.toString());
+                } else {
+                    first = true;
+                }
+            }
+            last = tz;
+        }
+        Log.e(TAG, "Total number of tz's = " + mTimeZones.size());
     }
 
     private void populateDisplayNameOverrides(Resources resources) {
@@ -503,50 +512,4 @@
         }
         return -1;
     }
-
-    private void printTz() {
-        for (TimeZoneInfo tz : mTimeZones) {
-            Log.e(TAG, "" + tz.toString());
-        }
-
-        Log.e(TAG, "Total number of tz's = " + mTimeZones.size());
-    }
-
-    // void checkForNameDups(TimeZone tz, String country, boolean dls, int
-    // style, int idx,
-    // boolean print) {
-    // if (country == null) {
-    // return;
-    // }
-    // String displayName = tz.getDisplayName(dls, style);
-    //
-    // if (print) {
-    // Log.e(TAG, "" + idx + " " + tz.getID() + " " + country + " ## " +
-    // displayName);
-    // }
-    //
-    // if (tz.useDaylightTime()) {
-    // if (displayName.matches("GMT[+-][0-9][0-9]:[0-9][0-9]")) {
-    // return;
-    // }
-    //
-    // if (displayName.length() == 3 && displayName.charAt(2) == 'T' &&
-    // (displayName.charAt(1) == 'S' || displayName.charAt(1) == 'D')) {
-    // displayName = "" + displayName.charAt(0) + 'T';
-    // } else {
-    // displayName = displayName.replace(" Daylight ",
-    // " ").replace(" Standard ", " ");
-    // }
-    // }
-    //
-    // String tzNameWithCountry = country + " ## " + displayName;
-    // Integer groupId = mCountryPlusTzName2Tzs.get(tzNameWithCountry);
-    // if (groupId == null) {
-    // mCountryPlusTzName2Tzs.put(tzNameWithCountry, idx);
-    // } else if (groupId != idx) {
-    // Log.e(TAG, "Yikes: " + tzNameWithCountry + " matches " + groupId +
-    // " and " + idx);
-    // }
-    // }
-
 }
diff --git a/src/com/android/timezonepicker/TimeZoneFilterTypeAdapter.java b/src/com/android/timezonepicker/TimeZoneFilterTypeAdapter.java
index 4cec7b4..5d0e881 100644
--- a/src/com/android/timezonepicker/TimeZoneFilterTypeAdapter.java
+++ b/src/com/android/timezonepicker/TimeZoneFilterTypeAdapter.java
@@ -256,15 +256,20 @@
             boolean first = true;
             for (String country : mTimeZoneData.mTimeZonesByCountry.keySet()) {
                 // TODO Perf - cache toLowerCase()?
-                if (country != null && country.toLowerCase().startsWith(prefixString)) {
-                    FilterTypeResult r;
-                    if (first) {
-                        r = new FilterTypeResult(true, FILTER_TYPE_COUNTRY, null, 0);
+                if (!TextUtils.isEmpty(country)) {
+                    final String lowerCaseCountry = country.toLowerCase();
+                    if (lowerCaseCountry.startsWith(prefixString)
+                            || (lowerCaseCountry.charAt(0) == prefixString.charAt(0) &&
+                            isStartingInitialsFor(prefixString, lowerCaseCountry))) {
+                        FilterTypeResult r;
+                        if (first) {
+                            r = new FilterTypeResult(true, FILTER_TYPE_COUNTRY, null, 0);
+                            filtered.add(r);
+                            first = false;
+                        }
+                        r = new FilterTypeResult(false, FILTER_TYPE_COUNTRY, country, 0);
                         filtered.add(r);
-                        first = false;
                     }
-                    r = new FilterTypeResult(false, FILTER_TYPE_COUNTRY, country, 0);
-                    filtered.add(r);
                 }
             }
 
@@ -297,6 +302,45 @@
         }
 
         /**
+         * Returns true if the prefixString is an initial for string. Note that
+         * this method will return true even if prefixString does not cover all
+         * the words. Words are separated by non-letters which includes spaces
+         * and symbols).
+         *
+         * For example:
+         * isStartingInitialsFor("UA", "United Arb Emirates") would return true
+         * isStartingInitialsFor("US", "U.S. Virgin Island") would return true
+
+         * @param prefixString
+         * @param string
+         * @return
+         */
+        private boolean isStartingInitialsFor(String prefixString, String string) {
+            final int initialLen = prefixString.length();
+            final int strLen = string.length();
+
+            int initialIdx = 0;
+            boolean wasWordBreak = true;
+            for (int i = 0; i < strLen; i++) {
+                if (!Character.isLetter(string.charAt(i))) {
+                    wasWordBreak = true;
+                    continue;
+                }
+
+                if (wasWordBreak) {
+                    if (prefixString.charAt(initialIdx++) != string.charAt(i)) {
+                        return false;
+                    }
+                    if (initialIdx == initialLen) {
+                        return true;
+                    }
+                    wasWordBreak = false;
+                }
+            }
+            return false;
+        }
+
+        /**
          * @param filtered
          * @param num
          */
diff --git a/src/com/android/timezonepicker/TimeZoneInfo.java b/src/com/android/timezonepicker/TimeZoneInfo.java
index 3cc2f92..c8d6eed 100644
--- a/src/com/android/timezonepicker/TimeZoneInfo.java
+++ b/src/com/android/timezonepicker/TimeZoneInfo.java
@@ -182,7 +182,9 @@
             mSB.append(')');
 
             if (hasFutureDST) {
-                mSB.append(" \u2600"); // Sun symbol
+                String dstSymbol = TimeZonePickerUtils.getDstSymbol();
+                mSB.append(" ");
+                mSB.append(dstSymbol); // Sun symbol
             }
 
             displayName = mSB.toString();
@@ -335,7 +337,11 @@
         }
 
         // Finally diff by display name
+        if (mDisplayName != null && other.mDisplayName != null)
+            return this.mDisplayName.compareTo(other.mDisplayName);
+
         return this.mTz.getDisplayName(Locale.getDefault()).compareTo(
                 other.mTz.getDisplayName(Locale.getDefault()));
+
     }
 }
diff --git a/src/com/android/timezonepicker/TimeZonePickerUtils.java b/src/com/android/timezonepicker/TimeZonePickerUtils.java
index 0580df3..a6a8bfe 100644
--- a/src/com/android/timezonepicker/TimeZonePickerUtils.java
+++ b/src/com/android/timezonepicker/TimeZonePickerUtils.java
@@ -16,6 +16,8 @@
 
 package com.android.timezonepicker;
 
+import android.content.Context;
+import android.os.Build;
 import android.text.format.DateUtils;
 import android.text.format.Time;
 
@@ -24,6 +26,8 @@
 
 public class TimeZonePickerUtils {
 
+    private static String mDstSymbol;
+
     /**
      * Given a timezone id (e.g. America/Los_Angeles), returns the corresponding timezone
      * display name (e.g. (GMT-7.00) Pacific Time).
@@ -73,9 +77,19 @@
         sb.append(displayName);
 
         if (tz.useDaylightTime()) {
-            sb.append(" \u2600"); // Sun symbol
+            String dstSymbol = getDstSymbol();
+            sb.append(" ");
+            sb.append(dstSymbol); // Sun symbol
         }
         return sb.toString();
     }
 
+    public static String getDstSymbol() {
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
+            return "\u2600"; // The Sun emoji icon.
+        } else {
+            return "*";
+        }
+    }
+
 }