summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher2/AppInfoCache.java
blob: a2d98307ffb95d1477f68cf275b3609eae072c31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * 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 AppInfoCache {
    private static final String TAG = "Launcher.AppInfoCache";

    private static final int INITIAL_ICON_CACHE_CAPACITY = 50;

    private static final HashMap<ComponentName, ApplicationInfo> sCache =
            new HashMap<ComponentName, ApplicationInfo>(INITIAL_ICON_CACHE_CAPACITY);

    /**
     * no public constructor.
     */
    private AppInfoCache() {
    }

    /**
     * For the given ResolveInfo, return an ApplicationInfo and cache the result for later.
     */
    public static ApplicationInfo cache(ResolveInfo info, Context context,
            Utilities.BubbleText bubble) {
        synchronized (sCache) {
            ComponentName componentName = new ComponentName(
                    info.activityInfo.applicationInfo.packageName,
                    info.activityInfo.name);
            ApplicationInfo application = sCache.get(componentName);

            if (application == null) {
                application = new ApplicationInfo();
                application.container = ItemInfo.NO_ID;

                updateTitleAndIcon(info, application, context, bubble);

                application.setActivity(componentName,
                        Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

                sCache.put(componentName, application);
            }

            return application;
        }
    }

    /**
     * Update the entry in the in the cache with its new metadata.
     */
    public static void update(ResolveInfo info, ApplicationInfo applicationInfo, Context context,
            Utilities.BubbleText bubble) {
        synchronized (sCache) {
            updateTitleAndIcon(info, applicationInfo, context, bubble);

            ComponentName componentName = new ComponentName(
                    info.activityInfo.applicationInfo.packageName, info.activityInfo.name);
            sCache.put(componentName, applicationInfo);
        }
    }

    /**
     * Remove any records for the supplied ComponentName.
     */
    public static void remove(ComponentName componentName) {
        synchronized (sCache) {
            sCache.remove(componentName);
        }
    }

    /**
     * Empty out the cache.
     */
    public static void flush() {
        synchronized (sCache) {
            sCache.clear();
        }
    }

    /**
     * Get the icon for the supplied ApplicationInfo.  If that activity already
     * exists in the cache, use that.
     */
    public static Drawable getIconDrawable(PackageManager packageManager, ApplicationInfo info) {
        final ResolveInfo resolveInfo = packageManager.resolveActivity(info.intent, 0);
        if (resolveInfo == null) {
            return null;
        }

        ComponentName componentName = new ComponentName(
                resolveInfo.activityInfo.applicationInfo.packageName,
                resolveInfo.activityInfo.name);
        ApplicationInfo cached;
        synchronized (sCache) {
            cached = sCache.get(componentName);
            if (cached != null) {
                if (cached.icon == null) {
                    cached.icon = resolveInfo.activityInfo.loadIcon(packageManager);
                }
                return cached.icon;
            } else {
                return resolveInfo.activityInfo.loadIcon(packageManager);
            }
        }
    }

    /**
     * Go through the cache and disconnect any of the callbacks in the drawables or we
     * leak the previous Home screen on orientation change.
     */
    public static void unbindDrawables() {
        synchronized (sCache) {
            for (ApplicationInfo appInfo: sCache.values()) {
                if (appInfo.icon != null) {
                    appInfo.icon.setCallback(null);
                }
            }
        }
    }

    /**
     * Update the title and icon.  Don't keep a reference to the context!
     */
    private static void updateTitleAndIcon(ResolveInfo info, ApplicationInfo application,
            Context context, Utilities.BubbleText bubble) {
        final PackageManager packageManager = context.getPackageManager();

        application.title = info.loadLabel(packageManager);
        if (application.title == null) {
            application.title = info.activityInfo.name;
        }

        application.iconBitmap = Utilities.createAllAppsBitmap(
                info.activityInfo.loadIcon(packageManager), context);

        application.titleBitmap = bubble.createTextBitmap(application.title.toString());
    }
}