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

Commit 60f175e2 authored by Isaac Katzenelson's avatar Isaac Katzenelson
Browse files

Read cities names for DB and not from list

Bug: 7413861

The list of user selected cities in world clock was using the name from the usr's list and
not from the DB. When a change of locale is done on the device, the cities name were not changed.
Changed the WorldClock adapter to use the DB name and timezone if available.
Added suport in both app and widget.

Change-Id: I029581f9fd6cf245ac980c1d1352e0d777924ccd
parent a2b0a7e1
Loading
Loading
Loading
Loading
+21 −3
Original line number Original line Diff line number Diff line
@@ -47,6 +47,7 @@ public class DigitalWidgetViewsFactory extends BroadcastReceiver implements Remo
    private int mId = AppWidgetManager.INVALID_APPWIDGET_ID;
    private int mId = AppWidgetManager.INVALID_APPWIDGET_ID;
    private RemoteWorldClockAdapter mAdapter;
    private RemoteWorldClockAdapter mAdapter;
    private boolean mReloadCitiesList = true;
    private boolean mReloadCitiesList = true;
    private boolean mReloadCitiesDb = true;
    private float mFontScale = 1;
    private float mFontScale = 1;


    // An adapter to provide the view for the list of cities in the world clock.
    // An adapter to provide the view for the list of cities in the world clock.
@@ -89,14 +90,23 @@ public class DigitalWidgetViewsFactory extends BroadcastReceiver implements Remo
            final Calendar now = Calendar.getInstance();
            final Calendar now = Calendar.getInstance();
            now.setTimeInMillis(System.currentTimeMillis());
            now.setTimeInMillis(System.currentTimeMillis());
            int myDayOfWeek = now.get(Calendar.DAY_OF_WEEK);
            int myDayOfWeek = now.get(Calendar.DAY_OF_WEEK);
            now.setTimeZone(TimeZone.getTimeZone(cityObj.mTimeZone));
            CityObj cityInDb = mCitiesDb.get(cityObj.mCityId);
            String cityTZ = (cityInDb != null) ? cityInDb.mTimeZone:cityObj.mTimeZone;
            now.setTimeZone(TimeZone.getTimeZone(cityTZ));
            int cityDayOfWeek = now.get(Calendar.DAY_OF_WEEK);
            int cityDayOfWeek = now.get(Calendar.DAY_OF_WEEK);


            clock.setTextViewTextSize(clockId1, TypedValue.COMPLEX_UNIT_PX, mFontSize * mFontScale);
            clock.setTextViewTextSize(clockId1, TypedValue.COMPLEX_UNIT_PX, mFontSize * mFontScale);
            clock.setTextViewTextSize(clockId2, TypedValue.COMPLEX_UNIT_PX, mFontSize * mFontScale);
            clock.setTextViewTextSize(clockId2, TypedValue.COMPLEX_UNIT_PX, mFontSize * mFontScale);
            clock.setString(clockId1, "setTimeZone", cityObj.mTimeZone);
            clock.setString(clockId1, "setTimeZone", cityObj.mTimeZone);
            clock.setString(clockId2, "setTimeZone", cityObj.mTimeZone);
            clock.setString(clockId2, "setTimeZone", cityObj.mTimeZone);

            // Home city or city not in DB , use data from the save selected cities list
            if (cityObj.mCityId == null || cityInDb == null) {
                clock.setTextViewText(labelId, cityObj.mCityName);
                clock.setTextViewText(labelId, cityObj.mCityName);
            }else {
                clock.setTextViewText(labelId, cityInDb.mCityName);
            }

            if (myDayOfWeek != cityDayOfWeek) {
            if (myDayOfWeek != cityDayOfWeek) {
                clock.setTextViewText(dayId, mContext.getString(
                clock.setTextViewText(dayId, mContext.getString(
                        R.string.world_day_of_week_label, now.getDisplayName(
                        R.string.world_day_of_week_label, now.getDisplayName(
@@ -190,6 +200,11 @@ public class DigitalWidgetViewsFactory extends BroadcastReceiver implements Remo
            mAdapter.loadData(mContext);
            mAdapter.loadData(mContext);
            mReloadCitiesList = false;
            mReloadCitiesList = false;
        }
        }
        if (mReloadCitiesDb) {
            mAdapter.loadCitiesDb(mContext);
            mReloadCitiesDb = false;
        }

        mFontScale = WidgetUtils.getScaleRatio(mContext, null, mId);
        mFontScale = WidgetUtils.getScaleRatio(mContext, null, mId);
    }
    }


@@ -225,8 +240,11 @@ public class DigitalWidgetViewsFactory extends BroadcastReceiver implements Remo
            widgetManager.partiallyUpdateAppWidget(mId, widget);
            widgetManager.partiallyUpdateAppWidget(mId, widget);
        } else {
        } else {
            if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
            if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) {
                // refresh the list to make sure home time zone is displayed / removed.
                // refresh the list to make sure home time zone is displayed / removed
                mReloadCitiesList = true;
                mReloadCitiesList = true;
            } else if (action.equals(Intent.ACTION_LOCALE_CHANGED)) {
                // reload the cities DB to pick up the cities name in the new language
                mReloadCitiesDb = true;
            }
            }
            // For any time change or locale change, refresh all
            // For any time change or locale change, refresh all
            widgetManager.notifyAppWidgetViewDataChanged(mId, R.id.digital_appwidget_listview);
            widgetManager.notifyAppWidgetViewDataChanged(mId, R.id.digital_appwidget_listview);
+16 −8
Original line number Original line Diff line number Diff line
@@ -27,7 +27,6 @@ import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View;
@@ -63,21 +62,28 @@ public class ClockFragment extends DeskClockFragment implements OnSharedPreferen
    private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
    private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
            @Override
            @Override
        public void onReceive(Context context, Intent intent) {
        public void onReceive(Context context, Intent intent) {
            boolean changed = intent.getAction().equals(Intent.ACTION_TIME_CHANGED)
            String action = intent.getAction();
                    || intent.getAction().equals(Intent.ACTION_TIMEZONE_CHANGED);
            boolean changed = action.equals(Intent.ACTION_TIME_CHANGED)
            if (changed || intent.getAction().equals(Utils.ACTION_ON_QUARTER_HOUR)) {
                    || action.equals(Intent.ACTION_TIMEZONE_CHANGED)
                    || action.equals(Intent.ACTION_LOCALE_CHANGED);
            if (changed || action.equals(Utils.ACTION_ON_QUARTER_HOUR)) {
                Utils.updateDate(mDateFormat, mDateFormatForAccessibility,mClockFrame);
                Utils.updateDate(mDateFormat, mDateFormatForAccessibility,mClockFrame);
                if (mAdapter != null) {
                if (mAdapter != null) {
                    // *CHANGED may modify the need for showing the Home City
                    // *CHANGED may modify the need for showing the Home City
                    if (changed && (mAdapter.hasHomeCity() != mAdapter.needHomeCity())) {
                    if (changed && (mAdapter.hasHomeCity() != mAdapter.needHomeCity())) {
                        mAdapter.loadData(context);
                        mAdapter.reloadData(context);
                    } else {
                    } else {
                        mAdapter.notifyDataSetChanged();
                        mAdapter.notifyDataSetChanged();
                    }
                    }
                    // Reloading the cities list with new localized names
                    if (action.equals(Intent.ACTION_LOCALE_CHANGED)) {
                        mAdapter.loadCitiesDb(context);
                        mAdapter.notifyDataSetChanged();
                    }
                }
                }
            }
            }
            if (changed || intent.getAction().equals(Alarms.ALARM_DONE_ACTION)
            if (changed || action.equals(Alarms.ALARM_DONE_ACTION)
                    || intent.getAction().equals(Alarms.ALARM_SNOOZE_CANCELLED)) {
                    || action.equals(Alarms.ALARM_SNOOZE_CANCELLED)) {
                Utils.refreshAlarm(getActivity(), mClockFrame);
                Utils.refreshAlarm(getActivity(), mClockFrame);
            }
            }
        }
        }
@@ -166,12 +172,14 @@ public class ClockFragment extends DeskClockFragment implements OnSharedPreferen
        filter.addAction(Alarms.ALARM_SNOOZE_CANCELLED);
        filter.addAction(Alarms.ALARM_SNOOZE_CANCELLED);
        filter.addAction(Intent.ACTION_TIME_CHANGED);
        filter.addAction(Intent.ACTION_TIME_CHANGED);
        filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
        filter.addAction(Intent.ACTION_LOCALE_CHANGED);
        getActivity().registerReceiver(mIntentReceiver, filter);
        getActivity().registerReceiver(mIntentReceiver, filter);


        mButtons.setAlpha(mButtonsHidden ? 0 : 1);
        mButtons.setAlpha(mButtonsHidden ? 0 : 1);


        // Resume can invoked after changing the cities list.
        // Resume can invoked after changing the cities list or a change in locale
        if (mAdapter != null) {
        if (mAdapter != null) {
            mAdapter.loadCitiesDb(getActivity());
            mAdapter.reloadData(getActivity());
            mAdapter.reloadData(getActivity());
        }
        }
        // Resume can invoked after changing the clock style.
        // Resume can invoked after changing the clock style.
+31 −0
Original line number Original line Diff line number Diff line
@@ -25,6 +25,7 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuff;
@@ -44,8 +45,12 @@ import android.widget.TextView;


import com.android.deskclock.stopwatch.Stopwatches;
import com.android.deskclock.stopwatch.Stopwatches;
import com.android.deskclock.timer.Timers;
import com.android.deskclock.timer.Timers;
import com.android.deskclock.worldclock.CityObj;


import java.text.Collator;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Locale;
import java.util.Locale;




@@ -390,4 +395,30 @@ public class Utils {
        }
        }
    }
    }


    public static CityObj[] loadCitiesDataBase(Context c) {
        final Collator collator = Collator.getInstance();
        Resources r = c.getResources();
        // Read strings array of name,timezone, id
        // make sure the list are the same length
        String [] cities = r.getStringArray(R.array.cities_names);
        String [] timezones = r.getStringArray(R.array.cities_tz);
        String [] ids = r.getStringArray(R.array.cities_id);
        if (cities.length != timezones.length || ids.length != cities.length) {
            Log.wtf("City lists sizes are not the same, cannot use the data");
            return null;
         }
         CityObj[] tempList = new CityObj [cities.length];
         for (int i = 0; i < cities.length; i++) {
            tempList[i] = new CityObj(cities[i], timezones[i], ids[i]);
         }
         // Sort alphabetically
        Arrays.sort(tempList, new Comparator<CityObj> () {
            @Override
            public int compare(CityObj c1, CityObj c2) {
                Comparator<CityObj> mCollator;
                return collator.compare(c1.mCityName, c2.mCityName);
            }
        });
        return tempList;
    }
}
}
+24 −25
Original line number Original line Diff line number Diff line
@@ -28,7 +28,6 @@ public class Cities {
    public static final String WORLDCLOCK_UPDATE_INTENT = "com.android.deskclock.worldclock.update";
    public static final String WORLDCLOCK_UPDATE_INTENT = "com.android.deskclock.worldclock.update";
    private static final String NUMBER_OF_CITIES = "number_of_cities";
    private static final String NUMBER_OF_CITIES = "number_of_cities";



    public static void saveCitiesToSharedPrefs(
    public static void saveCitiesToSharedPrefs(
            SharedPreferences prefs, HashMap<String, CityObj> cities) {
            SharedPreferences prefs, HashMap<String, CityObj> cities) {
        SharedPreferences.Editor editor = prefs.edit();
        SharedPreferences.Editor editor = prefs.edit();
+16 −37
Original line number Original line Diff line number Diff line
@@ -21,7 +21,6 @@ import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Context;
import android.content.Intent;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.preference.PreferenceManager;
import android.text.format.DateFormat;
import android.text.format.DateFormat;
@@ -40,16 +39,13 @@ import android.widget.TextView;


import com.android.deskclock.Alarms;
import com.android.deskclock.Alarms;
import com.android.deskclock.DeskClock;
import com.android.deskclock.DeskClock;
import com.android.deskclock.Log;
import com.android.deskclock.R;
import com.android.deskclock.R;
import com.android.deskclock.SettingsActivity;
import com.android.deskclock.SettingsActivity;
import com.android.deskclock.Utils;
import com.android.deskclock.Utils;


import java.text.Collator;
import java.text.Collator;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Calendar;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashMap;
import java.util.TimeZone;
import java.util.TimeZone;


@@ -68,8 +64,6 @@ public class CitiesActivity extends Activity implements OnCheckedChangeListener,
    private CityAdapter mAdapter;
    private CityAdapter mAdapter;
    private HashMap<String, CityObj> mUserSelectedCities;
    private HashMap<String, CityObj> mUserSelectedCities;
    private Calendar mCalendar;
    private Calendar mCalendar;
    private final Collator mCollator = Collator.getInstance();



/***
/***
* Adapter for a list of cities with the respected time zone.
* Adapter for a list of cities with the respected time zone.
@@ -98,12 +92,12 @@ public class CitiesActivity extends Activity implements OnCheckedChangeListener,


        @Override
        @Override
        public int getCount() {
        public int getCount() {
            return mAllTheCitiesList.length;
            return (mAllTheCitiesList != null) ? mAllTheCitiesList.length: 0;
        }
        }


        @Override
        @Override
        public Object getItem(int p) {
        public Object getItem(int p) {
            if (p >=0 && p < mAllTheCitiesList.length) {
            if (mAllTheCitiesList != null && p >=0 && p < mAllTheCitiesList.length) {
                return mAllTheCitiesList [p];
                return mAllTheCitiesList [p];
            }
            }
            return null;
            return null;
@@ -116,12 +110,12 @@ public class CitiesActivity extends Activity implements OnCheckedChangeListener,


        @Override
        @Override
        public boolean isEnabled(int p) {
        public boolean isEnabled(int p) {
            return ((CityObj)mAllTheCitiesList[p]).mCityId != null;
            return mAllTheCitiesList != null && ((CityObj)mAllTheCitiesList[p]).mCityId != null;
        }
        }


        @Override
        @Override
        public View getView(int position, View view, ViewGroup parent) {
        public View getView(int position, View view, ViewGroup parent) {
            if (position < 0 || position >=  mAllTheCitiesList.length) {
            if (mAllTheCitiesList == null || position < 0 || position >= mAllTheCitiesList.length) {
                return null;
                return null;
            }
            }
            CityObj c = (CityObj)mAllTheCitiesList [position];
            CityObj c = (CityObj)mAllTheCitiesList [position];
@@ -157,27 +151,10 @@ public class CitiesActivity extends Activity implements OnCheckedChangeListener,
        }
        }


        private void loadCitiesDataBase(Context c) {
        private void loadCitiesDataBase(Context c) {
            Resources r = c.getResources();
            CityObj[] tempList = Utils.loadCitiesDataBase(c);
            // Read strings array of name,timezone, id
            if (tempList == null) {
            // make sure the list are the same length
            String [] cities = r.getStringArray(R.array.cities_names);
            String [] timezones = r.getStringArray(R.array.cities_tz);
            String [] ids = r.getStringArray(R.array.cities_id);
            if (cities.length != timezones.length || ids.length != cities.length) {
                Log.wtf("City lists sizes are not the same, cannot use the data");
                return;
                return;
            }
            }
             CityObj[] tempList = new CityObj [cities.length];
             for (int i = 0; i < cities.length; i++) {
                tempList[i] = new CityObj(cities[i], timezones[i], ids[i]);
             }
             // Sort alphabetically
            Arrays.sort(tempList, new Comparator<CityObj> () {
                @Override
                public int compare(CityObj c1, CityObj c2) {
                    return mCollator.compare(c1.mCityName, c2.mCityName);
                }
            });
            //Create section indexer and add headers to the cities list
            //Create section indexer and add headers to the cities list
            String val = null;
            String val = null;
            ArrayList<String> sections = new ArrayList<String> ();
            ArrayList<String> sections = new ArrayList<String> ();
@@ -207,11 +184,12 @@ public class CitiesActivity extends Activity implements OnCheckedChangeListener,


        @Override
        @Override
        public int getPositionForSection(int section) {
        public int getPositionForSection(int section) {
            return (Integer) mSectionPositions[section];
            return (mSectionPositions != null) ? (Integer) mSectionPositions[section] : 0;
        }
        }


        @Override
        @Override
        public int getSectionForPosition(int p) {
        public int getSectionForPosition(int p) {
            if (mSectionPositions != null) {
                for (int i = 0; i < mSectionPositions.length - 1; i++) {
                for (int i = 0; i < mSectionPositions.length - 1; i++) {
                    if (p >= (Integer)mSectionPositions[i] && p < (Integer)mSectionPositions[i + 1]) {
                    if (p >= (Integer)mSectionPositions[i] && p < (Integer)mSectionPositions[i + 1]) {
                        return i;
                        return i;
@@ -220,6 +198,7 @@ public class CitiesActivity extends Activity implements OnCheckedChangeListener,
                if (p >= (Integer)mSectionPositions[mSectionPositions.length - 1]) {
                if (p >= (Integer)mSectionPositions[mSectionPositions.length - 1]) {
                    return mSectionPositions.length - 1;
                    return mSectionPositions.length - 1;
                }
                }
            }
            return 0;
            return 0;
        }
        }


Loading