Example usage for android.provider ContactsContract AUTHORITY

List of usage examples for android.provider ContactsContract AUTHORITY

Introduction

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

Prototype

String AUTHORITY

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

Click Source Link

Document

The authority for the contacts provider

Usage

From source file:org.c99.SyncProviderDemo.ContactsSyncAdapterService.java

private static void addContact(Account account, String name, String username) {
    Log.i(TAG, "Adding contact: " + name);
    ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();

    ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
    builder.withValue(RawContacts.ACCOUNT_NAME, account.name);
    builder.withValue(RawContacts.ACCOUNT_TYPE, account.type);
    builder.withValue(RawContacts.SYNC1, username);
    operationList.add(builder.build());/*  w  ww .j  a va  2 s  . com*/

    builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
    builder.withValueBackReference(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID, 0);
    builder.withValue(ContactsContract.Data.MIMETYPE,
            ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name);
    operationList.add(builder.build());

    builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
    builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
    builder.withValue(ContactsContract.Data.MIMETYPE,
            "vnd.android.cursor.item/vnd.org.c99.SyncProviderDemo.profile");
    builder.withValue(ContactsContract.Data.DATA1, username);
    builder.withValue(ContactsContract.Data.DATA2, "SyncProviderDemo Profile");
    builder.withValue(ContactsContract.Data.DATA3, "View profile");
    operationList.add(builder.build());

    try {
        mContentResolver.applyBatch(ContactsContract.AUTHORITY, operationList);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.kontalk.sync.SyncAdapter.java

/**
 * Requests a manual sync to the system.
 * @return true if the sync has been actually requested to the system.
 *//* w  w  w .  j a va 2 s.  c om*/
public static boolean requestSync(Context context, boolean force) {
    if (!force && isThrottling()) {
        Log.d(TAG, "not requesting sync - throttling");
        return false;
    }

    // do not start if offline
    if (Preferences.getOfflineMode()) {
        Log.d(TAG, "not requesting sync - offline mode");
        return false;
    }

    Account acc = Authenticator.getDefaultAccount(context);
    Bundle extra = new Bundle();
    // override auto-sync and background data settings
    extra.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
    // put our sync ahead of other sync operations :)
    extra.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
    ContentResolver.requestSync(acc, ContactsContract.AUTHORITY, extra);
    return true;
}

From source file:org.kontalk.sync.SyncAdapter.java

public static boolean isPending(Context context) {
    Account acc = Authenticator.getDefaultAccount(context);
    return ContentResolver.isSyncPending(acc, ContactsContract.AUTHORITY);
}

From source file:com.germainz.identiconizer.services.IdenticonRemovalService.java

private void processPhotos(ArrayList<ContactInfo> contactInfos) {
    for (int i = 0, j = contactInfos.size(); i < j; i++) {
        updateNotification(getString(R.string.identicons_remove_service_running_title),
                String.format(getString(R.string.identicons_remove_service_contact_summary), i, j));
        // ContactsListActivity gives us name_raw_contact_id, which is not unique.
        // For example, if the user has 3 accounts (e.g. Google, WhatsApp and Viber) then three
        // photo rows will exist, one for each.
        // We want to get those that have a photo set (any photo, not identicons.)
        // We can't assume name_raw_contact_id == raw_contact_id because it might only match
        // one photo row (which might be empty) when multiple accounts are present.
        Cursor cursor = getIdenticonPhotos(contactInfos.get(i).nameRawContactId);
        while (cursor.moveToNext()) {
            int contactId = cursor.getInt(0);
            byte[] data = cursor.getBlob(1);
            if (data != null)
                removeIdenticon(contactId);
        }/*  ww w .j  a  v a  2  s .co  m*/
    }

    if (!mOps.isEmpty()) {
        updateNotification(getString(R.string.identicons_remove_service_running_title),
                getString(R.string.identicons_remove_service_contact_summary_finishing));
        try {
            // Perform operations in batches of 100, to avoid TransactionTooLargeExceptions
            for (int i = 0, j = mOps.size(); i < j; i += 100)
                getContentResolver().applyBatch(ContactsContract.AUTHORITY,
                        new ArrayList<ContentProviderOperation>(mOps.subList(i, i + Math.min(100, j - i))));
        } catch (RemoteException | OperationApplicationException e) {
            Log.e(TAG, "Unable to apply batch", e);
        }
    }
}

From source file:org.codarama.haxsync.services.ContactsSyncAdapterService.java

private static void addContact(Account account, String name, String username) {
    ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();

    ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(RawContacts.CONTENT_URI);
    builder.withValue(RawContacts.ACCOUNT_NAME, account.name);
    builder.withValue(RawContacts.ACCOUNT_TYPE, account.type);
    builder.withValue(RawContacts.SYNC1, username);
    operationList.add(builder.build());/*from  w w  w . j a  v  a  2s  .  co m*/

    builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
    builder.withValueBackReference(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID, 0);
    builder.withValue(ContactsContract.Data.MIMETYPE,
            ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
    builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name);
    operationList.add(builder.build());

    builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
    builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
    builder.withValue(ContactsContract.Data.MIMETYPE,
            "vnd.android.cursor.item/vnd.org.codarama.haxsync.profile");
    builder.withValue(ContactsContract.Data.DATA1, username);
    builder.withValue(ContactsContract.Data.DATA2, "Facebook Profile");
    builder.withValue(ContactsContract.Data.DATA3, "View profile");
    operationList.add(builder.build());

    try {
        mContentResolver.applyBatch(ContactsContract.AUTHORITY, operationList);
    } catch (Exception e) {
        Log.e("Error", e.getLocalizedMessage());
    }
}

From source file:com.canelmas.let.sample.ContactsActivity.java

/**
 * Accesses the Contacts content provider directly to insert a new contact.
 * <p>//from  ww w  .  ja va  2s.  c  om
 * The contact is called "__DUMMY ENTRY" and only contains a name.
 */
private void insertDummyContact() {
    // Two operations are needed to insert a new contact.
    ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(2);

    // First, set up a new raw contact.
    ContentProviderOperation.Builder op = ContentProviderOperation
            .newInsert(ContactsContract.RawContacts.CONTENT_URI)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null);
    operations.add(op.build());

    // Next, set the name for the contact.
    op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.MIMETYPE,
                    ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, DUMMY_CONTACT_NAME);
    operations.add(op.build());

    // Apply the operations.
    ContentResolver resolver = getContentResolver();
    try {
        resolver.applyBatch(ContactsContract.AUTHORITY, operations);
    } catch (RemoteException e) {
        Log.d(TAG, "Could not add a new contact: " + e.getMessage());
    } catch (OperationApplicationException e) {
        Log.d(TAG, "Could not add a new contact: " + e.getMessage());
    }
}

From source file:org.kontalk.sync.SyncAdapter.java

public static boolean isActive(Context context) {
    Account acc = Authenticator.getDefaultAccount(context);
    return acc != null && ContentResolver.isSyncActive(acc, ContactsContract.AUTHORITY);
}

From source file:org.sufficientlysecure.keychain.service.ContactSyncAdapterService.java

public static void requestContactsSync() {
    // if user has disabled automatic sync, do nothing
    boolean isSyncEnabled = ContentResolver.getSyncAutomatically(
            new Account(Constants.ACCOUNT_NAME, Constants.ACCOUNT_TYPE), ContactsContract.AUTHORITY);

    if (!isSyncEnabled) {
        return;/*from   ww  w.j a  v  a2  s  . co  m*/
    }

    Bundle extras = new Bundle();
    // no need to wait, do it immediately
    extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
    extras.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
    ContentResolver.requestSync(new Account(Constants.ACCOUNT_NAME, Constants.ACCOUNT_TYPE),
            ContactsContract.AUTHORITY, extras);
}

From source file:org.linphone.ContactsListFragment.java

private void deleteExistingContact(Contact contact) {
    String select = ContactsContract.Data.CONTACT_ID + " = ?";
    String[] args = new String[] { contact.getID() };

    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    ops.add(ContentProviderOperation.newDelete(ContactsContract.RawContacts.CONTENT_URI)
            .withSelection(select, args).build());

    try {// www.j  a va  2s .  co  m
        getActivity().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        ContactsManager.getInstance().removeAllFriends(contact);
    } catch (Exception e) {
        Log.w(e.getMessage() + ":" + e.getStackTrace());
    }
}

From source file:at.bitfire.davdroid.ui.setup.AccountDetailsFragment.java

protected boolean createAccount(String accountName, DavResourceFinder.Configuration config) {
    Account account = new Account(accountName, Constants.ACCOUNT_TYPE);

    // create Android account
    Bundle userData = AccountSettings.initialUserData(config.userName);
    App.log.log(Level.INFO, "Creating Android account with initial config", new Object[] { account, userData });

    AccountManager accountManager = AccountManager.get(getContext());
    if (!accountManager.addAccountExplicitly(account, config.password, userData))
        return false;

    // add entries for account to service DB
    App.log.log(Level.INFO, "Writing account configuration to database", config);
    @Cleanup//  w ww .j  a  va 2  s  .c  o  m
    OpenHelper dbHelper = new OpenHelper(getContext());
    SQLiteDatabase db = dbHelper.getWritableDatabase();
    try {
        AccountSettings settings = new AccountSettings(getContext(), account);

        Intent refreshIntent = new Intent(getActivity(), DavService.class);
        refreshIntent.setAction(DavService.ACTION_REFRESH_COLLECTIONS);

        if (config.cardDAV != null) {
            // insert CardDAV service
            long id = insertService(db, accountName, Services.SERVICE_CARDDAV, config.cardDAV);

            // start CardDAV service detection (refresh collections)
            refreshIntent.putExtra(DavService.EXTRA_DAV_SERVICE_ID, id);
            getActivity().startService(refreshIntent);

            // initial CardDAV account settings
            int idx = spnrGroupMethod.getSelectedItemPosition();
            String groupMethodName = getResources()
                    .getStringArray(R.array.settings_contact_group_method_values)[idx];
            settings.setGroupMethod(GroupMethod.valueOf(groupMethodName));

            // enable contact sync
            ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1);
            settings.setSyncInterval(ContactsContract.AUTHORITY, DEFAULT_SYNC_INTERVAL);
        } else
            // disable contact sync when CardDAV is not available
            ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 0);

        if (config.calDAV != null) {
            // insert CalDAV service
            long id = insertService(db, accountName, Services.SERVICE_CALDAV, config.calDAV);

            // start CalDAV service detection (refresh collections)
            refreshIntent.putExtra(DavService.EXTRA_DAV_SERVICE_ID, id);
            getActivity().startService(refreshIntent);

            // enable calendar sync
            ContentResolver.setIsSyncable(account, CalendarContract.AUTHORITY, 1);
            settings.setSyncInterval(CalendarContract.AUTHORITY, DEFAULT_SYNC_INTERVAL);

            // enable task sync, if possible
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                /* Android >=6, it's possible to gain OpenTasks permissions dynamically, so
                 * OpenTasks sync will be enabled by default. Setting the sync interval to "manually"
                 * if OpenTasks is not installed avoids the "sync error" in Android settings / Accounts. */
                ContentResolver.setIsSyncable(account, TaskProvider.ProviderName.OpenTasks.authority, 1);
                settings.setSyncInterval(TaskProvider.ProviderName.OpenTasks.authority,
                        LocalTaskList.tasksProviderAvailable(getContext()) ? DEFAULT_SYNC_INTERVAL
                                : AccountSettings.SYNC_INTERVAL_MANUALLY);
            } else {
                // Android <6: enable task sync according to whether OpenTasks is accessible
                if (LocalTaskList.tasksProviderAvailable(getContext())) {
                    ContentResolver.setIsSyncable(account, TaskProvider.ProviderName.OpenTasks.authority, 1);
                    settings.setSyncInterval(TaskProvider.ProviderName.OpenTasks.authority,
                            DEFAULT_SYNC_INTERVAL);
                } else
                    // Android <6 only: disable OpenTasks sync forever when OpenTasks is not installed
                    // because otherwise, there will be a non-catchable SecurityException as soon as OpenTasks is installed
                    ContentResolver.setIsSyncable(account, TaskProvider.ProviderName.OpenTasks.authority, 0);
            }
        } else {
            // disable calendar and task sync when CalDAV is not available
            ContentResolver.setIsSyncable(account, CalendarContract.AUTHORITY, 0);
            ContentResolver.setIsSyncable(account, TaskProvider.ProviderName.OpenTasks.authority, 0);
        }

    } catch (InvalidAccountException e) {
        App.log.log(Level.SEVERE, "Couldn't access account settings", e);
    }

    return true;
}