PageRenderTime 61ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/core/java/android/widget/QuickContactBadge.java

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