| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.compat.annotation.UnsupportedAppUsage; |
| 20 |
import android.content.AsyncQueryHandler; |
| 21 |
import android.content.ContentResolver; |
| 22 |
import android.content.Context; |
| 23 |
import android.content.Intent; |
| 24 |
import android.content.res.TypedArray; |
| 25 |
import android.database.Cursor; |
| 26 |
import android.graphics.Canvas; |
| 27 |
import android.graphics.drawable.Drawable; |
| 28 |
import android.net.Uri; |
| 29 |
import android.os.Bundle; |
| 30 |
import android.provider.ContactsContract.CommonDataKinds.Email; |
| 31 |
import android.provider.ContactsContract.Contacts; |
| 32 |
import android.provider.ContactsContract.Intents; |
| 33 |
import android.provider.ContactsContract.PhoneLookup; |
| 34 |
import android.provider.ContactsContract.QuickContact; |
| 35 |
import android.provider.ContactsContract.RawContacts; |
| 36 |
import android.util.AttributeSet; |
| 37 |
import android.view.View; |
| 38 |
import android.view.View.OnClickListener; |
| 39 |
|
| 40 |
import com.android.internal.R; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class QuickContactBadge extends ImageView implements OnClickListener { |
| 47 |
private Uri mContactUri; |
| 48 |
private String mContactEmail; |
| 49 |
private String mContactPhone; |
| 50 |
@UnsupportedAppUsage |
| 51 |
private Drawable mOverlay; |
| 52 |
private QueryHandler mQueryHandler; |
| 53 |
private Drawable mDefaultAvatar; |
| 54 |
private Bundle mExtras = null; |
| 55 |
private String mPrioritizedMimeType; |
| 56 |
|
| 57 |
protected String[] mExcludeMimes = null; |
| 58 |
|
| 59 |
static final private int TOKEN_EMAIL_LOOKUP = 0; |
| 60 |
static final private int TOKEN_PHONE_LOOKUP = 1; |
| 61 |
static final private int TOKEN_EMAIL_LOOKUP_AND_TRIGGER = 2; |
| 62 |
static final private int TOKEN_PHONE_LOOKUP_AND_TRIGGER = 3; |
| 63 |
|
| 64 |
static final private String EXTRA_URI_CONTENT = "uri_content"; |
| 65 |
|
| 66 |
static final String[] EMAIL_LOOKUP_PROJECTION = new String[] { |
| 67 |
RawContacts.CONTACT_ID, |
| 68 |
Contacts.LOOKUP_KEY, |
| 69 |
}; |
| 70 |
static final int EMAIL_ID_COLUMN_INDEX = 0; |
| 71 |
static final int EMAIL_LOOKUP_STRING_COLUMN_INDEX = 1; |
| 72 |
|
| 73 |
static final String[] PHONE_LOOKUP_PROJECTION = new String[] { |
| 74 |
PhoneLookup._ID, |
| 75 |
PhoneLookup.LOOKUP_KEY, |
| 76 |
}; |
| 77 |
static final int PHONE_ID_COLUMN_INDEX = 0; |
| 78 |
static final int PHONE_LOOKUP_STRING_COLUMN_INDEX = 1; |
| 79 |
|
| 80 |
public QuickContactBadge(Context context) { |
| 81 |
this(context, null); |
| 82 |
} |
| 83 |
|
| 84 |
public QuickContactBadge(Context context, AttributeSet attrs) { |
| 85 |
this(context, attrs, 0); |
| 86 |
} |
| 87 |
|
| 88 |
public QuickContactBadge(Context context, AttributeSet attrs, int defStyleAttr) { |
| 89 |
this(context, attrs, defStyleAttr, 0); |
| 90 |
} |
| 91 |
|
| 92 |
public QuickContactBadge( |
| 93 |
Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 94 |
super(context, attrs, defStyleAttr, defStyleRes); |
| 95 |
|
| 96 |
TypedArray styledAttributes = mContext.obtainStyledAttributes(R.styleable.Theme); |
| 97 |
mOverlay = styledAttributes.getDrawable( |
| 98 |
com.android.internal.R.styleable.Theme_quickContactBadgeOverlay); |
| 99 |
styledAttributes.recycle(); |
| 100 |
|
| 101 |
setOnClickListener(this); |
| 102 |
} |
| 103 |
|
| 104 |
@Override |
| 105 |
protected void onAttachedToWindow() { |
| 106 |
super.onAttachedToWindow(); |
| 107 |
|
| 108 |
if (!isInEditMode()) { |
| 109 |
mQueryHandler = new QueryHandler(mContext.getContentResolver()); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
protected void drawableStateChanged() { |
| 115 |
super.drawableStateChanged(); |
| 116 |
|
| 117 |
final Drawable overlay = mOverlay; |
| 118 |
if (overlay != null && overlay.isStateful() |
| 119 |
&& overlay.setState(getDrawableState())) { |
| 120 |
invalidateDrawable(overlay); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
@Override |
| 125 |
public void drawableHotspotChanged(float x, float y) { |
| 126 |
super.drawableHotspotChanged(x, y); |
| 127 |
|
| 128 |
if (mOverlay != null) { |
| 129 |
mOverlay.setHotspot(x, y); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
@SuppressWarnings("unused") |
| 135 |
public void setMode(int size) { |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public void setPrioritizedMimeType(String prioritizedMimeType) { |
| 144 |
mPrioritizedMimeType = prioritizedMimeType; |
| 145 |
} |
| 146 |
|
| 147 |
@Override |
| 148 |
protected void onDraw(Canvas canvas) { |
| 149 |
super.onDraw(canvas); |
| 150 |
|
| 151 |
if (!isEnabled()) { |
| 152 |
|
| 153 |
return; |
| 154 |
} |
| 155 |
|
| 156 |
if (mOverlay == null || mOverlay.getIntrinsicWidth() == 0 || |
| 157 |
mOverlay.getIntrinsicHeight() == 0) { |
| 158 |
|
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
mOverlay.setBounds(0, 0, getWidth(), getHeight()); |
| 163 |
|
| 164 |
if (mPaddingTop == 0 && mPaddingLeft == 0) { |
| 165 |
mOverlay.draw(canvas); |
| 166 |
} else { |
| 167 |
int saveCount = canvas.getSaveCount(); |
| 168 |
canvas.save(); |
| 169 |
canvas.translate(mPaddingLeft, mPaddingTop); |
| 170 |
mOverlay.draw(canvas); |
| 171 |
canvas.restoreToCount(saveCount); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
private boolean isAssigned() { |
| 177 |
return mContactUri != null || mContactEmail != null || mContactPhone != null; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public void setImageToDefault() { |
| 184 |
if (mDefaultAvatar == null) { |
| 185 |
mDefaultAvatar = mContext.getDrawable(R.drawable.ic_contact_picture); |
| 186 |
} |
| 187 |
setImageDrawable(mDefaultAvatar); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public void assignContactUri(Uri contactUri) { |
| 200 |
mContactUri = contactUri; |
| 201 |
mContactEmail = null; |
| 202 |
mContactPhone = null; |
| 203 |
onContactUriChanged(); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public void assignContactFromEmail(String emailAddress, boolean lazyLookup) { |
| 216 |
assignContactFromEmail(emailAddress, lazyLookup, null); |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public void assignContactFromEmail(String emailAddress, boolean lazyLookup, Bundle extras) { |
| 234 |
mContactEmail = emailAddress; |
| 235 |
mExtras = extras; |
| 236 |
if (!lazyLookup && mQueryHandler != null) { |
| 237 |
mQueryHandler.startQuery(TOKEN_EMAIL_LOOKUP, null, |
| 238 |
Uri.withAppendedPath(Email.CONTENT_LOOKUP_URI, Uri.encode(mContactEmail)), |
| 239 |
EMAIL_LOOKUP_PROJECTION, null, null, null); |
| 240 |
} else { |
| 241 |
mContactUri = null; |
| 242 |
onContactUriChanged(); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public void assignContactFromPhone(String phoneNumber, boolean lazyLookup) { |
| 257 |
assignContactFromPhone(phoneNumber, lazyLookup, new Bundle()); |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
public void assignContactFromPhone(String phoneNumber, boolean lazyLookup, Bundle extras) { |
| 274 |
mContactPhone = phoneNumber; |
| 275 |
mExtras = extras; |
| 276 |
if (!lazyLookup && mQueryHandler != null) { |
| 277 |
mQueryHandler.startQuery(TOKEN_PHONE_LOOKUP, null, |
| 278 |
Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, mContactPhone), |
| 279 |
PHONE_LOOKUP_PROJECTION, null, null, null); |
| 280 |
} else { |
| 281 |
mContactUri = null; |
| 282 |
onContactUriChanged(); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
public void setOverlay(Drawable overlay) { |
| 293 |
mOverlay = overlay; |
| 294 |
} |
| 295 |
|
| 296 |
private void onContactUriChanged() { |
| 297 |
setEnabled(isAssigned()); |
| 298 |
} |
| 299 |
|
| 300 |
@Override |
| 301 |
public void onClick(View v) { |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
final Bundle extras = (mExtras == null) ? new Bundle() : mExtras; |
| 306 |
if (mContactUri != null) { |
| 307 |
QuickContact.showQuickContact(getContext(), QuickContactBadge.this, mContactUri, |
| 308 |
mExcludeMimes, mPrioritizedMimeType); |
| 309 |
} else if (mContactEmail != null && mQueryHandler != null) { |
| 310 |
extras.putString(EXTRA_URI_CONTENT, mContactEmail); |
| 311 |
mQueryHandler.startQuery(TOKEN_EMAIL_LOOKUP_AND_TRIGGER, extras, |
| 312 |
Uri.withAppendedPath(Email.CONTENT_LOOKUP_URI, Uri.encode(mContactEmail)), |
| 313 |
EMAIL_LOOKUP_PROJECTION, null, null, null); |
| 314 |
} else if (mContactPhone != null && mQueryHandler != null) { |
| 315 |
extras.putString(EXTRA_URI_CONTENT, mContactPhone); |
| 316 |
mQueryHandler.startQuery(TOKEN_PHONE_LOOKUP_AND_TRIGGER, extras, |
| 317 |
Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, mContactPhone), |
| 318 |
PHONE_LOOKUP_PROJECTION, null, null, null); |
| 319 |
} else { |
| 320 |
|
| 321 |
return; |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
@Override |
| 326 |
public CharSequence getAccessibilityClassName() { |
| 327 |
return QuickContactBadge.class.getName(); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
public void setExcludeMimes(String[] excludeMimes) { |
| 336 |
mExcludeMimes = excludeMimes; |
| 337 |
} |
| 338 |
|
| 339 |
private class QueryHandler extends AsyncQueryHandler { |
| 340 |
|
| 341 |
public QueryHandler(ContentResolver cr) { |
| 342 |
super(cr); |
| 343 |
} |
| 344 |
|
| 345 |
@Override |
| 346 |
protected void onQueryComplete(int token, Object cookie, Cursor cursor) { |
| 347 |
Uri lookupUri = null; |
| 348 |
Uri createUri = null; |
| 349 |
boolean trigger = false; |
| 350 |
Bundle extras = (cookie != null) ? (Bundle) cookie : new Bundle(); |
| 351 |
try { |
| 352 |
switch(token) { |
| 353 |
case TOKEN_PHONE_LOOKUP_AND_TRIGGER: |
| 354 |
trigger = true; |
| 355 |
createUri = Uri.fromParts("tel", extras.getString(EXTRA_URI_CONTENT), null); |
| 356 |
|
| 357 |
|
| 358 |
case TOKEN_PHONE_LOOKUP: { |
| 359 |
if (cursor != null && cursor.moveToFirst()) { |
| 360 |
long contactId = cursor.getLong(PHONE_ID_COLUMN_INDEX); |
| 361 |
String lookupKey = cursor.getString(PHONE_LOOKUP_STRING_COLUMN_INDEX); |
| 362 |
lookupUri = Contacts.getLookupUri(contactId, lookupKey); |
| 363 |
} |
| 364 |
|
| 365 |
break; |
| 366 |
} |
| 367 |
case TOKEN_EMAIL_LOOKUP_AND_TRIGGER: |
| 368 |
trigger = true; |
| 369 |
createUri = Uri.fromParts("mailto", |
| 370 |
extras.getString(EXTRA_URI_CONTENT), null); |
| 371 |
|
| 372 |
|
| 373 |
case TOKEN_EMAIL_LOOKUP: { |
| 374 |
if (cursor != null && cursor.moveToFirst()) { |
| 375 |
long contactId = cursor.getLong(EMAIL_ID_COLUMN_INDEX); |
| 376 |
String lookupKey = cursor.getString(EMAIL_LOOKUP_STRING_COLUMN_INDEX); |
| 377 |
lookupUri = Contacts.getLookupUri(contactId, lookupKey); |
| 378 |
} |
| 379 |
break; |
| 380 |
} |
| 381 |
} |
| 382 |
} finally { |
| 383 |
if (cursor != null) { |
| 384 |
cursor.close(); |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
mContactUri = lookupUri; |
| 389 |
onContactUriChanged(); |
| 390 |
|
| 391 |
if (trigger && mContactUri != null) { |
| 392 |
|
| 393 |
QuickContact.showQuickContact(getContext(), QuickContactBadge.this, mContactUri, |
| 394 |
mExcludeMimes, mPrioritizedMimeType); |
| 395 |
} else if (createUri != null) { |
| 396 |
|
| 397 |
final Intent intent = new Intent(Intents.SHOW_OR_CREATE_CONTACT, createUri); |
| 398 |
if (extras != null) { |
| 399 |
Bundle bundle = new Bundle(extras); |
| 400 |
bundle.remove(EXTRA_URI_CONTENT); |
| 401 |
intent.putExtras(bundle); |
| 402 |
} |
| 403 |
getContext().startActivity(intent); |
| 404 |
} |
| 405 |
} |
| 406 |
} |
| 407 |
} |