blob: 486ee22022f32ed3571157082205e2451b165075 [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;
35import android.content.pm.ApplicationInfo;
36import android.content.pm.PackageManager;
37import android.content.pm.PackageManager.NameNotFoundException;
Jeff Sharkey3f177592009-05-18 15:23:12 -070038import android.content.res.TypedArray;
39import android.database.Cursor;
40import android.graphics.Bitmap;
41import android.graphics.BitmapFactory;
42import android.graphics.Canvas;
43import android.graphics.Paint;
44import android.graphics.PaintFlagsDrawFilter;
Jeff Sharkey3f177592009-05-18 15:23:12 -070045import android.graphics.Rect;
Jeff Sharkey3f177592009-05-18 15:23:12 -070046import android.net.Uri;
47import android.os.Bundle;
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -070048import android.text.SpannableStringBuilder;
Jeff Sharkey3f177592009-05-18 15:23:12 -070049import android.text.format.DateUtils;
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -070050import android.text.style.TextAppearanceSpan;
Jeff Sharkey3f177592009-05-18 15:23:12 -070051import android.util.AttributeSet;
52import android.util.Log;
53import android.util.Xml;
54import android.view.LayoutInflater;
55import android.view.View;
56import android.view.ViewGroup;
Jeff Sharkey3f177592009-05-18 15:23:12 -070057import android.widget.CursorAdapter;
58import android.widget.ImageView;
59import android.widget.ListAdapter;
Jeff Sharkey3f177592009-05-18 15:23:12 -070060import android.widget.TextView;
61
62import java.io.IOException;
63import java.util.HashMap;
64import java.util.LinkedList;
65import java.util.List;
66
67public class SocialStreamActivity extends ListActivity implements EdgeTriggerListener {
68 private static final String TAG = "SocialStreamActivity";
69
70 private static final String[] PROJ_ACTIVITIES = new String[] {
71 Activities._ID,
72 Activities.PACKAGE,
73 Activities.MIMETYPE,
74 Activities.AUTHOR_CONTACT_ID,
75 Contacts.AGGREGATE_ID,
76 Aggregates.DISPLAY_NAME,
77 Activities.PUBLISHED,
78 Activities.TITLE,
79 Activities.SUMMARY,
80 };
81
82 private static final int COL_ID = 0;
83 private static final int COL_PACKAGE = 1;
84 private static final int COL_MIMETYPE = 2;
85 private static final int COL_AUTHOR_CONTACT_ID = 3;
86 private static final int COL_AGGREGATE_ID = 4;
87 private static final int COL_DISPLAY_NAME = 5;
88 private static final int COL_PUBLISHED = 6;
89 private static final int COL_TITLE = 7;
90 private static final int COL_SUMMARY = 8;
91
92 public static final int PHOTO_SIZE = 58;
93
94 private ListAdapter mAdapter;
95
96 private FloatyListView mListView;
97 private EdgeTriggerView mEdgeTrigger;
98 private FastTrackWindow mFastTrack;
99
100 private ContactsCache mContactsCache;
101 private MappingCache mMappingCache;
Dmitri Plotnikov06191cd2009-05-07 14:11:52 -0700102
103 @Override
104 protected void onCreate(Bundle icicle) {
105 super.onCreate(icicle);
106
Jeff Sharkey3f177592009-05-18 15:23:12 -0700107 setContentView(R.layout.social_list);
Dmitri Plotnikov06191cd2009-05-07 14:11:52 -0700108
Jeff Sharkey3f177592009-05-18 15:23:12 -0700109 mContactsCache = new ContactsCache(this);
110 mMappingCache = MappingCache.createAndFill(this);
111
112 Cursor cursor = managedQuery(Activities.CONTENT_URI, PROJ_ACTIVITIES, null, null);
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700113 mAdapter = new SocialAdapter(this, cursor, mContactsCache, mMappingCache);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700114
115 setListAdapter(mAdapter);
116
117 mListView = (FloatyListView)findViewById(android.R.id.list);
118
119 // Find and listen for edge triggers
120 mEdgeTrigger = (EdgeTriggerView)findViewById(R.id.edge_trigger);
121 mEdgeTrigger.setOnEdgeTriggerListener(this);
122 }
123
124 /** {@inheritDoc} */
125 public void onTrigger(float downX, float downY, int edge) {
126 // Find list item user triggered over
127 int position = mListView.pointToPosition((int)downX, (int)downY);
128
129 Cursor cursor = (Cursor)mAdapter.getItem(position);
130 long aggId = cursor.getLong(COL_AGGREGATE_ID);
131
132 Log.d(TAG, "onTrigger found position=" + position + ", contactId=" + aggId);
133
134 Uri aggUri = ContentUris.withAppendedId(ContactsContract.Aggregates.CONTENT_URI, aggId);
135
136 // Dismiss any existing window first
137 if (mFastTrack != null) {
138 mFastTrack.dismiss();
139 }
140
141 mFastTrack = new FastTrackWindow(this, mListView, aggUri, mMappingCache);
142 mListView.setFloatyWindow(mFastTrack, position);
143
144 }
145
146 /**
147 * List adapter for social stream data queried from
148 * {@link Activities#CONTENT_URI}.
149 */
150 private static class SocialAdapter extends CursorAdapter {
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700151 private final Context mContext;
152 private final LayoutInflater mInflater;
153 private final ContactsCache mContactsCache;
154 private final MappingCache mMappingCache;
155 private final TextAppearanceSpan mTextAppearanceName;
156 private final TextAppearanceSpan mTextAppearanceStatus;
Jeff Sharkey3f177592009-05-18 15:23:12 -0700157
158 private static class SocialHolder {
159 ImageView photo;
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700160 ImageView sourceIcon;
161 TextView content;
162 SpannableStringBuilder contentBuilder = new SpannableStringBuilder();
Jeff Sharkey3f177592009-05-18 15:23:12 -0700163 TextView published;
164 }
165
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700166 public SocialAdapter(Context context, Cursor c, ContactsCache contactsCache,
167 MappingCache mappingCache) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700168 super(context, c, true);
169 mContext = context;
170 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
171 mContactsCache = contactsCache;
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700172 mMappingCache = mappingCache;
173 mTextAppearanceName =
174 new TextAppearanceSpan(mContext, android.R.style.TextAppearance_Medium);
175 mTextAppearanceStatus =
176 new TextAppearanceSpan(mContext, android.R.style.TextAppearance_Small);
177 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700178
179 @Override
180 public void bindView(View view, Context context, Cursor cursor) {
181 SocialHolder holder = (SocialHolder)view.getTag();
182
183 long contactId = cursor.getLong(COL_AUTHOR_CONTACT_ID);
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700184 String name = cursor.getString(COL_DISPLAY_NAME);
185 String title = cursor.getString(COL_TITLE);
186 long published = cursor.getLong(COL_PUBLISHED);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700187
188 // TODO: trigger async query to find actual name and photo instead
189 // of using this lazy caching mechanism
190 holder.photo.setImageBitmap(mContactsCache.getPhoto(contactId));
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700191 holder.contentBuilder.clear();
192 holder.contentBuilder.append(name);
193 holder.contentBuilder.append(": ");
194 holder.contentBuilder.append(title);
195 holder.contentBuilder.setSpan(mTextAppearanceName, 0, name.length() + 2, 0);
196 holder.contentBuilder.setSpan(mTextAppearanceStatus, name.length() + 2,
197 holder.contentBuilder.length(), 0);
198 holder.content.setText(holder.contentBuilder);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700199
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700200 CharSequence relativePublished = DateUtils.getRelativeTimeSpanString(published,
201 System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700202 holder.published.setText(relativePublished);
203
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700204 String packageName = cursor.getString(COL_PACKAGE);
205 String mimeType = cursor.getString(COL_MIMETYPE);
206 Mapping mapping = mMappingCache.getMapping(packageName, mimeType);
207 if (mapping != null && mapping.icon != null) {
208 holder.sourceIcon.setImageBitmap(mapping.icon);
209 } else {
210 holder.sourceIcon.setImageDrawable(null);
211 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700212 }
213
214 @Override
215 public View newView(Context context, Cursor cursor, ViewGroup parent) {
216 View view = mInflater.inflate(R.layout.social_list_item, parent, false);
217
218 SocialHolder holder = new SocialHolder();
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700219 holder.photo = (ImageView) view.findViewById(R.id.photo);
220 holder.sourceIcon = (ImageView) view.findViewById(R.id.sourceIcon);
221 holder.content = (TextView) view.findViewById(R.id.content);
222 holder.published = (TextView) view.findViewById(R.id.published);
Jeff Sharkey3f177592009-05-18 15:23:12 -0700223 view.setTag(holder);
224
225 return view;
226 }
227 }
228
229 /**
230 * Keep a cache that maps from {@link Contacts#_ID} to {@link Photo#PHOTO}
231 * values.
232 */
233 private static class ContactsCache {
234 private static final String TAG = "ContactsCache";
235
236 private static final String[] PROJ_DETAILS = new String[] {
237 Data.MIMETYPE,
238 Data.CONTACT_ID,
239 Photo.PHOTO,
240 };
241
242 private static final int COL_MIMETYPE = 0;
243 private static final int COL_CONTACT_ID = 1;
Jeff Sharkey8da253a2009-05-18 21:23:19 -0700244 private static final int COL_PHOTO = 2;
Jeff Sharkey3f177592009-05-18 15:23:12 -0700245
246 private HashMap<Long, Bitmap> mPhoto = new HashMap<Long, Bitmap>();
247
248 public ContactsCache(Context context) {
249 Log.d(TAG, "building ContactsCache...");
250
251 ContentResolver resolver = context.getContentResolver();
252 Cursor cursor = resolver.query(Data.CONTENT_URI, PROJ_DETAILS,
253 Data.MIMETYPE + "=?", new String[] { Photo.CONTENT_ITEM_TYPE }, null);
254
255 while (cursor.moveToNext()) {
256 long contactId = cursor.getLong(COL_CONTACT_ID);
257 String mimeType = cursor.getString(COL_MIMETYPE);
258 if (Photo.CONTENT_ITEM_TYPE.equals(mimeType)) {
259 byte[] photoBlob = cursor.getBlob(COL_PHOTO);
260 Bitmap photo = BitmapFactory.decodeByteArray(photoBlob, 0, photoBlob.length);
261 photo = Utilities.createBitmapThumbnail(photo, context, PHOTO_SIZE);
262
263 mPhoto.put(contactId, photo);
264 }
265 }
266
267 cursor.close();
268 Log.d(TAG, "done building ContactsCache");
269 }
270
271 public Bitmap getPhoto(long contactId) {
272 return mPhoto.get(contactId);
273 }
274 }
275
276 /**
277 * Store a parsed <code>RemoteViewsMapping</code> object, which maps
278 * mime-types to <code>RemoteViews</code> XML resources and possible icons.
279 */
280 public static class MappingCache {
281 private static final String TAG = "MappingCache";
282
283 private static final String TAG_MAPPINGSET = "MappingSet";
284 private static final String TAG_MAPPING = "Mapping";
285
286 private static final String MAPPING_METADATA = "com.android.contacts.stylemap";
287
288 private LinkedList<Mapping> mappings = new LinkedList<Mapping>();
289
290 private MappingCache() {
291 }
292
293 public static class Mapping {
294 String packageName;
295 String mimeType;
296 int remoteViewsRes;
297 Bitmap icon;
298 }
299
300 public void addMapping(Mapping mapping) {
301 mappings.add(mapping);
302 }
303
304 /**
305 * Find matching <code>RemoteViews</code> XML resource for requested
306 * package and mime-type. Returns -1 if no mapping found.
307 */
308 public Mapping getMapping(String packageName, String mimeType) {
309 for (Mapping mapping : mappings) {
310 if (mapping.packageName.equals(packageName) && mapping.mimeType.equals(mimeType)) {
311 return mapping;
312 }
313 }
314 return null;
315 }
316
317 /**
318 * Create a new {@link MappingCache} object and fill by walking across
319 * all packages to find those that provide mappings.
320 */
321 public static MappingCache createAndFill(Context context) {
322 Log.d(TAG, "searching for mimetype mappings...");
323 final PackageManager pm = context.getPackageManager();
324 MappingCache building = new MappingCache();
325 List<ApplicationInfo> installed = pm
326 .getInstalledApplications(PackageManager.GET_META_DATA);
327 for (ApplicationInfo info : installed) {
328 if (info.metaData != null && info.metaData.containsKey(MAPPING_METADATA)) {
329 try {
330 // Found metadata, so clone into their context to
331 // inflate reference
332 Context theirContext = context.createPackageContext(info.packageName, 0);
333 XmlPullParser mappingParser = info.loadXmlMetaData(pm, MAPPING_METADATA);
334 building.inflateMappings(theirContext, info.uid, info.packageName,
335 mappingParser);
336 } catch (NameNotFoundException e) {
337 Log.w(TAG, "Problem creating context for remote package", e);
338 } catch (InflateException e) {
339 Log.w(TAG, "Problem inflating MappingSet from remote package", e);
340 }
341 }
342 }
343 return building;
344 }
345
346 public static class InflateException extends Exception {
347 public InflateException(String message) {
348 super(message);
349 }
350
351 public InflateException(String message, Throwable throwable) {
352 super(message, throwable);
353 }
354 }
355
356 /**
357 * Inflate a <code>MappingSet</code> from an XML resource, assuming the
358 * given package name as the source.
359 */
360 public void inflateMappings(Context context, int uid, String packageName,
361 XmlPullParser parser) throws InflateException {
362 final AttributeSet attrs = Xml.asAttributeSet(parser);
363
364 try {
365 int type;
366 while ((type = parser.next()) != XmlPullParser.START_TAG
367 && type != XmlPullParser.END_DOCUMENT) {
368 // Drain comments and whitespace
369 }
370
371 if (type != XmlPullParser.START_TAG) {
372 throw new InflateException("No start tag found");
373 }
374
375 if (!TAG_MAPPINGSET.equals(parser.getName())) {
376 throw new InflateException("Top level element must be MappingSet");
377 }
378
379 // Parse all children actions
380 final int depth = parser.getDepth();
381 while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
382 && type != XmlPullParser.END_DOCUMENT) {
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700383 if (type == XmlPullParser.END_TAG) {
Jeff Sharkey3f177592009-05-18 15:23:12 -0700384 continue;
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700385 }
Jeff Sharkey3f177592009-05-18 15:23:12 -0700386
387 if (!TAG_MAPPING.equals(parser.getName())) {
388 throw new InflateException("Expected Mapping tag");
389 }
390
391 // Parse kind, mime-type, and RemoteViews reference
392 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Mapping);
393
394 Mapping mapping = new Mapping();
395 mapping.packageName = packageName;
396 mapping.mimeType = a.getString(R.styleable.Mapping_mimeType);
397 mapping.remoteViewsRes = a.getResourceId(R.styleable.Mapping_remoteViews, -1);
398
399 // Read and resize icon if provided
400 int iconRes = a.getResourceId(R.styleable.Mapping_icon, -1);
401 if (iconRes != -1) {
402 mapping.icon = BitmapFactory
403 .decodeResource(context.getResources(), iconRes);
404 mapping.icon = Utilities.createBitmapThumbnail(mapping.icon, context,
405 FastTrackWindow.ICON_SIZE);
406 }
407
408 addMapping(mapping);
409 Log.d(TAG, "Added mapping for packageName=" + mapping.packageName
410 + ", mimetype=" + mapping.mimeType);
411 }
412 } catch (XmlPullParserException e) {
413 throw new InflateException("Problem reading XML", e);
414 } catch (IOException e) {
415 throw new InflateException("Problem reading XML", e);
416 }
417 }
418 }
419
420 /**
421 * Borrowed from Launcher for {@link Bitmap} resizing.
422 */
423 static final class Utilities {
424 private static final Paint sPaint = new Paint();
425 private static final Rect sBounds = new Rect();
426 private static final Rect sOldBounds = new Rect();
427 private static Canvas sCanvas = new Canvas();
428
429 static {
430 sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG,
431 Paint.FILTER_BITMAP_FLAG));
432 }
433
434 /**
435 * Returns a Bitmap representing the thumbnail of the specified Bitmap.
436 * The size of the thumbnail is defined by the dimension
437 * android.R.dimen.launcher_application_icon_size. This method is not
438 * thread-safe and should be invoked on the UI thread only.
Dmitri Plotnikov3c690ce2009-05-19 14:43:45 -0700439 *
Jeff Sharkey3f177592009-05-18 15:23:12 -0700440 * @param bitmap The bitmap to get a thumbnail of.
441 * @param context The application's context.
442 * @return A thumbnail for the specified bitmap or the bitmap itself if
443 * the thumbnail could not be created.
444 */
445 static Bitmap createBitmapThumbnail(Bitmap bitmap, Context context, int size) {
446 int width = size;
447 int height = size;
448
449 final int bitmapWidth = bitmap.getWidth();
450 final int bitmapHeight = bitmap.getHeight();
451
452 if (width > 0 && height > 0 && (width < bitmapWidth || height < bitmapHeight)) {
453 final float ratio = (float)bitmapWidth / bitmapHeight;
454
455 if (bitmapWidth > bitmapHeight) {
456 height = (int)(width / ratio);
457 } else if (bitmapHeight > bitmapWidth) {
458 width = (int)(height * ratio);
459 }
460
461 final Bitmap.Config c = (width == size && height == size) ? bitmap.getConfig()
462 : Bitmap.Config.ARGB_8888;
463 final Bitmap thumb = Bitmap.createBitmap(size, size, c);
464 final Canvas canvas = sCanvas;
465 final Paint paint = sPaint;
466 canvas.setBitmap(thumb);
467 paint.setDither(false);
468 paint.setFilterBitmap(true);
469 sBounds.set((size - width) / 2, (size - height) / 2, width, height);
470 sOldBounds.set(0, 0, bitmapWidth, bitmapHeight);
471 canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint);
472 return thumb;
473 }
474
475 return bitmap;
476 }
Dmitri Plotnikov06191cd2009-05-07 14:11:52 -0700477 }
478}