Example usage for android.content ContentUris withAppendedId

List of usage examples for android.content ContentUris withAppendedId

Introduction

In this page you can find the example usage for android.content ContentUris withAppendedId.

Prototype

public static Uri withAppendedId(Uri contentUri, long id) 

Source Link

Document

Appends the given ID to the end of the path.

Usage

From source file:org.mythtv.android.db.dvr.ProgramHelperV27.java

public void processProgram(final Context context, final LocationProfile locationProfile, Uri uri, String table,
        ArrayList<ContentProviderOperation> ops, Program program, String tag) {
    //      Log.d( TAG, "processProgram : enter" );

    ContentValues programValues = convertProgramToContentValues(locationProfile, program, tag);

    if (table.equals(ProgramConstants.TABLE_NAME_RECORDED) || table.equals(ProgramConstants.TABLE_NAME_UPCOMING)
            || table.equals(ProgramConstants.TABLE_NAME_GUIDE)) {

        //Log.v( TAG, "processProgram : INSERT PROGRAM : " + program.getTitle() + ":" + program.getSubTitle() + ":" + program.getChannel().getChanId() + ":" + program.getStartTime() + ":" + program.getEndTime() + ":" + program.getHostName() + ":" + table );
        ops.add(ContentProviderOperation.newInsert(uri).withValues(programValues).withYieldAllowed(true)
                .build());/* www  .ja v a2 s . co  m*/

    } else {

        String programSelection = table + "." + ProgramConstants.FIELD_CHANNEL_ID + " = ? AND " + table + "."
                + ProgramConstants.FIELD_START_TIME + " = ?";
        String[] programSelectionArgs = new String[] { String.valueOf(program.getChannel().getChanId()),
                String.valueOf(program.getStartTime().getMillis()) };

        programSelection = appendLocationHostname(context, locationProfile, programSelection, table);

        Cursor programCursor = context.getContentResolver().query(uri, programProjection, programSelection,
                programSelectionArgs, null);
        if (programCursor.moveToFirst()) {
            Log.v(TAG,
                    "processProgram : UPDATE PROGRAM : " + program.getTitle() + ":" + program.getSubTitle()
                            + ":" + program.getChannel().getChanId() + ":" + program.getStartTime() + ":"
                            + program.getEndTime() + ":" + program.getHostName() + ":" + table);

            Long id = programCursor.getLong(programCursor.getColumnIndexOrThrow(ProgramConstants._ID));
            ops.add(ContentProviderOperation.newUpdate(ContentUris.withAppendedId(uri, id))
                    .withValues(programValues).withYieldAllowed(true).build());

        } else {
            //Log.v( TAG, "processProgram : INSERT PROGRAM : " + program.getTitle() + ":" + program.getSubTitle() + ":" + program.getChannel().getChanId() + ":" + program.getStartTime() + ":" + program.getEndTime() + ":" + program.getHostName() + ":" + table );

            ops.add(ContentProviderOperation.newInsert(uri).withValues(programValues).withYieldAllowed(true)
                    .build());

        }
        programCursor.close();

    }

    //      Log.d( TAG, "processProgram : exit" );
}

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

public void testLoadContactWithOldStyleUri() {
    // Use content Uris that only contain the ID but use the format used in Donut
    final Uri legacyUri = ContentUris.withAppendedId(Uri.parse("content://contacts"), RAW_CONTACT_ID);
    final Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, RAW_CONTACT_ID);
    final Uri baseUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, CONTACT_ID);
    final Uri lookupUri = ContentUris
            .withAppendedId(Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, LOOKUP_KEY), CONTACT_ID);
    final Uri entityUri = Uri.withAppendedPath(lookupUri, Contacts.Entity.CONTENT_DIRECTORY);

    ContactQueries queries = new ContactQueries();
    queries.fetchContactIdAndLookupFromRawContactUri(rawContactUri, CONTACT_ID, LOOKUP_KEY);
    queries.fetchAllData(entityUri, CONTACT_ID, RAW_CONTACT_ID, DATA_ID, LOOKUP_KEY);

    Contact contact = assertLoadContact(legacyUri);

    assertEquals(CONTACT_ID, contact.getId());
    assertEquals(RAW_CONTACT_ID, contact.getNameRawContactId());
    assertEquals(DisplayNameSources.STRUCTURED_NAME, contact.getDisplayNameSource());
    assertEquals(LOOKUP_KEY, contact.getLookupKey());
    assertEquals(lookupUri, contact.getLookupUri());
    assertEquals(1, contact.getRawContacts().size());
    assertEquals(1, contact.getStatuses().size());
    mContactsProvider.verify();/*from   w w  w  .jav  a2 s  .c o m*/
}

From source file:com.manning.androidhacks.hack023.provider.TodoContentProvider.java

@Override
public Uri insert(Uri uri, ContentValues initialValues) {
    ContentValues values;/*from  w  ww .  j  a v  a2  s. c o m*/
    if (initialValues != null) {
        values = new ContentValues(initialValues);
    } else {
        values = new ContentValues();
    }
    String table = null;
    String nullableCol = null;

    switch (sUriMatcher.match(uri)) {
    case TODO:
        table = TODO_TABLE_NAME;
        nullableCol = COLUMN_TITLE;
        break;
    default:
        new RuntimeException("Invalid URI for inserting: " + uri);
    }

    SQLiteDatabase db = dbHelper.getWritableDatabase();
    long rowId = db.insert(table, nullableCol, values);

    if (rowId > 0) {
        Uri noteUri = ContentUris.withAppendedId(uri, rowId);
        getContext().getContentResolver().notifyChange(noteUri, null);
        return noteUri;
    }

    throw new SQLException("Failed to insert row into " + uri);
}

From source file:org.mythtv.service.dvr.v26.ProgramHelperV26.java

public void processProgram(final Context context, final LocationProfile locationProfile, Uri uri, String table,
        ArrayList<ContentProviderOperation> ops, Program program, String tag) {
    //      Log.d( TAG, "processProgram : enter" );

    ContentValues programValues = convertProgramToContentValues(locationProfile, program, tag);

    if (table.equals(ProgramConstants.TABLE_NAME_RECORDED) || table.equals(ProgramConstants.TABLE_NAME_UPCOMING)
            || table.equals(ProgramConstants.TABLE_NAME_GUIDE)) {

        //Log.v( TAG, "processProgram : INSERT PROGRAM : " + program.getTitle() + ":" + program.getSubTitle() + ":" + program.getChannelInfo().getChannelId() + ":" + program.getStartTime() + ":" + program.getEndTime() + ":" + program.getHostname() + ":" + table );
        ops.add(ContentProviderOperation.newInsert(uri).withValues(programValues).withYieldAllowed(true)
                .build());/* w w w. ja  v  a2s . com*/

    } else {

        String programSelection = table + "." + ProgramConstants.FIELD_CHANNEL_ID + " = ? AND " + table + "."
                + ProgramConstants.FIELD_START_TIME + " = ?";
        String[] programSelectionArgs = new String[] { String.valueOf(program.getChannel().getChanId()),
                String.valueOf(program.getStartTime().getMillis()) };

        programSelection = appendLocationHostname(context, locationProfile, programSelection, table);

        Cursor programCursor = context.getContentResolver().query(uri, programProjection, programSelection,
                programSelectionArgs, null);
        if (programCursor.moveToFirst()) {
            Log.v(TAG,
                    "processProgram : UPDATE PROGRAM : " + program.getTitle() + ":" + program.getSubTitle()
                            + ":" + program.getChannel().getChanId() + ":" + program.getStartTime() + ":"
                            + program.getEndTime() + ":" + program.getHostName() + ":" + table);

            Long id = programCursor.getLong(programCursor.getColumnIndexOrThrow(ProgramConstants._ID));
            ops.add(ContentProviderOperation.newUpdate(ContentUris.withAppendedId(uri, id))
                    .withValues(programValues).withYieldAllowed(true).build());

        } else {
            //Log.v( TAG, "processProgram : INSERT PROGRAM : " + program.getTitle() + ":" + program.getSubTitle() + ":" + program.getChannel().getChanId() + ":" + program.getStartTime() + ":" + program.getEndTime() + ":" + program.getHostName() + ":" + table );

            ops.add(ContentProviderOperation.newInsert(uri).withValues(programValues).withYieldAllowed(true)
                    .build());

        }
        programCursor.close();

    }

    //      Log.d( TAG, "processProgram : exit" );
}

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

@Override
public String getCTag() throws LocalStorageException {
    try {/* www . j  a v  a2 s.co  m*/
        @Cleanup
        Cursor c = providerClient.query(ContentUris.withAppendedId(taskListsURI(account), id),
                new String[] { COLLECTION_COLUMN_CTAG }, null, null, null);
        if (c != null && c.moveToFirst())
            return c.getString(0);
        else
            throw new LocalStorageException("Couldn't query task list CTag");
    } catch (RemoteException e) {
        throw new LocalStorageException(e);
    }
}

From source file:com.android.calendar.selectcalendars.SelectVisibleCalendarsFragment.java

public void toggleVisibility(int position) {
    mUpdateToken = mService.getNextToken();
    Uri uri = ContentUris.withAppendedId(Calendars.CONTENT_URI, mAdapter.getItemId(position));
    ContentValues values = new ContentValues();
    // Toggle the current setting
    int visibility = mAdapter.getVisible(position) ^ 1;
    values.put(Calendars.VISIBLE, visibility);
    mService.startUpdate(mUpdateToken, null, uri, values, null, null, 0);
    mAdapter.setVisible(position, visibility);
}

From source file:net.dahanne.spring.android.ch3.restful.example.recipeapp.RecipesList.java

@Override
public boolean onContextItemSelected(MenuItem item) {
    // The data from the menu item.
    AdapterView.AdapterContextMenuInfo info;

    try {//from   w  w  w. jav a2 s  .c o m
        // Casts the data object in the item into the type for AdapterView objects.
        info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    } catch (ClassCastException e) {

        // If the object can't be cast, logs an error
        Log.e(TAG, "bad menuInfo", e);

        // Triggers default processing of the menu item.
        return false;
    }
    // Appends the selected recipe's ID to the URI sent with the incoming Intent.
    Uri recipeUri = ContentUris.withAppendedId(getIntent().getData(), info.id);

    /*
     * Gets the menu item's ID and compares it to known actions.
     */
    switch (item.getItemId()) {
    case R.id.context_open:
        // Launch activity to view/edit the currently selected item
        startActivity(new Intent(Intent.ACTION_EDIT, recipeUri));
        return true;

    case R.id.context_delete:
        getContentResolver().delete(recipeUri, // The URI of the provider
                null, // No where clause is needed, since only a single recipe ID is being
                // passed in.
                null // No where clause is used, so no where arguments are needed.
        );

        // Returns to the caller and skips further processing.
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}

From source file:com.example.android.inventoryapp.CatalogActivity.java

public void onSellClick(long id, int quantity, String mName) {
    Uri currentProductUri = ContentUris.withAppendedId(InventoryContract.ItemEntry.CONTENT_URI, id);

    if (quantity > 0) {
        quantity--;//from   w  ww.j a  v  a2s  .com
        ContentValues values = new ContentValues();
        values.put(InventoryContract.ItemEntry.COLUMN_ITEM_QUANTITY, quantity);
        getContentResolver().update(currentProductUri, values, null, null);
        Toast.makeText(this, getString(R.string.sold) + " " + mName, Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, getString(R.string.empty) + " " + mName, Toast.LENGTH_SHORT).show();
    }
}

From source file:com.android.unit_tests.CheckinProviderTest.java

@MediumTest
public void testCrashReport() throws Exception {
    long start = System.currentTimeMillis();
    ContentResolver r = getContext().getContentResolver();

    // Log a test (fake) crash report.
    Checkin.reportCrash(r, new CrashData("Test", "Test Activity", new BuildData("Test Build", "123", start),
            new ThrowableData(new RuntimeException("Test Exception"))));

    // Crashes aren't indexed; go through them all to find the one we added.
    Cursor c = r.query(Checkin.Crashes.CONTENT_URI, null, null, null, null);

    Uri uri = null;//from  www.j  ava 2  s .co m
    while (c.moveToNext()) {
        String coded = c.getString(c.getColumnIndex(Checkin.Crashes.DATA));
        byte[] bytes = Base64.decodeBase64(coded.getBytes());
        CrashData crash = new CrashData(new DataInputStream(new ByteArrayInputStream(bytes)));

        // Should be exactly one recently added "Test" crash.
        if (crash.getId().equals("Test") && crash.getTime() > start) {
            assertEquals("Test Activity", crash.getActivity());
            assertEquals("Test Build", crash.getBuildData().getFingerprint());
            assertEquals("Test Exception", crash.getThrowableData().getMessage());

            assertNull(uri);
            uri = ContentUris.withAppendedId(Checkin.Crashes.CONTENT_URI,
                    c.getInt(c.getColumnIndex(Checkin.Crashes._ID)));
        }
    }
    assertNotNull(uri);
    c.close();

    // Update the "logs" column.
    ContentValues values = new ContentValues();
    values.put(Checkin.Crashes.LOGS, "Test Logs");
    assertEquals(1, r.update(uri, values, null, null));

    c = r.query(uri, null, null, null, null);
    assertTrue(c.moveToNext());
    String logs = c.getString(c.getColumnIndex(Checkin.Crashes.LOGS));
    assertEquals("Test Logs", logs);
    c.deleteRow();
    c.close();

    c.requery();
    assertFalse(c.moveToNext());
    c.close();
}

From source file:com.markupartist.sthlmtraveling.FavoritesFragment.java

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {
    case CONTEXT_MENU_DELETE:
        getActivity().getContentResolver().delete(ContentUris.withAppendedId(Journeys.CONTENT_URI, menuInfo.id),
                null, null);/*from   ww w .  j a va 2  s  .c om*/
        return true;
    case CONTEXT_MENU_REVERSE_SEARCH:
        doSearch(menuInfo.id, true);
        return true;
    case CONTEXT_MENU_INCREASE_PRIO:
        updateListPosition(menuInfo.id, true);
        return true;
    case CONTEXT_MENU_DECREASE_PRIO:
        updateListPosition(menuInfo.id, false);
        return true;
    }
    return false;
}