diff options
Diffstat (limited to 'src/com/android/launcher2/IconCache.java')
-rw-r--r-- | src/com/android/launcher2/IconCache.java | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/src/com/android/launcher2/IconCache.java b/src/com/android/launcher2/IconCache.java new file mode 100644 index 000000000..847cab72e --- /dev/null +++ b/src/com/android/launcher2/IconCache.java @@ -0,0 +1,138 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.launcher2; + +import android.content.ComponentName; +import android.content.ContentResolver; +import android.content.ContentValues; +import android.content.Intent; +import android.content.Context; +import android.content.pm.ActivityInfo; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; +import android.content.res.Resources; +import android.database.Cursor; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.graphics.drawable.Drawable; +import android.graphics.drawable.BitmapDrawable; +import android.net.Uri; +import android.util.Log; +import android.os.Process; + +import java.lang.ref.WeakReference; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/** + * Cache of application icons. Icons can be made from any thread. + */ +public class IconCache { + private static final String TAG = "Launcher.IconCache"; + + private static final int INITIAL_ICON_CACHE_CAPACITY = 50; + + private static class CacheEntry { + public Bitmap icon; + public String title; + public Bitmap titleBitmap; + } + + private LauncherApplication mContext; + private PackageManager mPackageManager; + private Utilities.BubbleText mBubble; + private final HashMap<ComponentName, CacheEntry> mCache = + new HashMap<ComponentName, CacheEntry>(INITIAL_ICON_CACHE_CAPACITY); + + public IconCache(LauncherApplication context) { + mContext = context; + mPackageManager = context.getPackageManager(); + mBubble = new Utilities.BubbleText(context); + } + + /** + * Remove any records for the supplied ComponentName. + */ + public void remove(ComponentName componentName) { + synchronized (mCache) { + mCache.remove(componentName); + } + } + + /** + * Empty out the cache. + */ + public void flush() { + synchronized (mCache) { + mCache.clear(); + } + } + + /** + * Fill in "application" with the icon and label for "info." + */ + public void getTitleAndIcon(ApplicationInfo application, ResolveInfo info) { + synchronized (mCache) { + CacheEntry entry = cacheLocked(application.componentName, info); + + application.title = entry.title; + application.titleBitmap = entry.titleBitmap; + application.iconBitmap = entry.icon; + } + } + + public Bitmap getIcon(Intent intent) { + final ResolveInfo resolveInfo = mPackageManager.resolveActivity(intent, 0); + ComponentName component = intent.getComponent(); + + if (resolveInfo == null || component == null) { + return null; + } + + CacheEntry entry = cacheLocked(component, resolveInfo); + return entry.icon; + } + + public Bitmap getIcon(ComponentName component, ResolveInfo resolveInfo) { + if (resolveInfo == null || component == null) { + return null; + } + + CacheEntry entry = cacheLocked(component, resolveInfo); + return entry.icon; + } + + private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info) { + CacheEntry entry = mCache.get(componentName); + if (entry == null) { + entry = new CacheEntry(); + + entry.title = info.loadLabel(mPackageManager).toString(); + if (entry.title == null) { + entry.title = info.activityInfo.name; + } + entry.titleBitmap = mBubble.createTextBitmap(entry.title.toString()); + entry.icon = Utilities.createIconBitmap( + info.activityInfo.loadIcon(mPackageManager), mContext); + + mCache.put(componentName, entry); + } + return entry; + } +} + |