2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.launcher;
19 import android.widget.CursorAdapter;
20 import android.widget.TextView;
21 import android.widget.ImageView;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.res.Resources;
25 import android.content.pm.PackageManager;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.view.LayoutInflater;
29 import android.database.Cursor;
30 import android.provider.LiveFolders;
31 import android.graphics.drawable.Drawable;
32 import android.graphics.BitmapFactory;
33 import android.graphics.Bitmap;
35 import java.net.URISyntaxException;
36 import java.util.HashMap;
37 import java.lang.ref.SoftReference;
39 class LiveFolderAdapter extends CursorAdapter {
40 private boolean mIsList;
41 private LayoutInflater mInflater;
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;
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);
54 mLauncher.startManagingCursor(getCursor());
57 private static Cursor query(Context context, LiveFolderInfo info) {
58 return context.getContentResolver().query(info.uri, null, null, null, LiveFolders.NAME + " ASC");
61 public View newView(Context context, Cursor cursor, ViewGroup parent) {
63 final ViewHolder holder = new ViewHolder();
66 view = mInflater.inflate(R.layout.application_boxed, parent, false);
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);
73 holder.name = (TextView) view.findViewById(R.id.name);
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);
88 public void bindView(View view, Context context, Cursor cursor) {
89 final ViewHolder holder = (ViewHolder) view.getTag();
91 holder.id = cursor.getLong(holder.idIndex);
92 final Drawable icon = loadIcon(context, cursor, holder);
94 holder.name.setText(cursor.getString(holder.nameIndex));
97 holder.name.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null);
99 final boolean hasIcon = icon != null;
100 holder.icon.setVisibility(hasIcon ? View.VISIBLE : View.GONE);
101 if (hasIcon) holder.icon.setImageDrawable(icon);
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);
109 holder.description.setVisibility(View.GONE);
112 holder.description.setVisibility(View.GONE);
116 if (holder.intentIndex != -1) {
118 holder.intent = Intent.getIntent(cursor.getString(holder.intentIndex));
119 } catch (URISyntaxException e) {
123 holder.useBaseIntent = true;
127 private Drawable loadIcon(Context context, Cursor cursor, ViewHolder holder) {
128 Drawable icon = null;
131 if (holder.iconBitmapIndex != -1) {
132 data = cursor.getBlob(holder.iconBitmapIndex);
136 final SoftReference<Drawable> reference = mCustomIcons.get(holder.id);
137 if (reference != null) {
138 icon = reference.get();
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));
146 } else if (holder.iconResourceIndex != -1 && holder.iconPackageIndex != -1) {
147 final String resource = cursor.getString(holder.iconResourceIndex);
148 icon = mIcons.get(resource);
151 final PackageManager packageManager = context.getPackageManager();
152 Resources resources = packageManager.getResourcesForApplication(
153 cursor.getString(holder.iconPackageIndex));
154 final int id = resources.getIdentifier(resource,
156 icon = Utilities.createIconThumbnail(resources.getDrawable(id), mContext);
157 mIcons.put(resource, icon);
158 } catch (Exception e) {
168 for (Drawable icon : mIcons.values()) {
169 icon.setCallback(null);
173 for (SoftReference<Drawable> icon : mCustomIcons.values()) {
174 final Drawable drawable = icon.get();
175 if (drawable != null) {
176 drawable.setCallback(null);
179 mCustomIcons.clear();
181 final Cursor cursor = getCursor();
182 if (cursor != null) {
186 mLauncher.stopManagingCursor(cursor);
191 static class ViewHolder {
193 TextView description;
198 boolean useBaseIntent;
202 int descriptionIndex = -1;
203 int intentIndex = -1;
204 int iconBitmapIndex = -1;
205 int iconResourceIndex = -1;
206 int iconPackageIndex = -1;