Dmitri Plotnikov | 06191cd | 2009-05-07 14:11:52 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.contacts; |
| 18 | |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 19 | import com.android.contacts.EdgeTriggerView.EdgeTriggerListener; |
| 20 | import com.android.providers.contacts2.ContactsContract; |
| 21 | import com.android.providers.contacts2.ContactsContract.Aggregates; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 22 | import com.android.providers.contacts2.ContactsContract.CommonDataKinds; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 23 | import com.android.providers.contacts2.ContactsContract.Contacts; |
| 24 | import com.android.providers.contacts2.ContactsContract.Data; |
| 25 | import com.android.providers.contacts2.ContactsContract.CommonDataKinds.Photo; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 26 | import com.android.providers.contacts2.SocialContract.Activities; |
Dmitri Plotnikov | 06191cd | 2009-05-07 14:11:52 -0700 | [diff] [blame] | 27 | |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 28 | import org.xmlpull.v1.XmlPullParser; |
| 29 | import org.xmlpull.v1.XmlPullParserException; |
| 30 | |
| 31 | import android.app.ListActivity; |
| 32 | import android.content.ContentResolver; |
| 33 | import android.content.ContentUris; |
| 34 | import android.content.Context; |
Dmitri Plotnikov | 672cbe6 | 2009-05-20 19:07:59 -0700 | [diff] [blame] | 35 | import android.content.Intent; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 36 | import android.content.pm.ApplicationInfo; |
| 37 | import android.content.pm.PackageManager; |
| 38 | import android.content.pm.PackageManager.NameNotFoundException; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 39 | import android.content.res.Resources; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 40 | import android.content.res.TypedArray; |
| 41 | import android.database.Cursor; |
| 42 | import android.graphics.Bitmap; |
| 43 | import android.graphics.BitmapFactory; |
| 44 | import android.graphics.Canvas; |
| 45 | import android.graphics.Paint; |
| 46 | import android.graphics.PaintFlagsDrawFilter; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 47 | import android.graphics.Rect; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 48 | import android.net.Uri; |
| 49 | import android.os.Bundle; |
Dmitri Plotnikov | 3c690ce | 2009-05-19 14:43:45 -0700 | [diff] [blame] | 50 | import android.text.SpannableStringBuilder; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 51 | import android.text.format.DateUtils; |
Dmitri Plotnikov | 9a41d43 | 2009-05-19 18:33:46 -0700 | [diff] [blame] | 52 | import android.text.style.StyleSpan; |
Dmitri Plotnikov | 05f158f | 2009-05-21 11:37:15 -0700 | [diff] [blame] | 53 | import android.text.style.UnderlineSpan; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 54 | import android.util.AttributeSet; |
| 55 | import android.util.Log; |
| 56 | import android.util.Xml; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 57 | import android.view.KeyEvent; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 58 | import android.view.LayoutInflater; |
| 59 | import android.view.View; |
| 60 | import android.view.ViewGroup; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 61 | import android.view.View.OnClickListener; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 62 | import android.widget.CursorAdapter; |
| 63 | import android.widget.ImageView; |
| 64 | import android.widget.ListAdapter; |
Dmitri Plotnikov | 672cbe6 | 2009-05-20 19:07:59 -0700 | [diff] [blame] | 65 | import android.widget.ListView; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 66 | import android.widget.TextView; |
| 67 | |
| 68 | import java.io.IOException; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 69 | import java.util.ArrayList; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 70 | import java.util.HashMap; |
| 71 | import java.util.LinkedList; |
| 72 | import java.util.List; |
| 73 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 74 | public class SocialStreamActivity extends ListActivity implements OnClickListener, EdgeTriggerListener { |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 75 | 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 Plotnikov | f0eb9f5 | 2009-05-20 14:44:56 -0700 | [diff] [blame] | 87 | Activities.THREAD_PUBLISHED, |
Dmitri Plotnikov | 672cbe6 | 2009-05-20 19:07:59 -0700 | [diff] [blame] | 88 | Activities.LINK, |
| 89 | Activities.THUMBNAIL, |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 90 | }; |
| 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 Plotnikov | f0eb9f5 | 2009-05-20 14:44:56 -0700 | [diff] [blame] | 101 | private static final int COL_THREAD_PUBLISHED = 9; |
Dmitri Plotnikov | 672cbe6 | 2009-05-20 19:07:59 -0700 | [diff] [blame] | 102 | private static final int COL_LINK = 10; |
| 103 | private static final int COL_THUMBNAIL = 11; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 104 | |
Dmitri Plotnikov | 672cbe6 | 2009-05-20 19:07:59 -0700 | [diff] [blame] | 105 | public static final int PHOTO_SIZE = 54; |
| 106 | public static final int THUMBNAIL_SIZE = 54; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 107 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 108 | private SocialAdapter mAdapter; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 109 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 110 | private ListView mListView; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 111 | private EdgeTriggerView mEdgeTrigger; |
| 112 | private FastTrackWindow mFastTrack; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 113 | private MappingCache mMappingCache; |
| 114 | |
| 115 | private static final boolean USE_GESTURE = false; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 116 | |
| 117 | private ContactsCache mContactsCache; |
Dmitri Plotnikov | 06191cd | 2009-05-07 14:11:52 -0700 | [diff] [blame] | 118 | |
| 119 | @Override |
| 120 | protected void onCreate(Bundle icicle) { |
| 121 | super.onCreate(icicle); |
| 122 | |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 123 | setContentView(R.layout.social_list); |
Dmitri Plotnikov | 06191cd | 2009-05-07 14:11:52 -0700 | [diff] [blame] | 124 | |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 125 | mContactsCache = new ContactsCache(this); |
| 126 | mMappingCache = MappingCache.createAndFill(this); |
| 127 | |
| 128 | Cursor cursor = managedQuery(Activities.CONTENT_URI, PROJ_ACTIVITIES, null, null); |
Dmitri Plotnikov | 3c690ce | 2009-05-19 14:43:45 -0700 | [diff] [blame] | 129 | mAdapter = new SocialAdapter(this, cursor, mContactsCache, mMappingCache); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 130 | mAdapter.setPhotoListener(this); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 131 | |
| 132 | setListAdapter(mAdapter); |
| 133 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 134 | mListView = getListView(); |
| 135 | mFastTrack = new FastTrackWindow(this); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 136 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 137 | 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 Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /** {@inheritDoc} */ |
| 152 | public void onTrigger(float downX, float downY, int edge) { |
| 153 | // Find list item user triggered over |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 154 | 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 Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 160 | |
| 161 | Cursor cursor = (Cursor)mAdapter.getItem(position); |
| 162 | long aggId = cursor.getLong(COL_AGGREGATE_ID); |
| 163 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 164 | showFastTrack(anchor, aggId); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 165 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | private int[] mLocation = new int[2]; |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame^] | 169 | |
| 170 | private static final int PHOTO_WIDTH = 54; |
| 171 | private static final int PHOTO_HEIGHT = 54; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 172 | |
| 173 | private void showFastTrack(View anchor, long aggId) { |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 174 | Uri aggUri = ContentUris.withAppendedId(ContactsContract.Aggregates.CONTENT_URI, aggId); |
| 175 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 176 | anchor.getLocationInWindow(mLocation); |
| 177 | final int entryTop = mLocation[1]; |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame^] | 178 | final int x = (PHOTO_WIDTH / 2) + 3; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 179 | |
| 180 | mFastTrack.dismiss(); |
Jeff Sharkey | 80a193a | 2009-05-21 14:18:18 -0700 | [diff] [blame^] | 181 | mFastTrack.show(aggUri, x, entryTop, PHOTO_HEIGHT); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 182 | } |
| 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 Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 189 | mFastTrack.dismiss(); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 190 | return true; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 193 | return super.onKeyDown(keyCode, event); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Dmitri Plotnikov | 672cbe6 | 2009-05-20 19:07:59 -0700 | [diff] [blame] | 196 | @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 | } |
| 205 | |
| 206 | startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(link))); |
| 207 | } |
| 208 | |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 209 | /** |
| 210 | * List adapter for social stream data queried from |
| 211 | * {@link Activities#CONTENT_URI}. |
| 212 | */ |
| 213 | private static class SocialAdapter extends CursorAdapter { |
Dmitri Plotnikov | 3c690ce | 2009-05-19 14:43:45 -0700 | [diff] [blame] | 214 | private final Context mContext; |
| 215 | private final LayoutInflater mInflater; |
| 216 | private final ContactsCache mContactsCache; |
| 217 | private final MappingCache mMappingCache; |
Dmitri Plotnikov | 9a41d43 | 2009-05-19 18:33:46 -0700 | [diff] [blame] | 218 | private final StyleSpan mTextStyleName; |
Dmitri Plotnikov | 05f158f | 2009-05-21 11:37:15 -0700 | [diff] [blame] | 219 | private final UnderlineSpan mTextStyleLink; |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 220 | private OnClickListener mPhotoListener; |
| 221 | private SpannableStringBuilder mBuilder = new SpannableStringBuilder(); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 222 | |
| 223 | private static class SocialHolder { |
| 224 | ImageView photo; |
Dmitri Plotnikov | 3c690ce | 2009-05-19 14:43:45 -0700 | [diff] [blame] | 225 | ImageView sourceIcon; |
| 226 | TextView content; |
| 227 | SpannableStringBuilder contentBuilder = new SpannableStringBuilder(); |
Dmitri Plotnikov | 672cbe6 | 2009-05-20 19:07:59 -0700 | [diff] [blame] | 228 | TextView summary; |
Dmitri Plotnikov | 05f158f | 2009-05-21 11:37:15 -0700 | [diff] [blame] | 229 | SpannableStringBuilder summaryBuilder = new SpannableStringBuilder(); |
Dmitri Plotnikov | 672cbe6 | 2009-05-20 19:07:59 -0700 | [diff] [blame] | 230 | ImageView thumbnail; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 231 | TextView published; |
| 232 | } |
| 233 | |
Dmitri Plotnikov | 3c690ce | 2009-05-19 14:43:45 -0700 | [diff] [blame] | 234 | public SocialAdapter(Context context, Cursor c, ContactsCache contactsCache, |
| 235 | MappingCache mappingCache) { |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 236 | super(context, c, true); |
| 237 | mContext = context; |
| 238 | mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); |
| 239 | mContactsCache = contactsCache; |
Dmitri Plotnikov | 3c690ce | 2009-05-19 14:43:45 -0700 | [diff] [blame] | 240 | mMappingCache = mappingCache; |
Dmitri Plotnikov | 9a41d43 | 2009-05-19 18:33:46 -0700 | [diff] [blame] | 241 | mTextStyleName = new StyleSpan(android.graphics.Typeface.BOLD); |
Dmitri Plotnikov | 05f158f | 2009-05-21 11:37:15 -0700 | [diff] [blame] | 242 | mTextStyleLink = new UnderlineSpan(); |
Dmitri Plotnikov | f0eb9f5 | 2009-05-20 14:44:56 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 245 | public void setPhotoListener(OnClickListener listener) { |
| 246 | mPhotoListener = listener; |
| 247 | } |
| 248 | |
Dmitri Plotnikov | f0eb9f5 | 2009-05-20 14:44:56 -0700 | [diff] [blame] | 249 | @Override |
| 250 | public int getViewTypeCount() { |
| 251 | return 2; |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public int getItemViewType(int position) { |
| 256 | Cursor cursor = (Cursor) getItem(position); |
| 257 | return isReply(cursor) ? 0 : 1; |
| 258 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 259 | |
| 260 | @Override |
| 261 | public void bindView(View view, Context context, Cursor cursor) { |
| 262 | SocialHolder holder = (SocialHolder)view.getTag(); |
| 263 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 264 | long aggId = cursor.getLong(COL_AGGREGATE_ID); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 265 | long contactId = cursor.getLong(COL_AUTHOR_CONTACT_ID); |
Dmitri Plotnikov | 3c690ce | 2009-05-19 14:43:45 -0700 | [diff] [blame] | 266 | String name = cursor.getString(COL_DISPLAY_NAME); |
| 267 | String title = cursor.getString(COL_TITLE); |
Dmitri Plotnikov | 672cbe6 | 2009-05-20 19:07:59 -0700 | [diff] [blame] | 268 | String summary = cursor.getString(COL_SUMMARY); |
Dmitri Plotnikov | 3c690ce | 2009-05-19 14:43:45 -0700 | [diff] [blame] | 269 | long published = cursor.getLong(COL_PUBLISHED); |
Dmitri Plotnikov | 672cbe6 | 2009-05-20 19:07:59 -0700 | [diff] [blame] | 270 | byte[] thumbnailBlob = cursor.getBlob(COL_THUMBNAIL); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 271 | |
| 272 | // TODO: trigger async query to find actual name and photo instead |
| 273 | // of using this lazy caching mechanism |
Dmitri Plotnikov | 9a41d43 | 2009-05-19 18:33:46 -0700 | [diff] [blame] | 274 | Bitmap photo = mContactsCache.getPhoto(contactId); |
| 275 | if (photo != null) { |
| 276 | holder.photo.setImageBitmap(photo); |
| 277 | } else { |
| 278 | holder.photo.setImageResource(R.drawable.ic_contact_list_picture); |
| 279 | } |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 280 | holder.photo.setTag(aggId); |
| 281 | |
| 282 | mBuilder.clear(); |
| 283 | mBuilder.append(name); |
| 284 | mBuilder.append(" "); |
| 285 | mBuilder.append(title); |
| 286 | mBuilder.setSpan(mTextStyleName, 0, name.length(), 0); |
| 287 | holder.content.setText(mBuilder); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 288 | |
Dmitri Plotnikov | 672cbe6 | 2009-05-20 19:07:59 -0700 | [diff] [blame] | 289 | if (summary == null) { |
| 290 | holder.summary.setVisibility(View.GONE); |
| 291 | } else { |
Dmitri Plotnikov | 05f158f | 2009-05-21 11:37:15 -0700 | [diff] [blame] | 292 | holder.summaryBuilder.clear(); |
| 293 | holder.summaryBuilder.append(summary); |
| 294 | holder.summaryBuilder.setSpan(mTextStyleLink, 0, summary.length(), 0); |
| 295 | holder.summary.setText(holder.summaryBuilder); |
Dmitri Plotnikov | 672cbe6 | 2009-05-20 19:07:59 -0700 | [diff] [blame] | 296 | holder.summary.setVisibility(View.VISIBLE); |
| 297 | } |
| 298 | |
| 299 | if (thumbnailBlob != null) { |
| 300 | Bitmap thumbnail = |
| 301 | BitmapFactory.decodeByteArray(thumbnailBlob, 0, thumbnailBlob.length); |
| 302 | holder.thumbnail.setImageBitmap(thumbnail); |
| 303 | holder.thumbnail.setVisibility(View.VISIBLE); |
| 304 | } else { |
| 305 | holder.thumbnail.setVisibility(View.GONE); |
| 306 | } |
| 307 | |
Dmitri Plotnikov | 3c690ce | 2009-05-19 14:43:45 -0700 | [diff] [blame] | 308 | CharSequence relativePublished = DateUtils.getRelativeTimeSpanString(published, |
| 309 | System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 310 | holder.published.setText(relativePublished); |
| 311 | |
Dmitri Plotnikov | f0eb9f5 | 2009-05-20 14:44:56 -0700 | [diff] [blame] | 312 | if (holder.sourceIcon != null) { |
| 313 | String packageName = cursor.getString(COL_PACKAGE); |
| 314 | String mimeType = cursor.getString(COL_MIMETYPE); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 315 | Mapping mapping = mMappingCache.findMapping(packageName, mimeType); |
Dmitri Plotnikov | f0eb9f5 | 2009-05-20 14:44:56 -0700 | [diff] [blame] | 316 | if (mapping != null && mapping.icon != null) { |
| 317 | holder.sourceIcon.setImageBitmap(mapping.icon); |
| 318 | } else { |
| 319 | holder.sourceIcon.setImageDrawable(null); |
| 320 | } |
Dmitri Plotnikov | 3c690ce | 2009-05-19 14:43:45 -0700 | [diff] [blame] | 321 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | @Override |
| 325 | public View newView(Context context, Cursor cursor, ViewGroup parent) { |
Dmitri Plotnikov | f0eb9f5 | 2009-05-20 14:44:56 -0700 | [diff] [blame] | 326 | View view = mInflater.inflate( |
| 327 | isReply(cursor) ? R.layout.social_list_item_reply : R.layout.social_list_item, |
| 328 | parent, false); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 329 | |
| 330 | SocialHolder holder = new SocialHolder(); |
Dmitri Plotnikov | 3c690ce | 2009-05-19 14:43:45 -0700 | [diff] [blame] | 331 | holder.photo = (ImageView) view.findViewById(R.id.photo); |
| 332 | holder.sourceIcon = (ImageView) view.findViewById(R.id.sourceIcon); |
| 333 | holder.content = (TextView) view.findViewById(R.id.content); |
Dmitri Plotnikov | 672cbe6 | 2009-05-20 19:07:59 -0700 | [diff] [blame] | 334 | holder.summary = (TextView) view.findViewById(R.id.summary); |
| 335 | holder.thumbnail = (ImageView) view.findViewById(R.id.thumbnail); |
Dmitri Plotnikov | 3c690ce | 2009-05-19 14:43:45 -0700 | [diff] [blame] | 336 | holder.published = (TextView) view.findViewById(R.id.published); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 337 | view.setTag(holder); |
| 338 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 339 | if (!USE_GESTURE) { |
| 340 | holder.photo.setOnClickListener(mPhotoListener); |
| 341 | } |
| 342 | |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 343 | return view; |
| 344 | } |
Dmitri Plotnikov | f0eb9f5 | 2009-05-20 14:44:56 -0700 | [diff] [blame] | 345 | |
| 346 | private boolean isReply(Cursor cursor) { |
| 347 | |
| 348 | /* |
| 349 | * Comparing the message timestamp to the thread timestamp rather than checking the |
| 350 | * in_reply_to field. The rationale for this approach is that in the case when the |
| 351 | * original message to which the reply was posted is missing, we want to display |
| 352 | * the message as if it was an original; otherwise it would appear to be a reply |
| 353 | * to whatever message preceded it in the list. In the case when the original message |
| 354 | * of the thread is missing, the two timestamps will be the same. |
| 355 | */ |
| 356 | long published = cursor.getLong(COL_PUBLISHED); |
| 357 | long threadPublished = cursor.getLong(COL_THREAD_PUBLISHED); |
| 358 | return published != threadPublished; |
| 359 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Keep a cache that maps from {@link Contacts#_ID} to {@link Photo#PHOTO} |
| 364 | * values. |
| 365 | */ |
| 366 | private static class ContactsCache { |
| 367 | private static final String TAG = "ContactsCache"; |
| 368 | |
| 369 | private static final String[] PROJ_DETAILS = new String[] { |
| 370 | Data.MIMETYPE, |
| 371 | Data.CONTACT_ID, |
| 372 | Photo.PHOTO, |
| 373 | }; |
| 374 | |
| 375 | private static final int COL_MIMETYPE = 0; |
| 376 | private static final int COL_CONTACT_ID = 1; |
Jeff Sharkey | 8da253a | 2009-05-18 21:23:19 -0700 | [diff] [blame] | 377 | private static final int COL_PHOTO = 2; |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 378 | |
| 379 | private HashMap<Long, Bitmap> mPhoto = new HashMap<Long, Bitmap>(); |
| 380 | |
| 381 | public ContactsCache(Context context) { |
| 382 | Log.d(TAG, "building ContactsCache..."); |
| 383 | |
| 384 | ContentResolver resolver = context.getContentResolver(); |
| 385 | Cursor cursor = resolver.query(Data.CONTENT_URI, PROJ_DETAILS, |
| 386 | Data.MIMETYPE + "=?", new String[] { Photo.CONTENT_ITEM_TYPE }, null); |
| 387 | |
| 388 | while (cursor.moveToNext()) { |
| 389 | long contactId = cursor.getLong(COL_CONTACT_ID); |
| 390 | String mimeType = cursor.getString(COL_MIMETYPE); |
| 391 | if (Photo.CONTENT_ITEM_TYPE.equals(mimeType)) { |
| 392 | byte[] photoBlob = cursor.getBlob(COL_PHOTO); |
| 393 | Bitmap photo = BitmapFactory.decodeByteArray(photoBlob, 0, photoBlob.length); |
| 394 | photo = Utilities.createBitmapThumbnail(photo, context, PHOTO_SIZE); |
| 395 | |
| 396 | mPhoto.put(contactId, photo); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | cursor.close(); |
| 401 | Log.d(TAG, "done building ContactsCache"); |
| 402 | } |
| 403 | |
| 404 | public Bitmap getPhoto(long contactId) { |
| 405 | return mPhoto.get(contactId); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | /** |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 410 | * Store a mapping from a package name and mime-type pair to a set of |
| 411 | * {@link RemoteViews}, default icon, and column to use from the |
| 412 | * {@link Data} table to use as a summary. |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 413 | */ |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 414 | public static class Mapping { |
| 415 | String packageName; |
| 416 | String mimeType; |
| 417 | String summaryColumn; |
| 418 | int remoteViewsRes; |
| 419 | Bitmap icon; |
| 420 | |
| 421 | public Mapping() { |
| 422 | } |
| 423 | |
| 424 | public Mapping(String packageName, String mimeType) { |
| 425 | this.packageName = packageName; |
| 426 | this.mimeType = mimeType; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Store a parsed <code>Mapping</code> object, which maps package and |
| 432 | * mime-type combinations to {@link RemoteViews} XML resources, default |
| 433 | * icons, and summary columns in the {@link Data} table. |
| 434 | */ |
| 435 | public static class MappingCache extends HashMap<String, Mapping> { |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 436 | private static final String TAG = "MappingCache"; |
| 437 | |
| 438 | private static final String TAG_MAPPINGSET = "MappingSet"; |
| 439 | private static final String TAG_MAPPING = "Mapping"; |
| 440 | |
| 441 | private static final String MAPPING_METADATA = "com.android.contacts.stylemap"; |
| 442 | |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 443 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 444 | /** |
| 445 | * Only allow inflating through |
| 446 | * {@link MappingCache#createAndFill(Context)}. |
| 447 | */ |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 448 | private MappingCache() { |
| 449 | } |
| 450 | |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 451 | /** |
| 452 | * Add a {@link Mapping} instance to this cache, correctly using |
| 453 | * {@link #generateKey(String, String)} when storing. |
| 454 | */ |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 455 | public void addMapping(Mapping mapping) { |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 456 | String hashKey = generateKey(mapping.packageName, mapping.mimeType); |
| 457 | put(hashKey, mapping); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /** |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 461 | * Generate a key used internally for mapping a specific package name |
| 462 | * and mime-type to a {@link Mapping}. |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 463 | */ |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 464 | private String generateKey(String packageName, String mimeType) { |
| 465 | return packageName + ";" + mimeType; |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Find matching mapping for requested package and mime-type. Returns |
| 470 | * null if no mapping found. |
| 471 | */ |
| 472 | public Mapping findMapping(String packageName, String mimeType) { |
| 473 | // Search for common mapping first |
| 474 | final String commonMapping = generateKey(CommonDataKinds.PACKAGE_COMMON, mimeType); |
| 475 | if (containsKey(commonMapping)) { |
| 476 | return get(commonMapping); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 477 | } |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 478 | |
| 479 | // Otherwise search for package-specific mapping |
| 480 | final String specificMapping = generateKey(packageName, mimeType); |
| 481 | return get(specificMapping); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | /** |
| 485 | * Create a new {@link MappingCache} object and fill by walking across |
| 486 | * all packages to find those that provide mappings. |
| 487 | */ |
| 488 | public static MappingCache createAndFill(Context context) { |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 489 | Log.d(TAG, "building mime-type mapping cache..."); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 490 | final PackageManager pm = context.getPackageManager(); |
| 491 | MappingCache building = new MappingCache(); |
| 492 | List<ApplicationInfo> installed = pm |
| 493 | .getInstalledApplications(PackageManager.GET_META_DATA); |
| 494 | for (ApplicationInfo info : installed) { |
| 495 | if (info.metaData != null && info.metaData.containsKey(MAPPING_METADATA)) { |
| 496 | try { |
| 497 | // Found metadata, so clone into their context to |
| 498 | // inflate reference |
| 499 | Context theirContext = context.createPackageContext(info.packageName, 0); |
| 500 | XmlPullParser mappingParser = info.loadXmlMetaData(pm, MAPPING_METADATA); |
| 501 | building.inflateMappings(theirContext, info.uid, info.packageName, |
| 502 | mappingParser); |
| 503 | } catch (NameNotFoundException e) { |
| 504 | Log.w(TAG, "Problem creating context for remote package", e); |
| 505 | } catch (InflateException e) { |
| 506 | Log.w(TAG, "Problem inflating MappingSet from remote package", e); |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | return building; |
| 511 | } |
| 512 | |
| 513 | public static class InflateException extends Exception { |
| 514 | public InflateException(String message) { |
| 515 | super(message); |
| 516 | } |
| 517 | |
| 518 | public InflateException(String message, Throwable throwable) { |
| 519 | super(message, throwable); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Inflate a <code>MappingSet</code> from an XML resource, assuming the |
| 525 | * given package name as the source. |
| 526 | */ |
| 527 | public void inflateMappings(Context context, int uid, String packageName, |
| 528 | XmlPullParser parser) throws InflateException { |
| 529 | final AttributeSet attrs = Xml.asAttributeSet(parser); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 530 | final Resources res = context.getResources(); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 531 | |
| 532 | try { |
| 533 | int type; |
| 534 | while ((type = parser.next()) != XmlPullParser.START_TAG |
| 535 | && type != XmlPullParser.END_DOCUMENT) { |
| 536 | // Drain comments and whitespace |
| 537 | } |
| 538 | |
| 539 | if (type != XmlPullParser.START_TAG) { |
| 540 | throw new InflateException("No start tag found"); |
| 541 | } |
| 542 | |
| 543 | if (!TAG_MAPPINGSET.equals(parser.getName())) { |
| 544 | throw new InflateException("Top level element must be MappingSet"); |
| 545 | } |
| 546 | |
| 547 | // Parse all children actions |
| 548 | final int depth = parser.getDepth(); |
| 549 | while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) |
| 550 | && type != XmlPullParser.END_DOCUMENT) { |
Dmitri Plotnikov | 3c690ce | 2009-05-19 14:43:45 -0700 | [diff] [blame] | 551 | if (type == XmlPullParser.END_TAG) { |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 552 | continue; |
Dmitri Plotnikov | 3c690ce | 2009-05-19 14:43:45 -0700 | [diff] [blame] | 553 | } |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 554 | |
| 555 | if (!TAG_MAPPING.equals(parser.getName())) { |
| 556 | throw new InflateException("Expected Mapping tag"); |
| 557 | } |
| 558 | |
| 559 | // Parse kind, mime-type, and RemoteViews reference |
| 560 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Mapping); |
| 561 | |
| 562 | Mapping mapping = new Mapping(); |
| 563 | mapping.packageName = packageName; |
| 564 | mapping.mimeType = a.getString(R.styleable.Mapping_mimeType); |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 565 | mapping.summaryColumn = a.getString(R.styleable.Mapping_summaryColumn); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 566 | mapping.remoteViewsRes = a.getResourceId(R.styleable.Mapping_remoteViews, -1); |
| 567 | |
| 568 | // Read and resize icon if provided |
| 569 | int iconRes = a.getResourceId(R.styleable.Mapping_icon, -1); |
| 570 | if (iconRes != -1) { |
Jeff Sharkey | 549aa16 | 2009-05-21 01:33:30 -0700 | [diff] [blame] | 571 | mapping.icon = BitmapFactory.decodeResource(res, iconRes); |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | addMapping(mapping); |
| 575 | Log.d(TAG, "Added mapping for packageName=" + mapping.packageName |
| 576 | + ", mimetype=" + mapping.mimeType); |
| 577 | } |
| 578 | } catch (XmlPullParserException e) { |
| 579 | throw new InflateException("Problem reading XML", e); |
| 580 | } catch (IOException e) { |
| 581 | throw new InflateException("Problem reading XML", e); |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | /** |
| 587 | * Borrowed from Launcher for {@link Bitmap} resizing. |
| 588 | */ |
| 589 | static final class Utilities { |
| 590 | private static final Paint sPaint = new Paint(); |
| 591 | private static final Rect sBounds = new Rect(); |
| 592 | private static final Rect sOldBounds = new Rect(); |
| 593 | private static Canvas sCanvas = new Canvas(); |
| 594 | |
| 595 | static { |
| 596 | sCanvas.setDrawFilter(new PaintFlagsDrawFilter(Paint.DITHER_FLAG, |
| 597 | Paint.FILTER_BITMAP_FLAG)); |
| 598 | } |
| 599 | |
| 600 | /** |
| 601 | * Returns a Bitmap representing the thumbnail of the specified Bitmap. |
| 602 | * The size of the thumbnail is defined by the dimension |
| 603 | * android.R.dimen.launcher_application_icon_size. This method is not |
| 604 | * thread-safe and should be invoked on the UI thread only. |
Dmitri Plotnikov | 3c690ce | 2009-05-19 14:43:45 -0700 | [diff] [blame] | 605 | * |
Jeff Sharkey | 3f17759 | 2009-05-18 15:23:12 -0700 | [diff] [blame] | 606 | * @param bitmap The bitmap to get a thumbnail of. |
| 607 | * @param context The application's context. |
| 608 | * @return A thumbnail for the specified bitmap or the bitmap itself if |
| 609 | * the thumbnail could not be created. |
| 610 | */ |
| 611 | static Bitmap createBitmapThumbnail(Bitmap bitmap, Context context, int size) { |
| 612 | int width = size; |
| 613 | int height = size; |
| 614 | |
| 615 | final int bitmapWidth = bitmap.getWidth(); |
| 616 | final int bitmapHeight = bitmap.getHeight(); |
| 617 | |
| 618 | if (width > 0 && height > 0 && (width < bitmapWidth || height < bitmapHeight)) { |
| 619 | final float ratio = (float)bitmapWidth / bitmapHeight; |
| 620 | |
| 621 | if (bitmapWidth > bitmapHeight) { |
| 622 | height = (int)(width / ratio); |
| 623 | } else if (bitmapHeight > bitmapWidth) { |
| 624 | width = (int)(height * ratio); |
| 625 | } |
| 626 | |
| 627 | final Bitmap.Config c = (width == size && height == size) ? bitmap.getConfig() |
| 628 | : Bitmap.Config.ARGB_8888; |
| 629 | final Bitmap thumb = Bitmap.createBitmap(size, size, c); |
| 630 | final Canvas canvas = sCanvas; |
| 631 | final Paint paint = sPaint; |
| 632 | canvas.setBitmap(thumb); |
| 633 | paint.setDither(false); |
| 634 | paint.setFilterBitmap(true); |
| 635 | sBounds.set((size - width) / 2, (size - height) / 2, width, height); |
| 636 | sOldBounds.set(0, 0, bitmapWidth, bitmapHeight); |
| 637 | canvas.drawBitmap(bitmap, sOldBounds, sBounds, paint); |
| 638 | return thumb; |
| 639 | } |
| 640 | |
| 641 | return bitmap; |
| 642 | } |
Dmitri Plotnikov | 06191cd | 2009-05-07 14:11:52 -0700 | [diff] [blame] | 643 | } |
| 644 | } |