Dropdown list tweaks.

Fix country sorting in the drop-down to be alphabetical.
Dismiss keyboard when a country is selected from the drop-down.

Bug: 8600495
Change-Id: Ia8b32dbac64b9c91ce30c5d8e68da9c555f15b1b
diff --git a/src/com/android/timezonepicker/TimeZoneFilterTypeAdapter.java b/src/com/android/timezonepicker/TimeZoneFilterTypeAdapter.java
index 0303cab..fab1e8a 100644
--- a/src/com/android/timezonepicker/TimeZoneFilterTypeAdapter.java
+++ b/src/com/android/timezonepicker/TimeZoneFilterTypeAdapter.java
@@ -29,6 +29,7 @@
 import android.widget.TextView;
 
 import java.util.ArrayList;
+import java.util.Collections;
 
 public class TimeZoneFilterTypeAdapter extends BaseAdapter implements Filterable, OnClickListener {
     public static final String TAG = "TimeZoneFilterTypeAdapter";
@@ -209,6 +210,7 @@
             // ////////////////////////////////////////
             // Search by country
             // ////////////////////////////////////////
+            ArrayList<String> countries = new ArrayList<String>();
             for (String country : mTimeZoneData.mTimeZonesByCountry.keySet()) {
                 // TODO Perf - cache toLowerCase()?
                 if (!TextUtils.isEmpty(country)) {
@@ -229,10 +231,17 @@
                         }
                     }
                     if (isMatch) {
-                        filtered.add(new FilterTypeResult(FILTER_TYPE_COUNTRY, country, 0));
+                        countries.add(country);
                     }
                 }
             }
+            if (countries.size() > 0) {
+                // Sort countries alphabetically.
+                Collections.sort(countries);
+                for (String country : countries) {
+                    filtered.add(new FilterTypeResult(FILTER_TYPE_COUNTRY, country, 0));
+                }
+            }
 
             // ////////////////////////////////////////
             // TODO Search by state
diff --git a/src/com/android/timezonepicker/TimeZoneInfo.java b/src/com/android/timezonepicker/TimeZoneInfo.java
index d16c5cb..d11bd2b 100644
--- a/src/com/android/timezonepicker/TimeZoneInfo.java
+++ b/src/com/android/timezonepicker/TimeZoneInfo.java
@@ -303,7 +303,7 @@
     @Override
     public int compareTo(TimeZoneInfo other) {
         if (this.getNowOffsetMillis() != other.getNowOffsetMillis()) {
-            return other.getNowOffsetMillis() - this.getNowOffsetMillis();
+            return (other.getNowOffsetMillis() < this.getNowOffsetMillis()) ? -1 : 1;
         }
 
         // By country
diff --git a/src/com/android/timezonepicker/TimeZonePickerView.java b/src/com/android/timezonepicker/TimeZonePickerView.java
index a56d95b..ae51eb4 100644
--- a/src/com/android/timezonepicker/TimeZonePickerView.java
+++ b/src/com/android/timezonepicker/TimeZonePickerView.java
@@ -27,6 +27,7 @@
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.View.OnClickListener;
+import android.view.inputmethod.InputMethodManager;
 import android.widget.AdapterView;
 import android.widget.AdapterView.OnItemClickListener;
 import android.widget.AutoCompleteTextView;
@@ -149,6 +150,10 @@
 
     @Override
     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
+        // Hide the keyboard since the user explicitly selected an item.
+        InputMethodManager manager =
+                (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
+        manager.hideSoftInputFromWindow(mAutoCompleteTextView.getWindowToken(), 0);
         // An onClickListener for the view item because I haven't figured out a
         // way to update the AutoCompleteTextView without causing an infinite loop.
         mHideFilterSearchOnStart = true;