The Android Open Source Project | d097a18 | 2008-12-17 18:05:58 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher; |
| 18 | |
| 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; |
| 34 | |
| 35 | import java.net.URISyntaxException; |
| 36 | import java.util.HashMap; |
| 37 | import java.lang.ref.SoftReference; |
| 38 | |
| 39 | class 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 | |
The Android Open Source Project | 98baae6 | 2009-02-13 12:57:53 -0800 | [diff] [blame] | 181 | final Cursor cursor = getCursor(); |
| 182 | if (cursor != null) { |
| 183 | try { |
| 184 | cursor.close(); |
| 185 | } finally { |
| 186 | mLauncher.stopManagingCursor(cursor); |
| 187 | } |
The Android Open Source Project | d097a18 | 2008-12-17 18:05:58 -0800 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
| 191 | static class ViewHolder { |
| 192 | TextView name; |
| 193 | TextView description; |
| 194 | ImageView icon; |
| 195 | |
| 196 | Intent intent; |
| 197 | long id; |
| 198 | boolean useBaseIntent; |
| 199 | |
| 200 | int idIndex; |
| 201 | int nameIndex; |
| 202 | int descriptionIndex = -1; |
| 203 | int intentIndex = -1; |
| 204 | int iconBitmapIndex = -1; |
| 205 | int iconResourceIndex = -1; |
| 206 | int iconPackageIndex = -1; |
| 207 | } |
| 208 | } |