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

Commit 583a3b34 authored by Danny Baumann's avatar Danny Baumann Committed by Gerrit Code Review
Browse files

Merge "DeskClock: Fix timezone of user-defined cities" into cm-10.1

parents 6bb43994 4c4ad082
Loading
Loading
Loading
Loading
+37 −45
Original line number Diff line number Diff line
@@ -81,27 +81,22 @@ public class AddCityDialog implements OnClickListener,
    }

    private final AsyncTask<Void, Void, Void> mTzLoadTask = new AsyncTask<Void, Void, Void>() {
        private String[] mZones;
        private CityTimeZone[] mZones;

        @Override
        protected Void doInBackground(Void... params) {
            List<CityTimeZone> zones = loadTimeZones();
            Collections.sort(zones);

            List<String> tzLabels = new ArrayList<String>();
            for (CityTimeZone zone : zones)  {
                tzLabels.add(zone.toString());
            }

            mZones = tzLabels.toArray(new String[tzLabels.size()]);
            mDefaultTimeZoneId = zones.indexOf(mDefaultTimeZoneLabel);
            mZones = zones.toArray(new CityTimeZone[zones.size()]);
            mDefaultTimeZonePos = zones.indexOf(mDefaultTimeZone);
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            if (!isCancelled()) {
                int id = mSavedTimeZonePos != -1 ? mSavedTimeZonePos : mDefaultTimeZoneId;
                int id = mSavedTimeZonePos != -1 ? mSavedTimeZonePos : mDefaultTimeZonePos;
                setTimeZoneData(mZones, id, true);
                mLoadingTz = false;
            }
@@ -133,6 +128,7 @@ public class AddCityDialog implements OnClickListener,
    };

    private static class CityTimeZone implements Comparable<CityTimeZone> {
        String mId;
        int mSign;
        int mHours;
        int mMinutes;
@@ -141,6 +137,10 @@ public class AddCityDialog implements OnClickListener,

        @Override
        public String toString() {
            if (mId == null) {
                // Loading
                return mLabel;
            }
            return String.format("GMT%s%02d:%02d - %s",
                    (mSign == -1 ? "-" : "+"), mHours, mMinutes, mLabel);
        }
@@ -155,8 +155,8 @@ public class AddCityDialog implements OnClickListener,

        @Override
        public int compareTo(CityTimeZone other) {
            int offset = getOffset();
            int otherOffset = other.getOffset();
            long offset = getOffset();
            long otherOffset = other.getOffset();
            if (offset != otherOffset) {
                return offset < otherOffset ? -1 : 1;
            }
@@ -166,8 +166,8 @@ public class AddCityDialog implements OnClickListener,
            return mLabel.compareTo(other.mLabel);
        }

        private int getOffset() {
            return mSign * (mHours + mMinutes * 60);
        private long getOffset() {
            return mSign * (mHours * HOURS_1 + mMinutes * 60000);
        }
    }

@@ -180,24 +180,22 @@ public class AddCityDialog implements OnClickListener,
    private final Spinner mTimeZones;
    private Button mButton;

    private List<String> mCurrentTimeZones;

    private LocationManager mLocationMgr;
    private ConnectivityManager mConnectivityMgr;
    private CityAndTimeZoneLocator mLocator;
    private boolean mGpsRequesting;
    private boolean mLoadingTz;

    private int mDefaultTimeZoneId;
    private int mDefaultTimeZonePos;
    private int mSavedTimeZonePos;
    private CityTimeZone mDefaultTimeZoneLabel;
    private CityTimeZone mDefaultTimeZone;

    public AddCityDialog(Context context, LayoutInflater inflater, OnCitySelected listener) {
        mContext = context;
        mHandler = new Handler();
        mListener = listener;
        mDefaultTimeZoneId = 0;
        mDefaultTimeZoneLabel = null;
        mDefaultTimeZonePos = 0;
        mDefaultTimeZone = null;
        mSavedTimeZonePos = -1;
        mLocationMgr = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        mLocationMgr.addGpsStatusListener(new GpsStatus.Listener() {
@@ -215,7 +213,10 @@ public class AddCityDialog implements OnClickListener,
        mCityName.addTextChangedListener(this);

        mTimeZones = (Spinner) dlgView.findViewById(R.id.add_city_tz);
        setTimeZoneData(new String[]{ context.getString(R.string.cities_add_loading) }, 0, false);
        CityTimeZone loading = new CityTimeZone();
        loading.mId = null;
        loading.mLabel = context.getString(R.string.cities_add_loading);
        setTimeZoneData(new CityTimeZone[]{ loading }, 0, false);
        mTimeZones.setEnabled(false);
        mTimeZones.setOnItemSelectedListener(this);

@@ -286,8 +287,8 @@ public class AddCityDialog implements OnClickListener,
        mReceiverRegistered = true;
    }

    private void setTimeZoneData(String[] data, int selected, boolean enabled) {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(mContext,
    private void setTimeZoneData(CityTimeZone[] data, int selected, boolean enabled) {
        ArrayAdapter<CityTimeZone> adapter = new ArrayAdapter<CityTimeZone>(mContext,
                android.R.layout.simple_spinner_item, data);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mTimeZones.setAdapter(adapter);
@@ -296,15 +297,13 @@ public class AddCityDialog implements OnClickListener,
        if (mButton != null) {
            checkSelectionStatus();
        }

        mCurrentTimeZones = Arrays.asList(data);
    }

    private List<CityTimeZone> loadTimeZones() {
        ArrayList<CityTimeZone> timeZones = new ArrayList<CityTimeZone>();
        Resources res = mContext.getResources();
        final long date = Calendar.getInstance().getTimeInMillis();
        mDefaultTimeZoneLabel = buildCityTimeZone(TimeZone.getDefault().getID(), date);
        mDefaultTimeZone = buildCityTimeZone(TimeZone.getDefault().getID(), date);
        String[] ids = res.getStringArray(R.array.cities_tz);
        for (String id : ids) {
            CityTimeZone zone = buildCityTimeZone(id, date);
@@ -325,6 +324,7 @@ public class AddCityDialog implements OnClickListener,
        final boolean inDst = tz.inDaylightTime(new Date(date));

        CityTimeZone timeZone = new CityTimeZone();
        timeZone.mId = tz.getID();
        timeZone.mLabel = tz.getDisplayName(inDst, TimeZone.LONG);
        timeZone.mSign = offset < 0 ? -1 : 1;
        timeZone.mHours = p / (HOURS_1);
@@ -426,12 +426,12 @@ public class AddCityDialog implements OnClickListener,

    public void onClick(DialogInterface dialog, int which) {
        String name = mCityName.getText().toString();
        String tz = null;
        CityTimeZone ctz = null;
        if (mTimeZones.getSelectedItem() != null) {
            tz = mTimeZones.getSelectedItem().toString();
            ctz = (CityTimeZone)mTimeZones.getSelectedItem();
        }
        if (tz != null && mListener != null) {
            mListener.onCitySelected(name, tz.substring(tz.indexOf(" - ") + 3));
        if (ctz != null && mListener != null) {
            mListener.onCitySelected(name, ctz.mId);
        }
    }

@@ -501,26 +501,18 @@ public class AddCityDialog implements OnClickListener,
        CityAndTimeZoneLocator mLocator = new CityAndTimeZoneLocator(
                mContext, location, mConnectivityMgr, new OnCityAndTimeZoneLocatedCallback() {
            @Override
            @SuppressWarnings("unchecked")
            public void onCityAndTimeZoneLocated(String city, TZ timezone) {
                // Now we need to resolve the timezone info into an existing timezone
                // First try to resolve the id, otherwise select the first occurrence
                // with the same offset
                CityTimeZone ctzInfo = toCityTimeZone(timezone);
                int tz = mCurrentTimeZones.indexOf(ctzInfo.toString());
                if (tz == -1) {
                    String offset = ctzInfo.toString().substring(0, 9); //xe: GMT+12:00
                    int cc = mCurrentTimeZones.size();
                    for (int i = 0; i < cc; i++) {
                        String ctz = mCurrentTimeZones.get(i);
                        if (ctz.startsWith(offset)) {
                            tz = i;
                            break;
                        }
                    }
                CityTimeZone ctz = toCityTimeZone(timezone);
                int pos = ((ArrayAdapter<CityTimeZone>)mTimeZones.getAdapter()).getPosition(ctz);
                if (pos == -1) {
                    // This mean you are in the middle of the ocean and Android doesn't have
                    // a timezone definition for you.
                    pos = mDefaultTimeZonePos;
                }

                // Update the views with the new information
                updateViews(city, tz);
                updateViews(city, pos);
            }

            @Override