blob: 941228785ee5d5eb89ca0f727aca8e4a3d745b59 [file] [log] [blame]
Dmitri Plotnikov06191cd2009-05-07 14:11:52 -07001/*
2 * Copyright (C) 2009 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.contacts;
18
Jeff Sharkey3f177592009-05-18 15:23:12 -070019import com.android.contacts.EdgeTriggerView.EdgeTriggerListener;
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -070020import com.android.contacts.SocialStreamActivity.MappingCache.Mapping;
Jeff Sharkey3f177592009-05-18 15:23:12 -070021import com.android.providers.contacts2.ContactsContract;
22import com.android.providers.contacts2.ContactsContract.Aggregates;
23import com.android.providers.contacts2.ContactsContract.Contacts;
24import com.android.providers.contacts2.ContactsContract.Data;
25import com.android.providers.contacts2.ContactsContract.CommonDataKinds.Photo;
Jeff Sharkey3f177592009-05-18 15:23:12 -070026import com.android.providers.contacts2.SocialContract.Activities;
Dmitri Plotnikov06191cd2009-05-07 14:11:52 -070027
Jeff Sharkey3f177592009-05-18 15:23:12 -070028import org.xmlpull.v1.XmlPullParser;
29import org.xmlpull.v1.XmlPullParserException;
30
31import android.app.ListActivity;
32import android.content.ContentResolver;
33import android.content.ContentUris;
34import android.content.Context;
Dmitri Plotnikov672cbe62009-05-20 19:07:59 -070035import android.content.Intent;
Jeff Sharkey3f177592009-05-18 15:23:12 -070036import android.content.pm.ApplicationInfo;
37import android.content.pm.PackageManager;
38import android.content.pm.PackageManager.NameNotFoundException;
Jeff Sharkey3f177592009-05-18 15:23:12 -070039import android.content.res.TypedArray;
40import android.database.Cursor;
41import android.graphics.Bitmap;
42import android.graphics.BitmapFactory;
43import android.graphics.Canvas;
44import android.graphics.Paint;
45import android.graphics.PaintFlagsDrawFilter;
Jeff Sharkey3f177592009-05-18 15:23:12 -070046import android.graphics.Rect;
Jeff Sharkey3f177592009-05-18 15:23:12 -070047import android.net.Uri;
48import android.os.Bundle;
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -070049import android.text.SpannableStringBuilder;
Jeff Sharkey3f177592009-05-18 15:23:12 -070050import android.text.format.DateUtils;
Dmitri Plotnikov9a41d432009-05-19 18:33:46 -070051import android.text.style.StyleSpan;
Jeff Sharkey3f177592009-05-18 15:23:12 -070052import android.util.AttributeSet;
53import android.util.Log;
54import android.util.Xml;
55import android.view.LayoutInflater;
56import android.view.View;
57import android.view.ViewGroup;
Jeff Sharkey3f177592009-05-18 15:23:12 -070058import android.widget.CursorAdapter;
59import android.widget.ImageView;
60import android.widget.ListAdapter;
Dmitri Plotnikov672cbe62009-05-20 19:07:59 -070061import android.widget.ListView;
Jeff Sharkey3f177592009-05-18 15:23:12 -070062import android.widget.TextView;
63
64import java.io.IOException;
65import java.util.HashMap;
66import java.util.LinkedList;
67import java.util.List;
68
69public class SocialStreamActivity extends ListActivity implements EdgeTriggerListener {
70 private static final String TAG = "SocialStreamActivity";
71
72 private static final String[] PROJ_ACTIVITIES = new String[] {
73 Activities._ID,
74 Activities.PACKAGE,
75 Activities.MIMETYPE,
76 Activities.AUTHOR_CONTACT_ID,
77 Contacts.AGGREGATE_ID,
78 Aggregates.DISPLAY_NAME,
79 Activities.PUBLISHED,
80 Activities.TITLE,
81 Activities.SUMMARY,
Dmitri Plotnikovf0eb9f52009-05-20 14:44:56 -070082 Activities.THREAD_PUBLISHED,
Dmitri Plotnikov672cbe62009-05-20 19:07:59 -070083 Activities.LINK,
84 Activities.THUMBNAIL,
Jeff Sharkey3f177592009-05-18 15:23:12 -070085 };
86
87 private static final int COL_ID = 0;
88 private static final int COL_PACKAGE = 1;
89 private static final int COL_MIMETYPE = 2;
90 private static final int COL_AUTHOR_CONTACT_ID = 3;
91 private static final int COL_AGGREGATE_ID = 4;
92 private static final int COL_DISPLAY_NAME = 5;
93 private static final int COL_PUBLISHED = 6;
94 private static final int COL_TITLE = 7;
95 private static final int COL_SUMMARY = 8;
Dmitri Plotnikovf0eb9f52009-05-20 14:44:56 -070096 private static final int COL_THREAD_PUBLISHED = 9;
Dmitri Plotnikov672cbe62009-05-20 19:07:59 -070097 private static final int COL_LINK = 10;
98 private static final int COL_THUMBNAIL = 11;
Jeff Sharkey3f177592009-05-18 15:23:12 -070099
Dmitri Plotnikov672cbe62009-05-20 19:07:59 -0700100 public static final int PHOTO_SIZE = 54;
101 public static final int THUMBNAIL_SIZE = 54;
Jeff Sharkey3f177592009-05-18 15:23:12 -0700102
103 private ListAdapter mAdapter;
104
105 private FloatyListView mListView;
106 private EdgeTriggerView mEdgeTrigger;
107 private FastTrackWindow mFastTrack;
108
109 private ContactsCache mContactsCache;
110 private MappingCache mMappingCache;
Dmitri Plotnikov06191cd2009-05-07 14:11:52 -0700111
112 @Override
113 protected void onCreate(Bundle icicle) {
114 super.onCreate(icicle);
115
Jeff Sharkey3f177592009-05-18 15:23:12 -0700116 setContentView(R.layout.social_list);
Dmitri Plotnikov06191cd2009-05-07 14:11:52 -0700117
Jeff Sharkey3f177592009-05-18 15:23:12 -0700118 mContactsCache = new ContactsCache(this);
119 mMappingCache = MappingCache.createAndFill(this);
120
121 Cursor cursor = managedQuery(Activities.CONTENT_URI, PROJ_ACTIVITIES, null, null);
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700122 mAdapter = new SocialAdapter(this, cursor, mContactsCache, mMappingCache);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700123
124 setListAdapter(mAdapter);
125
126 mListView = (FloatyListView)findViewById(android.R.id.list);
127
128 // Find and listen for edge triggers
129 mEdgeTrigger = (EdgeTriggerView)findViewById(R.id.edge_trigger);
130 mEdgeTrigger.setOnEdgeTriggerListener(this);
131 }
132
133 /** {@inheritDoc} */
134 public void onTrigger(float downX, float downY, int edge) {
135 // Find list item user triggered over
136 int position = mListView.pointToPosition((int)downX, (int)downY);
137
138 Cursor cursor = (Cursor)mAdapter.getItem(position);
139 long aggId = cursor.getLong(COL_AGGREGATE_ID);
140
141 Log.d(TAG, "onTrigger found position=" + position + ", contactId=" + aggId);
142
143 Uri aggUri = ContentUris.withAppendedId(ContactsContract.Aggregates.CONTENT_URI, aggId);
144
145 // Dismiss any existing window first
146 if (mFastTrack != null) {
147 mFastTrack.dismiss();
148 }
149
150 mFastTrack = new FastTrackWindow(this, mListView, aggUri, mMappingCache);
151 mListView.setFloatyWindow(mFastTrack, position);
152
153 }
154
Dmitri Plotnikov672cbe62009-05-20 19:07:59 -0700155 @Override
156 protected void onListItemClick(ListView l, View v, int position, long id) {
157 Cursor cursor = (Cursor)getListAdapter().getItem(position);
158
159 // TODO check mime type and if it is supported, launch the corresponding app
160 String link = cursor.getString(COL_LINK);
161 if (link == null) {
162 return;
163 }
164
165 startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(link)));
166 }
167
Jeff Sharkey3f177592009-05-18 15:23:12 -0700168 /**
169 * List adapter for social stream data queried from
170 * {@link Activities#CONTENT_URI}.
171 */
172 private static class SocialAdapter extends CursorAdapter {
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700173 private final Context mContext;
174 private final LayoutInflater mInflater;
175 private final ContactsCache mContactsCache;
176 private final MappingCache mMappingCache;
Dmitri Plotnikov9a41d432009-05-19 18:33:46 -0700177 private final StyleSpan mTextStyleName;
Jeff Sharkey3f177592009-05-18 15:23:12 -0700178
179 private static class SocialHolder {
180 ImageView photo;
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700181 ImageView sourceIcon;
182 TextView content;
183 SpannableStringBuilder contentBuilder = new SpannableStringBuilder();
Dmitri Plotnikov672cbe62009-05-20 19:07:59 -0700184 TextView summary;
185 ImageView thumbnail;
Jeff Sharkey3f177592009-05-18 15:23:12 -0700186 TextView published;
187 }
188
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700189 public SocialAdapter(Context context, Cursor c, ContactsCache contactsCache,
190 MappingCache mappingCache) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700191 super(context, c, true);
192 mContext = context;
193 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
194 mContactsCache = contactsCache;
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700195 mMappingCache = mappingCache;
Dmitri Plotnikov9a41d432009-05-19 18:33:46 -0700196 mTextStyleName = new StyleSpan(android.graphics.Typeface.BOLD);
Dmitri Plotnikovf0eb9f52009-05-20 14:44:56 -0700197 }
198
199 @Override
200 public int getViewTypeCount() {
201 return 2;
202 }
203
204 @Override
205 public int getItemViewType(int position) {
206 Cursor cursor = (Cursor) getItem(position);
207 return isReply(cursor) ? 0 : 1;
208 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700209
210 @Override
211 public void bindView(View view, Context context, Cursor cursor) {
212 SocialHolder holder = (SocialHolder)view.getTag();
213
214 long contactId = cursor.getLong(COL_AUTHOR_CONTACT_ID);
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700215 String name = cursor.getString(COL_DISPLAY_NAME);
216 String title = cursor.getString(COL_TITLE);
Dmitri Plotnikov672cbe62009-05-20 19:07:59 -0700217 String summary = cursor.getString(COL_SUMMARY);
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700218 long published = cursor.getLong(COL_PUBLISHED);
Dmitri Plotnikov672cbe62009-05-20 19:07:59 -0700219 byte[] thumbnailBlob = cursor.getBlob(COL_THUMBNAIL);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700220
221 // TODO: trigger async query to find actual name and photo instead
222 // of using this lazy caching mechanism
Dmitri Plotnikov9a41d432009-05-19 18:33:46 -0700223 Bitmap photo = mContactsCache.getPhoto(contactId);
224 if (photo != null) {
225 holder.photo.setImageBitmap(photo);
226 } else {
227 holder.photo.setImageResource(R.drawable.ic_contact_list_picture);
228 }
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700229 holder.contentBuilder.clear();
230 holder.contentBuilder.append(name);
Dmitri Plotnikov9a41d432009-05-19 18:33:46 -0700231 holder.contentBuilder.append(" ");
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700232 holder.contentBuilder.append(title);
Dmitri Plotnikov9a41d432009-05-19 18:33:46 -0700233 holder.contentBuilder.setSpan(mTextStyleName, 0, name.length(), 0);
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700234 holder.content.setText(holder.contentBuilder);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700235
Dmitri Plotnikov672cbe62009-05-20 19:07:59 -0700236 if (summary == null) {
237 holder.summary.setVisibility(View.GONE);
238 } else {
239 holder.summary.setText(summary);
240 holder.summary.setVisibility(View.VISIBLE);
241 }
242
243 if (thumbnailBlob != null) {
244 Bitmap thumbnail =
245 BitmapFactory.decodeByteArray(thumbnailBlob, 0, thumbnailBlob.length);
246 holder.thumbnail.setImageBitmap(thumbnail);
247 holder.thumbnail.setVisibility(View.VISIBLE);
248 } else {
249 holder.thumbnail.setVisibility(View.GONE);
250 }
251
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700252 CharSequence relativePublished = DateUtils.getRelativeTimeSpanString(published,
253 System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700254 holder.published.setText(relativePublished);
255
Dmitri Plotnikovf0eb9f52009-05-20 14:44:56 -0700256 if (holder.sourceIcon != null) {
257 String packageName = cursor.getString(COL_PACKAGE);
258 String mimeType = cursor.getString(COL_MIMETYPE);
259 Mapping mapping = mMappingCache.getMapping(packageName, mimeType);
260 if (mapping != null && mapping.icon != null) {
261 holder.sourceIcon.setImageBitmap(mapping.icon);
262 } else {
263 holder.sourceIcon.setImageDrawable(null);
264 }
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700265 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700266 }
267
268 @Override
269 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Dmitri Plotnikovf0eb9f52009-05-20 14:44:56 -0700270 View view = mInflater.inflate(
271 isReply(cursor) ? R.layout.social_list_item_reply : R.layout.social_list_item,
272 parent, false);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700273
274 SocialHolder holder = new SocialHolder();
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700275 holder.photo = (ImageView) view.findViewById(R.id.photo);
276 holder.sourceIcon = (ImageView) view.findViewById(R.id.sourceIcon);
277 holder.content = (TextView) view.findViewById(R.id.content);
Dmitri Plotnikov672cbe62009-05-20 19:07:59 -0700278 holder.summary = (TextView) view.findViewById(R.id.summary);
279 holder.thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700280 holder.published = (TextView) view.findViewById(R.id.published);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700281 view.setTag(holder);
282
283 return view;
284 }
Dmitri Plotnikovf0eb9f52009-05-20 14:44:56 -0700285
286 private boolean isReply(Cursor cursor) {
287
288 /*
289 * Comparing the message timestamp to the thread timestamp rather than checking the
290 * in_reply_to field. The rationale for this approach is that in the case when the
291 * original message to which the reply was posted is missing, we want to display
292 * the message as if it was an original; otherwise it would appear to be a reply
293 * to whatever message preceded it in the list. In the case when the original message
294 * of the thread is missing, the two timestamps will be the same.
295 */
296 long published = cursor.getLong(COL_PUBLISHED);
297 long threadPublished = cursor.getLong(COL_THREAD_PUBLISHED);
298 return published != threadPublished;
299 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700300 }
301
302 /**
303 * Keep a cache that maps from {@link Contacts#_ID} to {@link Photo#PHOTO}
304 * values.
305 */
306 private static class ContactsCache {
307 private static final String TAG = "ContactsCache";
308
309 private static final String[] PROJ_DETAILS = new String[] {
310 Data.MIMETYPE,
311 Data.CONTACT_ID,
312 Photo.PHOTO,
313 };
314
315 private static final int COL_MIMETYPE = 0;
316 private static final int COL_CONTACT_ID = 1;
Jeff Sharkey8da253a2009-05-18 21:23:19 -0700317 private static final int COL_PHOTO = 2;
Jeff Sharkey3f177592009-05-18 15:23:12 -0700318
319 private HashMap<Long, Bitmap> mPhoto = new HashMap<Long, Bitmap>();
320
321 public ContactsCache(Context context) {
322 Log.d(TAG, "building ContactsCache...");
323
324 ContentResolver resolver = context.getContentResolver();
325 Cursor cursor = resolver.query(Data.CONTENT_URI, PROJ_DETAILS,
326 Data.MIMETYPE + "=?", new String[] { Photo.CONTENT_ITEM_TYPE }, null);
327
328 while (cursor.moveToNext()) {
329 long contactId = cursor.getLong(COL_CONTACT_ID);
330 String mimeType = cursor.getString(COL_MIMETYPE);
331 if (Photo.CONTENT_ITEM_TYPE.equals(mimeType)) {
332 byte[] photoBlob = cursor.getBlob(COL_PHOTO);
333 Bitmap photo = BitmapFactory.decodeByteArray(photoBlob, 0, photoBlob.length);
334 photo = Utilities.createBitmapThumbnail(photo, context, PHOTO_SIZE);
335
336 mPhoto.put(contactId, photo);
337 }
338 }
339
340 cursor.close();
341 Log.d(TAG, "done building ContactsCache");
342 }
343
344 public Bitmap getPhoto(long contactId) {
345 return mPhoto.get(contactId);
346 }
347 }
348
349 /**
350 * Store a parsed <code>RemoteViewsMapping</code> object, which maps
351 * mime-types to <code>RemoteViews</code> XML resources and possible icons.
352 */
353 public static class MappingCache {
354 private static final String TAG = "MappingCache";
355
356 private static final String TAG_MAPPINGSET = "MappingSet";
357 private static final String TAG_MAPPING = "Mapping";
358
359 private static final String MAPPING_METADATA = "com.android.contacts.stylemap";
360
361 private LinkedList<Mapping> mappings = new LinkedList<Mapping>();
362
363 private MappingCache() {
364 }
365
366 public static class Mapping {
367 String packageName;
368 String mimeType;
369 int remoteViewsRes;
370 Bitmap icon;
371 }
372
373 public void addMapping(Mapping mapping) {
374 mappings.add(mapping);
375 }
376
377 /**
378 * Find matching <code>RemoteViews</code> XML resource for requested
379 * package and mime-type. Returns -1 if no mapping found.
380 */
381 public Mapping getMapping(String packageName, String mimeType) {
382 for (Mapping mapping : mappings) {
383 if (mapping.packageName.equals(packageName) && mapping.mimeType.equals(mimeType)) {
384 return mapping;
385 }
386 }
387 return null;
388 }
389
390 /**
391 * Create a new {@link MappingCache} object and fill by walking across
392 * all packages to find those that provide mappings.
393 */
394 public static MappingCache createAndFill(Context context) {
395 Log.d(TAG, "searching for mimetype mappings...");
396 final PackageManager pm = context.getPackageManager();
397 MappingCache building = new MappingCache();
398 List<ApplicationInfo> installed = pm
399 .getInstalledApplications(PackageManager.GET_META_DATA);
400 for (ApplicationInfo info : installed) {
401 if (info.metaData != null && info.metaData.containsKey(MAPPING_METADATA)) {
402 try {
403 // Found metadata, so clone into their context to
404 // inflate reference
405 Context theirContext = context.createPackageContext(info.packageName, 0);
406 XmlPullParser mappingParser = info.loadXmlMetaData(pm, MAPPING_METADATA);
407 building.inflateMappings(theirContext, info.uid, info.packageName,
408 mappingParser);
409 } catch (NameNotFoundException e) {
410 Log.w(TAG, "Problem creating context for remote package", e);
411 } catch (InflateException e) {
412 Log.w(TAG, "Problem inflating MappingSet from remote package", e);
413 }
414 }
415 }
416 return building;
417 }
418
419 public static class InflateException extends Exception {
420 public InflateException(String message) {
421 super(message);
422 }
423
424 public InflateException(String message, Throwable throwable) {
425 super(message, throwable);
426 }
427 }
428
429 /**
430 * Inflate a <code>MappingSet</code> from an XML resource, assuming the
431 * given package name as the source.
432 */
433 public void inflateMappings(Context context, int uid, String packageName,
434 XmlPullParser parser) throws InflateException {
435 final AttributeSet attrs = Xml.asAttributeSet(parser);
436
437 try {
438 int type;
439 while ((type = parser.next()) != XmlPullParser.START_TAG
440 && type != XmlPullParser.END_DOCUMENT) {
441 // Drain comments and whitespace
442 }
443
444 if (type != XmlPullParser.START_TAG) {
445 throw new InflateException("No start tag found");
446 }
447
448 if (!TAG_MAPPINGSET.equals(parser.getName())) {
449 throw new InflateException("Top level element must be MappingSet");
450 }
451
452 // Parse all children actions
453 final int depth = parser.getDepth();
454 while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
455 && type != XmlPullParser.END_DOCUMENT) {
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700456 if (type == XmlPullParser.END_TAG) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700457 continue;
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700458 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700459
460 if (!TAG_MAPPING.equals(parser.getName())) {
461 throw new InflateException("Expected Mapping tag");
462 }
463
464 // Parse kind, mime-type, and RemoteViews reference
465 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Mapping);
466
467 Mapping mapping = new Mapping();
468 mapping.packageName = packageName;
469 mapping.mimeType = a.getString(R.styleable.Mapping_mimeType);
470 mapping.remoteViewsRes = a.getResourceId(R.styleable.Mapping_remoteViews, -1);
471
472 // Read and resize icon if provided
473 int iconRes = a.getResourceId(R.styleable.Mapping_icon, -1);
474 if (iconRes != -1) {
475 mapping.icon = BitmapFactory
476 .decodeResource(context.getResources(), iconRes);
477 mapping.icon = Utilities.createBitmapThumbnail(mapping.icon, context,
478 FastTrackWindow.ICON_SIZE);
479 }
480
481 addMapping(mapping);
482 Log.d(TAG, "Added mapping for packageName=" + mapping.packageName
483 + ", mimetype=" + mapping.mimeType);
484 }
485 } catch (XmlPullParserException e) {
486 throw new InflateException("Problem reading XML", e);
487 } catch (IOException e) {
488 throw new InflateException("Problem reading XML", e);
489 }
490 }
491 }
492
493 /**
494 * Borrowed from Launcher for {@link Bitmap} resizing.
495 */
496 static final class Utilities {
497 private static final Paint sPaint = new Paint();
498 private static final Rect sBounds = new Rect();
499 private static final Rect sOldBounds = new Rect();
500 private static Canvas sCanvas = new Canvas();
501
502 static {
503 sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
504 Paint.FILTER_BITMAP_FLAG));
505 }
506
507 /**
508 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
509 * The size of the thumbnail is defined by the dimension
510 * android.R.dimen.launcher_application_icon_size. This method is not
511 * thread-safe and should be invoked on the UI thread only.
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700512 *
Jeff Sharkey3f177592009-05-18 15:23:12 -0700513 * @param bitmap The bitmap to get a thumbnail of.
514 * @param context The application's context.
515 * @return A thumbnail for the specified bitmap or the bitmap itself if
516 * the thumbnail could not be created.
517 */
518 static Bitmap createBitmapThumbnail(Bitmap bitmap, Context context, int size) {
519 int width = size;
520 int height = size;
521
522 final int bitmapWidth = bitmap.getWidth();
523 final int bitmapHeight = bitmap.getHeight();
524
525 if (width > 0 && height > 0 && (width < bitmapWidth || height < bitmapHeight)) {
526 final float ratio = (float)bitmapWidth / bitmapHeight;
527
528 if (bitmapWidth > bitmapHeight) {
529 height = (int)(width / ratio);
530 } else if (bitmapHeight > bitmapWidth) {
531 width = (int)(height * ratio);
532 }
533
534 final Bitmap.Config c = (width == size && height == size) ? bitmap.getConfig()
535 : Bitmap.Config.ARGB_8888;
536 final Bitmap thumb = Bitmap.createBitmap(size, size, c);
537 final Canvas canvas = sCanvas;
538 final Paint paint = sPaint;
539 canvas.setBitmap(thumb);
540 paint.setDither(false);
541 paint.setFilterBitmap(true);
542 sBounds.set((size - width) / 2, (size - height) / 2, width, height);
543 sOldBounds.set(0, 0, bitmapWidth, bitmapHeight);
544 canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint);
545 return thumb;
546 }
547
548 return bitmap;
549 }
Dmitri Plotnikov06191cd2009-05-07 14:11:52 -0700550 }
551}