Evan Millar | e727f18 | 2009-08-27 11:15:55 -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 android.widget; |
| 18 | |
| 19 | import android.content.AsyncQueryHandler; |
| 20 | import android.content.ContentResolver; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.res.TypedArray; |
| 24 | import android.database.Cursor; |
Mark Wagner | f5935a9 | 2009-09-22 13:49:26 -0700 | [diff] [blame] | 25 | import android.graphics.drawable.Drawable; |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 26 | import android.net.Uri; |
| 27 | import android.provider.ContactsContract.Contacts; |
| 28 | import android.provider.ContactsContract.Intents; |
| 29 | import android.provider.ContactsContract.PhoneLookup; |
Tadashi G. Takaoka | 01fd4e1 | 2009-10-01 18:10:46 -0700 | [diff] [blame] | 30 | import android.provider.ContactsContract.QuickContact; |
Dmitri Plotnikov | ea36747 | 2009-09-18 15:04:58 -0700 | [diff] [blame] | 31 | import android.provider.ContactsContract.RawContacts; |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 32 | import android.provider.ContactsContract.CommonDataKinds.Email; |
| 33 | import android.util.AttributeSet; |
| 34 | import android.view.View; |
| 35 | import android.view.View.OnClickListener; |
Mark Wagner | b2c6429 | 2009-09-28 16:39:20 -0700 | [diff] [blame] | 36 | import com.android.internal.R; |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
Evan Millar | c1a2a82 | 2009-09-29 18:02:06 -0700 | [diff] [blame] | 39 | * Widget used to show an image with the standard QuickContact badge |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 40 | * and on-click behavior. |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 41 | */ |
Evan Millar | c1a2a82 | 2009-09-29 18:02:06 -0700 | [diff] [blame] | 42 | public class QuickContactBadge extends ImageView implements OnClickListener { |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 43 | |
| 44 | private Uri mContactUri; |
| 45 | private String mContactEmail; |
| 46 | private String mContactPhone; |
| 47 | private int mMode; |
| 48 | private QueryHandler mQueryHandler; |
Mark Wagner | f5935a9 | 2009-09-22 13:49:26 -0700 | [diff] [blame] | 49 | private Drawable mBadgeBackground; |
Mark Wagner | b2c6429 | 2009-09-28 16:39:20 -0700 | [diff] [blame] | 50 | private Drawable mNoBadgeBackground; |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 51 | |
| 52 | protected String[] mExcludeMimes = null; |
| 53 | |
| 54 | static final private int TOKEN_EMAIL_LOOKUP = 0; |
| 55 | static final private int TOKEN_PHONE_LOOKUP = 1; |
| 56 | static final private int TOKEN_EMAIL_LOOKUP_AND_TRIGGER = 2; |
| 57 | static final private int TOKEN_PHONE_LOOKUP_AND_TRIGGER = 3; |
Tadashi G. Takaoka | 01fd4e1 | 2009-10-01 18:10:46 -0700 | [diff] [blame] | 58 | static final private int TOKEN_CONTACT_LOOKUP_AND_TRIGGER = 4; |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 59 | |
| 60 | static final String[] EMAIL_LOOKUP_PROJECTION = new String[] { |
Dmitri Plotnikov | ea36747 | 2009-09-18 15:04:58 -0700 | [diff] [blame] | 61 | RawContacts.CONTACT_ID, |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 62 | Contacts.LOOKUP_KEY, |
| 63 | }; |
Tadashi G. Takaoka | 01fd4e1 | 2009-10-01 18:10:46 -0700 | [diff] [blame] | 64 | static final int EMAIL_ID_COLUMN_INDEX = 0; |
| 65 | static final int EMAIL_LOOKUP_STRING_COLUMN_INDEX = 1; |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 66 | |
| 67 | static final String[] PHONE_LOOKUP_PROJECTION = new String[] { |
| 68 | PhoneLookup._ID, |
| 69 | PhoneLookup.LOOKUP_KEY, |
| 70 | }; |
Tadashi G. Takaoka | 01fd4e1 | 2009-10-01 18:10:46 -0700 | [diff] [blame] | 71 | static final int PHONE_ID_COLUMN_INDEX = 0; |
| 72 | static final int PHONE_LOOKUP_STRING_COLUMN_INDEX = 1; |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 73 | |
Tadashi G. Takaoka | 01fd4e1 | 2009-10-01 18:10:46 -0700 | [diff] [blame] | 74 | static final String[] CONTACT_LOOKUP_PROJECTION = new String[] { |
| 75 | Contacts._ID, |
| 76 | Contacts.LOOKUP_KEY, |
| 77 | }; |
| 78 | static final int CONTACT_ID_COLUMN_INDEX = 0; |
| 79 | static final int CONTACT_LOOKUPKEY_COLUMN_INDEX = 1; |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 80 | |
| 81 | |
Evan Millar | c1a2a82 | 2009-09-29 18:02:06 -0700 | [diff] [blame] | 82 | public QuickContactBadge(Context context) { |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 83 | this(context, null); |
| 84 | } |
| 85 | |
Evan Millar | c1a2a82 | 2009-09-29 18:02:06 -0700 | [diff] [blame] | 86 | public QuickContactBadge(Context context, AttributeSet attrs) { |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 87 | this(context, attrs, 0); |
| 88 | } |
| 89 | |
Evan Millar | c1a2a82 | 2009-09-29 18:02:06 -0700 | [diff] [blame] | 90 | public QuickContactBadge(Context context, AttributeSet attrs, int defStyle) { |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 91 | super(context, attrs, defStyle); |
| 92 | |
| 93 | TypedArray a = |
| 94 | context.obtainStyledAttributes(attrs, |
Evan Millar | c1a2a82 | 2009-09-29 18:02:06 -0700 | [diff] [blame] | 95 | com.android.internal.R.styleable.QuickContactBadge, defStyle, 0); |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 96 | |
Evan Millar | c1a2a82 | 2009-09-29 18:02:06 -0700 | [diff] [blame] | 97 | mMode = a.getInt(com.android.internal.R.styleable.QuickContactBadge_quickContactWindowSize, |
Evan Millar | e3ec997 | 2009-09-30 19:37:36 -0700 | [diff] [blame] | 98 | QuickContact.MODE_MEDIUM); |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 99 | |
| 100 | a.recycle(); |
| 101 | |
| 102 | init(); |
Mark Wagner | f5935a9 | 2009-09-22 13:49:26 -0700 | [diff] [blame] | 103 | |
| 104 | mBadgeBackground = getBackground(); |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | private void init() { |
| 108 | mQueryHandler = new QueryHandler(mContext.getContentResolver()); |
| 109 | setOnClickListener(this); |
| 110 | } |
| 111 | |
| 112 | /** |
Evan Millar | e3ec997 | 2009-09-30 19:37:36 -0700 | [diff] [blame] | 113 | * Set the QuickContact window mode. Options are {@link QuickContact#MODE_SMALL}, |
| 114 | * {@link QuickContact#MODE_MEDIUM}, {@link QuickContact#MODE_LARGE}. |
| 115 | * @param size |
| 116 | */ |
| 117 | public void setMode(int size) { |
| 118 | mMode = size; |
| 119 | } |
| 120 | |
| 121 | /** |
Evan Millar | c1a2a82 | 2009-09-29 18:02:06 -0700 | [diff] [blame] | 122 | * Assign the contact uri that this QuickContactBadge should be associated |
| 123 | * with. Note that this is only used for displaying the QuickContact window and |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 124 | * won't bind the contact's photo for you. |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 125 | * |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 126 | * @param contactUri Either a {@link Contacts#CONTENT_URI} or |
| 127 | * {@link Contacts#CONTENT_LOOKUP_URI} style URI. |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 128 | */ |
| 129 | public void assignContactUri(Uri contactUri) { |
| 130 | mContactUri = contactUri; |
Mark Wagner | f5935a9 | 2009-09-22 13:49:26 -0700 | [diff] [blame] | 131 | mContactEmail = null; |
| 132 | mContactPhone = null; |
| 133 | onContactUriChanged(); |
| 134 | } |
| 135 | |
| 136 | private void onContactUriChanged() { |
| 137 | if (mContactUri == null && mContactEmail == null && mContactPhone == null) { |
Mark Wagner | b2c6429 | 2009-09-28 16:39:20 -0700 | [diff] [blame] | 138 | if (mNoBadgeBackground == null) { |
Evan Millar | c1a2a82 | 2009-09-29 18:02:06 -0700 | [diff] [blame] | 139 | mNoBadgeBackground = getResources().getDrawable(R.drawable.quickcontact_nobadge); |
Mark Wagner | b2c6429 | 2009-09-28 16:39:20 -0700 | [diff] [blame] | 140 | } |
| 141 | setBackgroundDrawable(mNoBadgeBackground); |
Mark Wagner | f5935a9 | 2009-09-22 13:49:26 -0700 | [diff] [blame] | 142 | } else { |
| 143 | setBackgroundDrawable(mBadgeBackground); |
| 144 | } |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Assign a contact based on an email address. This should only be used when |
| 149 | * the contact's URI is not available, as an extra query will have to be |
| 150 | * performed to lookup the URI based on the email. |
| 151 | * |
| 152 | * @param emailAddress The email address of the contact. |
| 153 | * @param lazyLookup If this is true, the lookup query will not be performed |
| 154 | * until this view is clicked. |
| 155 | */ |
| 156 | public void assignContactFromEmail(String emailAddress, boolean lazyLookup) { |
| 157 | mContactEmail = emailAddress; |
| 158 | if (!lazyLookup) { |
| 159 | mQueryHandler.startQuery(TOKEN_EMAIL_LOOKUP, null, |
| 160 | Uri.withAppendedPath(Email.CONTENT_LOOKUP_URI, Uri.encode(mContactEmail)), |
| 161 | EMAIL_LOOKUP_PROJECTION, null, null, null); |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 162 | } else { |
| 163 | mContactUri = null; |
Mark Wagner | f5935a9 | 2009-09-22 13:49:26 -0700 | [diff] [blame] | 164 | onContactUriChanged(); |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Assign a contact based on a phone number. This should only be used when |
| 170 | * the contact's URI is not available, as an extra query will have to be |
| 171 | * performed to lookup the URI based on the phone number. |
| 172 | * |
| 173 | * @param phoneNumber The phone number of the contact. |
| 174 | * @param lazyLookup If this is true, the lookup query will not be performed |
| 175 | * until this view is clicked. |
| 176 | */ |
| 177 | public void assignContactFromPhone(String phoneNumber, boolean lazyLookup) { |
| 178 | mContactPhone = phoneNumber; |
| 179 | if (!lazyLookup) { |
| 180 | mQueryHandler.startQuery(TOKEN_PHONE_LOOKUP, null, |
| 181 | Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, mContactPhone), |
| 182 | PHONE_LOOKUP_PROJECTION, null, null, null); |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 183 | } else { |
| 184 | mContactUri = null; |
Mark Wagner | f5935a9 | 2009-09-22 13:49:26 -0700 | [diff] [blame] | 185 | onContactUriChanged(); |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 189 | public void onClick(View v) { |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 190 | if (mContactUri != null) { |
Tadashi G. Takaoka | 01fd4e1 | 2009-10-01 18:10:46 -0700 | [diff] [blame] | 191 | mQueryHandler.startQuery(TOKEN_CONTACT_LOOKUP_AND_TRIGGER, null, |
| 192 | mContactUri, |
| 193 | CONTACT_LOOKUP_PROJECTION, null, null, null); |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 194 | } else if (mContactEmail != null) { |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 195 | mQueryHandler.startQuery(TOKEN_EMAIL_LOOKUP_AND_TRIGGER, mContactEmail, |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 196 | Uri.withAppendedPath(Email.CONTENT_LOOKUP_URI, Uri.encode(mContactEmail)), |
| 197 | EMAIL_LOOKUP_PROJECTION, null, null, null); |
| 198 | } else if (mContactPhone != null) { |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 199 | mQueryHandler.startQuery(TOKEN_PHONE_LOOKUP_AND_TRIGGER, mContactPhone, |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 200 | Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, mContactPhone), |
| 201 | PHONE_LOOKUP_PROJECTION, null, null, null); |
| 202 | } else { |
| 203 | // If a contact hasn't been assigned, don't react to click. |
| 204 | return; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Set a list of specific MIME-types to exclude and not display. For |
| 210 | * example, this can be used to hide the {@link Contacts#CONTENT_ITEM_TYPE} |
| 211 | * profile icon. |
| 212 | */ |
| 213 | public void setExcludeMimes(String[] excludeMimes) { |
| 214 | mExcludeMimes = excludeMimes; |
| 215 | } |
| 216 | |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 217 | private void trigger(Uri lookupUri) { |
Evan Millar | e3ec997 | 2009-09-30 19:37:36 -0700 | [diff] [blame] | 218 | QuickContact.showQuickContact(getContext(), this, lookupUri, mMode, mExcludeMimes); |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | private class QueryHandler extends AsyncQueryHandler { |
| 222 | |
| 223 | public QueryHandler(ContentResolver cr) { |
| 224 | super(cr); |
| 225 | } |
| 226 | |
| 227 | @Override |
| 228 | protected void onQueryComplete(int token, Object cookie, Cursor cursor) { |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 229 | Uri lookupUri = null; |
| 230 | Uri createUri = null; |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 231 | boolean trigger = false; |
| 232 | |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 233 | try { |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 234 | switch(token) { |
| 235 | case TOKEN_PHONE_LOOKUP_AND_TRIGGER: |
| 236 | trigger = true; |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 237 | createUri = Uri.fromParts("tel", (String)cookie, null); |
| 238 | |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 239 | case TOKEN_PHONE_LOOKUP: { |
| 240 | if (cursor != null && cursor.moveToFirst()) { |
| 241 | long contactId = cursor.getLong(PHONE_ID_COLUMN_INDEX); |
| 242 | String lookupKey = cursor.getString(PHONE_LOOKUP_STRING_COLUMN_INDEX); |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 243 | lookupUri = Contacts.getLookupUri(contactId, lookupKey); |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 244 | } |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 245 | |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 246 | break; |
| 247 | } |
| 248 | case TOKEN_EMAIL_LOOKUP_AND_TRIGGER: |
| 249 | trigger = true; |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 250 | createUri = Uri.fromParts("mailto", (String)cookie, null); |
| 251 | |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 252 | case TOKEN_EMAIL_LOOKUP: { |
| 253 | if (cursor != null && cursor.moveToFirst()) { |
| 254 | long contactId = cursor.getLong(EMAIL_ID_COLUMN_INDEX); |
| 255 | String lookupKey = cursor.getString(EMAIL_LOOKUP_STRING_COLUMN_INDEX); |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 256 | lookupUri = Contacts.getLookupUri(contactId, lookupKey); |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 257 | } |
| 258 | } |
Tadashi G. Takaoka | 01fd4e1 | 2009-10-01 18:10:46 -0700 | [diff] [blame] | 259 | |
| 260 | case TOKEN_CONTACT_LOOKUP_AND_TRIGGER: { |
| 261 | if (cursor != null && cursor.moveToFirst()) { |
| 262 | long contactId = cursor.getLong(CONTACT_ID_COLUMN_INDEX); |
| 263 | String lookupKey = cursor.getString(CONTACT_LOOKUPKEY_COLUMN_INDEX); |
| 264 | lookupUri = Contacts.getLookupUri(contactId, lookupKey); |
| 265 | trigger = true; |
| 266 | } |
| 267 | |
| 268 | break; |
| 269 | } |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 270 | } |
| 271 | } finally { |
| 272 | if (cursor != null) { |
| 273 | cursor.close(); |
| 274 | } |
| 275 | } |
| 276 | |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 277 | mContactUri = lookupUri; |
Mark Wagner | f5935a9 | 2009-09-22 13:49:26 -0700 | [diff] [blame] | 278 | onContactUriChanged(); |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 279 | |
| 280 | if (trigger && lookupUri != null) { |
| 281 | // Found contact, so trigger track |
| 282 | trigger(lookupUri); |
| 283 | } else if (createUri != null) { |
| 284 | // Prompt user to add this person to contacts |
| 285 | final Intent intent = new Intent(Intents.SHOW_OR_CREATE_CONTACT, createUri); |
| 286 | getContext().startActivity(intent); |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |