blob: 58b43e33a12f41294ec4ac6b8be1372a720d7bf9 [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
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
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 {
Romain Guy1ce1a242009-06-23 17:34:54 -0700119 holder.intent = Intent.parseUri(cursor.getString(holder.intentIndex), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 } 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);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800144 final Bitmap resampled = Utilities.resampleIconBitmap(bitmap, mContext);
145 if (bitmap != resampled) {
146 // If we got back a different object, we don't need the old one any more.
147 bitmap.recycle();
148 }
149 icon = new FastBitmapDrawable(resampled);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 mCustomIcons.put(holder.id, new SoftReference<Drawable>(icon));
151 }
152 } else if (holder.iconResourceIndex != -1 && holder.iconPackageIndex != -1) {
153 final String resource = cursor.getString(holder.iconResourceIndex);
154 icon = mIcons.get(resource);
155 if (icon == null) {
156 try {
157 final PackageManager packageManager = context.getPackageManager();
158 Resources resources = packageManager.getResourcesForApplication(
159 cursor.getString(holder.iconPackageIndex));
160 final int id = resources.getIdentifier(resource,
161 null, null);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800162 icon = new FastBitmapDrawable(
163 Utilities.createIconBitmap(resources.getDrawable(id), mContext));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 mIcons.put(resource, icon);
165 } catch (Exception e) {
166 // Ignore
167 }
168 }
169 }
170
171 return icon;
172 }
173
174 void cleanup() {
175 for (Drawable icon : mIcons.values()) {
176 icon.setCallback(null);
177 }
178 mIcons.clear();
179
180 for (SoftReference<Drawable> icon : mCustomIcons.values()) {
181 final Drawable drawable = icon.get();
182 if (drawable != null) {
183 drawable.setCallback(null);
184 }
185 }
186 mCustomIcons.clear();
187
188 final Cursor cursor = getCursor();
189 if (cursor != null) {
190 try {
191 cursor.close();
192 } finally {
193 mLauncher.stopManagingCursor(cursor);
194 }
195 }
196 }
197
198 static class ViewHolder {
199 TextView name;
200 TextView description;
201 ImageView icon;
202
203 Intent intent;
204 long id;
205 boolean useBaseIntent;
206
207 int idIndex;
208 int nameIndex;
209 int descriptionIndex = -1;
210 int intentIndex = -1;
211 int iconBitmapIndex = -1;
212 int iconResourceIndex = -1;
213 int iconPackageIndex = -1;
214 }
215}