From 8d3e159b3baa13adeee1d12f040842d49166ffdb Mon Sep 17 00:00:00 2001 From: Hiroaki Kuriyama Date: Fri, 9 Oct 2015 16:30:03 +0200 Subject: [PATCH] onAccountsUpdated shouldn't be called back after listener unregistered There is a problem of AccountManager that onAccountsUpdated() is called back even after the OnAccountsUpdatedListener is unregistered. It may cause application crash. For example, when rotating a tablet 180 degree with Settings apk running, com.android.settings.Settings is re-launched 2 times successively. (Destroy->Create->Destroy->Create) It repeats adding&removing OnAccountUpdatedListener. When dialog was being opened in the following cases, NullPointerException at BackStackRecord.getBreadCrumbTitle() was happened on 10 inch tablet which has 2 panes on Settings. * Settings > Language&input > Language * Settings > Language & input > Text-to-speech output > Speech rate * Settings > Wi-Fi > Menu > Advanced > Keep Wi-Fi on during sleep * Settings > Wi-Fi > Menu > Wi-Fi Direct > Rename device This fix prevents the undesirable callback. Change-Id: I081f69c90539cca821cf686b4587495135f375e4 --- core/java/android/accounts/AccountManager.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/core/java/android/accounts/AccountManager.java b/core/java/android/accounts/AccountManager.java index 9ef13de6d4443..b9727198a6733 100644 --- a/core/java/android/accounts/AccountManager.java +++ b/core/java/android/accounts/AccountManager.java @@ -1857,12 +1857,16 @@ public class AccountManager { handler = (handler == null) ? mMainHandler : handler; handler.post(new Runnable() { public void run() { - try { - listener.onAccountsUpdated(accountsCopy); - } catch (SQLException e) { - // Better luck next time. If the problem was disk-full, - // the STORAGE_OK intent will re-trigger the update. - Log.e(TAG, "Can't update accounts", e); + synchronized (mAccountsUpdatedListeners) { + try { + if (mAccountsUpdatedListeners.containsKey(listener)) { + listener.onAccountsUpdated(accountsCopy); + } + } catch (SQLException e) { + // Better luck next time. If the problem was disk-full, + // the STORAGE_OK intent will re-trigger the update. + Log.e(TAG, "Can't update accounts", e); + } } } });