blob: 01db5a639b4262da42e92d89ea9767b2d834ef31 [file] [log] [blame]
The Android Open Source Projectd097a182008-12-17 18:05:58 -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) {
49 super(launcher, query(launcher, info), 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 private static Cursor query(Context context, LiveFolderInfo info) {
58 return context.getContentResolver().query(info.uri, null, null, null, LiveFolders.NAME + " ASC");
59 }
60
61 public View newView(Context context, Cursor cursor, ViewGroup parent) {
62 View view;
63 final ViewHolder holder = new ViewHolder();
64
65 if (!mIsList) {
66 view = mInflater.inflate(R.layout.application_boxed, parent, false);
67 } else {
68 view = mInflater.inflate(R.layout.application_list, parent, false);
69 holder.description = (TextView) view.findViewById(R.id.description);
70 holder.icon = (ImageView) view.findViewById(R.id.icon);
71 }
72
73 holder.name = (TextView) view.findViewById(R.id.name);
74
75 holder.idIndex = cursor.getColumnIndexOrThrow(LiveFolders._ID);
76 holder.nameIndex = cursor.getColumnIndexOrThrow(LiveFolders.NAME);
77 holder.descriptionIndex = cursor.getColumnIndex(LiveFolders.DESCRIPTION);
78 holder.intentIndex = cursor.getColumnIndex(LiveFolders.INTENT);
79 holder.iconBitmapIndex = cursor.getColumnIndex(LiveFolders.ICON_BITMAP);
80 holder.iconResourceIndex = cursor.getColumnIndex(LiveFolders.ICON_RESOURCE);
81 holder.iconPackageIndex = cursor.getColumnIndex(LiveFolders.ICON_PACKAGE);
82
83 view.setTag(holder);
84
85 return view;
86 }
87
88 public void bindView(View view, Context context, Cursor cursor) {
89 final ViewHolder holder = (ViewHolder) view.getTag();
90
91 holder.id = cursor.getLong(holder.idIndex);
92 final Drawable icon = loadIcon(context, cursor, holder);
93
94 holder.name.setText(cursor.getString(holder.nameIndex));
95
96 if (!mIsList) {
97 holder.name.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null);
98 } else {
99 final boolean hasIcon = icon != null;
100 holder.icon.setVisibility(hasIcon ? View.VISIBLE : View.GONE);
101 if (hasIcon) holder.icon.setImageDrawable(icon);
102
103 if (holder.descriptionIndex != -1) {
104 final String description = cursor.getString(holder.descriptionIndex);
105 if (description != null) {
106 holder.description.setText(description);
107 holder.description.setVisibility(View.VISIBLE);
108 } else {
109 holder.description.setVisibility(View.GONE);
110 }
111 } else {
112 holder.description.setVisibility(View.GONE);
113 }
114 }
115
116 if (holder.intentIndex != -1) {
117 try {
118 holder.intent = Intent.getIntent(cursor.getString(holder.intentIndex));
119 } catch (URISyntaxException e) {
120 // Ignore
121 }
122 } else {
123 holder.useBaseIntent = true;
124 }
125 }
126
127 private Drawable loadIcon(Context context, Cursor cursor, ViewHolder holder) {
128 Drawable icon = null;
129 byte[] data = null;
130
131 if (holder.iconBitmapIndex != -1) {
132 data = cursor.getBlob(holder.iconBitmapIndex);
133 }
134
135 if (data != null) {
136 final SoftReference<Drawable> reference = mCustomIcons.get(holder.id);
137 if (reference != null) {
138 icon = reference.get();
139 }
140
141 if (icon == null) {
142 final Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
143 icon = new FastBitmapDrawable(Utilities.createBitmapThumbnail(bitmap, mContext));
144 mCustomIcons.put(holder.id, new SoftReference<Drawable>(icon));
145 }
146 } else if (holder.iconResourceIndex != -1 && holder.iconPackageIndex != -1) {
147 final String resource = cursor.getString(holder.iconResourceIndex);
148 icon = mIcons.get(resource);
149 if (icon == null) {
150 try {
151 final PackageManager packageManager = context.getPackageManager();
152 Resources resources = packageManager.getResourcesForApplication(
153 cursor.getString(holder.iconPackageIndex));
154 final int id = resources.getIdentifier(resource,
155 null, null);
156 icon = Utilities.createIconThumbnail(resources.getDrawable(id), mContext);
157 mIcons.put(resource, icon);
158 } catch (Exception e) {
159 // Ignore
160 }
161 }
162 }
163
164 return icon;
165 }
166
167 void cleanup() {
168 for (Drawable icon : mIcons.values()) {
169 icon.setCallback(null);
170 }
171 mIcons.clear();
172
173 for (SoftReference<Drawable> icon : mCustomIcons.values()) {
174 final Drawable drawable = icon.get();
175 if (drawable != null) {
176 drawable.setCallback(null);
177 }
178 }
179 mCustomIcons.clear();
180
181 try {
182 getCursor().close();
183 } finally {
184 mLauncher.stopManagingCursor(getCursor());
185 }
186 }
187
188 static class ViewHolder {
189 TextView name;
190 TextView description;
191 ImageView icon;
192
193 Intent intent;
194 long id;
195 boolean useBaseIntent;
196
197 int idIndex;
198 int nameIndex;
199 int descriptionIndex = -1;
200 int intentIndex = -1;
201 int iconBitmapIndex = -1;
202 int iconResourceIndex = -1;
203 int iconPackageIndex = -1;
204 }
205}