7 package com.google.appinventor.components.runtime;
20 import android.app.Activity;
21 import android.content.ContentUris;
22 import android.content.Intent;
23 import android.database.Cursor;
24 import android.net.Uri;
25 import android.provider.Contacts;
26 import android.util.Log;
28 import java.util.Arrays;
29 import java.util.ArrayList;
30 import java.util.List;
51 @DesignerComponent(version = YaVersion.PHONENUMBERPICKER_COMPONENT_VERSION,
52 description =
"A button that, when clicked on, displays a list of " +
53 "the contacts' phone numbers to choose among. After the user has made a " +
54 "selection, the following properties will be set to information about " +
55 "the chosen contact: <ul>\n" +
56 "<li> <code>ContactName</code>: the contact's name </li>\n " +
57 "<li> <code>PhoneNumber</code>: the contact's phone number </li>\n " +
58 "<li> <code>EmailAddress</code>: the contact's email address </li> " +
59 "<li> <code>Picture</code>: the name of the file containing the contact's " +
60 "image, which can be used as a <code>Picture</code> property value for " +
61 "the <code>Image</code> or <code>ImageSprite</code> component.</li></ul>\n" +
62 "</p><p>Other properties affect the appearance of the button " +
63 "(<code>TextAlignment</code>, <code>BackgroundColor</code>, etc.) and " +
64 "whether it can be clicked on (<code>Enabled</code>).</p>\n" +
65 "<p>The PhoneNumberPicker component may not work on all Android " +
66 "devices. For example, on Android systems before system 3.0, the " +
67 "returned lists of phone numbers and email addresses will be empty.\n",
68 category = ComponentCategory.SOCIAL)
70 @UsesPermissions(permissionNames =
"android.permission.READ_CONTACTS")
73 private static String[] NAME_PROJECTION;
74 private static String[] DATA_PROJECTION;
75 private static final String[] PROJECTION = {
76 Contacts.PeopleColumns.NAME,
77 Contacts.PhonesColumns.NUMBER,
78 Contacts.Phones.PERSON_ID,
79 Contacts.People.PRIMARY_EMAIL_ID,
81 private static final int NAME_INDEX = 0;
82 private static final int NUMBER_INDEX = 1;
83 private static final int PERSON_INDEX = 2;
84 private static final int EMAIL_INDEX = 3;
85 private static final String LOG_TAG =
"PhoneNumberPicker";
93 super(container, Contacts.Phones.CONTENT_URI);
102 public String PhoneNumber() {
103 return ensureNotNull(phoneNumber);
122 if (requestCode == this.requestCode && resultCode == Activity.RESULT_OK) {
123 Log.i(LOG_TAG,
"received intent is " + data);
124 Uri phoneUri = data.getData();
126 String desiredPhoneUri =
"";
128 desiredPhoneUri =
"//com.android.contacts/data";
130 desiredPhoneUri =
"//contacts/phones";
133 if (checkContactUri(phoneUri, desiredPhoneUri)) {
134 Cursor contactCursor =
null;
135 Cursor dataCursor =
null;
139 contactCursor = activityContext.getContentResolver().query(phoneUri,
140 NAME_PROJECTION,
null,
null,
null);
141 String
id = postHoneycombGetContactNameAndPicture(contactCursor);
145 postHoneycombGetContactEmailsAndPhones(dataCursor);
147 contactCursor = activityContext.getContentResolver().query(phoneUri,
148 PROJECTION,
null,
null,
null);
149 preHoneycombGetContactInfo(contactCursor);
153 "Contact name = " + contactName +
", phone number = " + phoneNumber +
154 ", emailAddress = " + emailAddress +
", contactPhotoUri = " + contactPictureUri);
155 }
catch (Exception e) {
159 Log.e(LOG_TAG,
"Exception in resultReturned", e);
162 if (contactCursor !=
null) {
163 contactCursor.close();
165 if (dataCursor !=
null){
179 if (cursor.moveToFirst()) {
180 contactName = guardCursorGetString(cursor, NAME_INDEX);
181 phoneNumber = guardCursorGetString(cursor, NUMBER_INDEX);
182 int contactId = cursor.getInt(PERSON_INDEX);
183 Uri cUri = ContentUris.withAppendedId(Contacts.People.CONTENT_URI, contactId);
184 contactPictureUri = cUri.toString();
185 String emailId = guardCursorGetString(cursor, EMAIL_INDEX);
186 emailAddress = getEmailAddress(emailId);
196 if (contactCursor.moveToFirst()) {
201 phoneNumber = guardCursorGetString(contactCursor, PHONE_INDEX);
202 id = guardCursorGetString(contactCursor, CONTACT_ID_INDEX);
203 contactName = guardCursorGetString(contactCursor, NAME_INDEX);
204 contactPictureUri = guardCursorGetString(contactCursor, PHOTO_INDEX);
214 List<String> phoneListToStore =
new ArrayList<String>();
215 List<String> emailListToStore =
new ArrayList<String>();
216 if (dataCursor.moveToFirst()) {
225 while (!dataCursor.isAfterLast()) {
226 String type = guardCursorGetString(dataCursor, MIME_INDEX);
227 if (type.contains(phoneType)) {
228 phoneListToStore.add(guardCursorGetString(dataCursor, PHONE_INDEX));
229 }
else if (type.contains(emailType)) {
230 emailListToStore.add(guardCursorGetString(dataCursor, EMAIL_INDEX));
232 Log.i(
"ContactPicker",
"Type mismatch: " + type +
233 " not " + phoneType +
236 dataCursor.moveToNext();
238 phoneNumberList = phoneListToStore;
239 emailAddressList = emailListToStore;
240 if (!emailAddressList.isEmpty()) {
241 emailAddress = (String) emailAddressList.get(0);