From 2972c0136de098a7721555821bfb7451324b33ca Mon Sep 17 00:00:00 2001 From: Amith Yamasani Date: Wed, 2 Oct 2013 11:48:46 -0700 Subject: [PATCH] Load account type icons in a background thread before scrolling down This reduces the jank when scrolling down in top-level Settings if you happen to have several account types like Google, Twitter, Facebook, etc. In the process of doing this, discovered that headers get built even when they're not showing (SubSettings). Skip setting up the headers, which can be expensive when there are several accounts. Bug: 11006601 Change-Id: I8b4eafdcc2fc2beecac01317c3c8f07a82febcc0 --- src/com/android/settings/Settings.java | 7 +++++-- .../accounts/AuthenticatorHelper.java | 21 ++++++++++++++++--- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/com/android/settings/Settings.java b/src/com/android/settings/Settings.java index 13f71665e16..f01c03a7f66 100644 --- a/src/com/android/settings/Settings.java +++ b/src/com/android/settings/Settings.java @@ -516,8 +516,10 @@ public class Settings extends PreferenceActivity */ @Override public void onBuildHeaders(List
headers) { - loadHeadersFromResource(R.xml.settings_headers, headers); - updateHeaderList(headers); + if (!onIsHidingHeaders()) { + loadHeadersFromResource(R.xml.settings_headers, headers); + updateHeaderList(headers); + } } private void updateHeaderList(List
target) { @@ -655,6 +657,7 @@ public class Settings extends PreferenceActivity } } accountHeaders.add(accHeader); + mAuthenticatorHelper.preloadDrawableForType(this, accountType); } // Sort by label diff --git a/src/com/android/settings/accounts/AuthenticatorHelper.java b/src/com/android/settings/accounts/AuthenticatorHelper.java index eba785bad4d..a164b8be10e 100644 --- a/src/com/android/settings/accounts/AuthenticatorHelper.java +++ b/src/com/android/settings/accounts/AuthenticatorHelper.java @@ -23,6 +23,7 @@ import android.content.Context; import android.content.pm.PackageManager; import android.content.res.Resources; import android.graphics.drawable.Drawable; +import android.os.AsyncTask; import android.util.Log; import java.util.ArrayList; @@ -45,6 +46,16 @@ public class AuthenticatorHelper { return mEnabledAccountTypes.toArray(new String[mEnabledAccountTypes.size()]); } + public void preloadDrawableForType(final Context context, final String accountType) { + new AsyncTask() { + @Override + protected Void doInBackground(Void... params) { + getDrawableForType(context, accountType); + return null; + } + }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null); + } + /** * Gets an icon associated with a particular account type. If none found, return null. * @param accountType the type of account @@ -52,15 +63,19 @@ public class AuthenticatorHelper { */ public Drawable getDrawableForType(Context context, final String accountType) { Drawable icon = null; - if (mAccTypeIconCache.containsKey(accountType)) { - return mAccTypeIconCache.get(accountType); + synchronized (mAccTypeIconCache) { + if (mAccTypeIconCache.containsKey(accountType)) { + return mAccTypeIconCache.get(accountType); + } } if (mTypeToAuthDescription.containsKey(accountType)) { try { AuthenticatorDescription desc = mTypeToAuthDescription.get(accountType); Context authContext = context.createPackageContext(desc.packageName, 0); icon = authContext.getResources().getDrawable(desc.iconId); - mAccTypeIconCache.put(accountType, icon); + synchronized (mAccTypeIconCache) { + mAccTypeIconCache.put(accountType, icon); + } } catch (PackageManager.NameNotFoundException e) { } catch (Resources.NotFoundException e) { }