From 3d4445bb2a85048a735f21f7ec3ee9f9af6cee22 Mon Sep 17 00:00:00 2001
From: Michael Chan <mchan@android.com>
Date: Wed, 10 Apr 2013 23:05:29 -0700
Subject: [PATCH] Allow for country initials like US, UK, UAE, etc

Bug: 8558228
Change-Id: I631a4d146b3852185340321e070b75965eb3d0aa
---
 .../android/timezonepicker/TimeZoneData.java  | 115 ++++++------------
 .../TimeZoneFilterTypeAdapter.java            |  58 +++++++--
 2 files changed, 90 insertions(+), 83 deletions(-)

diff --git a/src/com/android/timezonepicker/TimeZoneData.java b/src/com/android/timezonepicker/TimeZoneData.java
index a0cfa4b..c8e9355 100644
--- a/src/com/android/timezonepicker/TimeZoneData.java
+++ b/src/com/android/timezonepicker/TimeZoneData.java
@@ -123,10 +123,9 @@ public class TimeZoneData {
             }
 
             /*
-             * 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 @@ public class TimeZoneData {
 
         // 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 @@ public class TimeZoneData {
 
             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) {
@@ -472,50 +481,4 @@ public class TimeZoneData {
         }
         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 @@ public class TimeZoneFilterTypeAdapter extends BaseAdapter implements Filterable
             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);
                 }
             }
 
@@ -296,6 +301,45 @@ public class TimeZoneFilterTypeAdapter extends BaseAdapter implements Filterable
             return results;
         }
 
+        /**
+         * 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
-- 
GitLab