Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e06d8673 authored by Isaac Katzenelson's avatar Isaac Katzenelson
Browse files

Sort timezones in timezone picker

Bug: 7596888
Change-Id: Ia122cf85c36f402008b5cbf6249481cb9605126b
parent 6add973e
Loading
Loading
Loading
Loading
+59 −36
Original line number Diff line number Diff line
@@ -33,6 +33,9 @@ import android.view.MenuItem;

import com.android.deskclock.worldclock.Cities;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.TimeZone;

/**
@@ -64,7 +67,6 @@ public class SettingsActivity extends PreferenceActivity
    private static CharSequence[][] mTimezones;
    private long mTime;

    private static final boolean SHOW_DAYLIGHT_SAVINGS_INDICATOR = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
@@ -214,44 +216,33 @@ public class SettingsActivity extends PreferenceActivity
        SnoozeLengthDialog snoozePref = (SnoozeLengthDialog) findPreference(KEY_ALARM_SNOOZE);
        snoozePref.setSummary();
    }
    /**
     * Returns an array of ids/time zones. This returns a double indexed array
     * of ids and time zones for Calendar. It is an inefficient method and
     * shouldn't be called often, but can be used for one time generation of
     * this list.
     *
     * @return double array of tz ids and tz names
     */
    public CharSequence[][] getAllTimezones() {
        Resources resources = this.getResources();
        String[] ids = resources.getStringArray(R.array.timezone_values);
        String[] labels = resources.getStringArray(R.array.timezone_labels);
        if (ids.length != labels.length) {
            Log.wtf("Timezone ids and labels have different length!");
        }
        CharSequence[][] timeZones = new CharSequence[2][ids.length];
        for (int i = 0; i < ids.length; i++) {
            timeZones[0][i] = ids[i];
            timeZones[1][i] = buildGmtDisplayName(ids[i], labels[i]);
        }
        return timeZones;
    }

    public String buildGmtDisplayName(String id, String displayName) {
    private class TimeZoneRow implements Comparable<TimeZoneRow> {
        private static final boolean SHOW_DAYLIGHT_SAVINGS_INDICATOR = false;

        public final String mId;
        public final String mDisplayName;
        public final int mOffset;

        public TimeZoneRow(String id, String name) {
            mId = id;
            TimeZone tz = TimeZone.getTimeZone(id);
        boolean mUseDaylightTime = tz.useDaylightTime();
        int mOffset = tz.getOffset(mTime);
        int p = Math.abs(mOffset);
        StringBuilder name = new StringBuilder();
        name.append("GMT");
            boolean useDaylightTime = tz.useDaylightTime();
            mOffset = tz.getOffset(mTime);
            mDisplayName = buildGmtDisplayName(id, name, useDaylightTime);
        }

        if (mOffset < 0) {
            name.append('-');
        } else {
            name.append('+');
        @Override
        public int compareTo(TimeZoneRow another) {
            return mOffset - another.mOffset;
        }

        name.append(p / (DateUtils.HOUR_IN_MILLIS));
        public String buildGmtDisplayName(String id, String displayName, boolean useDaylightTime) {
            int p = Math.abs(mOffset);
            StringBuilder name = new StringBuilder("(GMT");
            name.append(mOffset < 0 ? '-' : '+');

            name.append(p / DateUtils.HOUR_IN_MILLIS);
            name.append(':');

            int min = p / 60000;
@@ -261,12 +252,44 @@ public class SettingsActivity extends PreferenceActivity
                name.append('0');
            }
            name.append(min);
        name.insert(0, "(");
            name.append(") ");
            name.append(displayName);
        if (mUseDaylightTime && SHOW_DAYLIGHT_SAVINGS_INDICATOR) {
            if (useDaylightTime && SHOW_DAYLIGHT_SAVINGS_INDICATOR) {
                name.append(" \u2600"); // Sun symbol
            }
            return name.toString();
        }
    }


    /**
     * Returns an array of ids/time zones. This returns a double indexed array
     * of ids and time zones for Calendar. It is an inefficient method and
     * shouldn't be called often, but can be used for one time generation of
     * this list.
     *
     * @return double array of tz ids and tz names
     */
    public CharSequence[][] getAllTimezones() {
        Resources resources = this.getResources();
        String[] ids = resources.getStringArray(R.array.timezone_values);
        String[] labels = resources.getStringArray(R.array.timezone_labels);
        if (ids.length != labels.length) {
            Log.wtf("Timezone ids and labels have different length!");
        }
        List<TimeZoneRow> timezones = new ArrayList<TimeZoneRow>();
        for (int i = 0; i < ids.length; i++) {
            timezones.add(new TimeZoneRow(ids[i], labels[i]));
        }
        Collections.sort(timezones);

        CharSequence[][] timeZones = new CharSequence[2][timezones.size()];
        int i = 0;
        for (TimeZoneRow row : timezones) {
            timeZones[0][i] = row.mId;
            timeZones[1][i++] = row.mDisplayName;
        }
        return timeZones;
    }

}