resolved conflicts for merge of 9ccb1dd0 to master

Change-Id: Ifcc5c862dca3f296c87aa63dfa5bd6c54026d79a
diff --git a/src/com/android/calendar/CalendarPreferenceActivity.java b/src/com/android/calendar/CalendarPreferenceActivity.java
index 518523f..a0fbee6 100644
--- a/src/com/android/calendar/CalendarPreferenceActivity.java
+++ b/src/com/android/calendar/CalendarPreferenceActivity.java
@@ -76,6 +76,9 @@
     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;
@@ -84,6 +87,8 @@
     ListPreference mVibrateWhen;
     RingtonePreference mRingtone;
     CheckBoxPreference mPopup;
+    CheckBoxPreference mUseHomeTZ;
+    ListPreference mHomeTZ;
 
     /** Return a properly configured SharedPreferences instance */
     public static SharedPreferences getSharedPreferences(Context context) {
@@ -115,6 +120,9 @@
         mVibrateWhen = (ListPreference) preferenceScreen.findPreference(KEY_ALERTS_VIBRATE_WHEN);
         mRingtone = (RingtonePreference) preferenceScreen.findPreference(KEY_ALERTS_RINGTONE);
         mPopup = (CheckBoxPreference) preferenceScreen.findPreference(KEY_ALERTS_POPUP);
+        mUseHomeTZ = (CheckBoxPreference) preferenceScreen.findPreference(KEY_HOME_TZ_ENABLED);
+        mHomeTZ = (ListPreference) preferenceScreen.findPreference(KEY_HOME_TZ);
+        mHomeTZ.setSummary(mHomeTZ.getEntry());
 
         migrateOldPreferences(sharedPreferences);
 
@@ -132,6 +140,18 @@
         if (key.equals(KEY_ALERTS)) {
             updateChildPreferences();
         }
+        if (key.equals(KEY_HOME_TZ) || key.equals(KEY_HOME_TZ_ENABLED)) {
+            updateTZPreferences();
+        }
+    }
+
+    private void updateTZPreferences() {
+        String tz = mHomeTZ.getValue();
+        if (!mUseHomeTZ.isChecked()) {
+            tz = LOCAL_TZ;
+        }
+        Utils.setTimeZone(this, tz);
+        mHomeTZ.setSummary(mHomeTZ.getEntry());
     }
 
     /**
diff --git a/src/com/android/calendar/Utils.java b/src/com/android/calendar/Utils.java
index b1c3d2f..c6ea6d3 100644
--- a/src/com/android/calendar/Utils.java
+++ b/src/com/android/calendar/Utils.java
@@ -41,6 +41,8 @@
 import java.util.TimeZone;
 
 public class Utils {
+    private static final boolean DEBUG = true;
+    private static final String TAG = "CalUtils";
     // Set to 0 until we have UI to perform undo
     public static final long UNDO_DELAY = 0;
 
@@ -62,14 +64,13 @@
 
     protected static final String OPEN_EMAIL_MARKER = " <";
     protected static final String CLOSE_EMAIL_MARKER = ">";
-
     /* The corner should be rounded on the top right and bottom right */
     private static final float[] CORNERS = new float[] {0, 0, 5, 5, 5, 5, 0, 0};
 
     public static final String INTENT_KEY_DETAIL_VIEW = "DETAIL_VIEW";
     public static final String INTENT_KEY_VIEW_TYPE = "VIEW";
     public static final String INTENT_VALUE_VIEW_TYPE_DAY = "DAY";
-    
+
     private volatile static boolean mFirstTZRequest = true;
     private volatile static boolean mTZQueryInProgress = false;
 
@@ -103,6 +104,44 @@
     }
 
     /**
+     * Writes a new home time zone to the db.
+     *
+     * Updates the home time zone in the db asynchronously and updates
+     * the local cache. Sending a time zone of **tbd** will cause it to
+     * be set to the device's time zone. null or empty tz will be ignored.
+     *
+     * @param context The calling activity
+     * @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;
+        }
+        synchronized (mTZCallbacks) {
+            if (CalendarPreferenceActivity.LOCAL_TZ.equals(timeZone)) {
+                if (!mUseHomeTZ) {
+                    return;
+                }
+                mUseHomeTZ = false;
+            } else {
+                if (TextUtils.equals(mHomeTZ, timeZone)) {
+                    return;
+                }
+                mUseHomeTZ = true;
+                mHomeTZ = timeZone;
+            }
+        }
+        setSharedPreference(context, CalendarPreferenceActivity.KEY_HOME_TZ_ENABLED, mUseHomeTZ);
+        if (mUseHomeTZ) {
+            setSharedPreference(context, CalendarPreferenceActivity.KEY_HOME_TZ, mHomeTZ);
+        }
+        // TODO async update db
+    }
+
+    /**
      * Gets the time zone that Calendar should be displayed in
      *
      * This is a helper method to get the appropriate time zone for Calendar. If this
@@ -164,6 +203,13 @@
         prefs.edit().putString(key, value).apply();
     }
 
+    static void setSharedPreference(Context context, String key, boolean value) {
+        SharedPreferences prefs = CalendarPreferenceActivity.getSharedPreferences(context);
+        SharedPreferences.Editor editor = prefs.edit();
+        editor.putBoolean(key, value);
+        editor.apply();
+    }
+
     /**
      * Save default agenda/day/week/month view for next time
      *