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