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:com.andrew.apollo.menu.DeletePlaylistDialog.java

@NonNull
@Override/* ww  w. j ava  2s.  co m*/
public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new AlertDialog.Builder(getActivity())
            .setTitle(getActivity().getString(R.string.delete_dialog_title, title))
            .setPositiveButton(R.string.context_menu_delete, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(final DialogInterface dialog, final int which) {
                    final Uri mUri = ContentUris.withAppendedId(Uris.EXTERNAL_MEDIASTORE_PLAYLISTS, playlist);
                    getActivity().getContentResolver().delete(mUri, null, null);
                    getActivity().getContentResolver().notifyChange(Uris.EXTERNAL_MEDIASTORE_PLAYLISTS, null);
                }
            }).setNegativeButton(R.string.cancel, null).setMessage(R.string.cannot_be_undone).create();
}

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

public static Uri getContentUriForId(long modeId) {
    return ContentUris.withAppendedId(CONTENT_URI, modeId);
}

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

@MediumTest
public void testEventReport() {
    long start = System.currentTimeMillis();
    ContentResolver r = getContext().getContentResolver();
    Checkin.logEvent(r, Checkin.Events.Tag.TEST, "Test Value");

    Cursor c = r.query(Checkin.Events.CONTENT_URI, null, Checkin.Events.TAG + "=?",
            new String[] { Checkin.Events.Tag.TEST.toString() }, null);

    long id = -1;
    while (c.moveToNext()) {
        String tag = c.getString(c.getColumnIndex(Checkin.Events.TAG));
        String value = c.getString(c.getColumnIndex(Checkin.Events.VALUE));
        long date = c.getLong(c.getColumnIndex(Checkin.Events.DATE));
        assertEquals(Checkin.Events.Tag.TEST.toString(), tag);
        if ("Test Value".equals(value) && date >= start) {
            assertTrue(id < 0);/*from   w  w w  .j a v a2 s  .c  o m*/
            id = c.getInt(c.getColumnIndex(Checkin.Events._ID));
        }
    }
    assertTrue(id > 0);

    int rows = r.delete(ContentUris.withAppendedId(Checkin.Events.CONTENT_URI, id), null, null);
    assertEquals(1, rows);
    c.requery();
    while (c.moveToNext()) {
        long date = c.getLong(c.getColumnIndex(Checkin.Events.DATE));
        assertTrue(date < start); // Have deleted the only newer TEST.
    }

    c.close();
}

From source file:Main.java

/**
 * Get a file path from a Uri. This will get the the path for Storage Access
 * Framework Documents, as well as the _data field for the MediaStore and
 * other file-based ContentProviders.<br>
 * <br>//from w  ww  .ja  v a2s  .  c om
 * Callers should check whether the path is local before assuming it
 * represents a local file.
 *
 * @param context The context.
 * @param uri     The Uri to query.
 * @author paulburke
 * @see #isLocal(String)
 * @see #getFile(Context, Uri)
 */
@SuppressLint("NewApi")
public static String getPath(final Context context, final Uri uri) {

    if (DEBUG)
        Log.d(TAG + " File -",
                "Authority: " + uri.getAuthority() + ", Fragment: " + uri.getFragment() + ", Port: "
                        + uri.getPort() + ", Query: " + uri.getQuery() + ", Scheme: " + uri.getScheme()
                        + ", Host: " + uri.getHost() + ", Segments: " + uri.getPathSegments().toString());

    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

    // DocumentProvider
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }

            // TODO handle non-primary volumes
        }
        // DownloadsProvider
        else if (isDownloadsDocument(uri)) {

            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
                    Long.valueOf(id));

            return getDataColumn(context, contentUri, null, null);
        }
        // MediaProvider
        else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[] { split[1] };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    }
    // MediaStore (and general)
    else if ("content".equalsIgnoreCase(uri.getScheme())) {

        // Return the remote address
        if (isGooglePhotosUri(uri))
            return uri.getLastPathSegment();

        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:com.commonsware.android.arXiv.FavouritesListFragment.java

@Override
public boolean onContextItemSelected(MenuItem item) {
    if (!getUserVisibleHint())
        return false;
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    if (info == null)
        return false;
    final Uri feedUri = ContentUris.withAppendedId(Feeds.CONTENT_URI, info.id);
    new Thread() {
        @Override//from www .  j  ava  2s .c  om
        public void run() {
            getActivity().getContentResolver().delete(feedUri, null, null);
        }
    }.start();
    return true;
}

From source file:com.googlecode.android_scripting.facade.ContactsFacade.java

private Uri buildUri(Integer id) {
    Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, id);
    return uri;
}

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_catalog);

    // Setup FAB to open EditorActivity
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override//from  w ww  . j  a v  a  2  s. com
        public void onClick(View view) {
            Intent intent = new Intent(CatalogActivity.this, EditorActivity.class);
            startActivity(intent);
        }
    });

    ListView displayView = (ListView) findViewById(R.id.list);
    /*View emptyView =findViewById(R.id.Empty_view);
     displayView.setEmptyView(emptyView);*/

    mCursorAdapter = new PetsCursorAdapter(this, null);
    displayView.setAdapter(mCursorAdapter);

    displayView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {

            Intent intent = new Intent(CatalogActivity.this, EditorActivity.class);

            Uri currentPetUri = ContentUris.withAppendedId(PetsEntry.CONTENT_URI, id);

            intent.setData(currentPetUri);

            startActivity(intent);

        }
    });

    //kick off the Loader
    getLoaderManager().initLoader(PET_LOADER, null, this);

}

From source file:com.innoc.secureline.contacts.ContactAccessor.java

public Bitmap getPhoto(Context context, long rowId) {
    Uri photoLookupUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, rowId);
    return getPhoto(context, photoLookupUri);
}

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

public static AndroidTaskList findByID(Account account, TaskProvider provider, AndroidTaskListFactory factory,
        long id) throws FileNotFoundException, CalendarStorageException {
    try {/*w w w.ja v  a  2 s . co m*/
        @Cleanup
        Cursor cursor = provider.client.query(
                syncAdapterURI(ContentUris.withAppendedId(provider.taskListsUri(), id), account), null, null,
                null, null);
        if (cursor != null && cursor.moveToNext()) {
            AndroidTaskList taskList = factory.newInstance(account, provider, id);

            ContentValues values = new ContentValues(cursor.getColumnCount());
            DatabaseUtils.cursorRowToContentValues(cursor, values);
            taskList.populate(values);
            return taskList;
        } else
            throw new FileNotFoundException();
    } catch (RemoteException e) {
        throw new CalendarStorageException("Couldn't query task list by ID", e);
    }
}

From source file:com.ultramegatech.ey.UpdateService.java

@Override
protected void onHandleIntent(Intent intent) {
    if (!HttpHelper.isConnected(this)) {
        return;//from  w w  w.  ja v  a2 s .  c  o m
    }

    final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

    final long lastCheck = preferences.getLong(KEY_LAST_CHECK, 0);
    final long now = System.currentTimeMillis();
    if (now - lastCheck < CHECK_INTERVAL) {
        return;
    }

    Log.v(TAG, "Checking for updates");

    final int version = preferences.getInt(KEY_VERSION, 0);
    final int newVersion = HttpHelper.getVersion();
    if (newVersion > version) {
        Log.v(TAG, "Downloading updates...");

        final ContentValues[] valuesArray = fetchElementData(newVersion);
        if (valuesArray == null) {
            return;
        }

        final ContentResolver cr = getContentResolver();
        Uri uri;
        int changed;
        for (int i = 0; i < valuesArray.length; i++) {
            uri = ContentUris.withAppendedId(Elements.CONTENT_URI_NUMBER,
                    valuesArray[i].getAsLong(Elements.NUMBER));
            changed = cr.update(uri, valuesArray[i], null, null);

            if (changed == 0) {
                cr.insert(Elements.CONTENT_URI, valuesArray[i]);
            }
        }

        preferences.edit().putInt(KEY_VERSION, newVersion).commit();

        Log.v(TAG, "Update completed successfully");
    }

    preferences.edit().putLong(KEY_LAST_CHECK, System.currentTimeMillis()).commit();
}