Added labels for current and recent time zones

Bug: 8558228

Change-Id: I85422fb381714ba6a88924a194dd676a14647aca
diff --git a/res/layout/time_zone_item.xml b/res/layout/time_zone_item.xml
index 94a192b..4007bbb 100644
--- a/res/layout/time_zone_item.xml
+++ b/res/layout/time_zone_item.xml
@@ -21,12 +21,24 @@
     android:layout_gravity="center_vertical"
     android:background="?android:attr/selectableItemBackground"
     android:gravity="center_vertical"
-    android:minHeight="?android:attr/listPreferredItemHeight"
     android:orientation="vertical"
-    android:paddingBottom="8dp"
     android:paddingLeft="20dp"
-    android:paddingRight="20dp"
-    android:paddingTop="8dp" >
+    android:paddingRight="20dp" >
+
+    <TextView
+        android:id="@+id/label"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center_vertical"
+        android:layout_marginBottom="0dp"
+        android:layout_marginTop="0dp"
+        android:ellipsize="marquee"
+        android:gravity="center_vertical"
+        android:minHeight="32dp"
+        android:padding="0dp"
+        android:singleLine="true"
+        android:textAppearance="?android:attr/textAppearanceSmall"
+        android:textStyle="bold" />
 
     <TextView
         android:id="@+id/time_zone"
@@ -34,6 +46,7 @@
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:ellipsize="marquee"
+        android:paddingTop="8dp"
         android:singleLine="true"
         android:textAppearance="?android:attr/textAppearanceMedium" />
 
@@ -51,6 +64,8 @@
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:ellipsize="marquee"
+        android:paddingBottom="8dp"
         android:singleLine="true"
         android:textAppearance="?android:attr/textAppearanceSmall" />
+
 </LinearLayout>
\ No newline at end of file
diff --git a/res/values-v17/styles.xml b/res/values-v17/styles.xml
deleted file mode 100644
index 043fe71..0000000
--- a/res/values-v17/styles.xml
+++ /dev/null
@@ -1,22 +0,0 @@
-<?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:android="http://schemas.android.com/apk/res/android">
-    <!-- Style for dialog labels. -->
-    <style name="time_label_thin">
-        <item name="android:fontFamily">sans-serif-thin</item>
-    </style>
-</resources>
\ No newline at end of file
diff --git a/res/values/strings.xml b/res/values/strings.xml
index b7d21da..a00d22e 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -20,10 +20,10 @@
      [CHAR LIMIT=25] -->
     <string name="hint_time_zone_search">Country</string>
 
-    <!-- Label for the currently selected time zone [CHAR LIMIT=25] -->
-    <string name="currenttime_zone">CURRENT TIME ZONE</string>
+    <!-- Label for the currently selected time zone [CHAR LIMIT=30] -->
+    <string name="current_time_zone">CURRENT TIME ZONE</string>
 
-    <!-- Label for the recently selected time zones [CHAR LIMIT=25] -->
+    <!-- Label for the recently selected time zones [CHAR LIMIT=30] -->
     <plurals name="recent_time_zone">
         <item quantity="one">RECENT TIME ZONE</item>
         <!-- This is the label for 2 or more time zones. -->
diff --git a/src/com/android/timezonepicker/TimeZoneResultAdapter.java b/src/com/android/timezonepicker/TimeZoneResultAdapter.java
index 5b718cc..3adbf43 100644
--- a/src/com/android/timezonepicker/TimeZoneResultAdapter.java
+++ b/src/com/android/timezonepicker/TimeZoneResultAdapter.java
@@ -53,16 +53,21 @@
     /** The maximum number of recent timezones to save */
     private static final int MAX_RECENT_TIMEZONES = 3;
 
+    private static final int RESULT_LABEL_RECENT = -100;
+    private static final int RESULT_LABEL_CURRENT = -200;
+
     static class ViewHolder {
         TextView timeZone;
         TextView timeOffset;
         TextView location;
+        TextView label;
 
         static void setupViewHolder(View v) {
             ViewHolder vh = new ViewHolder();
             vh.timeZone = (TextView) v.findViewById(R.id.time_zone);
             vh.timeOffset = (TextView) v.findViewById(R.id.time_offset);
             vh.location = (TextView) v.findViewById(R.id.location);
+            vh.label = (TextView) v.findViewById(R.id.label);
             v.setTag(vh);
         }
     }
@@ -108,6 +113,7 @@
                 // Show the default/current value first
                 int defaultTzIndex = mTimeZoneData.getDefaultTimeZoneIndex();
                 if (defaultTzIndex != -1) {
+                    mFilteredTimeZoneIndices[mFilteredTimeZoneLength++] = RESULT_LABEL_CURRENT;
                     mFilteredTimeZoneIndices[mFilteredTimeZoneLength++] = defaultTzIndex;
                 }
 
@@ -117,11 +123,17 @@
                 String recentsString = prefs.getString(KEY_RECENT_TIMEZONES, null);
                 if (!TextUtils.isEmpty(recentsString)) {
                     String[] recents = recentsString.split(RECENT_TIMEZONES_DELIMITER);
+                    boolean first = true;
                     for (int i = recents.length - 1; i >= 0; i--) {
                         if (!TextUtils.isEmpty(recents[i])
                                 && !recents[i].equals(mTimeZoneData.mDefaultTimeZoneId)) {
                             int index = mTimeZoneData.findIndexByTimeZoneIdSlow(recents[i]);
                             if (index != -1) {
+                                if (first) {
+                                    mFilteredTimeZoneIndices[mFilteredTimeZoneLength++] =
+                                            RESULT_LABEL_RECENT;
+                                    first = false;
+                                }
                                 mFilteredTimeZoneIndices[mFilteredTimeZoneLength++] = index;
                             }
                         }
@@ -145,7 +157,7 @@
                     boolean match = localHr == time;
                     if (!match && !TimeZoneData.is24HourFormat) {
                         // PM + noon cases
-                        if((time + 12 == localHr) || (time == 12 && localHr == 0)) {
+                        if ((time + 12 == localHr) || (time == 12 && localHr == 0)) {
                             match = true;
                         }
                     }
@@ -200,7 +212,7 @@
             List<String> recents = new ArrayList<String>(
                     Arrays.asList(recentsString.split(RECENT_TIMEZONES_DELIMITER)));
             Iterator<String> it = recents.iterator();
-            while(it.hasNext()) {
+            while (it.hasNext()) {
                 String tz = it.next();
                 if (id.equals(tz)) {
                     it.remove();
@@ -234,15 +246,27 @@
     }
 
     @Override
-    public TimeZoneInfo getItem(int position) {
+    public Object getItem(int position) {
         if (position < 0 || position >= mFilteredTimeZoneLength) {
             return null;
         }
 
+        switch (mFilteredTimeZoneIndices[position]) {
+            case RESULT_LABEL_CURRENT:
+                return "CURRENT TIME ZONE";
+            case RESULT_LABEL_RECENT:
+                return "RECENT TIME ZONE";
+        }
+
         return mTimeZoneData.get(mFilteredTimeZoneIndices[position]);
     }
 
     @Override
+    public boolean isEnabled(int position) {
+        return mFilteredTimeZoneIndices[position] >= 0;
+    }
+
+    @Override
     public long getItemId(int position) {
         return mFilteredTimeZoneIndices[position];
     }
@@ -257,20 +281,38 @@
             ViewHolder.setupViewHolder(v);
         }
 
-        TimeZoneInfo tzi = mTimeZoneData.get(mFilteredTimeZoneIndices[position]);
-        v.setTag(VIEW_TAG_TIME_ZONE, tzi);
-
         ViewHolder vh = (ViewHolder) v.getTag();
-        vh.timeOffset.setText(tzi.getGmtDisplayName(mContext));
 
-        vh.timeZone.setText(tzi.mDisplayName);
+        if (mFilteredTimeZoneIndices[position] >= 0) {
+            TimeZoneInfo tzi = mTimeZoneData.get(mFilteredTimeZoneIndices[position]);
+            v.setTag(VIEW_TAG_TIME_ZONE, tzi);
 
-        String location = tzi.mCountry;
-        if (location == null) {
-            vh.location.setVisibility(View.INVISIBLE);
+            vh.label.setVisibility(View.GONE);
+            vh.timeZone.setText(tzi.mDisplayName);
+            vh.timeZone.setVisibility(View.VISIBLE);
+
+            vh.timeOffset.setText(tzi.getGmtDisplayName(mContext));
+            vh.timeOffset.setVisibility(View.VISIBLE);
+
+            String location = tzi.mCountry;
+            if (location == null) {
+                vh.location.setVisibility(View.INVISIBLE);
+            } else {
+                vh.location.setText(location);
+                vh.location.setVisibility(View.VISIBLE);
+            }
         } else {
-            vh.location.setText(location);
-            vh.location.setVisibility(View.VISIBLE);
+            if (mFilteredTimeZoneIndices[position] == RESULT_LABEL_CURRENT) {
+                vh.label.setText(v.getResources().getText(R.string.current_time_zone));
+            } else if (mFilteredTimeZoneIndices[position] == RESULT_LABEL_RECENT) {
+                vh.label.setText(v.getResources().getQuantityText(R.plurals.recent_time_zone,
+                        /* num of recent tzs */ mFilteredTimeZoneLength - position - 1));
+            }
+            vh.label.setVisibility(View.VISIBLE);
+            vh.timeZone.setVisibility(View.GONE);
+            vh.timeOffset.setVisibility(View.GONE);
+            vh.location.setVisibility(View.GONE);
+            v.setTag(VIEW_TAG_TIME_ZONE, null);
         }
 
         return v;