From 4cf3252fb07623fe4985aaf3bf63360099320a41 Mon Sep 17 00:00:00 2001 From: Marc Blank Date: Tue, 22 Sep 2009 10:24:39 -0700 Subject: [PATCH] Workaround system peculiarity to fix #2134509 * Our AccountManager listener was getting unregistered due to the way ApplicationContext caches AccountManager in a static member * The result was that AccountManager registers its listeners against the FIRST context it is instantiated with! Future calls will all obtain this pre-initialized AccountManager. * In this case, AccountSetupOptions was the first caller (to create the AccountManager Account for Exchange) * The subsequent call to register a listener therefore had its registration tied to the context of AccountSetupOptions, and was therefore leaked when AccountSetupOptions finished! * This caused a chain of problems - the leaked receiver, an Exception in AccountManager when trying to ping the listener in an expired context, and eventually the bug referenced above, which became a P1 issue Change-Id: I478e28d21ca77419afa8011e4ed8101cdf67ab79 --- src/com/android/email/activity/setup/AccountSetupOptions.java | 4 +++- src/com/android/exchange/SyncManager.java | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/com/android/email/activity/setup/AccountSetupOptions.java b/src/com/android/email/activity/setup/AccountSetupOptions.java index f66abcad6..3d2bd0e78 100644 --- a/src/com/android/email/activity/setup/AccountSetupOptions.java +++ b/src/com/android/email/activity/setup/AccountSetupOptions.java @@ -202,7 +202,9 @@ public class AccountSetupOptions extends Activity implements OnClickListener { && mAccount.mHostAuthRecv != null && mAccount.mHostAuthRecv.mProtocol.equals("eas")) { boolean alsoSyncContacts = mSyncContactsView.isChecked(); - ExchangeStore.addSystemAccount(this, mAccount, + // NOTE: We must use the Application here, rather than the current context, because + // all future references to AccountManager will end up using the context passed in here! + ExchangeStore.addSystemAccount(getApplication(), mAccount, alsoSyncContacts, mAccountManagerCallback); } else { finishOnDone(); diff --git a/src/com/android/exchange/SyncManager.java b/src/com/android/exchange/SyncManager.java index fa0174a6b..7bb4376ea 100644 --- a/src/com/android/exchange/SyncManager.java +++ b/src/com/android/exchange/SyncManager.java @@ -1333,8 +1333,8 @@ public class SyncManager extends Service implements Runnable { ContentResolver.addStatusChangeListener(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, mSyncStatusObserver); mAccountsUpdatedListener = new EasAccountsUpdatedListener(); - AccountManager.get(this).addOnAccountsUpdatedListener(mAccountsUpdatedListener, - mHandler, true); + AccountManager.get(getApplication()) + .addOnAccountsUpdatedListener(mAccountsUpdatedListener, mHandler, true); mConnectivityReceiver = new ConnectivityReceiver(); registerReceiver(mConnectivityReceiver,