Remove any empty space by resizing the dialog dynamically.

Also a few small bug fixes and tweaks.

Bug: 8697117
Change-Id: If6e4f77df8e5c1cb09dddc4da5105ec050181c55
diff --git a/res/layout/empty_time_zone_item.xml b/res/layout/empty_time_zone_item.xml
new file mode 100644
index 0000000..9d50d7a
--- /dev/null
+++ b/res/layout/empty_time_zone_item.xml
@@ -0,0 +1,43 @@
+<?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.
+-->
+<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:layout_gravity="center_vertical"
+    android:background="?android:attr/selectableItemBackground"
+    android:gravity="center_vertical"
+    android:orientation="vertical"
+    android:paddingLeft="20dp"
+    android:paddingRight="20dp" >
+
+    <include layout="@layout/time_zone_item" />
+
+    <TextView
+        android:id="@+id/empty_item"
+        style="@style/font_family_thin"
+        android:layout_width="wrap_content"
+        android:layout_height="match_parent"
+        android:ellipsize="marquee"
+        android:paddingTop="8dp"
+        android:singleLine="true"
+        android:text="@string/no_results_found"
+        android:textAppearance="?android:attr/textAppearanceMedium"
+        android:textSize="16sp"
+        android:layout_gravity="center_vertical"
+        android:gravity="center_vertical" />
+
+</FrameLayout>
\ No newline at end of file
diff --git a/res/layout/timezonepickerview.xml b/res/layout/timezonepickerview.xml
index 7597eee..a41d279 100644
--- a/res/layout/timezonepickerview.xml
+++ b/res/layout/timezonepickerview.xml
@@ -18,8 +18,7 @@
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
-    android:layout_height="match_parent"
-    android:minHeight="400dp"
+    android:layout_height="wrap_content"
     android:minWidth="300dp"
     android:orientation="vertical" >
 
@@ -62,8 +61,7 @@
     <ListView
         android:id="@+id/timezonelist"
         android:layout_width="match_parent"
-        android:layout_height="0dip"
-        android:layout_weight="1"
+        android:layout_height="wrap_content"
         android:background="#FFECECEC"
         android:choiceMode="singleChoice" >
     </ListView>
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 355675b..bc2f7c5 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -18,8 +18,11 @@
 
     <!-- Hint text to encourage the user to enter a country name to find a time zone
      [CHAR LIMIT=25] -->
-    <string name="hint_time_zone_search">Country</string>
+    <string name="hint_time_zone_search">Type country name</string>
 
     <!-- Description of clear icon in the search bar, for accessibility. [CHAR LIMIT=NONE]-->
     <string name="searchview_description_clear">Clear query</string>
+
+    <!-- Item to show when no timezones are found. [CHAR LIMIT=NONE]-->
+    <string name="no_results_found">No results found</string>
 </resources>
\ No newline at end of file
diff --git a/src/com/android/timezonepicker/TimeZoneData.java b/src/com/android/timezonepicker/TimeZoneData.java
index c6eeeaa..6ef0e01 100644
--- a/src/com/android/timezonepicker/TimeZoneData.java
+++ b/src/com/android/timezonepicker/TimeZoneData.java
@@ -267,7 +267,11 @@
 
         for (int i = 0; i < length; i++) {
             TimeZoneInfo tzi = mTimeZonesById.get(ids[i]);
-            tzi.mDisplayName = labels[i];
+            if (tzi != null) {
+                tzi.mDisplayName = labels[i];
+            } else {
+                Log.e(TAG, "Could not find timezone with label: "+labels[i]);
+            }
         }
     }
 
diff --git a/src/com/android/timezonepicker/TimeZoneFilterTypeAdapter.java b/src/com/android/timezonepicker/TimeZoneFilterTypeAdapter.java
index fab1e8a..31317d1 100644
--- a/src/com/android/timezonepicker/TimeZoneFilterTypeAdapter.java
+++ b/src/com/android/timezonepicker/TimeZoneFilterTypeAdapter.java
@@ -262,9 +262,9 @@
          * and symbols).
          *
          * For example:
-         * isStartingInitialsFor("UA", "United Arb Emirates") would return true
+         * isStartingInitialsFor("UA", "United Arab Emirates") would return true
          * isStartingInitialsFor("US", "U.S. Virgin Island") would return true
-
+         *
          * @param prefixString
          * @param string
          * @return
@@ -291,6 +291,11 @@
                     wasWordBreak = false;
                 }
             }
+
+            // Special case for "USA". Note that both strings have been turned to lowercase already.
+            if (prefixString.equals("usa") && string.equals("united states")) {
+                return true;
+            }
             return false;
         }
 
diff --git a/src/com/android/timezonepicker/TimeZoneResultAdapter.java b/src/com/android/timezonepicker/TimeZoneResultAdapter.java
index efc275e..4708ad9 100644
--- a/src/com/android/timezonepicker/TimeZoneResultAdapter.java
+++ b/src/com/android/timezonepicker/TimeZoneResultAdapter.java
@@ -40,6 +40,7 @@
     private static final String TAG = "TimeZoneResultAdapter";
     private static final boolean DEBUG = false;
     private static final int VIEW_TAG_TIME_ZONE = R.id.time_zone;
+    private static final int EMPTY_INDEX = -100;
 
     /** SharedPref name and key for recent time zones */
     private static final String SHARED_PREFS_NAME = "com.android.calendar_preferences";
@@ -130,6 +131,7 @@
 
         switch (filterType) {
             case TimeZoneFilterTypeAdapter.FILTER_TYPE_EMPTY:
+                mFilteredTimeZoneIndices[mFilteredTimeZoneLength++] = EMPTY_INDEX;
                 break;
             case TimeZoneFilterTypeAdapter.FILTER_TYPE_NONE:
                 // Show the default/current value first
@@ -265,7 +267,13 @@
     public View getView(int position, View convertView, ViewGroup parent) {
         View v = convertView;
 
-        if (v == null) {
+        if (mFilteredTimeZoneIndices[position] == EMPTY_INDEX) {
+            v = mInflater.inflate(R.layout.empty_time_zone_item, null);
+            return v;
+        }
+
+        // We'll need to re-inflate the view if it was null, or if it was used as an empty item.
+        if (v == null || v.findViewById(R.id.empty_item) != null) {
             v = mInflater.inflate(R.layout.time_zone_item, null);
             ViewHolder.setupViewHolder(v);
         }