Bug 722752 - Show/hide sync banner when sync accounts are added/removed (r=mfinkle a=akeybl)
authorLucas Rocha <lucasr@mozilla.com>
Thu, 02 Feb 2012 16:08:58 +0000
changeset 88340 b1cd565bc9991489adce96e442f68c057e76e617
parent 88339 01693365cd8501f4f76e71b2783fa14c03a4f32e
child 88341 53b837964a4b5fba0a13cd740cba707d6c0a3133
push idunknown
push userunknown
push dateunknown
reviewersmfinkle, akeybl
bugs722752
milestone12.0a2
Bug 722752 - Show/hide sync banner when sync accounts are added/removed (r=mfinkle a=akeybl)
mobile/android/base/AboutHomeContent.java
--- a/mobile/android/base/AboutHomeContent.java
+++ b/mobile/android/base/AboutHomeContent.java
@@ -54,16 +54,17 @@ import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 import org.mozilla.gecko.db.BrowserDB;
 import org.mozilla.gecko.db.BrowserDB.URLColumns;
 import org.mozilla.gecko.sync.setup.activities.SetupSyncActivity;
 
 import android.accounts.Account;
 import android.accounts.AccountManager;
+import android.accounts.OnAccountsUpdateListener;
 import android.app.Activity;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.res.Configuration;
 import android.database.Cursor;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
@@ -105,31 +106,53 @@ public class AboutHomeContent extends Sc
 
         public static final EnumSet<UpdateFlags> ALL = EnumSet.allOf(UpdateFlags.class);
     }
 
     private Cursor mCursor;
     UriLoadCallback mUriLoadCallback = null;
     private LayoutInflater mInflater;
 
+    private AccountManager mAccountManager;
+
     protected SimpleCursorAdapter mTopSitesAdapter;
     protected GridView mTopSitesGrid;
 
     protected LinearLayout mAddonsLayout;
     protected LinearLayout mLastTabsLayout;
 
     public interface UriLoadCallback {
         public void callback(String uriSpec);
     }
 
     public AboutHomeContent(Context context) {
         super(context);
         mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         mInflater.inflate(R.layout.abouthome_content, this);
 
+        mAccountManager = AccountManager.get(context);
+
+        // The listener will run on the background thread (see 2nd argument)
+        mAccountManager.addOnAccountsUpdatedListener(new OnAccountsUpdateListener() {
+            public void onAccountsUpdated(Account[] accounts) {
+                final GeckoApp.StartupMode startupMode = GeckoApp.mAppContext.getStartupMode();
+                final boolean syncIsSetup = isSyncSetup();
+
+                GeckoApp.mAppContext.mMainHandler.post(new Runnable() {
+                    public void run() {
+                        // The listener might run before the UI is initially updated.
+                        // In this case, we should simply wait for the initial setup
+                        // to happen.
+                        if (mTopSitesAdapter != null)
+                            updateLayout(startupMode, syncIsSetup);
+                    }
+                });
+            }
+        }, GeckoAppShell.getHandler(), true);
+
         setScrollContainer(true);
         setBackgroundResource(R.drawable.abouthome_bg_repeat);
 
         mTopSitesGrid = (GridView)findViewById(R.id.top_sites_grid);
         mTopSitesGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
             public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                 Cursor c = (Cursor) parent.getItemAtPosition(position);
 
@@ -214,18 +237,17 @@ public class AboutHomeContent extends Sc
 
         params.addRule(RelativeLayout.BELOW, below);
         syncContainer.setLayoutParams(params);
 
         syncContainer.setBackgroundResource(background);
     }
 
     private boolean isSyncSetup() {
-        AccountManager accountManager = AccountManager.get(getContext());
-        Account[] accounts = accountManager.getAccountsByType("org.mozilla.firefox_sync");
+        Account[] accounts = mAccountManager.getAccountsByType("org.mozilla.firefox_sync");
         return accounts.length > 0;
     }
 
     private void updateLayout(GeckoApp.StartupMode startupMode, boolean syncIsSetup) {
         // The idea here is that we only show the sync invitation
         // on the very first run. Show sync banner below the top
         // sites section in all other cases.