Example usage for android.provider ContactsContract DIRECTORY_PARAM_KEY

List of usage examples for android.provider ContactsContract DIRECTORY_PARAM_KEY

Introduction

In this page you can find the example usage for android.provider ContactsContract DIRECTORY_PARAM_KEY.

Prototype

String DIRECTORY_PARAM_KEY

To view the source code for android.provider ContactsContract DIRECTORY_PARAM_KEY.

Click Source Link

Document

Query parameter that should be used by the client to access a specific Directory .

Usage

From source file:com.android.contacts.common.model.ContactBuilder.java

public ContactBuilder(Uri encodedContactUri) throws JSONException {
    String jsonData = encodedContactUri.getEncodedFragment();
    String directoryId = encodedContactUri.getQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY);
    if (!TextUtils.isEmpty(directoryId)) {
        try {/*w ww  . j av a  2  s . c o m*/
            mDirectoryId = Long.parseLong(directoryId);
        } catch (NumberFormatException e) {
            Log.e(TAG, "Error parsing directory id of uri " + encodedContactUri, e);
        }
    }
    try {
        // name
        JSONObject json = new JSONObject(jsonData);
        JSONObject contact = json.optJSONObject(Contacts.CONTENT_ITEM_TYPE);
        JSONObject nameObj = contact.optJSONObject(StructuredName.CONTENT_ITEM_TYPE);
        mName = new Name(nameObj);

        if (contact != null) {
            // numbers
            if (contact.has(Phone.CONTENT_ITEM_TYPE)) {
                String phoneData = contact.getString(Phone.CONTENT_ITEM_TYPE);
                Object phoneObject = new JSONTokener(phoneData).nextValue();
                JSONArray phoneNumbers;
                if (phoneObject instanceof JSONObject) {
                    phoneNumbers = new JSONArray();
                    phoneNumbers.put(phoneObject);
                } else {
                    phoneNumbers = contact.getJSONArray(Phone.CONTENT_ITEM_TYPE);
                }
                for (int i = 0; i < phoneNumbers.length(); ++i) {
                    JSONObject phoneObj = phoneNumbers.getJSONObject(i);
                    mPhoneNumbers.add(new PhoneNumber(phoneObj));
                }
            }

            // address
            if (contact.has(StructuredPostal.CONTENT_ITEM_TYPE)) {
                JSONArray addresses = contact.getJSONArray(StructuredPostal.CONTENT_ITEM_TYPE);
                for (int i = 0; i < addresses.length(); ++i) {
                    JSONObject addrObj = addresses.getJSONObject(i);
                    mAddresses.add(new Address(addrObj));
                }
            }

            // websites
            if (contact.has(Website.CONTENT_ITEM_TYPE)) {
                JSONArray websites = contact.getJSONArray(Website.CONTENT_ITEM_TYPE);
                for (int i = 0; i < websites.length(); ++i) {
                    JSONObject websiteObj = websites.getJSONObject(i);
                    final WebsiteUrl websiteUrl = new WebsiteUrl(websiteObj);
                    if (!TextUtils.isEmpty(websiteUrl.url)) {
                        mWebsites.add(new WebsiteUrl(websiteObj));
                    }
                }
            }

            mSpamCount = contact.optInt(CallerMetaData.SPAM_COUNT, 0);
            mInfoProviderName = contact.optString(CallerMetaData.INFO_PROVIDER, null);
            mSuccinctLocation = contact.optString(CallerMetaData.SUCCINCT_LOCATION, null);
            mPhotoUrl = contact.optString(CallerMetaData.PHOTO_URL, null);
        }

    } catch (JSONException e) {
        Log.e(TAG, "Error parsing encoded fragment of uri " + encodedContactUri, e);
        throw e;
    }
}

From source file:com.android.dialer.calllog.ContactInfoHelper.java

/**
 * Creates a JSON-encoded lookup uri for a unknown number without an associated contact
 *
 * @param number - Unknown phone number//from  w w w .  jav a2  s.  c o  m
 * @return JSON-encoded URI that can be used to perform a lookup when clicking on the quick
 *         contact card.
 */
private static Uri createTemporaryContactUri(String number) {
    try {
        final JSONObject contactRows = new JSONObject().put(Phone.CONTENT_ITEM_TYPE,
                new JSONObject().put(Phone.NUMBER, number).put(Phone.TYPE, Phone.TYPE_CUSTOM));

        final String jsonString = new JSONObject().put(Contacts.DISPLAY_NAME, number)
                .put(Contacts.DISPLAY_NAME_SOURCE, DisplayNameSources.PHONE)
                .put(Contacts.CONTENT_ITEM_TYPE, contactRows).toString();

        return Contacts.CONTENT_LOOKUP_URI.buildUpon().appendPath(Constants.LOOKUP_URI_ENCODED)
                .appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(Long.MAX_VALUE))
                .encodedFragment(jsonString).build();
    } catch (JSONException e) {
        return null;
    }
}

From source file:cm.confide.ex.chips.RecipientAlternatesAdapter.java

private static Cursor doQuery(CharSequence constraint, int limit, Long directoryId, Account account,
        ContentResolver resolver, Query query) {
    final Uri.Builder builder = query.getContentFilterUri().buildUpon().appendPath(constraint.toString())
            .appendQueryParameter(ContactsContract.LIMIT_PARAM_KEY,
                    String.valueOf(limit + BaseRecipientAdapter.ALLOWANCE_FOR_DUPLICATES));
    if (directoryId != null) {
        builder.appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(directoryId));
    }/*from  ww  w .  ja v  a 2s  .c  om*/
    if (account != null) {
        builder.appendQueryParameter(BaseRecipientAdapter.PRIMARY_ACCOUNT_NAME, account.name);
        builder.appendQueryParameter(BaseRecipientAdapter.PRIMARY_ACCOUNT_TYPE, account.type);
    }
    final Cursor cursor = resolver.query(builder.build(), query.getProjection(), null, null, null);
    return cursor;
}

From source file:com.android.contacts.common.model.ContactLoader.java

private static Contact loadEncodedContactEntity(Uri uri, Uri lookupUri) throws JSONException {
    final String jsonString = uri.getEncodedFragment();
    final JSONObject json = new JSONObject(jsonString);

    final long directoryId = Long.valueOf(uri.getQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY));

    final String displayName = json.optString(Contacts.DISPLAY_NAME);
    final String altDisplayName = json.optString(Contacts.DISPLAY_NAME_ALTERNATIVE, displayName);
    final int displayNameSource = json.getInt(Contacts.DISPLAY_NAME_SOURCE);
    final String photoUri = json.optString(Contacts.PHOTO_URI, null);
    final Contact contact = new Contact(uri, uri, lookupUri, directoryId, null /* lookupKey */, -1 /* id */,
            -1 /* nameRawContactId */, displayNameSource, 0 /* photoId */, photoUri, displayName,
            altDisplayName, null /* phoneticName */, false /* starred */, null /* presence */,
            false /* sendToVoicemail */, null /* customRingtone */, false /* isUserProfile */);

    contact.setStatuses(new ImmutableMap.Builder<Long, DataStatus>().build());

    final String accountName = json.optString(RawContacts.ACCOUNT_NAME, null);
    final String directoryName = uri.getQueryParameter(Directory.DISPLAY_NAME);
    if (accountName != null) {
        final String accountType = json.getString(RawContacts.ACCOUNT_TYPE);
        contact.setDirectoryMetaData(directoryName, null, accountName, accountType,
                json.optInt(Directory.EXPORT_SUPPORT, Directory.EXPORT_SUPPORT_SAME_ACCOUNT_ONLY));
    } else {//from  www  .  j  a  va2 s .  c o m
        contact.setDirectoryMetaData(directoryName, null, null, null,
                json.optInt(Directory.EXPORT_SUPPORT, Directory.EXPORT_SUPPORT_ANY_ACCOUNT));
    }

    final ContentValues values = new ContentValues();
    values.put(Data._ID, -1);
    values.put(Data.CONTACT_ID, -1);
    final RawContact rawContact = new RawContact(values);

    final JSONObject items = json.getJSONObject(Contacts.CONTENT_ITEM_TYPE);
    final Iterator keys = items.keys();
    while (keys.hasNext()) {
        final String mimetype = (String) keys.next();

        // Could be single object or array.
        final JSONObject obj = items.optJSONObject(mimetype);
        if (obj == null) {
            final JSONArray array = items.getJSONArray(mimetype);
            for (int i = 0; i < array.length(); i++) {
                final JSONObject item = array.getJSONObject(i);
                processOneRecord(rawContact, item, mimetype);
            }
        } else {
            processOneRecord(rawContact, obj, mimetype);
        }
    }

    contact.setRawContacts(new ImmutableList.Builder<RawContact>().add(rawContact).build());
    return contact;
}

From source file:com.android.contacts.common.model.ContactLoaderTest.java

public void testLoadContactReturnDirectoryContactWithoutDisplayName() throws JSONException {
    // Use lookup-style Uri that contains encoded json object which encapsulates the
    // directory contact. The test json object is:
    // {// w w  w . ja va 2  s . c  o m
    //   display_name_source": 40,
    //   "vnd.android.cursor.item\/contact":{"email":{"data1":"test@google.com" }}
    // }
    JSONObject itemJson = new JSONObject();
    itemJson.put("email", new JSONObject().put("data1", "test@google.com"));
    JSONObject json = new JSONObject();
    json.put(Contacts.NAME_RAW_CONTACT_ID, CONTACT_ID);
    json.put(Contacts.DISPLAY_NAME_SOURCE, DisplayNameSources.STRUCTURED_NAME);
    json.put(Contacts.CONTENT_ITEM_TYPE, itemJson);

    final Uri lookupUri = Contacts.CONTENT_LOOKUP_URI.buildUpon().encodedFragment(json.toString())
            .appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, "1")
            .appendPath(Constants.LOOKUP_URI_ENCODED).build();

    mContactsProvider.expectTypeQuery(lookupUri, Contacts.CONTENT_ITEM_TYPE);
    Contact contact = assertLoadContact(lookupUri);

    assertEquals(-1, contact.getId());
    assertEquals(-1, contact.getNameRawContactId());
    assertEquals(DisplayNameSources.STRUCTURED_NAME, contact.getDisplayNameSource());
    assertEquals("", contact.getDisplayName());
    assertEquals(lookupUri, contact.getLookupUri());
    assertEquals(1, contact.getRawContacts().size());
    mContactsProvider.verify();
}

From source file:com.android.contacts.common.model.ContactBuilder.java

public Contact build() {
    if (mName == null) {
        throw new IllegalStateException("Name has not been set");
    }/*from ww  w  .  jav a2s  .  com*/
    Uri lookupUri = null;

    if (mDirectoryType != FORWARD_LOOKUP && mDirectoryType != PEOPLE_LOOKUP
            && mDirectoryType != REVERSE_LOOKUP) {
        throw new IllegalStateException("Invalid directory type");
    }

    final ContentValues values = new ContentValues();
    values.put(ContactsContract.Data._ID, -1);
    final RawContact rawContact = new RawContact(values);

    final ContentValues numberValues = new ContentValues();
    numberValues.put(ContactsContract.Data._ID, -1);
    numberValues.put(ContactsContract.Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);

    // Use the incoming call's phone number if no other phone number
    // is specified. The reverse lookup source could present the phone
    // number differently (eg. without the area code).
    if (mPhoneNumbers.size() == 0) {
        PhoneNumber pn = new PhoneNumber();
        // Use the formatted number where possible
        pn.number = mFormattedNumber != null ? mFormattedNumber : mNormalizedNumber;
        pn.type = Phone.TYPE_MAIN;
        numberValues.put(ContactsContract.Data.DATA1, pn.number);
        addPhoneNumber(pn);
    }

    try {
        JSONObject contact = new JSONObject();

        // Insert the name
        contact.put(StructuredName.CONTENT_ITEM_TYPE, mName.getJsonObject());

        final String dataKeyPrefix = "data";

        // Insert phone numbers
        JSONArray phoneNumbers = new JSONArray();
        for (int i = 0; i < mPhoneNumbers.size(); i++) {
            phoneNumbers.put(mPhoneNumbers.get(i).getJsonObject());
            if (i + 1 < 15) { // data14 is max
                numberValues.put(dataKeyPrefix + (i + 1), mPhoneNumbers.get(i).number);
            }
        }
        rawContact.addDataItemValues(numberValues);
        contact.put(Phone.CONTENT_ITEM_TYPE, phoneNumbers);

        final ContentValues addressValues = new ContentValues();
        addressValues.put(ContactsContract.Data._ID, -1);
        addressValues.put(ContactsContract.Data.MIMETYPE, StructuredPostal.CONTENT_ITEM_TYPE);

        // Insert addresses if there are any
        if (mAddresses.size() > 0) {
            JSONArray addresses = new JSONArray();
            for (int i = 0; i < mAddresses.size(); i++) {
                addresses.put(mAddresses.get(i).getJsonObject());
                if (i + 1 < 15) { // data14 is max
                    addressValues.put(dataKeyPrefix + (i + 1), mAddresses.get(i).formattedAddress);
                }
            }
            contact.put(StructuredPostal.CONTENT_ITEM_TYPE, addresses);
            rawContact.addDataItemValues(addressValues);
        }

        // Insert websites if there are any
        if (mWebsites.size() > 0) {
            JSONArray websites = new JSONArray();
            for (int i = 0; i < mWebsites.size(); i++) {
                websites.put(mWebsites.get(i).getJsonObject());
            }
            contact.put(Website.CONTENT_ITEM_TYPE, websites);
        }

        // add spam count and attribution
        contact.put(CallerMetaData.SPAM_COUNT, getSpamCount());
        contact.put(CallerMetaData.INFO_PROVIDER, getInfoProviderName());
        contact.put(CallerMetaData.SUCCINCT_LOCATION, getSuccinctLocation());
        contact.put(CallerMetaData.PHOTO_URL, getPhotoUrl());
        mPhotoUrl = getPhotoUrl();

        String json = new JSONObject().put(Contacts.DISPLAY_NAME, mName.displayName)
                .put(Contacts.DISPLAY_NAME_SOURCE, mDisplayNameSource)
                .put(Directory.EXPORT_SUPPORT, Directory.EXPORT_SUPPORT_ANY_ACCOUNT)
                .put(Contacts.CONTENT_ITEM_TYPE, contact).toString();

        if (json != null) {
            long directoryId = -1;
            switch (mDirectoryType) {
            case FORWARD_LOOKUP:
                directoryId = DirectoryId.NEARBY;
                break;
            case PEOPLE_LOOKUP:
                directoryId = DirectoryId.PEOPLE;
                break;
            case REVERSE_LOOKUP:
                // use null directory to be backwards compatible with old code
                directoryId = DirectoryId.NULL;
                break;
            }

            lookupUri = Contacts.CONTENT_LOOKUP_URI.buildUpon().appendPath(Constants.LOOKUP_URI_ENCODED)
                    .appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(directoryId))
                    .encodedFragment(json).build();
        }

        final Contact contactToReturn = new Contact(lookupUri, lookupUri, lookupUri, mDirectoryId,
                null /* lookupKey */, -1 /* id */, -1 /* nameRawContactId */, mDisplayNameSource,
                0 /* photoId */, mPhotoUrl, mName.displayName, mName.givenName,
                mName.phoneticGivenName /* phoneticName */, false /* starred */, null /* presence */,
                false /* sendToVoicemail */, null /* customRingtone */, false /* isUserProfile */);

        contactToReturn.setStatuses(new ImmutableMap.Builder<Long, DataStatus>().build());
        final String accountName = contact.optString(ContactsContract.RawContacts.ACCOUNT_NAME, null);
        final String directoryName = lookupUri.getQueryParameter(Directory.DISPLAY_NAME);
        if (accountName != null) {
            final String accountType = contact.getString(ContactsContract.RawContacts.ACCOUNT_TYPE);
            contactToReturn.setDirectoryMetaData(directoryName, null, accountName, accountType,
                    contact.optInt(Directory.EXPORT_SUPPORT, Directory.EXPORT_SUPPORT_SAME_ACCOUNT_ONLY));
        } else {
            contactToReturn.setDirectoryMetaData(directoryName, null, null, null,
                    contact.optInt(Directory.EXPORT_SUPPORT, Directory.EXPORT_SUPPORT_ANY_ACCOUNT));
        }

        final ContentValues nameValues = new ContentValues();
        nameValues.put(ContactsContract.Data._ID, -1);
        nameValues.put(ContactsContract.Data.DATA1, mName.displayName);
        nameValues.put(ContactsContract.Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
        rawContact.addDataItemValues(nameValues);

        contactToReturn.setRawContacts(new ImmutableList.Builder<RawContact>().add(rawContact).build());

        contactToReturn.setSpamCount(getSpamCount());
        contactToReturn.setProviderName(getInfoProviderName());

        return contactToReturn;
    } catch (JSONException e) {
        Log.e(TAG, "Failed to build contactToReturn", e);
        return null;
    }
}

From source file:com.android.contacts.common.model.ContactLoader.java

/**
 * Extracts Contact level columns from the cursor.
 *//*from   w w w  . jav  a 2  s .  c om*/
private Contact loadContactHeaderData(final Cursor cursor, Uri contactUri) {
    final String directoryParameter = contactUri.getQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY);
    final long directoryId = directoryParameter == null ? Directory.DEFAULT
            : Long.parseLong(directoryParameter);
    final long contactId = cursor.getLong(ContactQuery.CONTACT_ID);
    final String lookupKey = cursor.getString(ContactQuery.LOOKUP_KEY);
    final long nameRawContactId = cursor.getLong(ContactQuery.NAME_RAW_CONTACT_ID);
    final int displayNameSource = cursor.getInt(ContactQuery.DISPLAY_NAME_SOURCE);
    final String displayName = cursor.getString(ContactQuery.DISPLAY_NAME);
    final String altDisplayName = cursor.getString(ContactQuery.ALT_DISPLAY_NAME);
    final String phoneticName = cursor.getString(ContactQuery.PHONETIC_NAME);
    final long photoId = cursor.getLong(ContactQuery.PHOTO_ID);
    final String photoUri = cursor.getString(ContactQuery.PHOTO_URI);
    final boolean starred = cursor.getInt(ContactQuery.STARRED) != 0;
    final Integer presence = cursor.isNull(ContactQuery.CONTACT_PRESENCE) ? null
            : cursor.getInt(ContactQuery.CONTACT_PRESENCE);
    final boolean sendToVoicemail = cursor.getInt(ContactQuery.SEND_TO_VOICEMAIL) == 1;
    final String customRingtone = cursor.getString(ContactQuery.CUSTOM_RINGTONE);
    final boolean isUserProfile = cursor.getInt(ContactQuery.IS_USER_PROFILE) == 1;

    Uri lookupUri;
    if (directoryId == Directory.DEFAULT || directoryId == Directory.LOCAL_INVISIBLE) {
        lookupUri = ContentUris.withAppendedId(Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, lookupKey),
                contactId);
    } else {
        lookupUri = contactUri;
    }

    return new Contact(mRequestedUri, contactUri, lookupUri, directoryId, lookupKey, contactId,
            nameRawContactId, displayNameSource, photoId, photoUri, displayName, altDisplayName, phoneticName,
            starred, presence, sendToVoicemail, customRingtone, isUserProfile);
}

From source file:com.android.contacts.list.ContactEntryListAdapter.java

protected Uri getContactUri(int partitionIndex, Cursor cursor, int contactIdColumn, int lookUpKeyColumn) {
    long contactId = cursor.getLong(contactIdColumn);
    String lookupKey = cursor.getString(lookUpKeyColumn);
    Uri uri = Contacts.getLookupUri(contactId, lookupKey);
    long directoryId = ((DirectoryPartition) getPartition(partitionIndex)).getDirectoryId();
    if (directoryId != Directory.DEFAULT) {
        uri = uri.buildUpon()//from w  w  w  . j  ava 2  s. co m
                .appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(directoryId))
                .build();
    }
    return uri;
}

From source file:cm.confide.ex.chips.BaseRecipientAdapter.java

private Cursor doQuery(CharSequence constraint, int limit, Long directoryId) {
    final Uri.Builder builder = mQuery.getContentFilterUri().buildUpon().appendPath(constraint.toString())
            .appendQueryParameter(ContactsContract.LIMIT_PARAM_KEY,
                    String.valueOf(limit + ALLOWANCE_FOR_DUPLICATES));
    if (directoryId != null) {
        builder.appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(directoryId));
    }//from w  ww .j  a v a  2  s  .  c  o  m
    if (mAccount != null) {
        builder.appendQueryParameter(PRIMARY_ACCOUNT_NAME, mAccount.name);
        builder.appendQueryParameter(PRIMARY_ACCOUNT_TYPE, mAccount.type);
    }
    final long start = System.currentTimeMillis();
    final Cursor cursor = mContentResolver.query(builder.build(), mQuery.getProjection(), null, null, null);
    final long end = System.currentTimeMillis();
    if (DEBUG) {
        Log.d(TAG,
                "Time for autocomplete (query: " + constraint + ", directoryId: " + directoryId
                        + ", num_of_results: " + (cursor != null ? cursor.getCount() : "null") + "): "
                        + (end - start) + " ms");
    }
    return cursor;
}

From source file:com.DGSD.Teexter.UI.Recipient.BaseRecipientAdapter.java

private Cursor doQuery(CharSequence constraint, int limit, Long directoryId) {
    final Uri.Builder builder = Phone.CONTENT_FILTER_URI.buildUpon().appendPath(constraint.toString())
            .appendQueryParameter(ContactsContract.LIMIT_PARAM_KEY,
                    String.valueOf(limit + ALLOWANCE_FOR_DUPLICATES));
    if (directoryId != null) {
        builder.appendQueryParameter(ContactsContract.DIRECTORY_PARAM_KEY, String.valueOf(directoryId));
    }/* ww  w .j  av  a2 s  .c o m*/
    if (mAccount != null) {
        builder.appendQueryParameter(PRIMARY_ACCOUNT_NAME, mAccount.name);
        builder.appendQueryParameter(PRIMARY_ACCOUNT_TYPE, mAccount.type);
    }
    final long start = System.currentTimeMillis();
    final Cursor cursor = mContentResolver.query(builder.build(), EmailQuery.PROJECTION, null, null, null);
    final long end = System.currentTimeMillis();
    if (DEBUG) {
        Log.d(TAG,
                "Time for autocomplete (query: " + constraint + ", directoryId: " + directoryId
                        + ", num_of_results: " + (cursor != null ? cursor.getCount() : "null") + "): "
                        + (end - start) + " ms");
    }
    return cursor;
}