Bug 722752 - Show/hide sync banner when sync accounts are added/removed (r=mfinkle)
authorLucas Rocha <lucasr@mozilla.com>
Thu, 02 Feb 2012 16:08:58 +0000
changeset 86075 36fb43ea8f0c75c8236cde01188608a3ab8bcafb
parent 86074 350ba395c507610e74b50fe7d64569ba48a339dd
child 86076 f60a70c9120f96788cd41d2c8e8b4ec080da81ea
push id21990
push userbmo@edmorley.co.uk
push dateFri, 03 Feb 2012 16:46:11 +0000
treeherdermozilla-central@4da18c2e4910 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle
bugs722752
milestone13.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 722752 - Show/hide sync banner when sync accounts are added/removed (r=mfinkle)
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);
 
@@ -221,18 +244,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.