SetupWizard: Use SettingsLib for Timezones

* Currently we are storing a copy of timezones.xml ourselves and parsing
  it manually
* Use ZoneGetter from SettingsLib instead to get the list of TimeZones
* That way we have less code to maintain and updates to the list are easier
  as well
* Also simplify constructTimezoneAdapter -> we never sort by name

Change-Id: Ifd690ed28c6fbecaee895b4063137e86c059e314
diff --git a/res/xml/timezones.xml b/res/xml/timezones.xml
deleted file mode 100644
index 895de13..0000000
--- a/res/xml/timezones.xml
+++ /dev/null
@@ -1,104 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-     Copyright (C) 2013 The CyanogenMod 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.
--->
-<timezones>
-    <timezone id="Pacific/Majuro">Marshall Islands</timezone>
-    <timezone id="Pacific/Midway">Midway Island</timezone>
-    <timezone id="Pacific/Honolulu">Hawaii</timezone>
-    <timezone id="America/Anchorage">Alaska</timezone>
-    <timezone id="America/Los_Angeles">Pacific Time</timezone>
-    <timezone id="America/Tijuana">Tijuana</timezone>
-    <timezone id="America/Phoenix">Arizona</timezone>
-    <timezone id="America/Chihuahua">Chihuahua</timezone>
-    <timezone id="America/Denver">Mountain Time</timezone>
-    <timezone id="America/Costa_Rica">Central America</timezone>
-    <timezone id="America/Chicago">Central Time</timezone>
-    <timezone id="America/Mexico_City">Mexico City</timezone>
-    <timezone id="America/Regina">Saskatchewan</timezone>
-    <timezone id="America/Bogota">Bogota</timezone>
-    <timezone id="America/New_York">Eastern Time</timezone>
-    <timezone id="America/Caracas">Venezuela</timezone>
-    <timezone id="America/Barbados">Atlantic Time (Barbados)</timezone>
-    <timezone id="America/Halifax">Atlantic Time (Canada)</timezone>
-    <timezone id="America/Manaus">Manaus</timezone>
-    <timezone id="America/Santiago">Santiago</timezone>
-    <timezone id="America/St_Johns">Newfoundland</timezone>
-    <timezone id="America/Sao_Paulo">Brasilia</timezone>
-    <timezone id="America/Argentina/Buenos_Aires">Buenos Aires</timezone>
-    <timezone id="America/Godthab">Greenland</timezone>
-    <timezone id="America/Montevideo">Montevideo</timezone>
-    <timezone id="Atlantic/South_Georgia">Mid-Atlantic</timezone>
-    <timezone id="Atlantic/Azores">Azores</timezone>
-    <timezone id="Atlantic/Cape_Verde">Cape Verde Islands</timezone>
-    <timezone id="Africa/Casablanca">Casablanca</timezone>
-    <timezone id="Europe/London">London, Dublin</timezone>
-    <timezone id="Europe/Amsterdam">Amsterdam, Berlin</timezone>
-    <timezone id="Europe/Belgrade">Belgrade</timezone>
-    <timezone id="Europe/Brussels">Brussels</timezone>
-    <timezone id="Europe/Sarajevo">Sarajevo</timezone>
-    <timezone id="Africa/Windhoek">Windhoek</timezone>
-    <timezone id="Africa/Brazzaville">W. Africa Time</timezone>
-    <timezone id="Asia/Amman">Amman, Jordan</timezone>
-    <timezone id="Europe/Athens">Athens, Istanbul</timezone>
-    <timezone id="Asia/Beirut">Beirut, Lebanon</timezone>
-    <timezone id="Africa/Cairo">Cairo</timezone>
-    <timezone id="Europe/Helsinki">Helsinki</timezone>
-    <timezone id="Asia/Jerusalem">Jerusalem</timezone>
-    <timezone id="Europe/Minsk">Minsk</timezone>
-    <timezone id="Africa/Harare">Harare</timezone>
-    <timezone id="Asia/Baghdad">Baghdad</timezone>
-    <timezone id="Europe/Moscow">Moscow</timezone>
-    <timezone id="Europe/Kiev">Kiev</timezone>
-    <timezone id="Asia/Kuwait">Kuwait</timezone>
-    <timezone id="Africa/Nairobi">Nairobi</timezone>
-    <timezone id="Asia/Tehran">Tehran</timezone>
-    <timezone id="Asia/Baku">Baku</timezone>
-    <timezone id="Asia/Tbilisi">Tbilisi</timezone>
-    <timezone id="Asia/Yerevan">Yerevan</timezone>
-    <timezone id="Asia/Dubai">Dubai</timezone>
-    <timezone id="Asia/Kabul">Kabul</timezone>
-    <timezone id="Asia/Karachi">Islamabad, Karachi</timezone>
-    <timezone id="Asia/Oral">Ural'sk</timezone>
-    <timezone id="Asia/Yekaterinburg">Yekaterinburg</timezone>
-    <timezone id="Asia/Calcutta">Kolkata</timezone>
-    <timezone id="Asia/Colombo">Sri Lanka</timezone>
-    <timezone id="Asia/Katmandu">Kathmandu</timezone>
-    <timezone id="Asia/Almaty">Astana</timezone>
-    <timezone id="Asia/Rangoon">Yangon</timezone>
-    <timezone id="Asia/Krasnoyarsk">Krasnoyarsk</timezone>
-    <timezone id="Asia/Bangkok">Bangkok</timezone>
-    <timezone id="Asia/Jakarta">Jakarta</timezone>
-    <timezone id="Asia/Shanghai">Beijing</timezone>
-    <timezone id="Asia/Hong_Kong">Hong Kong</timezone>
-    <timezone id="Asia/Irkutsk">Irkutsk</timezone>
-    <timezone id="Asia/Kuala_Lumpur">Kuala Lumpur</timezone>
-    <timezone id="Australia/Perth">Perth</timezone>
-    <timezone id="Asia/Taipei">Taipei</timezone>
-    <timezone id="Asia/Seoul">Seoul</timezone>
-    <timezone id="Asia/Tokyo">Tokyo, Osaka</timezone>
-    <timezone id="Asia/Yakutsk">Yakutsk</timezone>
-    <timezone id="Australia/Adelaide">Adelaide</timezone>
-    <timezone id="Australia/Darwin">Darwin</timezone>
-    <timezone id="Australia/Brisbane">Brisbane</timezone>
-    <timezone id="Australia/Hobart">Hobart</timezone>
-    <timezone id="Australia/Sydney">Sydney, Canberra</timezone>
-    <timezone id="Asia/Vladivostok">Vladivostok</timezone>
-    <timezone id="Pacific/Guam">Guam</timezone>
-    <timezone id="Asia/Magadan">Magadan</timezone>
-    <timezone id="Pacific/Auckland">Auckland</timezone>
-    <timezone id="Pacific/Fiji">Fiji</timezone>
-    <timezone id="Pacific/Tongatapu">Tonga</timezone>
-</timezones>
diff --git a/src/org/lineageos/setupwizard/DateTimeActivity.java b/src/org/lineageos/setupwizard/DateTimeActivity.java
index 8a6d3a1..f7e32f3 100644
--- a/src/org/lineageos/setupwizard/DateTimeActivity.java
+++ b/src/org/lineageos/setupwizard/DateTimeActivity.java
@@ -26,7 +26,6 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
-import android.content.res.XmlResourceParser;
 import android.os.Bundle;
 import android.os.Handler;
 import android.text.format.DateFormat;
@@ -39,15 +38,13 @@
 import android.widget.TextView;
 import android.widget.TimePicker;
 
+import com.android.settingslib.datetime.ZoneGetter;
+
 import org.lineageos.setupwizard.util.SetupWizardUtils;
 
-import org.xmlpull.v1.XmlPullParserException;
-
-import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Collections;
 import java.util.Comparator;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.TimeZone;
@@ -85,7 +82,7 @@
         setNextText(R.string.next);
 
         final Spinner spinner = (Spinner) findViewById(R.id.timezone_list);
-        final SimpleAdapter adapter = constructTimezoneAdapter(this, false);
+        final SimpleAdapter adapter = constructTimezoneAdapter(this);
         mCurrentTimeZone = TimeZone.getDefault();
         View dateView = findViewById(R.id.date_item);
         dateView.setOnClickListener((view) -> showDatePicker());
@@ -203,14 +200,12 @@
         mDateTextView.setText(shortDateFormat.format(now.getTime()));
     }
 
-    private static SimpleAdapter constructTimezoneAdapter(Context context,
-            boolean sortedByName) {
+    private static SimpleAdapter constructTimezoneAdapter(Context context) {
         final String[] from = new String[] {KEY_DISPLAYNAME, KEY_GMT};
         final int[] to = new int[] {android.R.id.text1, android.R.id.text2};
 
-        final String sortKey = (sortedByName ? KEY_DISPLAYNAME : KEY_OFFSET);
-        final TimeZoneComparator comparator = new TimeZoneComparator(sortKey);
-        final List<HashMap<String, Object>> sortedList = getZones(context);
+        final TimeZoneComparator comparator = new TimeZoneComparator(KEY_OFFSET);
+        final List<Map<String, Object>> sortedList = ZoneGetter.getZonesList(context);
         Collections.sort(sortedList, comparator);
         final SimpleAdapter adapter = new SimpleAdapter(context,
                 sortedList,
@@ -221,81 +216,11 @@
         return adapter;
     }
 
-    private static List<HashMap<String, Object>> getZones(Context context) {
-        final List<HashMap<String, Object>> myData = new ArrayList();
-        final long date = Calendar.getInstance().getTimeInMillis();
-        try {
-            XmlResourceParser xrp = context.getResources().getXml(R.xml.timezones);
-            while (xrp.next() != XmlResourceParser.START_TAG)
-                continue;
-            xrp.next();
-            while (xrp.getEventType() != XmlResourceParser.END_TAG) {
-                while (xrp.getEventType() != XmlResourceParser.START_TAG) {
-                    if (xrp.getEventType() == XmlResourceParser.END_DOCUMENT) {
-                        return myData;
-                    }
-                    xrp.next();
-                }
-                if (xrp.getName().equals(XMLTAG_TIMEZONE)) {
-                    String id = xrp.getAttributeValue(0);
-                    String displayName = xrp.nextText();
-                    addItem(myData, id, displayName, date);
-                }
-                while (xrp.getEventType() != XmlResourceParser.END_TAG) {
-                    xrp.next();
-                }
-                xrp.next();
-            }
-            xrp.close();
-        } catch (XmlPullParserException xppe) {
-            Log.e(TAG, "Ill-formatted timezones.xml file");
-        } catch (java.io.IOException ioe) {
-            Log.e(TAG, "Unable to read timezones.xml file");
-        }
-
-        return myData;
-    }
-
-    private static void addItem(
-            List<HashMap<String, Object>> myData, String id, String displayName, long date) {
-        final HashMap<String, Object> map = new HashMap();
-        map.put(KEY_ID, id);
-        map.put(KEY_DISPLAYNAME, displayName);
-        final TimeZone tz = TimeZone.getTimeZone(id);
-        final int offset = tz.getOffset(date);
-        final int p = Math.abs(offset);
-        final StringBuilder name = new StringBuilder();
-        name.append("GMT");
-
-        if (offset < 0) {
-            name.append('-');
-        } else {
-            name.append('+');
-        }
-
-        name.append(p / (HOURS_1));
-        name.append(':');
-
-        int min = p / 60000;
-        min %= 60;
-
-        if (min < 10) {
-            name.append('0');
-        }
-        name.append(min);
-
-        map.put(KEY_GMT, name.toString());
-        map.put(KEY_OFFSET, offset);
-
-        myData.add(map);
-    }
-
     private static int getTimeZoneIndex(SimpleAdapter adapter, TimeZone tz) {
         final String defaultId = tz.getID();
         final int listSize = adapter.getCount();
         for (int i = 0; i < listSize; i++) {
-            // Using HashMap<String, Object> induces unnecessary warning.
-            final HashMap<?,?> map = (HashMap<?,?>)adapter.getItem(i);
+            final Map<?,?> map = (Map<?,?>)adapter.getItem(i);
             final String id = (String)map.get(KEY_ID);
             if (defaultId.equals(id)) {
                 // If current timezone is in this list, move focus to it
@@ -332,7 +257,7 @@
         }
     }
 
-    private static class TimeZoneComparator implements Comparator<HashMap<?, ?>> {
+    private static class TimeZoneComparator implements Comparator<Map<?, ?>> {
         private String mSortingKey;
 
         public TimeZoneComparator(String sortingKey) {
@@ -343,7 +268,7 @@
             mSortingKey = sortingKey;
         }
 
-        public int compare(HashMap<?, ?> map1, HashMap<?, ?> map2) {
+        public int compare(Map<?, ?> map1, Map<?, ?> map2) {
             Object value1 = map1.get(mSortingKey);
             Object value2 = map2.get(mSortingKey);