Example usage for android.content ContentResolver acquireContentProviderClient

List of usage examples for android.content ContentResolver acquireContentProviderClient

Introduction

In this page you can find the example usage for android.content ContentResolver acquireContentProviderClient.

Prototype

public final @Nullable ContentProviderClient acquireContentProviderClient(@NonNull String name) 

Source Link

Document

Returns a ContentProviderClient that is associated with the ContentProvider with the authority of name, starting the provider if necessary.

Usage

From source file:at.bitfire.ical4android.AndroidCalendar.java

/**
 * Acquires a ContentProviderClient for the Android Calendar Contract.
 * @return A ContentProviderClient, or null if calendar storage is not available/accessible
 *         Caller is responsible for calling release()!
 *///w w w  .jav  a2 s.co  m
public static ContentProviderClient acquireContentProviderClient(ContentResolver resolver) {
    return resolver.acquireContentProviderClient(CalendarContract.AUTHORITY);
}

From source file:org.totschnig.myexpenses.provider.DbUtils.java

public static boolean restore(File backupFile) {
    boolean result = false;
    MyApplication app = MyApplication.getInstance();
    try {//from  w w  w.j a  va 2 s  .c  o m
        DailyAutoBackupScheduler.cancelAutoBackup(app);
        PlanExecutor.cancelPlans(app);
        Account.clear();
        PaymentMethod.clear();

        if (backupFile.exists()) {
            ContentResolver resolver = app.getContentResolver();
            ContentProviderClient client = resolver.acquireContentProviderClient(TransactionProvider.AUTHORITY);
            TransactionProvider provider = (TransactionProvider) client.getLocalContentProvider();
            result = provider.restore(backupFile);
            client.release();
        }
    } catch (Exception e) {
        AcraHelper.report(e);
    }
    app.initPlanner();
    DailyAutoBackupScheduler.updateAutoBackupAlarms(app);
    return result;
}

From source file:org.totschnig.myexpenses.provider.DbUtils.java

public static Result backup(File backupDir) {
    cacheEventData();//from ww w. j a  v  a 2s .  c om
    ContentResolver resolver = MyApplication.getInstance().getContentResolver();
    ContentProviderClient client = resolver.acquireContentProviderClient(TransactionProvider.AUTHORITY);
    TransactionProvider provider = (TransactionProvider) client.getLocalContentProvider();
    Result result = provider.backup(backupDir);
    client.release();
    return result;
}

From source file:com.granita.contacticloudsync.resource.LocalCalendar.java

@TargetApi(15)
public static Uri create(Account account, ContentResolver resolver, ServerInfo.ResourceInfo info)
        throws CalendarStorageException {
    @Cleanup("release")
    ContentProviderClient provider = resolver.acquireContentProviderClient(CalendarContract.AUTHORITY);
    if (provider == null)
        throw new CalendarStorageException(
                "Couldn't acquire ContentProviderClient for " + CalendarContract.AUTHORITY);

    ContentValues values = new ContentValues();
    values.put(Calendars.NAME, info.getUrl());
    values.put(Calendars.CALENDAR_DISPLAY_NAME, info.getTitle());
    values.put(Calendars.CALENDAR_COLOR, info.color != null ? info.color : defaultColor);

    if (info.isReadOnly())
        values.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_READ);
    else {//from w  w  w  .  j av  a  2 s  .  c  om
        values.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_OWNER);
        values.put(Calendars.CAN_MODIFY_TIME_ZONE, 1);
        values.put(Calendars.CAN_ORGANIZER_RESPOND, 1);
    }

    values.put(Calendars.OWNER_ACCOUNT, account.name);
    values.put(Calendars.SYNC_EVENTS, 1);
    values.put(Calendars.VISIBLE, 1);
    if (!TextUtils.isEmpty(info.timezone)) {
        VTimeZone timeZone = DateUtils.parseVTimeZone(info.timezone);
        if (timeZone != null && timeZone.getTimeZoneId() != null)
            values.put(Calendars.CALENDAR_TIME_ZONE,
                    DateUtils.findAndroidTimezoneID(timeZone.getTimeZoneId().getValue()));
    }
    values.put(Calendars.ALLOWED_REMINDERS, Reminders.METHOD_ALERT);
    if (Build.VERSION.SDK_INT >= 15) {
        values.put(Calendars.ALLOWED_AVAILABILITY,
                StringUtils.join(new int[] { Reminders.AVAILABILITY_TENTATIVE, Reminders.AVAILABILITY_FREE,
                        Reminders.AVAILABILITY_BUSY }, ","));
        values.put(Calendars.ALLOWED_ATTENDEE_TYPES,
                StringUtils.join(new int[] { CalendarContract.Attendees.TYPE_OPTIONAL,
                        CalendarContract.Attendees.TYPE_REQUIRED, CalendarContract.Attendees.TYPE_RESOURCE },
                        ", "));
    }
    return create(account, provider, values);
}

From source file:com.granita.icloudcalsync.resource.LocalTaskList.java

public static Uri create(Account account, ContentResolver resolver, ServerInfo.ResourceInfo info)
        throws LocalStorageException {
    final ContentProviderClient client = resolver.acquireContentProviderClient(TASKS_AUTHORITY);
    if (client == null)
        throw new LocalStorageException("No tasks provider found");

    ContentValues values = new ContentValues();
    values.put(TaskContract.TaskLists.ACCOUNT_NAME, account.name);
    values.put(TaskContract.TaskLists.ACCOUNT_TYPE, account.type);
    values.put(TaskContract.TaskLists._SYNC_ID, info.getURL());
    values.put(TaskContract.TaskLists.LIST_NAME, info.getTitle());
    values.put(TaskContract.TaskLists.LIST_COLOR, DAVUtils.CalDAVtoARGBColor(info.getColor()));
    values.put(TaskContract.TaskLists.OWNER, account.name);
    values.put(TaskContract.TaskLists.ACCESS_LEVEL, 0);
    values.put(TaskContract.TaskLists.SYNC_ENABLED, 1);
    values.put(TaskContract.TaskLists.VISIBLE, 1);

    Log.i(TAG, "Inserting task list: " + values.toString());
    try {// w w  w  .  ja  v  a 2s  .  co  m
        return client.insert(taskListsURI(account), values);
    } catch (RemoteException e) {
        throw new LocalStorageException(e);
    }
}

From source file:at.bitfire.davdroid.resource.LocalTaskList.java

public static Uri create(Account account, ContentResolver resolver, ServerInfo.ResourceInfo info)
        throws LocalStorageException {
    @Cleanup("release")
    final ContentProviderClient client = resolver.acquireContentProviderClient(TASKS_AUTHORITY);
    if (client == null)
        throw new LocalStorageException("No tasks provider found");

    ContentValues values = new ContentValues();
    values.put(TaskContract.TaskLists.ACCOUNT_NAME, account.name);
    values.put(TaskContract.TaskLists.ACCOUNT_TYPE, account.type);
    values.put(TaskContract.TaskLists._SYNC_ID, info.getURL());
    values.put(TaskContract.TaskLists.LIST_NAME, info.getTitle());
    values.put(TaskContract.TaskLists.LIST_COLOR,
            info.getColor() != null ? info.getColor() : DAVUtils.calendarGreen);
    values.put(TaskContract.TaskLists.OWNER, account.name);
    values.put(TaskContract.TaskLists.ACCESS_LEVEL, 0);
    values.put(TaskContract.TaskLists.SYNC_ENABLED, 1);
    values.put(TaskContract.TaskLists.VISIBLE, 1);

    Log.i(TAG, "Inserting task list: " + values.toString());
    try {/*w  w  w  . j  a v  a  2 s . co  m*/
        return client.insert(taskListsURI(account), values);
    } catch (RemoteException e) {
        throw new LocalStorageException(e);
    }
}

From source file:at.bitfire.davdroid.mirakel.resource.LocalCalendar.java

@SuppressLint("InlinedApi")
public static void create(Account account, ContentResolver resolver, ServerInfo.ResourceInfo info)
        throws LocalStorageException {
    ContentProviderClient client = resolver.acquireContentProviderClient(CalendarContract.AUTHORITY);
    if (client == null)
        throw new LocalStorageException("No Calendar Provider found (Calendar app disabled?)");

    int color = 0xFFC3EA6E; // fallback: "DAVdroid green"
    if (info.getColor() != null) {
        Pattern p = Pattern.compile("#(\\p{XDigit}{6})(\\p{XDigit}{2})?");
        Matcher m = p.matcher(info.getColor());
        if (m.find()) {
            int color_rgb = Integer.parseInt(m.group(1), 16);
            int color_alpha = m.group(2) != null ? (Integer.parseInt(m.group(2), 16) & 0xFF) : 0xFF;
            color = (color_alpha << 24) | color_rgb;
        }/*from  w  ww . ja v a  2  s .  co m*/
    }

    ContentValues values = new ContentValues();
    values.put(Calendars.ACCOUNT_NAME, account.name);
    values.put(Calendars.ACCOUNT_TYPE, account.type);
    values.put(Calendars.NAME, info.getURL());
    values.put(Calendars.CALENDAR_DISPLAY_NAME, info.getTitle());
    values.put(Calendars.CALENDAR_COLOR, color);
    values.put(Calendars.OWNER_ACCOUNT, account.name);
    values.put(Calendars.SYNC_EVENTS, 1);
    values.put(Calendars.VISIBLE, 1);
    values.put(Calendars.ALLOWED_REMINDERS, Reminders.METHOD_ALERT);

    if (info.isReadOnly())
        values.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_READ);
    else {
        values.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_OWNER);
        values.put(Calendars.CAN_ORGANIZER_RESPOND, 1);
        values.put(Calendars.CAN_MODIFY_TIME_ZONE, 1);
    }

    if (android.os.Build.VERSION.SDK_INT >= 15) {
        values.put(Calendars.ALLOWED_AVAILABILITY, Events.AVAILABILITY_BUSY + "," + Events.AVAILABILITY_FREE
                + "," + Events.AVAILABILITY_TENTATIVE);
        values.put(Calendars.ALLOWED_ATTENDEE_TYPES, Attendees.TYPE_NONE + "," + Attendees.TYPE_OPTIONAL + ","
                + Attendees.TYPE_REQUIRED + "," + Attendees.TYPE_RESOURCE);
    }

    if (info.getTimezone() != null)
        values.put(Calendars.CALENDAR_TIME_ZONE, info.getTimezone());

    Log.i(TAG, "Inserting calendar: " + values.toString() + " -> " + calendarsURI(account).toString());
    try {
        client.insert(calendarsURI(account), values);
    } catch (RemoteException e) {
        throw new LocalStorageException(e);
    }
}

From source file:saschpe.birthdays.service.CalendarSyncService.java

/**
 * Updates the calendar color//  ww  w  .  j  a v  a 2  s  . com
 */
public static void updateCalendarColor(Context context) {
    int color = PreferencesHelper.getCalendarColor(context);
    ContentResolver cr = context.getContentResolver();
    Uri uri = ContentUris.withAppendedId(getCalendarUri(context, CalendarContract.Calendars.CONTENT_URI),
            getCalendar(context));

    Log.d(TAG, "Updating calendar " + uri.toString() + " color " + color);

    ContentProviderClient client = cr.acquireContentProviderClient(CalendarContract.AUTHORITY);

    ContentValues values = new ContentValues();
    values.put(CalendarContract.Calendars.CALENDAR_COLOR, color);
    try {
        client.update(uri, values, null, null);
    } catch (RemoteException e) {
        Log.e(TAG, "Failed to update calendar color!", e);
    }
    client.release();
}

From source file:com.granita.icloudcalsync.resource.LocalCalendar.java

@SuppressLint("InlinedApi")
public static Uri create(Account account, ContentResolver resolver, ServerInfo.ResourceInfo info)
        throws LocalStorageException {
    final ContentProviderClient client = resolver.acquireContentProviderClient(CalendarContract.AUTHORITY);
    if (client == null)
        throw new LocalStorageException("No Calendar Provider found (Calendar app disabled?)");

    ContentValues values = new ContentValues();
    values.put(Calendars.ACCOUNT_NAME, account.name);
    values.put(Calendars.ACCOUNT_TYPE, account.type);
    values.put(Calendars.NAME, info.getURL());
    values.put(Calendars.CALENDAR_DISPLAY_NAME, info.getTitle());
    values.put(Calendars.CALENDAR_COLOR, DAVUtils.CalDAVtoARGBColor(info.getColor()));
    values.put(Calendars.OWNER_ACCOUNT, account.name);
    values.put(Calendars.SYNC_EVENTS, 1);
    values.put(Calendars.VISIBLE, 1);//  ww w . j  a  v a2  s .  c  o m
    values.put(Calendars.ALLOWED_REMINDERS, Reminders.METHOD_ALERT);

    if (info.isReadOnly())
        values.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_READ);
    else {
        values.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_OWNER);
        values.put(Calendars.CAN_ORGANIZER_RESPOND, 1);
        values.put(Calendars.CAN_MODIFY_TIME_ZONE, 1);
    }

    if (android.os.Build.VERSION.SDK_INT >= 15) {
        values.put(Calendars.ALLOWED_AVAILABILITY, Events.AVAILABILITY_BUSY + "," + Events.AVAILABILITY_FREE
                + "," + Events.AVAILABILITY_TENTATIVE);
        values.put(Calendars.ALLOWED_ATTENDEE_TYPES, Attendees.TYPE_NONE + "," + Attendees.TYPE_OPTIONAL + ","
                + Attendees.TYPE_REQUIRED + "," + Attendees.TYPE_RESOURCE);
    }

    if (info.getTimezone() != null)
        values.put(Calendars.CALENDAR_TIME_ZONE, info.getTimezone());

    Log.i(TAG, "Inserting calendar: " + values.toString());
    try {
        return client.insert(calendarsURI(account), values);
    } catch (RemoteException e) {
        throw new LocalStorageException(e);
    }
}

From source file:at.bitfire.davdroid.resource.LocalCalendar.java

@SuppressLint("InlinedApi")
public static Uri create(Account account, ContentResolver resolver, ServerInfo.ResourceInfo info)
        throws LocalStorageException {
    @Cleanup("release")
    final ContentProviderClient client = resolver.acquireContentProviderClient(CalendarContract.AUTHORITY);
    if (client == null)
        throw new LocalStorageException("No Calendar Provider found (Calendar app disabled?)");

    ContentValues values = new ContentValues();
    values.put(Calendars.ACCOUNT_NAME, account.name);
    values.put(Calendars.ACCOUNT_TYPE, account.type);
    values.put(Calendars.NAME, info.getURL());
    values.put(Calendars.CALENDAR_DISPLAY_NAME, info.getTitle());
    values.put(Calendars.CALENDAR_COLOR, info.getColor() != null ? info.getColor() : DAVUtils.calendarGreen);
    values.put(Calendars.OWNER_ACCOUNT, account.name);
    values.put(Calendars.SYNC_EVENTS, 1);
    values.put(Calendars.VISIBLE, 1);/*from  w  w  w  .j  a  v  a  2 s .co  m*/
    values.put(Calendars.ALLOWED_REMINDERS, Reminders.METHOD_ALERT);

    if (info.isReadOnly())
        values.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_READ);
    else {
        values.put(Calendars.CALENDAR_ACCESS_LEVEL, Calendars.CAL_ACCESS_OWNER);
        values.put(Calendars.CAN_ORGANIZER_RESPOND, 1);
        values.put(Calendars.CAN_MODIFY_TIME_ZONE, 1);
    }

    if (android.os.Build.VERSION.SDK_INT >= 15) {
        values.put(Calendars.ALLOWED_AVAILABILITY, Events.AVAILABILITY_BUSY + "," + Events.AVAILABILITY_FREE
                + "," + Events.AVAILABILITY_TENTATIVE);
        values.put(Calendars.ALLOWED_ATTENDEE_TYPES, Attendees.TYPE_NONE + "," + Attendees.TYPE_OPTIONAL + ","
                + Attendees.TYPE_REQUIRED + "," + Attendees.TYPE_RESOURCE);
    }

    if (info.getTimezone() != null)
        values.put(Calendars.CALENDAR_TIME_ZONE, DateUtils.findAndroidTimezoneID(info.getTimezone()));

    Log.i(TAG, "Inserting calendar: " + values.toString());
    try {
        return client.insert(calendarsURI(account), values);
    } catch (RemoteException e) {
        throw new LocalStorageException(e);
    }
}