blob: 888748b6a01d6d55d5f4a8b325fffa76d091c040 [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;
20import com.android.providers.contacts2.ContactsContract;
21import com.android.providers.contacts2.ContactsContract.Aggregates;
Jeff Sharkey549aa162009-05-21 01:33:30 -070022import com.android.providers.contacts2.ContactsContract.CommonDataKinds;
Jeff Sharkey3f177592009-05-18 15:23:12 -070023import 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 Sharkey549aa162009-05-21 01:33:30 -070039import android.content.res.Resources;
Jeff Sharkey3f177592009-05-18 15:23:12 -070040import android.content.res.TypedArray;
41import android.database.Cursor;
42import android.graphics.Bitmap;
43import android.graphics.BitmapFactory;
44import android.graphics.Canvas;
45import android.graphics.Paint;
46import android.graphics.PaintFlagsDrawFilter;
Jeff Sharkey3f177592009-05-18 15:23:12 -070047import android.graphics.Rect;
Jeff Sharkey3f177592009-05-18 15:23:12 -070048import android.net.Uri;
49import android.os.Bundle;
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -070050import android.text.SpannableStringBuilder;
Jeff Sharkey3f177592009-05-18 15:23:12 -070051import android.text.format.DateUtils;
Dmitri Plotnikov9a41d432009-05-19 18:33:46 -070052import android.text.style.StyleSpan;
Dmitri Plotnikov05f158f2009-05-21 11:37:15 -070053import android.text.style.UnderlineSpan;
Jeff Sharkey3f177592009-05-18 15:23:12 -070054import android.util.AttributeSet;
55import android.util.Log;
56import android.util.Xml;
Jeff Sharkey549aa162009-05-21 01:33:30 -070057import android.view.KeyEvent;
Jeff Sharkey3f177592009-05-18 15:23:12 -070058import android.view.LayoutInflater;
59import android.view.View;
60import android.view.ViewGroup;
Jeff Sharkey549aa162009-05-21 01:33:30 -070061import android.view.View.OnClickListener;
Jeff Sharkey3f177592009-05-18 15:23:12 -070062import android.widget.CursorAdapter;
63import android.widget.ImageView;
64import android.widget.ListAdapter;
Dmitri Plotnikov672cbe62009-05-20 19:07:59 -070065import android.widget.ListView;
Jeff Sharkey3f177592009-05-18 15:23:12 -070066import android.widget.TextView;
67
68import java.io.IOException;
Jeff Sharkey549aa162009-05-21 01:33:30 -070069import java.util.ArrayList;
Jeff Sharkey3f177592009-05-18 15:23:12 -070070import java.util.HashMap;
71import java.util.LinkedList;
72import java.util.List;
73
Jeff Sharkey549aa162009-05-21 01:33:30 -070074public class SocialStreamActivity extends ListActivity implements OnClickListener, EdgeTriggerListener {
Jeff Sharkey3f177592009-05-18 15:23:12 -070075 private static final String TAG = "SocialStreamActivity";
76
77 private static final String[] PROJ_ACTIVITIES = new String[] {
78 Activities._ID,
79 Activities.PACKAGE,
80 Activities.MIMETYPE,
81 Activities.AUTHOR_CONTACT_ID,
82 Contacts.AGGREGATE_ID,
83 Aggregates.DISPLAY_NAME,
84 Activities.PUBLISHED,
85 Activities.TITLE,
86 Activities.SUMMARY,
Dmitri Plotnikovf0eb9f52009-05-20 14:44:56 -070087 Activities.THREAD_PUBLISHED,
Dmitri Plotnikov672cbe62009-05-20 19:07:59 -070088 Activities.LINK,
89 Activities.THUMBNAIL,
Jeff Sharkey3f177592009-05-18 15:23:12 -070090 };
91
92 private static final int COL_ID = 0;
93 private static final int COL_PACKAGE = 1;
94 private static final int COL_MIMETYPE = 2;
95 private static final int COL_AUTHOR_CONTACT_ID = 3;
96 private static final int COL_AGGREGATE_ID = 4;
97 private static final int COL_DISPLAY_NAME = 5;
98 private static final int COL_PUBLISHED = 6;
99 private static final int COL_TITLE = 7;
100 private static final int COL_SUMMARY = 8;
Dmitri Plotnikovf0eb9f52009-05-20 14:44:56 -0700101 private static final int COL_THREAD_PUBLISHED = 9;
Dmitri Plotnikov672cbe62009-05-20 19:07:59 -0700102 private static final int COL_LINK = 10;
103 private static final int COL_THUMBNAIL = 11;
Jeff Sharkey3f177592009-05-18 15:23:12 -0700104
Dmitri Plotnikov672cbe62009-05-20 19:07:59 -0700105 public static final int PHOTO_SIZE = 54;
106 public static final int THUMBNAIL_SIZE = 54;
Jeff Sharkey3f177592009-05-18 15:23:12 -0700107
Jeff Sharkey549aa162009-05-21 01:33:30 -0700108 private SocialAdapter mAdapter;
Jeff Sharkey3f177592009-05-18 15:23:12 -0700109
Jeff Sharkey549aa162009-05-21 01:33:30 -0700110 private ListView mListView;
Jeff Sharkey3f177592009-05-18 15:23:12 -0700111 private EdgeTriggerView mEdgeTrigger;
112 private FastTrackWindow mFastTrack;
Jeff Sharkey549aa162009-05-21 01:33:30 -0700113 private MappingCache mMappingCache;
114
115 private static final boolean USE_GESTURE = false;
Jeff Sharkey3f177592009-05-18 15:23:12 -0700116
117 private ContactsCache mContactsCache;
Dmitri Plotnikov06191cd2009-05-07 14:11:52 -0700118
119 @Override
120 protected void onCreate(Bundle icicle) {
121 super.onCreate(icicle);
122
Jeff Sharkey3f177592009-05-18 15:23:12 -0700123 setContentView(R.layout.social_list);
Dmitri Plotnikov06191cd2009-05-07 14:11:52 -0700124
Jeff Sharkey3f177592009-05-18 15:23:12 -0700125 mContactsCache = new ContactsCache(this);
126 mMappingCache = MappingCache.createAndFill(this);
127
128 Cursor cursor = managedQuery(Activities.CONTENT_URI, PROJ_ACTIVITIES, null, null);
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700129 mAdapter = new SocialAdapter(this, cursor, mContactsCache, mMappingCache);
Jeff Sharkey549aa162009-05-21 01:33:30 -0700130 mAdapter.setPhotoListener(this);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700131
132 setListAdapter(mAdapter);
133
Jeff Sharkey549aa162009-05-21 01:33:30 -0700134 mListView = getListView();
135 mFastTrack = new FastTrackWindow(this);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700136
Jeff Sharkey549aa162009-05-21 01:33:30 -0700137 if (USE_GESTURE) {
138 // Find and listen for edge triggers
139 mEdgeTrigger = (EdgeTriggerView)findViewById(R.id.edge_trigger);
140 mEdgeTrigger.setOnEdgeTriggerListener(this);
141 }
142 }
143
144 /** {@inheritDoc} */
145 public void onClick(View v) {
146 // Clicked on photo, so show fast-track
147 View listItem = (View)v.getParent();
148 showFastTrack(listItem, (Long)v.getTag());
Jeff Sharkey3f177592009-05-18 15:23:12 -0700149 }
150
151 /** {@inheritDoc} */
152 public void onTrigger(float downX, float downY, int edge) {
153 // Find list item user triggered over
Jeff Sharkey549aa162009-05-21 01:33:30 -0700154 final int position = mListView.pointToPosition((int)downX, (int)downY);
155 if (position == ListView.INVALID_POSITION) return;
156
157 // Reverse to find the exact top of the triggered entry
158 final int index = position - mListView.getFirstVisiblePosition();
159 final View anchor = mListView.getChildAt(index);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700160
161 Cursor cursor = (Cursor)mAdapter.getItem(position);
162 long aggId = cursor.getLong(COL_AGGREGATE_ID);
163
Jeff Sharkey549aa162009-05-21 01:33:30 -0700164 showFastTrack(anchor, aggId);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700165
Jeff Sharkey549aa162009-05-21 01:33:30 -0700166 }
167
168 private int[] mLocation = new int[2];
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700169
170 private static final int PHOTO_WIDTH = 54;
171 private static final int PHOTO_HEIGHT = 54;
Jeff Sharkey549aa162009-05-21 01:33:30 -0700172
173 private void showFastTrack(View anchor, long aggId) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700174 Uri aggUri = ContentUris.withAppendedId(ContactsContract.Aggregates.CONTENT_URI, aggId);
175
Jeff Sharkey549aa162009-05-21 01:33:30 -0700176 anchor.getLocationInWindow(mLocation);
177 final int entryTop = mLocation[1];
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700178 final int x = (PHOTO_WIDTH / 2) + 3;
Jeff Sharkey549aa162009-05-21 01:33:30 -0700179
180 mFastTrack.dismiss();
Jeff Sharkey80a193a2009-05-21 14:18:18 -0700181 mFastTrack.show(aggUri, x, entryTop, PHOTO_HEIGHT);
Jeff Sharkey549aa162009-05-21 01:33:30 -0700182 }
183
184 /** {@inheritDoc} */
185 @Override
186 public boolean onKeyDown(int keyCode, KeyEvent event) {
187 // Back key dismisses fast-track when its visible
188 if (keyCode == KeyEvent.KEYCODE_BACK && mFastTrack.isShowing()) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700189 mFastTrack.dismiss();
Jeff Sharkey549aa162009-05-21 01:33:30 -0700190 return true;
Jeff Sharkey3f177592009-05-18 15:23:12 -0700191 }
192
Jeff Sharkey549aa162009-05-21 01:33:30 -0700193 return super.onKeyDown(keyCode, event);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700194 }
195
Dmitri Plotnikov672cbe62009-05-20 19:07:59 -0700196 @Override
197 protected void onListItemClick(ListView l, View v, int position, long id) {
198 Cursor cursor = (Cursor)getListAdapter().getItem(position);
199
200 // TODO check mime type and if it is supported, launch the corresponding app
201 String link = cursor.getString(COL_LINK);
202 if (link == null) {
203 return;
204 }
Dmitri Plotnikov672cbe62009-05-20 19:07:59 -0700205 startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(link)));
206 }
207
Jeff Sharkey3f177592009-05-18 15:23:12 -0700208 /**
209 * List adapter for social stream data queried from
210 * {@link Activities#CONTENT_URI}.
211 */
212 private static class SocialAdapter extends CursorAdapter {
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700213 private final Context mContext;
214 private final LayoutInflater mInflater;
215 private final ContactsCache mContactsCache;
216 private final MappingCache mMappingCache;
Dmitri Plotnikov9a41d432009-05-19 18:33:46 -0700217 private final StyleSpan mTextStyleName;
Dmitri Plotnikov05f158f2009-05-21 11:37:15 -0700218 private final UnderlineSpan mTextStyleLink;
Jeff Sharkey549aa162009-05-21 01:33:30 -0700219 private OnClickListener mPhotoListener;
220 private SpannableStringBuilder mBuilder = new SpannableStringBuilder();
Jeff Sharkey3f177592009-05-18 15:23:12 -0700221
222 private static class SocialHolder {
223 ImageView photo;
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700224 ImageView sourceIcon;
225 TextView content;
Dmitri Plotnikov672cbe62009-05-20 19:07:59 -0700226 TextView summary;
Dmitri Plotnikov05f158f2009-05-21 11:37:15 -0700227 SpannableStringBuilder summaryBuilder = new SpannableStringBuilder();
Dmitri Plotnikov672cbe62009-05-20 19:07:59 -0700228 ImageView thumbnail;
Jeff Sharkey3f177592009-05-18 15:23:12 -0700229 TextView published;
230 }
231
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700232 public SocialAdapter(Context context, Cursor c, ContactsCache contactsCache,
233 MappingCache mappingCache) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700234 super(context, c, true);
235 mContext = context;
236 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
237 mContactsCache = contactsCache;
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700238 mMappingCache = mappingCache;
Dmitri Plotnikov9a41d432009-05-19 18:33:46 -0700239 mTextStyleName = new StyleSpan(android.graphics.Typeface.BOLD);
Dmitri Plotnikov05f158f2009-05-21 11:37:15 -0700240 mTextStyleLink = new UnderlineSpan();
Dmitri Plotnikovf0eb9f52009-05-20 14:44:56 -0700241 }
242
Jeff Sharkey549aa162009-05-21 01:33:30 -0700243 public void setPhotoListener(OnClickListener listener) {
244 mPhotoListener = listener;
245 }
246
Dmitri Plotnikovf0eb9f52009-05-20 14:44:56 -0700247 @Override
248 public int getViewTypeCount() {
249 return 2;
250 }
251
252 @Override
253 public int getItemViewType(int position) {
254 Cursor cursor = (Cursor) getItem(position);
255 return isReply(cursor) ? 0 : 1;
256 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700257
258 @Override
259 public void bindView(View view, Context context, Cursor cursor) {
260 SocialHolder holder = (SocialHolder)view.getTag();
261
Jeff Sharkey549aa162009-05-21 01:33:30 -0700262 long aggId = cursor.getLong(COL_AGGREGATE_ID);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700263 long contactId = cursor.getLong(COL_AUTHOR_CONTACT_ID);
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700264 String name = cursor.getString(COL_DISPLAY_NAME);
265 String title = cursor.getString(COL_TITLE);
Dmitri Plotnikov672cbe62009-05-20 19:07:59 -0700266 String summary = cursor.getString(COL_SUMMARY);
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700267 long published = cursor.getLong(COL_PUBLISHED);
Dmitri Plotnikov672cbe62009-05-20 19:07:59 -0700268 byte[] thumbnailBlob = cursor.getBlob(COL_THUMBNAIL);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700269
270 // TODO: trigger async query to find actual name and photo instead
271 // of using this lazy caching mechanism
Dmitri Plotnikov9a41d432009-05-19 18:33:46 -0700272 Bitmap photo = mContactsCache.getPhoto(contactId);
273 if (photo != null) {
274 holder.photo.setImageBitmap(photo);
275 } else {
276 holder.photo.setImageResource(R.drawable.ic_contact_list_picture);
277 }
Jeff Sharkey549aa162009-05-21 01:33:30 -0700278 holder.photo.setTag(aggId);
279
280 mBuilder.clear();
281 mBuilder.append(name);
282 mBuilder.append(" ");
283 mBuilder.append(title);
284 mBuilder.setSpan(mTextStyleName, 0, name.length(), 0);
285 holder.content.setText(mBuilder);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700286
Dmitri Plotnikov672cbe62009-05-20 19:07:59 -0700287 if (summary == null) {
288 holder.summary.setVisibility(View.GONE);
289 } else {
Dmitri Plotnikov05f158f2009-05-21 11:37:15 -0700290 holder.summaryBuilder.clear();
291 holder.summaryBuilder.append(summary);
292 holder.summaryBuilder.setSpan(mTextStyleLink, 0, summary.length(), 0);
293 holder.summary.setText(holder.summaryBuilder);
Dmitri Plotnikov672cbe62009-05-20 19:07:59 -0700294 holder.summary.setVisibility(View.VISIBLE);
295 }
296
297 if (thumbnailBlob != null) {
298 Bitmap thumbnail =
299 BitmapFactory.decodeByteArray(thumbnailBlob, 0, thumbnailBlob.length);
300 holder.thumbnail.setImageBitmap(thumbnail);
301 holder.thumbnail.setVisibility(View.VISIBLE);
302 } else {
303 holder.thumbnail.setVisibility(View.GONE);
304 }
305
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700306 CharSequence relativePublished = DateUtils.getRelativeTimeSpanString(published,
307 System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700308 holder.published.setText(relativePublished);
309
Dmitri Plotnikovf0eb9f52009-05-20 14:44:56 -0700310 if (holder.sourceIcon != null) {
311 String packageName = cursor.getString(COL_PACKAGE);
312 String mimeType = cursor.getString(COL_MIMETYPE);
Jeff Sharkey549aa162009-05-21 01:33:30 -0700313 Mapping mapping = mMappingCache.findMapping(packageName, mimeType);
Dmitri Plotnikovf0eb9f52009-05-20 14:44:56 -0700314 if (mapping != null && mapping.icon != null) {
315 holder.sourceIcon.setImageBitmap(mapping.icon);
316 } else {
317 holder.sourceIcon.setImageDrawable(null);
318 }
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700319 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700320 }
321
322 @Override
323 public View newView(Context context, Cursor cursor, ViewGroup parent) {
Dmitri Plotnikovf0eb9f52009-05-20 14:44:56 -0700324 View view = mInflater.inflate(
325 isReply(cursor) ? R.layout.social_list_item_reply : R.layout.social_list_item,
326 parent, false);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700327
328 SocialHolder holder = new SocialHolder();
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700329 holder.photo = (ImageView) view.findViewById(R.id.photo);
330 holder.sourceIcon = (ImageView) view.findViewById(R.id.sourceIcon);
331 holder.content = (TextView) view.findViewById(R.id.content);
Dmitri Plotnikov672cbe62009-05-20 19:07:59 -0700332 holder.summary = (TextView) view.findViewById(R.id.summary);
333 holder.thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700334 holder.published = (TextView) view.findViewById(R.id.published);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700335 view.setTag(holder);
336
Jeff Sharkey549aa162009-05-21 01:33:30 -0700337 if (!USE_GESTURE) {
338 holder.photo.setOnClickListener(mPhotoListener);
339 }
340
Jeff Sharkey3f177592009-05-18 15:23:12 -0700341 return view;
342 }
Dmitri Plotnikovf0eb9f52009-05-20 14:44:56 -0700343
344 private boolean isReply(Cursor cursor) {
345
346 /*
347 * Comparing the message timestamp to the thread timestamp rather than checking the
348 * in_reply_to field. The rationale for this approach is that in the case when the
349 * original message to which the reply was posted is missing, we want to display
350 * the message as if it was an original; otherwise it would appear to be a reply
351 * to whatever message preceded it in the list. In the case when the original message
352 * of the thread is missing, the two timestamps will be the same.
353 */
354 long published = cursor.getLong(COL_PUBLISHED);
355 long threadPublished = cursor.getLong(COL_THREAD_PUBLISHED);
356 return published != threadPublished;
357 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700358 }
359
360 /**
361 * Keep a cache that maps from {@link Contacts#_ID} to {@link Photo#PHOTO}
362 * values.
363 */
364 private static class ContactsCache {
365 private static final String TAG = "ContactsCache";
366
367 private static final String[] PROJ_DETAILS = new String[] {
368 Data.MIMETYPE,
369 Data.CONTACT_ID,
370 Photo.PHOTO,
371 };
372
373 private static final int COL_MIMETYPE = 0;
374 private static final int COL_CONTACT_ID = 1;
Jeff Sharkey8da253a2009-05-18 21:23:19 -0700375 private static final int COL_PHOTO = 2;
Jeff Sharkey3f177592009-05-18 15:23:12 -0700376
377 private HashMap<Long, Bitmap> mPhoto = new HashMap<Long, Bitmap>();
378
379 public ContactsCache(Context context) {
380 Log.d(TAG, "building ContactsCache...");
381
382 ContentResolver resolver = context.getContentResolver();
383 Cursor cursor = resolver.query(Data.CONTENT_URI, PROJ_DETAILS,
384 Data.MIMETYPE + "=?", new String[] { Photo.CONTENT_ITEM_TYPE }, null);
385
386 while (cursor.moveToNext()) {
387 long contactId = cursor.getLong(COL_CONTACT_ID);
388 String mimeType = cursor.getString(COL_MIMETYPE);
389 if (Photo.CONTENT_ITEM_TYPE.equals(mimeType)) {
390 byte[] photoBlob = cursor.getBlob(COL_PHOTO);
391 Bitmap photo = BitmapFactory.decodeByteArray(photoBlob, 0, photoBlob.length);
392 photo = Utilities.createBitmapThumbnail(photo, context, PHOTO_SIZE);
393
394 mPhoto.put(contactId, photo);
395 }
396 }
397
398 cursor.close();
399 Log.d(TAG, "done building ContactsCache");
400 }
401
402 public Bitmap getPhoto(long contactId) {
403 return mPhoto.get(contactId);
404 }
405 }
406
407 /**
Jeff Sharkey549aa162009-05-21 01:33:30 -0700408 * Store a mapping from a package name and mime-type pair to a set of
409 * {@link RemoteViews}, default icon, and column to use from the
410 * {@link Data} table to use as a summary.
Jeff Sharkey3f177592009-05-18 15:23:12 -0700411 */
Jeff Sharkey549aa162009-05-21 01:33:30 -0700412 public static class Mapping {
413 String packageName;
414 String mimeType;
415 String summaryColumn;
416 int remoteViewsRes;
417 Bitmap icon;
418
419 public Mapping() {
420 }
421
422 public Mapping(String packageName, String mimeType) {
423 this.packageName = packageName;
424 this.mimeType = mimeType;
425 }
426 }
427
428 /**
429 * Store a parsed <code>Mapping</code> object, which maps package and
430 * mime-type combinations to {@link RemoteViews} XML resources, default
431 * icons, and summary columns in the {@link Data} table.
432 */
433 public static class MappingCache extends HashMap<String, Mapping> {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700434 private static final String TAG = "MappingCache";
435
436 private static final String TAG_MAPPINGSET = "MappingSet";
437 private static final String TAG_MAPPING = "Mapping";
438
439 private static final String MAPPING_METADATA = "com.android.contacts.stylemap";
440
Jeff Sharkey3f177592009-05-18 15:23:12 -0700441
Jeff Sharkey549aa162009-05-21 01:33:30 -0700442 /**
443 * Only allow inflating through
444 * {@link MappingCache#createAndFill(Context)}.
445 */
Jeff Sharkey3f177592009-05-18 15:23:12 -0700446 private MappingCache() {
447 }
448
Jeff Sharkey549aa162009-05-21 01:33:30 -0700449 /**
450 * Add a {@link Mapping} instance to this cache, correctly using
451 * {@link #generateKey(String, String)} when storing.
452 */
Jeff Sharkey3f177592009-05-18 15:23:12 -0700453 public void addMapping(Mapping mapping) {
Jeff Sharkey549aa162009-05-21 01:33:30 -0700454 String hashKey = generateKey(mapping.packageName, mapping.mimeType);
455 put(hashKey, mapping);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700456 }
457
458 /**
Jeff Sharkey549aa162009-05-21 01:33:30 -0700459 * Generate a key used internally for mapping a specific package name
460 * and mime-type to a {@link Mapping}.
Jeff Sharkey3f177592009-05-18 15:23:12 -0700461 */
Jeff Sharkey549aa162009-05-21 01:33:30 -0700462 private String generateKey(String packageName, String mimeType) {
463 return packageName + ";" + mimeType;
464 }
465
466 /**
467 * Find matching mapping for requested package and mime-type. Returns
468 * null if no mapping found.
469 */
470 public Mapping findMapping(String packageName, String mimeType) {
471 // Search for common mapping first
472 final String commonMapping = generateKey(CommonDataKinds.PACKAGE_COMMON, mimeType);
473 if (containsKey(commonMapping)) {
474 return get(commonMapping);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700475 }
Jeff Sharkey549aa162009-05-21 01:33:30 -0700476
477 // Otherwise search for package-specific mapping
478 final String specificMapping = generateKey(packageName, mimeType);
479 return get(specificMapping);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700480 }
481
482 /**
483 * Create a new {@link MappingCache} object and fill by walking across
484 * all packages to find those that provide mappings.
485 */
486 public static MappingCache createAndFill(Context context) {
Jeff Sharkey549aa162009-05-21 01:33:30 -0700487 Log.d(TAG, "building mime-type mapping cache...");
Jeff Sharkey3f177592009-05-18 15:23:12 -0700488 final PackageManager pm = context.getPackageManager();
489 MappingCache building = new MappingCache();
490 List<ApplicationInfo> installed = pm
491 .getInstalledApplications(PackageManager.GET_META_DATA);
492 for (ApplicationInfo info : installed) {
493 if (info.metaData != null && info.metaData.containsKey(MAPPING_METADATA)) {
494 try {
495 // Found metadata, so clone into their context to
496 // inflate reference
497 Context theirContext = context.createPackageContext(info.packageName, 0);
498 XmlPullParser mappingParser = info.loadXmlMetaData(pm, MAPPING_METADATA);
499 building.inflateMappings(theirContext, info.uid, info.packageName,
500 mappingParser);
501 } catch (NameNotFoundException e) {
502 Log.w(TAG, "Problem creating context for remote package", e);
503 } catch (InflateException e) {
504 Log.w(TAG, "Problem inflating MappingSet from remote package", e);
505 }
506 }
507 }
508 return building;
509 }
510
511 public static class InflateException extends Exception {
512 public InflateException(String message) {
513 super(message);
514 }
515
516 public InflateException(String message, Throwable throwable) {
517 super(message, throwable);
518 }
519 }
520
521 /**
522 * Inflate a <code>MappingSet</code> from an XML resource, assuming the
523 * given package name as the source.
524 */
525 public void inflateMappings(Context context, int uid, String packageName,
526 XmlPullParser parser) throws InflateException {
527 final AttributeSet attrs = Xml.asAttributeSet(parser);
Jeff Sharkey549aa162009-05-21 01:33:30 -0700528 final Resources res = context.getResources();
Jeff Sharkey3f177592009-05-18 15:23:12 -0700529
530 try {
531 int type;
532 while ((type = parser.next()) != XmlPullParser.START_TAG
533 && type != XmlPullParser.END_DOCUMENT) {
534 // Drain comments and whitespace
535 }
536
537 if (type != XmlPullParser.START_TAG) {
538 throw new InflateException("No start tag found");
539 }
540
541 if (!TAG_MAPPINGSET.equals(parser.getName())) {
542 throw new InflateException("Top level element must be MappingSet");
543 }
544
545 // Parse all children actions
546 final int depth = parser.getDepth();
547 while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
548 && type != XmlPullParser.END_DOCUMENT) {
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700549 if (type == XmlPullParser.END_TAG) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700550 continue;
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700551 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700552
553 if (!TAG_MAPPING.equals(parser.getName())) {
554 throw new InflateException("Expected Mapping tag");
555 }
556
557 // Parse kind, mime-type, and RemoteViews reference
558 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Mapping);
559
560 Mapping mapping = new Mapping();
561 mapping.packageName = packageName;
562 mapping.mimeType = a.getString(R.styleable.Mapping_mimeType);
Jeff Sharkey549aa162009-05-21 01:33:30 -0700563 mapping.summaryColumn = a.getString(R.styleable.Mapping_summaryColumn);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700564 mapping.remoteViewsRes = a.getResourceId(R.styleable.Mapping_remoteViews, -1);
565
566 // Read and resize icon if provided
567 int iconRes = a.getResourceId(R.styleable.Mapping_icon, -1);
568 if (iconRes != -1) {
Jeff Sharkey549aa162009-05-21 01:33:30 -0700569 mapping.icon = BitmapFactory.decodeResource(res, iconRes);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700570 }
571
572 addMapping(mapping);
573 Log.d(TAG, "Added mapping for packageName=" + mapping.packageName
574 + ", mimetype=" + mapping.mimeType);
575 }
576 } catch (XmlPullParserException e) {
577 throw new InflateException("Problem reading XML", e);
578 } catch (IOException e) {
579 throw new InflateException("Problem reading XML", e);
580 }
581 }
582 }
583
584 /**
585 * Borrowed from Launcher for {@link Bitmap} resizing.
586 */
587 static final class Utilities {
588 private static final Paint sPaint = new Paint();
589 private static final Rect sBounds = new Rect();
590 private static final Rect sOldBounds = new Rect();
591 private static Canvas sCanvas = new Canvas();
592
593 static {
594 sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
595 Paint.FILTER_BITMAP_FLAG));
596 }
597
598 /**
599 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
600 * The size of the thumbnail is defined by the dimension
601 * android.R.dimen.launcher_application_icon_size. This method is not
602 * thread-safe and should be invoked on the UI thread only.
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700603 *
Jeff Sharkey3f177592009-05-18 15:23:12 -0700604 * @param bitmap The bitmap to get a thumbnail of.
605 * @param context The application's context.
606 * @return A thumbnail for the specified bitmap or the bitmap itself if
607 * the thumbnail could not be created.
608 */
609 static Bitmap createBitmapThumbnail(Bitmap bitmap, Context context, int size) {
610 int width = size;
611 int height = size;
612
613 final int bitmapWidth = bitmap.getWidth();
614 final int bitmapHeight = bitmap.getHeight();
615
616 if (width > 0 && height > 0 && (width < bitmapWidth || height < bitmapHeight)) {
617 final float ratio = (float)bitmapWidth / bitmapHeight;
618
619 if (bitmapWidth > bitmapHeight) {
620 height = (int)(width / ratio);
621 } else if (bitmapHeight > bitmapWidth) {
622 width = (int)(height * ratio);
623 }
624
625 final Bitmap.Config c = (width == size && height == size) ? bitmap.getConfig()
626 : Bitmap.Config.ARGB_8888;
627 final Bitmap thumb = Bitmap.createBitmap(size, size, c);
628 final Canvas canvas = sCanvas;
629 final Paint paint = sPaint;
630 canvas.setBitmap(thumb);
631 paint.setDither(false);
632 paint.setFilterBitmap(true);
633 sBounds.set((size - width) / 2, (size - height) / 2, width, height);
634 sOldBounds.set(0, 0, bitmapWidth, bitmapHeight);
635 canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint);
636 return thumb;
637 }
638
639 return bitmap;
640 }
Dmitri Plotnikov06191cd2009-05-07 14:11:52 -0700641 }
642}