blob: 4a5c077c899cb88cf21f4472fdc873bc2c68ff1d [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher;
18
19import android.widget.CursorAdapter;
20import android.widget.TextView;
21import android.widget.ImageView;
22import android.content.Context;
23import android.content.Intent;
24import android.content.res.Resources;
25import android.content.pm.PackageManager;
26import android.view.View;
27import android.view.ViewGroup;
28import android.view.LayoutInflater;
29import android.database.Cursor;
30import android.provider.LiveFolders;
31import android.graphics.drawable.Drawable;
32import android.graphics.BitmapFactory;
33import android.graphics.Bitmap;
34
35import java.net.URISyntaxException;
36import java.util.HashMap;
37import java.lang.ref.SoftReference;
38
39class LiveFolderAdapter extends CursorAdapter {
40 private boolean mIsList;
41 private LayoutInflater mInflater;
42
43 private final HashMap<String, Drawable> mIcons = new HashMap<String, Drawable>();
44 private final HashMap<Long, SoftReference<Drawable>> mCustomIcons =
45 new HashMap<Long, SoftReference<Drawable>>();
46 private final Launcher mLauncher;
47
48 LiveFolderAdapter(Launcher launcher, LiveFolderInfo info, Cursor cursor) {
49 super(launcher, cursor, true);
50 mIsList = info.displayMode == LiveFolders.DISPLAY_MODE_LIST;
51 mInflater = LayoutInflater.from(launcher);
52 mLauncher = launcher;
53
54 mLauncher.startManagingCursor(getCursor());
55 }
56
57 static Cursor query(Context context, LiveFolderInfo info) {
58 return context.getContentResolver().query(info.uri, null, null,
59 null, LiveFolders.NAME + " ASC");
60 }
61
62 public View newView(Context context, Cursor cursor, ViewGroup parent) {
63 View view;
64 final ViewHolder holder = new ViewHolder();
65
66 if (!mIsList) {
67 view = mInflater.inflate(R.layout.application_boxed, parent, false);
68 } else {
69 view = mInflater.inflate(R.layout.application_list, parent, false);
70 holder.description = (TextView) view.findViewById(R.id.description);
71 holder.icon = (ImageView) view.findViewById(R.id.icon);
72 }
73
74 holder.name = (TextView) view.findViewById(R.id.name);
75
76 holder.idIndex = cursor.getColumnIndexOrThrow(LiveFolders._ID);
77 holder.nameIndex = cursor.getColumnIndexOrThrow(LiveFolders.NAME);
78 holder.descriptionIndex = cursor.getColumnIndex(LiveFolders.DESCRIPTION);
79 holder.intentIndex = cursor.getColumnIndex(LiveFolders.INTENT);
80 holder.iconBitmapIndex = cursor.getColumnIndex(LiveFolders.ICON_BITMAP);
81 holder.iconResourceIndex = cursor.getColumnIndex(LiveFolders.ICON_RESOURCE);
82 holder.iconPackageIndex = cursor.getColumnIndex(LiveFolders.ICON_PACKAGE);
83
84 view.setTag(holder);
85
86 return view;
87 }
88
89 public void bindView(View view, Context context, Cursor cursor) {
90 final ViewHolder holder = (ViewHolder) view.getTag();
91
92 holder.id = cursor.getLong(holder.idIndex);
93 final Drawable icon = loadIcon(context, cursor, holder);
94
95 holder.name.setText(cursor.getString(holder.nameIndex));
96
97 if (!mIsList) {
98 holder.name.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null);
99 } else {
100 final boolean hasIcon = icon != null;
101 holder.icon.setVisibility(hasIcon ? View.VISIBLE : View.GONE);
102 if (hasIcon) holder.icon.setImageDrawable(icon);
103
104 if (holder.descriptionIndex != -1) {
105 final String description = cursor.getString(holder.descriptionIndex);
106 if (description != null) {
107 holder.description.setText(description);
108 holder.description.setVisibility(View.VISIBLE);
109 } else {
110 holder.description.setVisibility(View.GONE);
111 }
112 } else {
113 holder.description.setVisibility(View.GONE);
114 }
115 }
116
117 if (holder.intentIndex != -1) {
118 try {
119 holder.intent = Intent.getIntent(cursor.getString(holder.intentIndex));
120 } catch (URISyntaxException e) {
121 // Ignore
122 }
123 } else {
124 holder.useBaseIntent = true;
125 }
126 }
127
128 private Drawable loadIcon(Context context, Cursor cursor, ViewHolder holder) {
129 Drawable icon = null;
130 byte[] data = null;
131
132 if (holder.iconBitmapIndex != -1) {
133 data = cursor.getBlob(holder.iconBitmapIndex);
134 }
135
136 if (data != null) {
137 final SoftReference<Drawable> reference = mCustomIcons.get(holder.id);
138 if (reference != null) {
139 icon = reference.get();
140 }
141
142 if (icon == null) {
143 final Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
144 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, mContext));
145 mCustomIcons.put(holder.id, new SoftReference<Drawable>(icon));
146 }
147 } else if (holder.iconResourceIndex != -1 && holder.iconPackageIndex != -1) {
148 final String resource = cursor.getString(holder.iconResourceIndex);
149 icon = mIcons.get(resource);
150 if (icon == null) {
151 try {
152 final PackageManager packageManager = context.getPackageManager();
153 Resources resources = packageManager.getResourcesForApplication(
154 cursor.getString(holder.iconPackageIndex));
155 final int id = resources.getIdentifier(resource,
156 null, null);
157 icon = Utilities.createIconThumbnail(resources.getDrawable(id), mContext);
158 mIcons.put(resource, icon);
159 } catch (Exception e) {
160 // Ignore
161 }
162 }
163 }
164
165 return icon;
166 }
167
168 void cleanup() {
169 for (Drawable icon : mIcons.values()) {
170 icon.setCallback(null);
171 }
172 mIcons.clear();
173
174 for (SoftReference<Drawable> icon : mCustomIcons.values()) {
175 final Drawable drawable = icon.get();
176 if (drawable != null) {
177 drawable.setCallback(null);
178 }
179 }
180 mCustomIcons.clear();
181
182 final Cursor cursor = getCursor();
183 if (cursor != null) {
184 try {
185 cursor.close();
186 } finally {
187 mLauncher.stopManagingCursor(cursor);
188 }
189 }
190 }
191
192 static class ViewHolder {
193 TextView name;
194 TextView description;
195 ImageView icon;
196
197 Intent intent;
198 long id;
199 boolean useBaseIntent;
200
201 int idIndex;
202 int nameIndex;
203 int descriptionIndex = -1;
204 int intentIndex = -1;
205 int iconBitmapIndex = -1;
206 int iconResourceIndex = -1;
207 int iconPackageIndex = -1;
208 }
209}