diff options
author | Daniel Sandler <dsandler@android.com> | 2010-04-13 16:23:58 -0400 |
---|---|---|
committer | Daniel Sandler <dsandler@android.com> | 2010-04-14 14:36:10 -0400 |
commit | dca661236c73ecd819cfea964c6f8170e5cc40ae (patch) | |
tree | 5edc07d1ac493d27b0346928be18d605dc1df186 /src/com/android/launcher2/LauncherModel.java | |
parent | 7018d8e32761d65816c01f62b094e17e44c7ffb9 (diff) | |
download | android_packages_apps_Trebuchet-dca661236c73ecd819cfea964c6f8170e5cc40ae.tar.gz android_packages_apps_Trebuchet-dca661236c73ecd819cfea964c6f8170e5cc40ae.tar.bz2 android_packages_apps_Trebuchet-dca661236c73ecd819cfea964c6f8170e5cc40ae.zip |
Batch loading of icons for AllApps.
AllAppsList now maintains <data> and <added> in sorted
order, to amortize the cost of sorting the apps list over
multiple batches.
Launcher boosts thread priority on first launch, but we now
reduce thread priority to normal after the main workspace
has been drawn but before all apps are loaded.
Experimental feature: a short delay is introduced between
batches to help free up the CPU (as well as to show that we
are indeed batching apps).
Bug: 2562420
Change-Id: I2035ec3e819b4e7993a80c6d03bfad3914c95a7a
Diffstat (limited to 'src/com/android/launcher2/LauncherModel.java')
-rw-r--r-- | src/com/android/launcher2/LauncherModel.java | 105 |
1 files changed, 69 insertions, 36 deletions
diff --git a/src/com/android/launcher2/LauncherModel.java b/src/com/android/launcher2/LauncherModel.java index 5e1abe6c2..9766831bb 100644 --- a/src/com/android/launcher2/LauncherModel.java +++ b/src/com/android/launcher2/LauncherModel.java @@ -62,6 +62,8 @@ public class LauncherModel extends BroadcastReceiver { static final boolean DEBUG_LOADERS = false; static final String TAG = "Launcher.Model"; + final int ALL_APPS_LOAD_DELAY = 150; // ms + private final LauncherApplication mApp; private final Object mLock = new Object(); private DeferredHandler mHandler = new DeferredHandler(); @@ -86,6 +88,7 @@ public class LauncherModel extends BroadcastReceiver { public void bindAppsAdded(ArrayList<ApplicationInfo> apps); public void bindAppsUpdated(ArrayList<ApplicationInfo> apps); public void bindAppsRemoved(ArrayList<ApplicationInfo> apps); + public int getAppBatchSize(); } LauncherModel(LauncherApplication app, IconCache iconCache) { @@ -575,6 +578,13 @@ public class LauncherModel extends BroadcastReceiver { } } + // Whew! Hard work done. + synchronized (mLock) { + if (mIsLaunching) { + android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT); + } + } + // Load all apps if they're dirty int allAppsSeq; boolean allAppsDirty; @@ -587,7 +597,7 @@ public class LauncherModel extends BroadcastReceiver { } } if (allAppsDirty) { - loadAllApps(); + loadAndBindAllApps(); } synchronized (mLock) { // If we're not stopped, and nobody has incremented mAllAppsSeq. @@ -599,11 +609,6 @@ public class LauncherModel extends BroadcastReceiver { } } - // Bind all apps - if (allAppsDirty) { - bindAllApps(); - } - // Clear out this reference, otherwise we end up holding it until all of the // callback runnables are done. mContext = null; @@ -1014,7 +1019,9 @@ public class LauncherModel extends BroadcastReceiver { }); } - private void loadAllApps() { + private void loadAndBindAllApps() { + final long t = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; + final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); @@ -1026,53 +1033,79 @@ public class LauncherModel extends BroadcastReceiver { final PackageManager packageManager = mContext.getPackageManager(); final List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0); + int N; + int batchSize = callbacks.getAppBatchSize(); + synchronized (mLock) { mBeforeFirstLoad = false; - mAllAppsList.clear(); - if (apps != null) { - long t = SystemClock.uptimeMillis(); + if (apps == null) return; + N = apps.size(); + if (batchSize <= 0) + batchSize = N; + } + + int i=0; + while (i < N && !mStopped) { + synchronized (mLock) { + final long t2 = DEBUG_LOADERS ? SystemClock.uptimeMillis() : 0; - int N = apps.size(); - for (int i=0; i<N && !mStopped; i++) { + for (int j=0; i<N && j<batchSize; j++) { // This builds the icon bitmaps. mAllAppsList.add(new ApplicationInfo(apps.get(i), mIconCache)); + i++; } + // re-sort before binding this batch to the grid Collections.sort(mAllAppsList.data, APP_NAME_COMPARATOR); Collections.sort(mAllAppsList.added, APP_NAME_COMPARATOR); if (DEBUG_LOADERS) { - Log.d(TAG, "cached app icons in " - + (SystemClock.uptimeMillis()-t) + "ms"); + Log.d(TAG, "batch of " + batchSize + " icons processed in " + + (SystemClock.uptimeMillis()-t2) + "ms"); } } + + mHandler.post(bindAllAppsTask); + + if (ALL_APPS_LOAD_DELAY > 0) { + try { + Thread.sleep(ALL_APPS_LOAD_DELAY); + } catch (InterruptedException exc) { } + } + } + + if (DEBUG_LOADERS) { + Log.d(TAG, "cached all " + N + " apps in " + + (SystemClock.uptimeMillis()-t) + "ms"); } } - private void bindAllApps() { - synchronized (mLock) { - final ArrayList<ApplicationInfo> results - = (ArrayList<ApplicationInfo>) mAllAppsList.data.clone(); - // We're adding this now, so clear out this so we don't re-send them. - mAllAppsList.added = new ArrayList<ApplicationInfo>(); - final Callbacks old = mCallbacks.get(); - mHandler.post(new Runnable() { - public void run() { - final long t = SystemClock.uptimeMillis(); - final int count = results.size(); + final Runnable bindAllAppsTask = new Runnable() { + public void run() { + final long t = SystemClock.uptimeMillis(); + int count = 0; + Callbacks callbacks = null; + ArrayList<ApplicationInfo> results = null; + synchronized (mLock) { + mHandler.cancelRunnable(this); + + results = (ArrayList<ApplicationInfo>) mAllAppsList.data.clone(); + // We're adding this now, so clear out this so we don't re-send them. + mAllAppsList.added = new ArrayList<ApplicationInfo>(); + count = results.size(); + + callbacks = tryGetCallbacks(mCallbacks.get()); + } - Callbacks callbacks = tryGetCallbacks(old); - if (callbacks != null) { - callbacks.bindAllApplications(results); - } + if (callbacks != null && count > 0) { + callbacks.bindAllApplications(results); + } - if (DEBUG_LOADERS) { - Log.d(TAG, "bound app " + count + " icons in " - + (SystemClock.uptimeMillis() - t) + "ms"); - } - } - }); + if (DEBUG_LOADERS) { + Log.d(TAG, "bound " + count + " apps in " + + (SystemClock.uptimeMillis() - t) + "ms"); + } } - } + }; public void dumpState() { Log.d(TAG, "mLoader.mLoaderThread.mContext=" + mContext); |