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