diff options
author | Erik <roboerik@android.com> | 2010-09-22 15:31:03 -0700 |
---|---|---|
committer | Erik <roboerik@android.com> | 2010-09-22 17:16:27 -0700 |
commit | a48b9d426236d8d26bd99602bf0a84315b3f1b09 (patch) | |
tree | 81c19b0c7d84e55ce9bf93d6743f4b8cc0160408 /src | |
parent | 28e2296066a3727cd95eb44e7c35713e7180446a (diff) | |
download | android_packages_apps_Calendar-a48b9d426236d8d26bd99602bf0a84315b3f1b09.tar.gz android_packages_apps_Calendar-a48b9d426236d8d26bd99602bf0a84315b3f1b09.tar.bz2 android_packages_apps_Calendar-a48b9d426236d8d26bd99602bf0a84315b3f1b09.zip |
Manual merge of tz settings changes.
Merges changes I4ad92a73 and Ia7260f4d. This moves Calendar to
using the hidden TimeZoneUtils API and properly displaying and
updating the home time zone in settings.
Change-Id: I51a80da0963ba5924a09e920f158cdc1c158fb2c
Diffstat (limited to 'src')
-rw-r--r-- | src/com/android/calendar/GeneralPreferences.java | 25 | ||||
-rw-r--r-- | src/com/android/calendar/TimezoneAdapter.java | 62 | ||||
-rw-r--r-- | src/com/android/calendar/Utils.java | 89 |
3 files changed, 76 insertions, 100 deletions
diff --git a/src/com/android/calendar/GeneralPreferences.java b/src/com/android/calendar/GeneralPreferences.java index 51f8bb32..45959863 100644 --- a/src/com/android/calendar/GeneralPreferences.java +++ b/src/com/android/calendar/GeneralPreferences.java @@ -20,8 +20,6 @@ import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager.NameNotFoundException; import android.os.Bundle; import android.preference.CheckBoxPreference; import android.preference.ListPreference; @@ -31,6 +29,7 @@ import android.preference.PreferenceFragment; import android.preference.PreferenceManager; import android.preference.PreferenceScreen; import android.preference.RingtonePreference; +import android.provider.Calendar.CalendarCache; import android.provider.SearchRecentSuggestions; import android.widget.Toast; @@ -77,9 +76,6 @@ public class GeneralPreferences extends PreferenceFragment implements static final String KEY_HOME_TZ_ENABLED = "preferences_home_tz_enabled"; static final String KEY_HOME_TZ = "preferences_home_tz"; - // The value to use when setting Calendar to use the device's time zone - public static final String LOCAL_TZ = "AUTO"; - // Default preference values public static final int DEFAULT_START_VIEW = CalendarController.ViewType.WEEK; public static final int DEFAULT_DETAILED_VIEW = CalendarController.ViewType.DAY; @@ -91,6 +87,8 @@ public class GeneralPreferences extends PreferenceFragment implements CheckBoxPreference mUseHomeTZ; ListPreference mHomeTZ; + private static CharSequence[][] mTimezones; + /** Return a properly configured SharedPreferences instance */ public static SharedPreferences getSharedPreferences(Context context) { return context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE); @@ -126,8 +124,18 @@ public class GeneralPreferences extends PreferenceFragment implements mUseHomeTZ = (CheckBoxPreference) preferenceScreen.findPreference(KEY_HOME_TZ_ENABLED); mUseHomeTZ.setOnPreferenceChangeListener(this); mHomeTZ = (ListPreference) preferenceScreen.findPreference(KEY_HOME_TZ); + String tz = mHomeTZ.getValue(); + + if (mTimezones == null) { + mTimezones = (new TimezoneAdapter(getActivity(), tz)).getAllTimezones(); + } + mHomeTZ.setEntryValues(mTimezones[0]); + mHomeTZ.setEntries(mTimezones[1]); mHomeTZ.setSummary(mHomeTZ.getEntry()); mHomeTZ.setOnPreferenceChangeListener(this); +// mHomeTZ = (ListPreference) preferenceScreen.findPreference(KEY_HOME_TZ); +// mHomeTZ.setSummary(mHomeTZ.getEntry()); +// mHomeTZ.setOnPreferenceChangeListener(this); migrateOldPreferences(sharedPreferences); @@ -150,12 +158,13 @@ public class GeneralPreferences extends PreferenceFragment implements if ((Boolean)newValue) { tz = mHomeTZ.getValue(); } else { - tz = LOCAL_TZ; + tz = CalendarCache.TIMEZONE_TYPE_AUTO; } } else if (preference == mHomeTZ) { - mHomeTZ.setValue((String)newValue); + tz = (String) newValue; + // We set the value here so we can read back the entry + mHomeTZ.setValue(tz); mHomeTZ.setSummary(mHomeTZ.getEntry()); - tz = (String)newValue; } else { return false; } diff --git a/src/com/android/calendar/TimezoneAdapter.java b/src/com/android/calendar/TimezoneAdapter.java index 694b659a..3890ebbe 100644 --- a/src/com/android/calendar/TimezoneAdapter.java +++ b/src/com/android/calendar/TimezoneAdapter.java @@ -31,7 +31,6 @@ import java.util.Collections; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; -import java.util.Map; import java.util.TimeZone; /** @@ -169,9 +168,13 @@ public class TimezoneAdapter extends ArrayAdapter<TimezoneRow> { /** * Static cache of all known timezones, mapped to their string IDs. This is - * lazily-loaded on the first call to {@link #loadFromResources(Resources)} + * lazily-loaded on the first call to {@link #loadFromResources(Resources)}. + * Loading is called in a synchronized block during initialization of this + * class and is based off the resources available to the calling context. + * This class should not be used outside of the initial context. + * LinkedHashMap is used to preserve ordering. */ - private static Map<String, TimezoneRow> sTimezones; + private static LinkedHashMap<String, TimezoneRow> sTimezones; private Context mContext; @@ -220,7 +223,6 @@ public class TimezoneAdapter extends ArrayAdapter<TimezoneRow> { * @param currentTimezone */ public void showInitialTimezones() { - loadFromResources(mContext.getResources()); // we use a linked hash set to guarantee only unique IDs are added, and // also to maintain the insertion order of the timezones @@ -243,20 +245,25 @@ public class TimezoneAdapter extends ArrayAdapter<TimezoneRow> { } clear(); - for (String id : ids) { - if (!sTimezones.containsKey(id)) { - // a timezone we don't know about, so try to add it... - TimeZone newTz = TimeZone.getTimeZone(id); - // since TimeZone.getTimeZone actually returns a clone of GMT - // when it doesn't recognize the ID, this appears to be the only - // reliable way to check to see if the ID is a valid timezone - if (!newTz.equals(TimeZone.getTimeZone("GMT"))) { - sTimezones.put(id, new TimezoneRow(id, newTz.getDisplayName())); - } else { - continue; + + synchronized (TimezoneAdapter.class) { + loadFromResources(mContext.getResources()); + TimeZone gmt = TimeZone.getTimeZone("GMT"); + for (String id : ids) { + if (!sTimezones.containsKey(id)) { + // a timezone we don't know about, so try to add it... + TimeZone newTz = TimeZone.getTimeZone(id); + // since TimeZone.getTimeZone actually returns a clone of GMT + // when it doesn't recognize the ID, this appears to be the only + // reliable way to check to see if the ID is a valid timezone + if (!newTz.equals(gmt)) { + sTimezones.put(id, new TimezoneRow(id, newTz.getDisplayName())); + } else { + continue; + } } + add(sTimezones.get(id)); } - add(sTimezones.get(id)); } mShowingAll = false; } @@ -265,7 +272,6 @@ public class TimezoneAdapter extends ArrayAdapter<TimezoneRow> { * Populates this adapter with all known timezones. */ public void showAllTimezones() { - loadFromResources(mContext.getResources()); List<TimezoneRow> timezones = new ArrayList<TimezoneRow>(sTimezones.values()); Collections.sort(timezones); clear(); @@ -315,7 +321,27 @@ public class TimezoneAdapter extends ArrayAdapter<TimezoneRow> { Utils.setSharedPreference(mContext, KEY_RECENT_TIMEZONES, recentsString); } - private static void loadFromResources(Resources resources) { + /** + * 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() { + CharSequence[][] timeZones = new CharSequence[2][sTimezones.size()]; + List<String> ids = new ArrayList<String>(sTimezones.keySet()); + List<TimezoneRow> timezones = new ArrayList<TimezoneRow>(sTimezones.values()); + int i = 0; + for (TimezoneRow row : timezones) { + timeZones[0][i] = ids.get(i); + timeZones[1][i++] = row.toString(); + } + return timeZones; + } + + private void loadFromResources(Resources resources) { if (sTimezones == null) { String[] ids = resources.getStringArray(R.array.timezone_values); String[] labels = resources.getStringArray(R.array.timezone_labels); diff --git a/src/com/android/calendar/Utils.java b/src/com/android/calendar/Utils.java index 9c800a07..0eed573b 100644 --- a/src/com/android/calendar/Utils.java +++ b/src/com/android/calendar/Utils.java @@ -31,15 +31,13 @@ import android.graphics.drawable.GradientDrawable; import android.net.Uri; import android.os.Bundle; import android.text.TextUtils; -import android.text.format.DateUtils; import android.text.format.Time; +import android.util.CalendarUtils.TimeZoneUtils; import android.util.Log; import java.util.Calendar; import java.util.Formatter; -import java.util.HashSet; import java.util.List; -import java.util.Locale; import java.util.Map; import java.util.TimeZone; @@ -74,16 +72,13 @@ public class Utils { public static final String INTENT_KEY_VIEW_TYPE = "VIEW"; public static final String INTENT_VALUE_VIEW_TYPE_DAY = "DAY"; - private static StringBuilder mSB = new StringBuilder(50); - private static Formatter mF = new Formatter(mSB, Locale.getDefault()); + // The name of the shared preferences file. This name must be maintained for + // historical + // reasons, as it's what PreferenceManager assigned the first time the file + // was created. + private static final String SHARED_PREFS_NAME = "com.android.calendar_preferences"; - private volatile static boolean mFirstTZRequest = true; - private volatile static boolean mTZQueryInProgress = false; - - private volatile static boolean mUseHomeTZ = false; - private volatile static String mHomeTZ = Time.getCurrentTimezone(); - - private static HashSet<Runnable> mTZCallbacks = new HashSet<Runnable>(); + private static final TimeZoneUtils mTZUtils = new TimeZoneUtils(SHARED_PREFS_NAME); public static int getViewTypeFromIntentAndSharedPref(Activity activity) { Intent intent = activity.getIntent(); @@ -120,33 +115,7 @@ public class Utils { * @param timeZone The time zone to set Calendar to, or **tbd** */ public static void setTimeZone(Context context, String timeZone) { - if (TextUtils.isEmpty(timeZone)) { - if (DEBUG) { - Log.d(TAG, "Empty time zone, nothing to be done."); - } - return; - } - boolean updatePrefs = false; - synchronized (mTZCallbacks) { - if (GeneralPreferences.LOCAL_TZ.equals(timeZone)) { - if (mUseHomeTZ) { - updatePrefs = true; - } - mUseHomeTZ = false; - } else { - if (!mUseHomeTZ || !TextUtils.equals(mHomeTZ, timeZone)) { - updatePrefs = true; - } - mUseHomeTZ = true; - mHomeTZ = timeZone; - } - } - if (updatePrefs) { - setSharedPreference(context, GeneralPreferences.KEY_HOME_TZ_ENABLED, - mUseHomeTZ); - setSharedPreference(context, GeneralPreferences.KEY_HOME_TZ, mHomeTZ); - } - // TODO async update db + mTZUtils.setTimeZone(context, timeZone); } /** @@ -164,34 +133,7 @@ public class Utils { * @return The string value representing the time zone Calendar should display */ public static String getTimeZone(Context context, Runnable callback) { - synchronized (mTZCallbacks){ - if (mFirstTZRequest) { - mTZQueryInProgress = true; - mFirstTZRequest = false; - - SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); - mUseHomeTZ = prefs.getBoolean( - GeneralPreferences.KEY_HOME_TZ_ENABLED, false); - mHomeTZ = prefs.getString( - GeneralPreferences.KEY_HOME_TZ, Time.getCurrentTimezone()); - // TODO kick off async query - // When the async query returns it should synchronize on - // mTZCallbacks, update mUseHomeTZ, mHomeTZ, and the - // preferences, set mTZQueryInProgress to false, and call all - // the runnables in mTZCallbacks. - // TODO remove this line when we have a query - mTZQueryInProgress = false; - } - if (mTZQueryInProgress) { - mTZCallbacks.add(callback); - } - } - return mUseHomeTZ ? mHomeTZ : Time.getCurrentTimezone(); - } - - public static String getSharedPreference(Context context, String key, String defaultValue) { - SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); - return prefs.getString(key, defaultValue); + return mTZUtils.getTimeZone(context, callback); } /** @@ -206,13 +148,12 @@ public class Utils { */ public static String formatDateRange(Context context, long startMillis, long endMillis, int flags) { - String date; - synchronized (mSB) { - mSB.setLength(0); - date = DateUtils.formatDateRange(context, mF, startMillis, endMillis, flags, - getTimeZone(context, null)).toString(); - } - return date; + return mTZUtils.formatDateRange(context, startMillis, endMillis, flags); + } + + public static String getSharedPreference(Context context, String key, String defaultValue) { + SharedPreferences prefs = GeneralPreferences.getSharedPreferences(context); + return prefs.getString(key, defaultValue); } public static int getSharedPreference(Context context, String key, int defaultValue) { |