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 | |
Gilles Debunne | 9235058 | 2011-01-07 19:39:26 -0800 | [diff] [blame] | 19 | import com.android.internal.R; |
| 20 | |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 21 | import android.content.AsyncQueryHandler; |
| 22 | import android.content.ContentResolver; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.res.TypedArray; |
| 26 | import android.database.Cursor; |
Gilles Debunne | 9235058 | 2011-01-07 19:39:26 -0800 | [diff] [blame] | 27 | import android.graphics.Canvas; |
Mark Wagner | f5935a9 | 2009-09-22 13:49:26 -0700 | [diff] [blame] | 28 | import android.graphics.drawable.Drawable; |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 29 | import android.net.Uri; |
Yorke Lee | 02f304f | 2013-01-14 08:57:43 -0800 | [diff] [blame] | 30 | import android.os.Bundle; |
Gilles Debunne | 9235058 | 2011-01-07 19:39:26 -0800 | [diff] [blame] | 31 | import android.provider.ContactsContract.CommonDataKinds.Email; |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 32 | import android.provider.ContactsContract.Contacts; |
| 33 | import android.provider.ContactsContract.Intents; |
| 34 | import android.provider.ContactsContract.PhoneLookup; |
Tadashi G. Takaoka | 01fd4e1 | 2009-10-01 18:10:46 -0700 | [diff] [blame] | 35 | import android.provider.ContactsContract.QuickContact; |
Dmitri Plotnikov | ea36747 | 2009-09-18 15:04:58 -0700 | [diff] [blame] | 36 | import android.provider.ContactsContract.RawContacts; |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 37 | import android.util.AttributeSet; |
| 38 | import android.view.View; |
| 39 | import android.view.View.OnClickListener; |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 40 | import android.view.accessibility.AccessibilityEvent; |
| 41 | import android.view.accessibility.AccessibilityNodeInfo; |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
Evan Millar | c1a2a82 | 2009-09-29 18:02:06 -0700 | [diff] [blame] | 44 | * Widget used to show an image with the standard QuickContact badge |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 45 | * and on-click behavior. |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 46 | */ |
Evan Millar | c1a2a82 | 2009-09-29 18:02:06 -0700 | [diff] [blame] | 47 | public class QuickContactBadge extends ImageView implements OnClickListener { |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 48 | private Uri mContactUri; |
| 49 | private String mContactEmail; |
| 50 | private String mContactPhone; |
Gilles Debunne | 9235058 | 2011-01-07 19:39:26 -0800 | [diff] [blame] | 51 | private Drawable mOverlay; |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 52 | private QueryHandler mQueryHandler; |
Evan Millar | 9a20aa7 | 2010-04-13 09:51:21 -0700 | [diff] [blame] | 53 | private Drawable mDefaultAvatar; |
Yorke Lee | 02f304f | 2013-01-14 08:57:43 -0800 | [diff] [blame] | 54 | private Bundle mExtras = null; |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 55 | |
| 56 | protected String[] mExcludeMimes = null; |
| 57 | |
| 58 | static final private int TOKEN_EMAIL_LOOKUP = 0; |
| 59 | static final private int TOKEN_PHONE_LOOKUP = 1; |
| 60 | static final private int TOKEN_EMAIL_LOOKUP_AND_TRIGGER = 2; |
| 61 | static final private int TOKEN_PHONE_LOOKUP_AND_TRIGGER = 3; |
| 62 | |
Yorke Lee | 02f304f | 2013-01-14 08:57:43 -0800 | [diff] [blame] | 63 | static final private String EXTRA_URI_CONTENT = "uri_content"; |
| 64 | |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 65 | static final String[] EMAIL_LOOKUP_PROJECTION = new String[] { |
Dmitri Plotnikov | ea36747 | 2009-09-18 15:04:58 -0700 | [diff] [blame] | 66 | RawContacts.CONTACT_ID, |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 67 | Contacts.LOOKUP_KEY, |
| 68 | }; |
Tadashi G. Takaoka | 01fd4e1 | 2009-10-01 18:10:46 -0700 | [diff] [blame] | 69 | static final int EMAIL_ID_COLUMN_INDEX = 0; |
| 70 | static final int EMAIL_LOOKUP_STRING_COLUMN_INDEX = 1; |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 71 | |
| 72 | static final String[] PHONE_LOOKUP_PROJECTION = new String[] { |
| 73 | PhoneLookup._ID, |
| 74 | PhoneLookup.LOOKUP_KEY, |
| 75 | }; |
Tadashi G. Takaoka | 01fd4e1 | 2009-10-01 18:10:46 -0700 | [diff] [blame] | 76 | static final int PHONE_ID_COLUMN_INDEX = 0; |
| 77 | static final int PHONE_LOOKUP_STRING_COLUMN_INDEX = 1; |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 78 | |
Evan Millar | c1a2a82 | 2009-09-29 18:02:06 -0700 | [diff] [blame] | 79 | public QuickContactBadge(Context context) { |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 80 | this(context, null); |
| 81 | } |
| 82 | |
Evan Millar | c1a2a82 | 2009-09-29 18:02:06 -0700 | [diff] [blame] | 83 | public QuickContactBadge(Context context, AttributeSet attrs) { |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 84 | this(context, attrs, 0); |
| 85 | } |
| 86 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 87 | public QuickContactBadge(Context context, AttributeSet attrs, int defStyleAttr) { |
| 88 | this(context, attrs, defStyleAttr, 0); |
| 89 | } |
| 90 | |
| 91 | public QuickContactBadge( |
| 92 | Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 93 | super(context, attrs, defStyleAttr, defStyleRes); |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 94 | |
Gilles Debunne | 9235058 | 2011-01-07 19:39:26 -0800 | [diff] [blame] | 95 | TypedArray styledAttributes = mContext.obtainStyledAttributes(R.styleable.Theme); |
Daniel Lehmann | e13e55b | 2011-08-07 19:51:28 -0700 | [diff] [blame] | 96 | mOverlay = styledAttributes.getDrawable( |
| 97 | com.android.internal.R.styleable.Theme_quickContactBadgeOverlay); |
Gilles Debunne | 9235058 | 2011-01-07 19:39:26 -0800 | [diff] [blame] | 98 | styledAttributes.recycle(); |
| 99 | |
Deepanshu Gupta | 34e9c69 | 2013-10-18 11:22:46 -0700 | [diff] [blame] | 100 | if (!isInEditMode()) { |
| 101 | mQueryHandler = new QueryHandler(mContext.getContentResolver()); |
| 102 | } |
Daniel Lehmann | e13e55b | 2011-08-07 19:51:28 -0700 | [diff] [blame] | 103 | setOnClickListener(this); |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Gilles Debunne | 5cba862 | 2011-01-12 19:52:56 -0800 | [diff] [blame] | 106 | @Override |
| 107 | protected void drawableStateChanged() { |
| 108 | super.drawableStateChanged(); |
Daniel Lehmann | e13e55b | 2011-08-07 19:51:28 -0700 | [diff] [blame] | 109 | if (mOverlay != null && mOverlay.isStateful()) { |
| 110 | mOverlay.setState(getDrawableState()); |
Gilles Debunne | 5cba862 | 2011-01-12 19:52:56 -0800 | [diff] [blame] | 111 | invalidate(); |
| 112 | } |
| 113 | } |
| 114 | |
Alan Viverette | cebc6ba | 2014-06-13 15:52:13 -0700 | [diff] [blame] | 115 | @Override |
Alan Viverette | 8de1494 | 2014-06-18 18:05:15 -0700 | [diff] [blame] | 116 | public void drawableHotspotChanged(float x, float y) { |
| 117 | super.drawableHotspotChanged(x, y); |
Alan Viverette | cebc6ba | 2014-06-13 15:52:13 -0700 | [diff] [blame] | 118 | |
| 119 | if (mOverlay != null) { |
| 120 | mOverlay.setHotspot(x, y); |
| 121 | } |
| 122 | } |
| 123 | |
Daniel Lehmann | e13e55b | 2011-08-07 19:51:28 -0700 | [diff] [blame] | 124 | /** This call has no effect anymore, as there is only one QuickContact mode */ |
| 125 | @SuppressWarnings("unused") |
Evan Millar | e3ec997 | 2009-09-30 19:37:36 -0700 | [diff] [blame] | 126 | public void setMode(int size) { |
Evan Millar | e3ec997 | 2009-09-30 19:37:36 -0700 | [diff] [blame] | 127 | } |
Gilles Debunne | 9235058 | 2011-01-07 19:39:26 -0800 | [diff] [blame] | 128 | |
| 129 | @Override |
| 130 | protected void onDraw(Canvas canvas) { |
| 131 | super.onDraw(canvas); |
| 132 | |
Daniel Lehmann | e13e55b | 2011-08-07 19:51:28 -0700 | [diff] [blame] | 133 | if (!isEnabled()) { |
| 134 | // not clickable? don't show triangle |
| 135 | return; |
| 136 | } |
| 137 | |
Gilles Debunne | 5cba862 | 2011-01-12 19:52:56 -0800 | [diff] [blame] | 138 | if (mOverlay == null || mOverlay.getIntrinsicWidth() == 0 || |
| 139 | mOverlay.getIntrinsicHeight() == 0) { |
Daniel Lehmann | e13e55b | 2011-08-07 19:51:28 -0700 | [diff] [blame] | 140 | // nothing to draw |
| 141 | return; |
Gilles Debunne | 9235058 | 2011-01-07 19:39:26 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | mOverlay.setBounds(0, 0, getWidth(), getHeight()); |
| 145 | |
| 146 | if (mPaddingTop == 0 && mPaddingLeft == 0) { |
| 147 | mOverlay.draw(canvas); |
| 148 | } else { |
| 149 | int saveCount = canvas.getSaveCount(); |
| 150 | canvas.save(); |
| 151 | canvas.translate(mPaddingLeft, mPaddingTop); |
| 152 | mOverlay.draw(canvas); |
| 153 | canvas.restoreToCount(saveCount); |
| 154 | } |
| 155 | } |
| 156 | |
Daniel Lehmann | e13e55b | 2011-08-07 19:51:28 -0700 | [diff] [blame] | 157 | /** True if a contact, an email address or a phone number has been assigned */ |
| 158 | private boolean isAssigned() { |
| 159 | return mContactUri != null || mContactEmail != null || mContactPhone != null; |
| 160 | } |
| 161 | |
Evan Millar | 9a20aa7 | 2010-04-13 09:51:21 -0700 | [diff] [blame] | 162 | /** |
| 163 | * Resets the contact photo to the default state. |
| 164 | */ |
| 165 | public void setImageToDefault() { |
| 166 | if (mDefaultAvatar == null) { |
Alan Viverette | 8eea3ea | 2014-02-03 18:40:20 -0800 | [diff] [blame] | 167 | mDefaultAvatar = mContext.getDrawable(R.drawable.ic_contact_picture); |
Evan Millar | 9a20aa7 | 2010-04-13 09:51:21 -0700 | [diff] [blame] | 168 | } |
| 169 | setImageDrawable(mDefaultAvatar); |
| 170 | } |
Evan Millar | e3ec997 | 2009-09-30 19:37:36 -0700 | [diff] [blame] | 171 | |
| 172 | /** |
Evan Millar | c1a2a82 | 2009-09-29 18:02:06 -0700 | [diff] [blame] | 173 | * Assign the contact uri that this QuickContactBadge should be associated |
| 174 | * with. Note that this is only used for displaying the QuickContact window and |
Gilles Debunne | 9235058 | 2011-01-07 19:39:26 -0800 | [diff] [blame] | 175 | * won't bind the contact's photo for you. Call {@link #setImageDrawable(Drawable)} to set the |
| 176 | * photo. |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 177 | * |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 178 | * @param contactUri Either a {@link Contacts#CONTENT_URI} or |
| 179 | * {@link Contacts#CONTENT_LOOKUP_URI} style URI. |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 180 | */ |
| 181 | public void assignContactUri(Uri contactUri) { |
| 182 | mContactUri = contactUri; |
Mark Wagner | f5935a9 | 2009-09-22 13:49:26 -0700 | [diff] [blame] | 183 | mContactEmail = null; |
| 184 | mContactPhone = null; |
| 185 | onContactUriChanged(); |
| 186 | } |
| 187 | |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 188 | /** |
| 189 | * Assign a contact based on an email address. This should only be used when |
| 190 | * the contact's URI is not available, as an extra query will have to be |
| 191 | * performed to lookup the URI based on the email. |
| 192 | * |
| 193 | * @param emailAddress The email address of the contact. |
| 194 | * @param lazyLookup If this is true, the lookup query will not be performed |
| 195 | * until this view is clicked. |
| 196 | */ |
| 197 | public void assignContactFromEmail(String emailAddress, boolean lazyLookup) { |
Yorke Lee | 02f304f | 2013-01-14 08:57:43 -0800 | [diff] [blame] | 198 | assignContactFromEmail(emailAddress, lazyLookup, null); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Assign a contact based on an email address. This should only be used when |
| 203 | * the contact's URI is not available, as an extra query will have to be |
| 204 | * performed to lookup the URI based on the email. |
| 205 | |
| 206 | @param emailAddress The email address of the contact. |
| 207 | @param lazyLookup If this is true, the lookup query will not be performed |
| 208 | until this view is clicked. |
| 209 | @param extras A bundle of extras to populate the contact edit page with if the contact |
| 210 | is not found and the user chooses to add the email address to an existing contact or |
| 211 | create a new contact. Uses the same string constants as those found in |
Yorke Lee | 3e839f4 | 2013-01-15 16:46:00 -0800 | [diff] [blame] | 212 | {@link android.provider.ContactsContract.Intents.Insert} |
Yorke Lee | 02f304f | 2013-01-14 08:57:43 -0800 | [diff] [blame] | 213 | */ |
| 214 | |
| 215 | public void assignContactFromEmail(String emailAddress, boolean lazyLookup, Bundle extras) { |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 216 | mContactEmail = emailAddress; |
Yorke Lee | 02f304f | 2013-01-14 08:57:43 -0800 | [diff] [blame] | 217 | mExtras = extras; |
Deepanshu Gupta | 34e9c69 | 2013-10-18 11:22:46 -0700 | [diff] [blame] | 218 | if (!lazyLookup && mQueryHandler != null) { |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 219 | mQueryHandler.startQuery(TOKEN_EMAIL_LOOKUP, null, |
| 220 | Uri.withAppendedPath(Email.CONTENT_LOOKUP_URI, Uri.encode(mContactEmail)), |
| 221 | EMAIL_LOOKUP_PROJECTION, null, null, null); |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 222 | } else { |
| 223 | mContactUri = null; |
Mark Wagner | f5935a9 | 2009-09-22 13:49:26 -0700 | [diff] [blame] | 224 | onContactUriChanged(); |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Yorke Lee | 02f304f | 2013-01-14 08:57:43 -0800 | [diff] [blame] | 228 | |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 229 | /** |
| 230 | * Assign a contact based on a phone number. This should only be used when |
| 231 | * the contact's URI is not available, as an extra query will have to be |
| 232 | * performed to lookup the URI based on the phone number. |
| 233 | * |
| 234 | * @param phoneNumber The phone number of the contact. |
| 235 | * @param lazyLookup If this is true, the lookup query will not be performed |
| 236 | * until this view is clicked. |
| 237 | */ |
| 238 | public void assignContactFromPhone(String phoneNumber, boolean lazyLookup) { |
Yorke Lee | 02f304f | 2013-01-14 08:57:43 -0800 | [diff] [blame] | 239 | assignContactFromPhone(phoneNumber, lazyLookup, new Bundle()); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Assign a contact based on a phone number. This should only be used when |
| 244 | * the contact's URI is not available, as an extra query will have to be |
| 245 | * performed to lookup the URI based on the phone number. |
| 246 | * |
| 247 | * @param phoneNumber The phone number of the contact. |
| 248 | * @param lazyLookup If this is true, the lookup query will not be performed |
| 249 | * until this view is clicked. |
| 250 | * @param extras A bundle of extras to populate the contact edit page with if the contact |
| 251 | * is not found and the user chooses to add the phone number to an existing contact or |
| 252 | * create a new contact. Uses the same string constants as those found in |
Yorke Lee | 3e839f4 | 2013-01-15 16:46:00 -0800 | [diff] [blame] | 253 | * {@link android.provider.ContactsContract.Intents.Insert} |
Yorke Lee | 02f304f | 2013-01-14 08:57:43 -0800 | [diff] [blame] | 254 | */ |
| 255 | public void assignContactFromPhone(String phoneNumber, boolean lazyLookup, Bundle extras) { |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 256 | mContactPhone = phoneNumber; |
Yorke Lee | 02f304f | 2013-01-14 08:57:43 -0800 | [diff] [blame] | 257 | mExtras = extras; |
Deepanshu Gupta | 34e9c69 | 2013-10-18 11:22:46 -0700 | [diff] [blame] | 258 | if (!lazyLookup && mQueryHandler != null) { |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 259 | mQueryHandler.startQuery(TOKEN_PHONE_LOOKUP, null, |
| 260 | Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, mContactPhone), |
| 261 | PHONE_LOOKUP_PROJECTION, null, null, null); |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 262 | } else { |
| 263 | mContactUri = null; |
Mark Wagner | f5935a9 | 2009-09-22 13:49:26 -0700 | [diff] [blame] | 264 | onContactUriChanged(); |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
Yorke Lee | a702ccd | 2014-04-28 12:41:44 -0700 | [diff] [blame] | 268 | /** |
| 269 | * Assigns the drawable that is to be drawn on top of the assigned contact photo. |
| 270 | * |
| 271 | * @param overlay Drawable to be drawn over the assigned contact photo. Must have a non-zero |
| 272 | * instrinsic width and height. |
| 273 | */ |
| 274 | public void setOverlay(Drawable overlay) { |
| 275 | mOverlay = overlay; |
| 276 | } |
| 277 | |
Daniel Lehmann | e13e55b | 2011-08-07 19:51:28 -0700 | [diff] [blame] | 278 | private void onContactUriChanged() { |
| 279 | setEnabled(isAssigned()); |
| 280 | } |
| 281 | |
| 282 | @Override |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 283 | public void onClick(View v) { |
Yorke Lee | 02f304f | 2013-01-14 08:57:43 -0800 | [diff] [blame] | 284 | // If contact has been assigned, mExtras should no longer be null, but do a null check |
| 285 | // anyway just in case assignContactFromPhone or Email was called with a null bundle or |
| 286 | // wasn't assigned previously. |
| 287 | final Bundle extras = (mExtras == null) ? new Bundle() : mExtras; |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 288 | if (mContactUri != null) { |
Daniel Lehmann | e13e55b | 2011-08-07 19:51:28 -0700 | [diff] [blame] | 289 | QuickContact.showQuickContact(getContext(), QuickContactBadge.this, mContactUri, |
| 290 | QuickContact.MODE_LARGE, mExcludeMimes); |
Deepanshu Gupta | 34e9c69 | 2013-10-18 11:22:46 -0700 | [diff] [blame] | 291 | } else if (mContactEmail != null && mQueryHandler != null) { |
Yorke Lee | 02f304f | 2013-01-14 08:57:43 -0800 | [diff] [blame] | 292 | extras.putString(EXTRA_URI_CONTENT, mContactEmail); |
| 293 | mQueryHandler.startQuery(TOKEN_EMAIL_LOOKUP_AND_TRIGGER, extras, |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 294 | Uri.withAppendedPath(Email.CONTENT_LOOKUP_URI, Uri.encode(mContactEmail)), |
| 295 | EMAIL_LOOKUP_PROJECTION, null, null, null); |
Deepanshu Gupta | 34e9c69 | 2013-10-18 11:22:46 -0700 | [diff] [blame] | 296 | } else if (mContactPhone != null && mQueryHandler != null) { |
Yorke Lee | 02f304f | 2013-01-14 08:57:43 -0800 | [diff] [blame] | 297 | extras.putString(EXTRA_URI_CONTENT, mContactPhone); |
| 298 | mQueryHandler.startQuery(TOKEN_PHONE_LOOKUP_AND_TRIGGER, extras, |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 299 | Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, mContactPhone), |
| 300 | PHONE_LOOKUP_PROJECTION, null, null, null); |
| 301 | } else { |
| 302 | // If a contact hasn't been assigned, don't react to click. |
| 303 | return; |
| 304 | } |
| 305 | } |
| 306 | |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 307 | @Override |
| 308 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) { |
| 309 | super.onInitializeAccessibilityEvent(event); |
| 310 | event.setClassName(QuickContactBadge.class.getName()); |
| 311 | } |
| 312 | |
| 313 | @Override |
| 314 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 315 | super.onInitializeAccessibilityNodeInfo(info); |
| 316 | info.setClassName(QuickContactBadge.class.getName()); |
| 317 | } |
| 318 | |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 319 | /** |
| 320 | * Set a list of specific MIME-types to exclude and not display. For |
| 321 | * example, this can be used to hide the {@link Contacts#CONTENT_ITEM_TYPE} |
| 322 | * profile icon. |
| 323 | */ |
| 324 | public void setExcludeMimes(String[] excludeMimes) { |
| 325 | mExcludeMimes = excludeMimes; |
| 326 | } |
| 327 | |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 328 | private class QueryHandler extends AsyncQueryHandler { |
| 329 | |
| 330 | public QueryHandler(ContentResolver cr) { |
| 331 | super(cr); |
| 332 | } |
| 333 | |
| 334 | @Override |
| 335 | protected void onQueryComplete(int token, Object cookie, Cursor cursor) { |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 336 | Uri lookupUri = null; |
| 337 | Uri createUri = null; |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 338 | boolean trigger = false; |
Yorke Lee | 02f304f | 2013-01-14 08:57:43 -0800 | [diff] [blame] | 339 | Bundle extras = (cookie != null) ? (Bundle) cookie : new Bundle(); |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 340 | try { |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 341 | switch(token) { |
| 342 | case TOKEN_PHONE_LOOKUP_AND_TRIGGER: |
| 343 | trigger = true; |
Yorke Lee | 02f304f | 2013-01-14 08:57:43 -0800 | [diff] [blame] | 344 | createUri = Uri.fromParts("tel", extras.getString(EXTRA_URI_CONTENT), null); |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 345 | |
Makoto Onuki | a362e1d | 2010-09-07 10:46:59 -0700 | [diff] [blame] | 346 | //$FALL-THROUGH$ |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 347 | case TOKEN_PHONE_LOOKUP: { |
| 348 | if (cursor != null && cursor.moveToFirst()) { |
| 349 | long contactId = cursor.getLong(PHONE_ID_COLUMN_INDEX); |
| 350 | String lookupKey = cursor.getString(PHONE_LOOKUP_STRING_COLUMN_INDEX); |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 351 | lookupUri = Contacts.getLookupUri(contactId, lookupKey); |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 352 | } |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 353 | |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 354 | break; |
| 355 | } |
| 356 | case TOKEN_EMAIL_LOOKUP_AND_TRIGGER: |
| 357 | trigger = true; |
Yorke Lee | 02f304f | 2013-01-14 08:57:43 -0800 | [diff] [blame] | 358 | createUri = Uri.fromParts("mailto", |
| 359 | extras.getString(EXTRA_URI_CONTENT), null); |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 360 | |
Makoto Onuki | a362e1d | 2010-09-07 10:46:59 -0700 | [diff] [blame] | 361 | //$FALL-THROUGH$ |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 362 | case TOKEN_EMAIL_LOOKUP: { |
| 363 | if (cursor != null && cursor.moveToFirst()) { |
| 364 | long contactId = cursor.getLong(EMAIL_ID_COLUMN_INDEX); |
| 365 | String lookupKey = cursor.getString(EMAIL_LOOKUP_STRING_COLUMN_INDEX); |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 366 | lookupUri = Contacts.getLookupUri(contactId, lookupKey); |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 367 | } |
Makoto Onuki | a362e1d | 2010-09-07 10:46:59 -0700 | [diff] [blame] | 368 | break; |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | } finally { |
| 372 | if (cursor != null) { |
| 373 | cursor.close(); |
| 374 | } |
| 375 | } |
| 376 | |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 377 | mContactUri = lookupUri; |
Mark Wagner | f5935a9 | 2009-09-22 13:49:26 -0700 | [diff] [blame] | 378 | onContactUriChanged(); |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 379 | |
| 380 | if (trigger && lookupUri != null) { |
Daniel Lehmann | e13e55b | 2011-08-07 19:51:28 -0700 | [diff] [blame] | 381 | // Found contact, so trigger QuickContact |
| 382 | QuickContact.showQuickContact(getContext(), QuickContactBadge.this, lookupUri, |
| 383 | QuickContact.MODE_LARGE, mExcludeMimes); |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 384 | } else if (createUri != null) { |
| 385 | // Prompt user to add this person to contacts |
| 386 | final Intent intent = new Intent(Intents.SHOW_OR_CREATE_CONTACT, createUri); |
Yorke Lee | 02f304f | 2013-01-14 08:57:43 -0800 | [diff] [blame] | 387 | if (extras != null) { |
| 388 | extras.remove(EXTRA_URI_CONTENT); |
| 389 | intent.putExtras(extras); |
| 390 | } |
Jeff Sharkey | 0050ee3 | 2009-09-17 16:11:32 -0700 | [diff] [blame] | 391 | getContext().startActivity(intent); |
Evan Millar | e727f18 | 2009-08-27 11:15:55 -0700 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | } |
| 395 | } |