Ensured list of calendars syncs for individual accounts.

Fixed bug where fragment showing individual account was not requesting
a sync of the list of calendars for that account.

Bug: 6620733
Change-Id: Id9335b2c40264763942e8c45a3cb47f3417ce5a2
diff --git a/src/com/android/calendar/Utils.java b/src/com/android/calendar/Utils.java
index 3afcfd4..a3198cd 100644
--- a/src/com/android/calendar/Utils.java
+++ b/src/com/android/calendar/Utils.java
@@ -18,9 +18,11 @@
 
 import static android.provider.CalendarContract.EXTRA_EVENT_BEGIN_TIME;
 
+import android.accounts.Account;
 import android.app.Activity;
 import android.app.SearchManager;
 import android.content.BroadcastReceiver;
+import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
@@ -36,6 +38,7 @@
 import android.os.Build;
 import android.os.Bundle;
 import android.os.Handler;
+import android.provider.CalendarContract.Calendars;
 import android.text.TextUtils;
 import android.text.format.DateFormat;
 import android.text.format.DateUtils;
@@ -1526,4 +1529,25 @@
         }
         return sVersion;
     }
+
+    /**
+     * Checks the server for an updated list of Calendars (in the background).
+     *
+     * If a Calendar is added on the web (and it is selected and not
+     * hidden) then it will be added to the list of calendars on the phone
+     * (when this finishes).  When a new calendar from the
+     * web is added to the phone, then the events for that calendar are also
+     * downloaded from the web.
+     *
+     * This sync is done automatically in the background when the
+     * SelectCalendars activity and fragment are started.
+     *
+     * @param account - The account to sync. May be null to sync all accounts.
+     */
+    public static void startCalendarMetafeedSync(Account account) {
+        Bundle extras = new Bundle();
+        extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
+        extras.putBoolean("metafeedonly", true);
+        ContentResolver.requestSync(account, Calendars.CONTENT_URI.getAuthority(), extras);
+    }
 }
diff --git a/src/com/android/calendar/selectcalendars/SelectCalendarsSyncFragment.java b/src/com/android/calendar/selectcalendars/SelectCalendarsSyncFragment.java
index 5d9cf69..2fb91c8 100644
--- a/src/com/android/calendar/selectcalendars/SelectCalendarsSyncFragment.java
+++ b/src/com/android/calendar/selectcalendars/SelectCalendarsSyncFragment.java
@@ -18,6 +18,7 @@
 
 import com.android.calendar.AsyncQueryService;
 import com.android.calendar.R;
+import com.android.calendar.Utils;
 import com.android.calendar.selectcalendars.SelectCalendarsSyncAdapter.CalendarRow;
 
 import android.accounts.Account;
@@ -31,9 +32,11 @@
 import android.content.Intent;
 import android.content.Loader;
 import android.content.res.Resources;
+import android.database.ContentObserver;
 import android.database.Cursor;
 import android.net.Uri;
 import android.os.Bundle;
+import android.os.Handler;
 import android.provider.CalendarContract;
 import android.provider.CalendarContract.Calendars;
 import android.view.LayoutInflater;
@@ -69,6 +72,16 @@
     private Account mAccount;
     private final String[] mArgs = new String[2];
     private AsyncQueryService mService;
+    private Handler mHandler = new Handler();
+    private ContentObserver mCalendarsObserver = new ContentObserver(mHandler) {
+        @Override
+        public void onChange(boolean selfChange) {
+            // We don't need our own sync changes to trigger refreshes.
+            if (!selfChange) {
+                getLoaderManager().initLoader(0, null, SelectCalendarsSyncFragment.this);
+            }
+        }
+    };
 
     public SelectCalendarsSyncFragment() {
     }
@@ -116,7 +129,12 @@
         } else {
             mSyncStatus.setVisibility(View.GONE);
             mAccountsButton.setVisibility(View.GONE);
-        }
+
+            // Start a background sync to get the list of calendars from the server.
+            Utils.startCalendarMetafeedSync(mAccount);
+            getActivity().getContentResolver().registerContentObserver(
+                    Calendars.CONTENT_URI, true, mCalendarsObserver);
+       }
     }
 
     @Override
@@ -157,6 +175,7 @@
                 changes.clear();
             }
         }
+        getActivity().getContentResolver().unregisterContentObserver(mCalendarsObserver);
         super.onPause();
     }
 
diff --git a/src/com/android/calendar/selectcalendars/SelectSyncedCalendarsMultiAccountActivity.java b/src/com/android/calendar/selectcalendars/SelectSyncedCalendarsMultiAccountActivity.java
index 5e13d2f..406112f 100644
--- a/src/com/android/calendar/selectcalendars/SelectSyncedCalendarsMultiAccountActivity.java
+++ b/src/com/android/calendar/selectcalendars/SelectSyncedCalendarsMultiAccountActivity.java
@@ -55,9 +55,8 @@
         setContentView(R.layout.select_calendars_multi_accounts_fragment);
         mList = getExpandableListView();
         mList.setEmptyView(findViewById(R.id.loading));
-//TODO change to something that supports group by queries.
         // Start a background sync to get the list of calendars from the server.
-        startCalendarMetafeedSync();
+        Utils.startCalendarMetafeedSync(null);
 
         findViewById(R.id.btn_done).setOnClickListener(this);
         findViewById(R.id.btn_discard).setOnClickListener(this);
@@ -99,6 +98,7 @@
                 "1) GROUP BY (" + ACCOUNT_UNIQUE_KEY, //Cheap hack to make WHERE a GROUP BY query
                 null /* selectionArgs */,
                 Calendars.ACCOUNT_NAME /*sort order*/);
+        //TODO change to something that supports group by queries.
     }
 
     @Override
@@ -166,23 +166,4 @@
         }
         return super.onOptionsItemSelected(item);
     }
-
-    // startCalendarMetafeedSync() checks the server for an updated list of
-    // Calendars (in the background).
-    //
-    // If a Calendar is added on the web (and it is selected and not
-    // hidden) then it will be added to the list of calendars on the phone
-    // (when this finishes).  When a new calendar from the
-    // web is added to the phone, then the events for that calendar are also
-    // downloaded from the web.
-    //
-    // This sync is done automatically in the background when the
-    // SelectCalendars activity is started.
-    private void startCalendarMetafeedSync() {
-        Bundle extras = new Bundle();
-        extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
-        extras.putBoolean("metafeedonly", true);
-        ContentResolver.requestSync(null /* all accounts */,
-                Calendars.CONTENT_URI.getAuthority(), extras);
-    }
 }