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.popcorntime.apps.remote.utils.Utils.java

public static String getRealPathFromUri(Context context, Uri contentUri) {
    Log.i("uri", contentUri.toString());
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        Cursor cursor = null;//from  w w w.j a v  a  2s.  com
        try {

            String[] proj = { MediaStore.Images.Media.DATA };
            cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    } else {
        Uri uri = contentUri;
        // DocumentProvider
        if (DocumentsContract.isDocumentUri(context, uri)) {
            // ExternalStorageProvider
            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 contentUri2 = ContentUris
                        .withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

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

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

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

                return getDataColumn(context, contentUri2, selection, selectionArgs);
            }
        }
        // MediaStore (and general)
        else if ("content".equalsIgnoreCase(uri.getScheme())) {
            return getDataColumn(context, uri, null, null);
        }
        // File
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }

        return null;
    }
}

From source file:com.csipsimple.ui.filters.AccountFiltersListFragment.java

@Override
public boolean onContextItemSelected(android.view.MenuItem item) {
    final long filterId = filterIdFromContextMenuInfo(item.getMenuInfo());
    if (filterId == -1) {
        // For some reason the requested item isn't available, do nothing
        return super.onContextItemSelected(item);
    }//from ww  w .j  a  v a 2s .c om

    switch (item.getItemId()) {
    case MENU_ITEM_DELETE: {
        getActivity().getContentResolver()
                .delete(ContentUris.withAppendedId(SipManager.FILTER_ID_URI_BASE, filterId), null, null);
        return true;
    }
    case MENU_ITEM_MODIFY: {
        showDetails(filterId);
        return true;
    }
    }
    return super.onContextItemSelected(item);

}

From source file:info.guardianproject.otr.app.im.app.WelcomeActivity.java

Intent getEditAccountIntent() {
    Intent intent = new Intent(Intent.ACTION_EDIT, ContentUris.withAppendedId(Imps.Account.CONTENT_URI,
            mProviderCursor.getLong(ACTIVE_ACCOUNT_ID_COLUMN)));
    intent.putExtra("isSignedIn", isSignedIn(mProviderCursor));
    intent.addCategory(getProviderCategory(mProviderCursor));
    return intent;
}

From source file:cn.suishen.email.LegacyConversions.java

/**
 * Save the body part of a single attachment, to a file in the attachments directory.
 *//* ww  w .  j ava 2  s .c om*/
public static void saveAttachmentBody(Context context, Part part, Attachment localAttachment, long accountId)
        throws MessagingException, IOException {
    if (part.getBody() != null) {
        long attachmentId = localAttachment.mId;

        InputStream in = part.getBody().getInputStream();

        File saveIn = AttachmentUtilities.getAttachmentDirectory(context, accountId);
        if (!saveIn.exists()) {
            saveIn.mkdirs();
        }
        File saveAs = AttachmentUtilities.getAttachmentFilename(context, accountId, attachmentId);
        saveAs.createNewFile();
        FileOutputStream out = new FileOutputStream(saveAs);
        long copySize = IOUtils.copy(in, out);
        in.close();
        out.close();

        // update the attachment with the extra information we now know
        String contentUriString = AttachmentUtilities.getAttachmentUri(accountId, attachmentId).toString();

        localAttachment.mSize = copySize;
        localAttachment.mContentUri = contentUriString;

        // update the attachment in the database as well
        ContentValues cv = new ContentValues();
        cv.put(AttachmentColumns.SIZE, copySize);
        cv.put(AttachmentColumns.CONTENT_URI, contentUriString);
        Uri uri = ContentUris.withAppendedId(Attachment.CONTENT_URI, attachmentId);
        context.getContentResolver().update(uri, cv, null, null);
    }
}

From source file:ro.weednet.contactssync.platform.ContactManager.java

public static void updateContact(Context context, ContentResolver resolver, RawContact rawContact,
        boolean updateAvatar, boolean inSync, long rawContactId, BatchOperation batchOperation) {

    ContactsSync app = ContactsSync.getInstance();
    boolean existingAvatar = false;

    final Cursor c = resolver.query(DataQuery.CONTENT_URI, DataQuery.PROJECTION, DataQuery.SELECTION,
            new String[] { String.valueOf(rawContactId) }, null);
    final ContactOperations contactOp = ContactOperations.updateExistingContact(context, rawContactId, inSync,
            batchOperation);/*from   w  w  w .j  a  v a 2 s  . c o m*/
    try {
        // Iterate over the existing rows of data, and update each one
        // with the information we received from the server.
        while (c.moveToNext()) {
            final long id = c.getLong(DataQuery.COLUMN_ID);
            final String mimeType = c.getString(DataQuery.COLUMN_MIMETYPE);
            final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, id);
            if (mimeType.equals(StructuredName.CONTENT_ITEM_TYPE)) {
                contactOp.updateName(uri, c.getString(DataQuery.COLUMN_GIVEN_NAME),
                        c.getString(DataQuery.COLUMN_FAMILY_NAME), rawContact.getFirstName(),
                        rawContact.getLastName());
                /*   } else if (mimeType.equals(Phone.CONTENT_ITEM_TYPE)) {
                      final int type = c.getInt(DataQuery.COLUMN_PHONE_TYPE);
                      if (type == Phone.TYPE_MOBILE) {
                         existingCellPhone = true;
                         contactOp.updatePhone(
                c.getString(DataQuery.COLUMN_PHONE_NUMBER),
                "5345345", uri);
                      } else if (type == Phone.TYPE_HOME) {
                         existingHomePhone = true;
                         contactOp.updatePhone(
                c.getString(DataQuery.COLUMN_PHONE_NUMBER),
                "5345345", uri);
                      } else if (type == Phone.TYPE_WORK) {
                         existingWorkPhone = true;
                         contactOp.updatePhone(
                c.getString(DataQuery.COLUMN_PHONE_NUMBER),
                "5345345", uri);
                      }
                   */
            } else if (mimeType.equals(Photo.CONTENT_ITEM_TYPE)) {
                existingAvatar = true;
                if (app.getSyncType() == ContactsSync.SyncType.LEGACY) {
                    contactOp.updateAvatar(c.getString(DataQuery.COLUMN_DATA1), rawContact.getAvatarUrl(), uri);
                }
            }
        } // while
    } finally {
        c.close();
    }

    // Add the avatar if we didn't update the existing avatar
    if (app.getSyncType() != ContactsSync.SyncType.HARD && !existingAvatar) {
        contactOp.addAvatar(rawContact.getAvatarUrl());
    }

    // If we don't have a status profile, then create one. This could
    // happen for contacts that were created on the client - we don't
    // create the status profile until after the first sync...
    final String serverId = rawContact.getUid();
    final long profileId = lookupProfile(resolver, serverId);
    if (profileId <= 0) {
        contactOp.addProfileAction(serverId);
    }
}

From source file:cn.code.notes.gtask.remote.GTaskManager.java

private void syncFolder() throws NetworkFailureException {
    Cursor c = null;//from   w  w w  . j  a v a 2s.  co  m
    String gid;
    Node node;
    int syncType;

    if (mCancelled) {
        return;
    }

    // for root folder
    try {
        c = mContentResolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, Notes.ID_ROOT_FOLDER),
                SqlNote.PROJECTION_NOTE, null, null, null);
        if (c != null) {
            c.moveToNext();
            gid = c.getString(SqlNote.GTASK_ID_COLUMN);
            node = mGTaskHashMap.get(gid);
            if (node != null) {
                mGTaskHashMap.remove(gid);
                mGidToNid.put(gid, (long) Notes.ID_ROOT_FOLDER);
                mNidToGid.put((long) Notes.ID_ROOT_FOLDER, gid);
                // for system folder, only update remote name if necessary
                if (!node.getName()
                        .equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT))
                    doContentSync(Node.SYNC_ACTION_UPDATE_REMOTE, node, c);
            } else {
                doContentSync(Node.SYNC_ACTION_ADD_REMOTE, node, c);
            }
        } else {
            Log.w(TAG, "failed to query root folder");
        }
    } finally {
        if (c != null) {
            c.close();
            c = null;
        }
    }

    // for call-note folder
    try {
        c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, "(_id=?)",
                new String[] { String.valueOf(Notes.ID_CALL_RECORD_FOLDER) }, null);
        if (c != null) {
            if (c.moveToNext()) {
                gid = c.getString(SqlNote.GTASK_ID_COLUMN);
                node = mGTaskHashMap.get(gid);
                if (node != null) {
                    mGTaskHashMap.remove(gid);
                    mGidToNid.put(gid, (long) Notes.ID_CALL_RECORD_FOLDER);
                    mNidToGid.put((long) Notes.ID_CALL_RECORD_FOLDER, gid);
                    // for system folder, only update remote name if
                    // necessary
                    if (!node.getName()
                            .equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_CALL_NOTE))
                        doContentSync(Node.SYNC_ACTION_UPDATE_REMOTE, node, c);
                } else {
                    doContentSync(Node.SYNC_ACTION_ADD_REMOTE, node, c);
                }
            }
        } else {
            Log.w(TAG, "failed to query call note folder");
        }
    } finally {
        if (c != null) {
            c.close();
            c = null;
        }
    }

    // for local existing folders
    try {
        c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, "(type=? AND parent_id<>?)",
                new String[] { String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER) },
                NoteColumns.TYPE + " DESC");
        if (c != null) {
            while (c.moveToNext()) {
                gid = c.getString(SqlNote.GTASK_ID_COLUMN);
                node = mGTaskHashMap.get(gid);
                if (node != null) {
                    mGTaskHashMap.remove(gid);
                    mGidToNid.put(gid, c.getLong(SqlNote.ID_COLUMN));
                    mNidToGid.put(c.getLong(SqlNote.ID_COLUMN), gid);
                    syncType = node.getSyncAction(c);
                } else {
                    if (c.getString(SqlNote.GTASK_ID_COLUMN).trim().length() == 0) {
                        // local add
                        syncType = Node.SYNC_ACTION_ADD_REMOTE;
                    } else {
                        // remote delete
                        syncType = Node.SYNC_ACTION_DEL_LOCAL;
                    }
                }
                doContentSync(syncType, node, c);
            }
        } else {
            Log.w(TAG, "failed to query existing folder");
        }
    } finally {
        if (c != null) {
            c.close();
            c = null;
        }
    }

    // for remote add folders
    Iterator<Map.Entry<String, TaskList>> iter = mGTaskListHashMap.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry<String, TaskList> entry = iter.next();
        gid = entry.getKey();
        node = entry.getValue();
        if (mGTaskHashMap.containsKey(gid)) {
            mGTaskHashMap.remove(gid);
            doContentSync(Node.SYNC_ACTION_ADD_LOCAL, node, null);
        }
    }

    if (!mCancelled)
        GTaskClient.getInstance().commitUpdate();
}

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

protected Uri taskSyncURI() {
    if (id == null)
        throw new IllegalStateException("Task doesn't have an ID yet");
    return taskList.syncAdapterURI(ContentUris.withAppendedId(taskList.provider.tasksUri(), id));
}

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

/** Enqueues deleting a resource from the local collection. Requires commit() to be effective! */
public void delete(Resource resource) {
    pendingOperations.add(/*from ww w  .j av a  2s . c  o m*/
            ContentProviderOperation.newDelete(ContentUris.withAppendedId(entriesURI(), resource.getLocalID()))
                    .withYieldAllowed(true).build());
}

From source file:com.android.email_ee.LegacyConversions.java

/**
 * Save the body part of a single attachment, to a file in the attachments directory.
 *//*  ww w  .  j av  a 2  s  . c  o m*/
public static void saveAttachmentBody(Context context, Part part, Attachment localAttachment, long accountId)
        throws MessagingException, IOException {
    if (part.getBody() != null) {
        long attachmentId = localAttachment.mId;

        InputStream in = part.getBody().getInputStream();

        File saveIn = AttachmentUtilities.getAttachmentDirectory(context, accountId);
        if (!saveIn.exists()) {
            saveIn.mkdirs();
        }
        File saveAs = AttachmentUtilities.getAttachmentFilename(context, accountId, attachmentId);
        saveAs.createNewFile();
        FileOutputStream out = new FileOutputStream(saveAs);
        long copySize = IOUtils.copy(in, out);
        in.close();
        out.close();

        // update the attachment with the extra information we now know
        String contentUriString = AttachmentUtilities.getAttachmentUri(accountId, attachmentId).toString();

        localAttachment.mSize = copySize;
        localAttachment.setContentUri(contentUriString);

        // update the attachment in the database as well
        ContentValues cv = new ContentValues();
        cv.put(AttachmentColumns.SIZE, copySize);
        cv.put(AttachmentColumns.CONTENT_URI, contentUriString);
        cv.put(AttachmentColumns.UI_STATE, UIProvider.AttachmentState.SAVED);
        Uri uri = ContentUris.withAppendedId(Attachment.CONTENT_URI, attachmentId);
        context.getContentResolver().update(uri, cv, null, null);
    }
}