Example usage for android.content ContentResolver update

List of usage examples for android.content ContentResolver update

Introduction

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

Prototype

public final int update(@RequiresPermission.Write @NonNull Uri uri, @Nullable ContentValues values,
        @Nullable String where, @Nullable String[] selectionArgs) 

Source Link

Document

Update row(s) in a content URI.

Usage

From source file:info.staticfree.android.units.UnitUsageDBHelper.java

/**
 * Increments the usage counter for the given unit.
 * @param unit name of the unit//from ww w  .j  a  va  2s .c om
 * @param db the unit usage database
 */
public static void logUnitUsed(String unit, ContentResolver cr) {
    final String[] selectionArgs = { unit };
    final Cursor c = cr.query(UsageEntry.CONTENT_URI, INCREMENT_QUERY_PROJECTION, UsageEntry._UNIT + "=?",
            selectionArgs, null);
    if (c.getCount() > 0) {
        c.moveToFirst();
        final int useCount = c.getInt(c.getColumnIndex(UsageEntry._USE_COUNT));
        final int id = c.getInt(c.getColumnIndex(UsageEntry._ID));
        final ContentValues cv = new ContentValues();
        cv.put(UsageEntry._USE_COUNT, useCount + 1);

        cr.update(ContentUris.withAppendedId(UsageEntry.CONTENT_URI, id), cv, null, null);
    } else {
        final ContentValues cv = new ContentValues();
        cv.put(UsageEntry._UNIT, unit);
        cv.put(UsageEntry._USE_COUNT, 1);
        cv.put(UsageEntry._FACTOR_FPRINT, getFingerprint(unit));
        cr.insert(UsageEntry.CONTENT_URI, cv);
    }
    c.close();
}

From source file:com.ksk.droidbatterybooster.provider.TimeSchedule.java

private static void enableTimeScheduleInternal(final Context context, final TimeSchedule schedule,
        boolean enabled) {
    if (schedule == null) {
        return;/*from  w ww .  j  a  va  2  s .c om*/
    }
    ContentResolver resolver = context.getContentResolver();

    ContentValues values = new ContentValues(2);
    values.put(ENABLED, enabled ? 1 : 0);

    // If we are enabling the schedule, calculate schedule time since the time
    // value in TimeSchedule may be old.
    if (enabled) {
        long time = 0;
        if (!schedule.daysOfWeek.isRepeatSet()) {
            time = calculateTimeSchedule(schedule);
        }
        values.put(SCHEDULE_TIME, time);
    }
    resolver.update(ContentUris.withAppendedId(CONTENT_URI, schedule.id), values, null, null);
}

From source file:com.smarthome.deskclock.Alarms.java

private static void enableAlarmInternal(final Context context, final Alarm alarm, boolean enabled) {
    if (alarm == null) {
        return;//w ww.ja v  a  2s  .  c  om
    }
    ContentResolver resolver = context.getContentResolver();

    ContentValues values = new ContentValues(2);
    values.put(Alarm.Columns.ENABLED, enabled ? 1 : 0);

    // If we are enabling the alarm, calculate alarm time since the time
    // value in Alarm may be old.
    if (enabled) {
        long time = 0;
        if (!alarm.daysOfWeek.isRepeatSet()) {
            time = calculateAlarm(alarm);
        }
        values.put(Alarm.Columns.ALARM_TIME, time);
    } else {
        // Clear the snooze if the id matches.
        disableSnoozeAlert(context, alarm.id);
    }

    resolver.update(ContentUris.withAppendedId(Alarm.Columns.CONTENT_URI, alarm.id), values, null, null);
}

From source file:com.android.calendar.alerts.AlertService.java

static void dismissOldAlerts(Context context) {
    ContentResolver cr = context.getContentResolver();
    final long currentTime = System.currentTimeMillis();
    ContentValues vals = new ContentValues();
    vals.put(CalendarAlerts.STATE, CalendarAlerts.STATE_DISMISSED);
    cr.update(CalendarAlerts.CONTENT_URI, vals, DISMISS_OLD_SELECTION,
            new String[] { Long.toString(currentTime), Integer.toString(CalendarAlerts.STATE_SCHEDULED) });
}

From source file:com.android.calendarDemo.alerts.DismissAlarmsService.java

@Override
public void onHandleIntent(Intent intent) {

    long eventId = intent.getLongExtra(AlertUtils.EVENT_ID_KEY, -1);
    long eventStart = intent.getLongExtra(AlertUtils.EVENT_START_KEY, -1);
    long eventEnd = intent.getLongExtra(AlertUtils.EVENT_END_KEY, -1);
    boolean showEvent = intent.getBooleanExtra(AlertUtils.SHOW_EVENT_KEY, false);
    long[] eventIds = intent.getLongArrayExtra(AlertUtils.EVENT_IDS_KEY);
    int notificationId = intent.getIntExtra(AlertUtils.NOTIFICATION_ID_KEY, -1);

    Uri uri = CalendarAlerts.CONTENT_URI;
    String selection;/*  w  w  w  . jav  a 2 s .com*/

    // Dismiss a specific fired alarm if id is present, otherwise, dismiss all alarms
    if (eventId != -1) {
        selection = CalendarAlerts.STATE + "=" + CalendarAlerts.STATE_FIRED + " AND " + CalendarAlerts.EVENT_ID
                + "=" + eventId;
    } else if (eventIds != null && eventIds.length > 0) {
        selection = buildMultipleEventsQuery(eventIds);
    } else {
        selection = CalendarAlerts.STATE + "=" + CalendarAlerts.STATE_FIRED;
    }

    ContentResolver resolver = getContentResolver();
    ContentValues values = new ContentValues();
    values.put(PROJECTION[COLUMN_INDEX_STATE], CalendarAlerts.STATE_DISMISSED);
    resolver.update(uri, values, selection, null);

    // Remove from notification bar.
    if (notificationId != -1) {
        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        nm.cancel(notificationId);
    }

    if (showEvent) {
        // Show event on Calendar app by building an intent and task stack to start
        // EventInfoActivity with AllInOneActivity as the parent activity rooted to home.
        Intent i = AlertUtils.buildEventViewIntent(this, eventId, eventStart, eventEnd);

        TaskStackBuilder.create(this).addParentStack(EventInfoActivity.class).addNextIntent(i)
                .startActivities();
    }

    // Stop this service
    stopSelf();
}

From source file:com.manning.androidhacks.hack023.dao.TodoDAO.java

public void clearAdd(ContentResolver contentResolver, long id, Todo serverTodo) {
    ContentValues cv = getTodoContentValues(serverTodo, StatusFlag.CLEAN);
    contentResolver.update(TodoContentProvider.CONTENT_URI, cv, TodoContentProvider.COLUMN_ID + "=" + id, null);

}

From source file:com.android.calendar.alerts.DismissAlarmsService.java

@Override
public void onHandleIntent(Intent intent) {

    long eventId = intent.getLongExtra(AlertUtils.EVENT_ID_KEY, -1);
    long eventStart = intent.getLongExtra(AlertUtils.EVENT_START_KEY, -1);
    long eventEnd = intent.getLongExtra(AlertUtils.EVENT_END_KEY, -1);
    boolean showEvent = intent.getBooleanExtra(AlertUtils.SHOW_EVENT_KEY, false);
    long[] eventIds = intent.getLongArrayExtra(AlertUtils.EVENT_IDS_KEY);
    long[] eventStarts = intent.getLongArrayExtra(AlertUtils.EVENT_STARTS_KEY);
    int notificationId = intent.getIntExtra(AlertUtils.NOTIFICATION_ID_KEY, -1);
    List<AlarmId> alarmIds = new LinkedList<AlarmId>();

    Uri uri = CalendarAlerts.CONTENT_URI;
    String selection;//  ww w  .  j a  v a2 s .c  o m

    // Dismiss a specific fired alarm if id is present, otherwise, dismiss all alarms
    if (eventId != -1) {
        alarmIds.add(new AlarmId(eventId, eventStart));
        selection = CalendarAlerts.STATE + "=" + CalendarAlerts.STATE_FIRED + " AND " + CalendarAlerts.EVENT_ID
                + "=" + eventId;
    } else if (eventIds != null && eventIds.length > 0 && eventStarts != null
            && eventIds.length == eventStarts.length) {
        selection = buildMultipleEventsQuery(eventIds);
        for (int i = 0; i < eventIds.length; i++) {
            alarmIds.add(new AlarmId(eventIds[i], eventStarts[i]));
        }
    } else {
        // NOTE: I don't believe that this ever happens.
        selection = CalendarAlerts.STATE + "=" + CalendarAlerts.STATE_FIRED;
    }

    GlobalDismissManager.dismissGlobally(getApplicationContext(), alarmIds);

    ContentResolver resolver = getContentResolver();
    ContentValues values = new ContentValues();
    values.put(PROJECTION[COLUMN_INDEX_STATE], CalendarAlerts.STATE_DISMISSED);
    resolver.update(uri, values, selection, null);

    // Remove from notification bar.
    if (notificationId != -1) {
        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        nm.cancel(notificationId);
    }

    if (showEvent) {
        // Show event on Calendar app by building an intent and task stack to start
        // EventInfoActivity with AllInOneActivity as the parent activity rooted to home.
        Intent i = AlertUtils.buildEventViewIntent(this, eventId, eventStart, eventEnd);

        TaskStackBuilder.create(this).addParentStack(EventInfoActivity.class).addNextIntent(i)
                .startActivities();
    }
}

From source file:com.android.my.calendar.alerts.DismissAlarmsService.java

@Override
public void onHandleIntent(Intent intent) {
    if (AlertService.DEBUG) {
        Log.d(TAG, "onReceive: a=" + intent.getAction() + " " + intent.toString());
    }/* ww  w  . j a  va  2  s .c  o  m*/

    long eventId = intent.getLongExtra(AlertUtils.EVENT_ID_KEY, -1);
    long eventStart = intent.getLongExtra(AlertUtils.EVENT_START_KEY, -1);
    long eventEnd = intent.getLongExtra(AlertUtils.EVENT_END_KEY, -1);
    long[] eventIds = intent.getLongArrayExtra(AlertUtils.EVENT_IDS_KEY);
    long[] eventStarts = intent.getLongArrayExtra(AlertUtils.EVENT_STARTS_KEY);
    int notificationId = intent.getIntExtra(AlertUtils.NOTIFICATION_ID_KEY, -1);
    List<AlarmId> alarmIds = new LinkedList<AlarmId>();

    Uri uri = CalendarAlerts.CONTENT_URI;
    String selection;

    // Dismiss a specific fired alarm if id is present, otherwise, dismiss all alarms
    if (eventId != -1) {
        alarmIds.add(new AlarmId(eventId, eventStart));
        selection = CalendarAlerts.STATE + "=" + CalendarAlerts.STATE_FIRED + " AND " + CalendarAlerts.EVENT_ID
                + "=" + eventId;
    } else if (eventIds != null && eventIds.length > 0 && eventStarts != null
            && eventIds.length == eventStarts.length) {
        selection = buildMultipleEventsQuery(eventIds);
        for (int i = 0; i < eventIds.length; i++) {
            alarmIds.add(new AlarmId(eventIds[i], eventStarts[i]));
        }
    } else {
        // NOTE: I don't believe that this ever happens.
        selection = CalendarAlerts.STATE + "=" + CalendarAlerts.STATE_FIRED;
    }

    GlobalDismissManager.dismissGlobally(getApplicationContext(), alarmIds);

    ContentResolver resolver = getContentResolver();
    ContentValues values = new ContentValues();
    values.put(PROJECTION[COLUMN_INDEX_STATE], CalendarAlerts.STATE_DISMISSED);
    resolver.update(uri, values, selection, null);

    // Remove from notification bar.
    if (notificationId != -1) {
        NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        nm.cancel(notificationId);
    }

    if (SHOW_ACTION.equals(intent.getAction())) {
        // Show event on Calendar app by building an intent and task stack to start
        // EventInfoActivity with AllInOneActivity as the parent activity rooted to home.
        Intent i = AlertUtils.buildEventViewIntent(this, eventId, eventStart, eventEnd);

        TaskStackBuilder.create(this).addParentStack(EventInfoActivity.class).addNextIntent(i)
                .startActivities();
    }
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

public static void updateNode(ContentResolver contentResolver, U1Node node, String data) {
    final String resourcePath = node.getResourcePath();
    final boolean isDirectory = node.getKind() == U1NodeKind.DIRECTORY;

    if (!isDirectory && ((U1File) node).getSize() == null) {
        // Ignore files with null size.
        return;/*from  w w  w .  j  a v a 2  s. c  om*/
    }

    ContentValues values = Nodes.valuesFromRepr(node, data);
    String selection = Nodes.NODE_KEY + "=?";
    String[] selectionArgs = new String[] { node.getKey() };
    if (node.getIsLive()) {
        int updated = contentResolver.update(Nodes.CONTENT_URI, values, selection, selectionArgs);
        if (updated == 0) {
            boolean isNonEmptyFile = !isDirectory && ((U1File) node).getSize() != null;
            if (isDirectory || isNonEmptyFile) {
                contentResolver.insert(Nodes.CONTENT_URI, values);
            }
        }
    } else {
        if (node.getKind() == U1NodeKind.FILE) {
            FileUtilities.removeSilently(FileUtilities.getFilePathFromResourcePath(resourcePath));
        }
        MetaUtilities.deleteByResourcePath(resourcePath);
    }
    MetaUtilities.notifyChange(Nodes.CONTENT_URI);
}

From source file:org.m2x.rssreader.activity.RssArticleActivity.java

@Override
public void onPageSelected(int position) {
    ContentResolver cr = MainApplication.getContext().getContentResolver();

    // Set the RSS entry as read.
    ContentValues value = new ContentValues();
    value.put(EntryColumns.IS_READ, true);
    cr.update(EntryColumns.CONTENT_URI(mEntryIds[position]), value, null, null);

    // Update the star icon.
    if (mMenuStar != null) { // mMenuStar could be null when OptionsMenu has not created yet.
        if (isEntryStared(mEntryIds[position])) {
            mMenuStar.setIcon(R.drawable.ic_action_important);
        } else {//from w w  w  .  ja  v  a2s . c om
            mMenuStar.setIcon(R.drawable.ic_action_not_important);
        }
    }

}