private void addContactVoiceSupport(List<ContentProviderOperation> operations, @NonNull String e164number, long rawContactId) { operations.add(ContentProviderOperation.newUpdate(RawContacts.CONTENT_URI) .withSelection(RawContacts._ID + " = ?", new String[] {String.valueOf(rawContactId)}) .withValue(RawContacts.SYNC4, "true") .build()); operations.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build()) .withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId) .withValue(ContactsContract.Data.MIMETYPE, CALL_MIMETYPE) .withValue(ContactsContract.Data.DATA1, e164number) .withValue(ContactsContract.Data.DATA2, context.getString(R.string.app_name)) .withValue(ContactsContract.Data.DATA3, context.getString(R.string.ContactsDatabase_signal_call_s, e164number)) .withYieldAllowed(true) .build()); }
@Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case id.menu_edit_profile: Intent intent = new Intent(getActivity(), ProfileEditActivity.class); startActivity(intent); break; case id.menu_add_contact: Intent add_contact_intent = new Intent(Insert.ACTION); add_contact_intent.setType(RawContacts.CONTENT_TYPE); startActivityForResult(add_contact_intent, TAG_ADD_CONTACT); break; case id.menu_about: Intent about_intent = new Intent(getActivity(), AboutActivity.class); startActivity(about_intent); break; } return super.onOptionsItemSelected(item); }
public static void addContact(String name, String number) { ContentValues values = new ContentValues(); //������RawContacts.CONTENT_URIִ��һ����ֵ���룬Ŀ���ǻ�ȡϵͳ���ص�rawContactId Uri rawContactUri = m_context.getContentResolver().insert(RawContacts.CONTENT_URI, values); long rawContactId = ContentUris.parseId(rawContactUri); //��data������������� values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);//�������� values.put(StructuredName.GIVEN_NAME, name); m_context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values); //��data�����绰���� values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); values.put(Phone.NUMBER, number); values.put(Phone.TYPE, Phone.TYPE_MOBILE); m_context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values); }
private void clearAllContacts(final JSONObject contactOptions, final String requestID) { ArrayList<ContentProviderOperation> deleteOptions = new ArrayList<ContentProviderOperation>(); // Delete all contacts from the selected account ContentProviderOperation.Builder deleteOptionsBuilder = ContentProviderOperation.newDelete(RawContacts.CONTENT_URI); if (mAccountName != null) { deleteOptionsBuilder.withSelection(RawContacts.ACCOUNT_NAME + "=?", new String[] {mAccountName}) .withSelection(RawContacts.ACCOUNT_TYPE + "=?", new String[] {mAccountType}); } deleteOptions.add(deleteOptionsBuilder.build()); // Clear the contacts String returnStatus = "KO"; if (applyBatch(deleteOptions) != null) { returnStatus = "OK"; } Log.i(LOGTAG, "Sending return status: " + returnStatus); sendCallbackToJavascript("Android:Contacts:Clear:Return:" + returnStatus, requestID, new String[] {"contactID"}, new Object[] {"undefined"}); }
private Cursor getAllRawContactIdsCursor() { // When a contact is deleted, it actually just sets the deleted field to 1 until the // sync adapter actually deletes the contact later so ignore any contacts with the deleted // flag set String selection = RawContacts.DELETED + "=0"; String[] selectionArgs = null; // Only get contacts from the selected account if (mAccountName != null) { selection += " AND " + RawContacts.ACCOUNT_NAME + "=? AND " + RawContacts.ACCOUNT_TYPE + "=?"; selectionArgs = new String[] {mAccountName, mAccountType}; } // Get the ID's of all contacts and use the number of contact ID's as // the total number of contacts return mContentResolver.query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID}, selection, selectionArgs, null); }
/** * Synchronously insert a contact with the designated @name into * the ContactsContentProvider. This code is explained at * http://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html. */ private void addContact(String name, List<ContentProviderOperation> cpops) { final int position = cpops.size(); // First part of operation. cpops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI) .withValue(RawContacts.ACCOUNT_TYPE, mOps.getAccountType()) .withValue(RawContacts.ACCOUNT_NAME, mOps.getAccountName()) .withValue(Contacts.STARRED, 1) .build()); // Second part of operation. cpops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) .withValueBackReference(Data.RAW_CONTACT_ID, position) .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE) .withValue(StructuredName.DISPLAY_NAME, name) .build()); }
/** * Each contact requires two (asynchronous) insertions into * the Contacts Provider. The first insert puts the * RawContact into the Contacts Provider and the second insert * puts the data associated with the RawContact into the * Contacts Provider. */ public void executeImpl() { if (getArgs().getIterator().hasNext()) { // If there are any contacts left to insert, make a // ContentValues object containing the RawContact // portion of the contact and initiate an asynchronous // insert on the Contacts ContentProvider. final ContentValues values = makeRawContact(1); getArgs().getAdapter() .startInsert(this, INSERT_RAW_CONTACT, RawContacts.CONTENT_URI, values); } else // Otherwise, print a toast with summary info. Utils.showToast(getArgs().getOps().getActivityContext(), getArgs().getCounter().getValue() +" contact(s) inserted"); }
/** * Update the custom row-modification-date * * @param serverId * the serverId for this contact * @param uri * Uri for the existing raw contact to be updated * @return instance of ContactOperations */ public ContactOperations updateClientMod(Long version, Long clientMod, Uri uri) { mValues.clear(); if (clientMod != null) { mValues.put(RawContacts.SYNC2, clientMod); } mValues.put(RawContacts.SYNC3, version); if (Log.isLoggable(TAG, Log.INFO)) { Log.i(TAG, "ClientMod updated: " + (clientMod != null ? new Date(clientMod).toString() : "null") + ",version:" + version + " for contactId:" + ContentUris.parseId(uri)); } addUpdateOp(uri); return this; }
protected void populatePhoto(Contact c) throws RemoteException { @Cleanup Cursor cursor = providerClient.query(dataURI(), new String[] { Photo.PHOTO_FILE_ID, Photo.PHOTO }, Photo.RAW_CONTACT_ID + "=? AND " + Data.MIMETYPE + "=?", new String[] { String.valueOf(c.getLocalID()), Photo.CONTENT_ITEM_TYPE }, null); if (cursor != null && cursor.moveToNext()) { if (!cursor.isNull(0)) { Uri photoUri = Uri.withAppendedPath( ContentUris.withAppendedId(RawContacts.CONTENT_URI, c.getLocalID()), RawContacts.DisplayPhoto.CONTENT_DIRECTORY); try { @Cleanup AssetFileDescriptor fd = providerClient.openAssetFile(photoUri, "r"); @Cleanup InputStream is = fd.createInputStream(); c.setPhoto(IOUtils.toByteArray(is)); } catch(IOException ex) { Log.w(TAG, "Couldn't read high-res contact photo", ex); } } else c.setPhoto(cursor.getBlob(1)); } }
public EasyDb(long contactId) { cursor = context.getContentResolver().query( RawContacts.CONTENT_URI, new String[]{RawContacts._ID, RawContacts.CONTACT_ID, RawContacts.AGGREGATION_MODE, RawContacts.DELETED, RawContacts.TIMES_CONTACTED, RawContacts.LAST_TIME_CONTACTED, RawContacts.STARRED, RawContacts.CUSTOM_RINGTONE, RawContacts.SEND_TO_VOICEMAIL, RawContacts.ACCOUNT_NAME, RawContacts.ACCOUNT_TYPE, RawContacts.DATA_SET, RawContacts.SOURCE_ID, RawContacts.VERSION, RawContacts.DIRTY, RawContacts.SYNC1, RawContacts.SYNC2, RawContacts.SYNC3, RawContacts.SYNC4, }, RawContacts.CONTACT_ID + "=?", new String[]{String.valueOf(contactId)}, null); cursor.moveToFirst(); columnNames = cursor.getColumnNames(); }
public Parcels(Context context, long contactId) { this.context = context; cursor = context.getContentResolver().query( RawContacts.CONTENT_URI, new String[]{RawContacts._ID, RawContacts.CONTACT_ID, RawContacts.AGGREGATION_MODE, RawContacts.DELETED, RawContacts.TIMES_CONTACTED, RawContacts.LAST_TIME_CONTACTED, RawContacts.STARRED, RawContacts.CUSTOM_RINGTONE, RawContacts.SEND_TO_VOICEMAIL, RawContacts.ACCOUNT_NAME, RawContacts.ACCOUNT_TYPE, RawContacts.DATA_SET, RawContacts.SOURCE_ID, RawContacts.VERSION, RawContacts.DIRTY, RawContacts.SYNC1, RawContacts.SYNC2, RawContacts.SYNC3, RawContacts.SYNC4, }, RawContacts.CONTACT_ID + "=?", new String[]{String.valueOf(contactId)}, null); //cursor.moveToFirst(); columnNames = cursor.getColumnNames(); }
private void getContactDetails(long id){ Cursor cursor = getActivity().getContentResolver().query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID, RawContacts.DISPLAY_NAME_PRIMARY, RawContacts.CONTACT_ID, RawContacts.SYNC1}, RawContacts._ID + "=" +id, null, null); if (cursor.getColumnCount() >= 1){ cursor.moveToFirst(); name = cursor.getString(cursor.getColumnIndex(RawContacts.DISPLAY_NAME_PRIMARY)); uid = cursor.getString(cursor.getColumnIndex(RawContacts.SYNC1)); joined = ContactUtil.getMergedContacts(getActivity(), id); contactID = cursor.getLong(cursor.getColumnIndex(RawContacts.CONTACT_ID)); } cursor.close(); imageURI = Uri.withAppendedPath( ContentUris.withAppendedId(RawContacts.CONTENT_URI, id), RawContacts.DisplayPhoto.CONTENT_DIRECTORY); Log.i("imageuri", imageURI.toString()); }
@Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setEmptyText(getActivity().getString(R.string.no_contacts)); setHasOptionsMenu(true); String[] columns = new String[] {RawContacts.DISPLAY_NAME_PRIMARY}; int[] to = new int[] { android.R.id.text1 }; mAdapter = new ContactsCursorAdapter( getActivity(), android.R.layout.simple_list_item_activated_1, null, columns, to, 0); setListAdapter(mAdapter); showSyncIndicator(); mContentProviderHandle = ContentResolver.addStatusChangeListener( ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE, this); }
public static Contact getMergedContact(Context c, long contactID, Account account){ Uri ContactUri = RawContacts.CONTENT_URI.buildUpon() .appendQueryParameter(RawContacts.ACCOUNT_NAME, account.name) .appendQueryParameter(RawContacts.ACCOUNT_TYPE, account.type) .build(); Cursor cursor = c.getContentResolver().query(ContactUri, new String[] { BaseColumns._ID, RawContacts.DISPLAY_NAME_PRIMARY}, RawContacts.CONTACT_ID +" = '" + contactID + "'", null, null); if (cursor.getCount() > 0){ cursor.moveToFirst(); Contact contact = new Contact(); contact.ID = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID)); contact.name = cursor.getString(cursor.getColumnIndex(RawContacts.DISPLAY_NAME_PRIMARY)); cursor.close(); return contact; } cursor.close(); return null; }
public static Set<Long> getRawContacts(ContentResolver c, long rawContactID, String accountType){ HashSet<Long> ids = new HashSet<Long>(); Cursor cursor = c.query(RawContacts.CONTENT_URI, new String[] { RawContacts.CONTACT_ID}, RawContacts._ID +" = '" + rawContactID + "'", null, null); if (cursor.getCount() > 0){ cursor.moveToFirst(); long contactID = cursor.getLong(cursor.getColumnIndex(RawContacts.CONTACT_ID)); // Log.i("QUERY", RawContacts.CONTACT_ID +" = '" + contactID + "'" + " AND " + BaseColumns._ID + " != " +rawContactID + " AND " + RawContacts.ACCOUNT_TYPE + " = '" + accountType+"'"); Cursor c2 = c.query(RawContacts.CONTENT_URI, new String[] { BaseColumns._ID}, RawContacts.CONTACT_ID +" = '" + contactID + "'" + " AND " + BaseColumns._ID + " != " +rawContactID + " AND " + RawContacts.ACCOUNT_TYPE + " = '" + accountType+"'", null, null); // Log.i("CURSOR SIZE", String.valueOf(c2.getCount())); while (c2.moveToNext()){ ids.add(c2.getLong(c2.getColumnIndex(BaseColumns._ID))); } c2.close(); } cursor.close(); return ids; }
public static void addEmail(Context c, long rawContactId, String email){ DeviceUtil.log(c, "adding email", email); String where = ContactsContract.Data.RAW_CONTACT_ID + " = '" + rawContactId + "' AND " + ContactsContract.Data.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE+ "'"; Cursor cursor = c.getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[] { RawContacts.CONTACT_ID}, where, null, null); if (cursor.getCount() == 0){ ContentValues contentValues = new ContentValues(); //op.put(ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID, ); contentValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE); contentValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); contentValues.put(ContactsContract.CommonDataKinds.Email.ADDRESS, email); c.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, contentValues); } cursor.close(); }
/** * <p>Delete a set of contacts based on their id.</p> * <p><i>Note:</i> the method used for bulk delete is a group selection * based on id (<i>{@ling BaseColumns#_ID} IN (id1, id2, ...)</i>). * @param ids The IDs if all users that should be deleted. */ public void bulkDelete(long[] ids) { if (ids.length == 0) { return; } StringBuilder where = new StringBuilder(); where.append(RawContacts._ID); where.append(" IN ("); where.append(Long.toString(ids[0])); for (int i = 1; i < ids.length; i++) { where.append(','); where.append(Long.toString(ids[i])); } where.append(')'); try { provider.delete(RawContacts.CONTENT_URI, where.toString(), null); } catch (RemoteException e) { e.printStackTrace(); } }
public int getContactByRawContactID(long id) { int result = -1; try { Cursor cursor = provider.query( RawContacts.CONTENT_URI, CONTACT_OF_RAW_CONTACT_PROJECTION, RawContacts._ID + "=" + id, null, null); try { if (cursor.moveToFirst()) { result = cursor.getInt(cursor.getColumnIndex( RawContacts.CONTACT_ID)); } } finally { cursor.close(); } } catch (RemoteException e) { e.printStackTrace(); } return result; }
/** * Retrieve a single jid as bound by a local account jid, with or without * metadata. * @param accountJid The local account jid. * @param jid The remote jid. * @param metadata True if a second fetch for metadata should be done. * @return A single contact. */ public RawContact getRawContactByJid(String accountJid, String jid, boolean metadata) { RawContact contact = null; try { Cursor cursor = provider.query( RawContacts.CONTENT_URI, RAW_CONTACT_PROJECTION_MAP, RawContacts.ACCOUNT_NAME + "=? AND " + RawContacts.SOURCE_ID + "=?", new String[]{accountJid, jid}, null); try { if (cursor.moveToFirst()) { contact = newRawContact(cursor); if (metadata) { fetchMetadata(contact); } } } finally { cursor.close(); } } catch (RemoteException e) { e.printStackTrace(); } return contact; }
/** * Create a new raw contact for the current cursor. * @param cursor The DB cursor, scrolled to the row in question. * @return */ private RawContact newRawContact(Cursor cursor) { RawContact contact = new RawContact(); int index = cursor.getColumnIndex(RawContacts._ID); contact.setID(cursor.getLong(index)); index = cursor.getColumnIndex(RawContacts.ACCOUNT_NAME); contact.setAccountName(cursor.getString(index)); index = cursor.getColumnIndex(RawContacts.ACCOUNT_TYPE); contact.setAccountType(cursor.getString(index)); index = cursor.getColumnIndex(RawContacts.SOURCE_ID); contact.setSourceID(cursor.getString(index)); for (int i = 0; i < RAW_CONTACTS_SYNC_FIELDS.length; i++) { index = cursor.getColumnIndex(RAW_CONTACTS_SYNC_FIELDS[i]); contact.setSync(i, cursor.getString(index)); } return contact; }
/** * 获取手机上所有联系人的contactId,sim卡和email帐户除外 */ private Set<String> getContactIdOnlyPhoneBook() { // sumsung sim account type: 'vnd.sec.contact.sim', // htc sim account type: 'com.anddroid.contacts.sim' // accountType='com.google' represent google account type, we think it // is phone account type Cursor cursor = queryDB(RawContacts.CONTENT_URI, new String[] { RawContacts.CONTACT_ID }, RawContacts.CONTACT_ID + " NOT NULL AND (" + RawContacts.ACCOUNT_TYPE + " NOT LIKE '%sim%' or " + RawContacts.ACCOUNT_TYPE + " IS NULL)", null); Set<String> contactIds = new HashSet<String>(); if (cursor.moveToFirst()) { contactIds.add(cursor.getString(0)); while (cursor.moveToNext()) { contactIds.add(cursor.getString(0)); } } cursor.close(); return contactIds; }
private void removeContactVoiceSupport(List<ContentProviderOperation> operations, long rawContactId) { operations.add(ContentProviderOperation.newUpdate(RawContacts.CONTENT_URI) .withSelection(RawContacts._ID + " = ?", new String[] {String.valueOf(rawContactId)}) .withValue(RawContacts.SYNC4, "false") .build()); operations.add(ContentProviderOperation.newDelete(ContactsContract.Data.CONTENT_URI.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build()) .withSelection(ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?", new String[] {String.valueOf(rawContactId), CALL_MIMETYPE}) .withYieldAllowed(true) .build()); }
private void removeTextSecureRawContact(List<ContentProviderOperation> operations, Account account, long rowId) { operations.add(ContentProviderOperation.newDelete(RawContacts.CONTENT_URI.buildUpon() .appendQueryParameter(RawContacts.ACCOUNT_NAME, account.name) .appendQueryParameter(RawContacts.ACCOUNT_TYPE, account.type) .appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build()) .withYieldAllowed(true) .withSelection(BaseColumns._ID + " = ?", new String[] {String.valueOf(rowId)}) .build()); }
@Override public List<Long> getSelfIdentityRawContactIds() { long selfIdentityContactId = getSelfIdentityContactId(); if (selfIdentityContactId == -1) return null; Cursor cursor = null; ArrayList<Long> rawContactIds = new ArrayList<Long>(); try { cursor = context.getContentResolver().query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID}, RawContacts.CONTACT_ID + " = ?", new String[] {selfIdentityContactId+""}, null); if (cursor == null || cursor.getCount() == 0) return null; while (cursor.moveToNext()) { rawContactIds.add(Long.valueOf(cursor.getLong(0))); } return rawContactIds; } finally { if (cursor != null) cursor.close(); } }
public static void AddContact(ContentResolver mResolver, String name, String number, String groupName) { Log.i("hoperun", "name= " + name + ";number=" + number); if (!queryFromContact(mResolver, name, number)) { ContentValues values = new ContentValues(); // 首先向RawContacts.CONTENT_URI执行一个空值插入,目的是获取系统返回的rawContactId Uri rawContactUri = mResolver.insert(RawContacts.CONTENT_URI, values); if (rawContactUri != null) { long rawContactId = ContentUris.parseId(rawContactUri); // 往data表插入姓名数据 values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);// 内容类型 values.put(StructuredName.GIVEN_NAME, name); mResolver.insert(ContactsContract.Data.CONTENT_URI, values); // 往data表插入电话数据 values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); values.put(Phone.NUMBER, number); values.put(Phone.TYPE, Phone.TYPE_MOBILE); mResolver.insert(ContactsContract.Data.CONTENT_URI, values); } else { Log.i("hoperun", "name= " + name + ";number=" + number); } } else { Log.i("hoperun", "repeat name= " + name + ";number=" + number); } }
/** * 往手机通讯录插入联系人 * * @param ct * @param name * @param tel * @param email */ public static void insertContact(Context ct, String name, String tel, String email) { ContentValues values = new ContentValues(); // 首先向RawContacts.CONTENT_URI执行一个空值插入,目的是获取系统返回的rawContactId Uri rawContactUri = ct.getContentResolver().insert(RawContacts.CONTENT_URI, values); long rawContactId = ContentUris.parseId(rawContactUri); // 往data表入姓名数据 if (!TextUtils.isEmpty(tel)) { values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);// 内容类型 values.put(StructuredName.GIVEN_NAME, name); ct.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values); } // 往data表入电话数据 if (!TextUtils.isEmpty(tel)) { values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);// 内容类型 values.put(Phone.NUMBER, tel); values.put(Phone.TYPE, Phone.TYPE_MOBILE); ct.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values); } // 往data表入Email数据 if (!TextUtils.isEmpty(email)) { values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);// 内容类型 values.put(Email.DATA, email); values.put(Email.TYPE, Email.TYPE_WORK); ct.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values); } }
/** * Called when user picks contact. * @param requestCode The request code originally supplied to startActivityForResult(), * allowing you to identify who this result came from. * @param resultCode The integer result code returned by the child activity through its setResult(). * @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras"). * @throws JSONException */ public void onActivityResult(int requestCode, int resultCode, final Intent intent) { if (requestCode == CONTACT_PICKER_RESULT) { if (resultCode == Activity.RESULT_OK) { String contactId = intent.getData().getLastPathSegment(); // to populate contact data we require Raw Contact ID // so we do look up for contact raw id first Cursor c = this.cordova.getActivity().getContentResolver().query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID}, RawContacts.CONTACT_ID + " = " + contactId, null, null); if (!c.moveToFirst()) { this.callbackContext.error("Error occured while retrieving contact raw id"); return; } String id = c.getString(c.getColumnIndex(RawContacts._ID)); c.close(); try { JSONObject contact = contactAccessor.getContactById(id); this.callbackContext.success(contact); return; } catch (JSONException e) { LOG.e(LOG_TAG, "JSON fail.", e); } } else if (resultCode == Activity.RESULT_CANCELED) { callbackContext.error(OPERATION_CANCELLED_ERROR); return; } this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, UNKNOWN_ERROR)); } }
/** * Called when user picks contact. * @param requestCode The request code originally supplied to startActivityForResult(), * allowing you to identify who this result came from. * @param resultCode The integer result code returned by the child activity through its setResult(). * @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras"). * @throws JSONException */ public void onActivityResult(int requestCode, int resultCode, final Intent intent) { if (requestCode == CONTACT_PICKER_RESULT) { if (resultCode == Activity.RESULT_OK) { String contactId = intent.getData().getLastPathSegment(); // to populate contact data we require Raw Contact ID // so we do look up for contact raw id first Cursor c = this.cordova.getActivity().getContentResolver().query(RawContacts.CONTENT_URI, new String[] {RawContacts._ID}, RawContacts.CONTACT_ID + " = " + contactId, null, null); if (!c.moveToFirst()) { this.callbackContext.error("Error occured while retrieving contact raw id"); return; } String id = c.getString(c.getColumnIndex(RawContacts._ID)); c.close(); try { JSONObject contact = contactAccessor.getContactById(id); this.callbackContext.success(contact); return; } catch (JSONException e) { Log.e(LOG_TAG, "JSON fail.", e); } } else if (resultCode == Activity.RESULT_CANCELED) { callbackContext.error(OPERATION_CANCELLED_ERROR); return; } this.callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, UNKNOWN_ERROR)); } }
public static void insertContact(Context context, String name, String phone) { // 首先插入空值,再得到rawContactsId ,用于下面插值 ContentValues values = new ContentValues(); // insert a null value Uri rawContactUri = context.getContentResolver().insert( RawContacts.CONTENT_URI, values); long rawContactsId = ContentUris.parseId(rawContactUri); // 往刚才的空记录中插入姓名 values.clear(); // A reference to the _ID that this data belongs to values.put(StructuredName.RAW_CONTACT_ID, rawContactsId); // "CONTENT_ITEM_TYPE" MIME type used when storing this in data table values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE); // The name that should be used to display the contact. values.put(StructuredName.DISPLAY_NAME, name); // insert the real values context.getContentResolver().insert(Data.CONTENT_URI, values); // 插入电话 values.clear(); values.put(Phone.RAW_CONTACT_ID, rawContactsId); // String "Data.MIMETYPE":The MIME type of the item represented by this // row // String "CONTENT_ITEM_TYPE": MIME type used when storing this in data // table. values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); values.put(Phone.NUMBER, phone); context.getContentResolver().insert(Data.CONTENT_URI, values); }