Example usage for android.content ContentResolver delete

List of usage examples for android.content ContentResolver delete

Introduction

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

Prototype

public final int delete(@RequiresPermission.Write @NonNull Uri url, @Nullable String where,
        @Nullable String[] selectionArgs) 

Source Link

Document

Deletes row(s) specified by a content URI.

Usage

From source file:org.getlantern.firetweet.activity.FiltersActivity.java

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    switch (requestCode) {
    case REQUEST_SELECT_USER: {
        final Fragment filter = mAdapter.getItem(mViewPager.getCurrentItem());
        if (resultCode != RESULT_OK || !(filter instanceof FilteredUsersFragment) || !data.hasExtra(EXTRA_USER))
            return;
        final ParcelableUser user = data.getParcelableExtra(EXTRA_USER);
        final ContentValues values = ContentValuesCreator.createFilteredUser(user);
        final ContentResolver resolver = getContentResolver();
        resolver.delete(Filters.Users.CONTENT_URI, Expression.equals(Filters.Users.USER_ID, user.id).getSQL(),
                null);/*from   w  ww  .j  av  a 2 s.c o m*/
        resolver.insert(Filters.Users.CONTENT_URI, values);
        break;
    }
    }
}

From source file:org.sufficientlysecure.keychain.util.FileHelper.java

/**
 * Deletes data at a URI securely by overwriting it with random data
 * before deleting it. This method is fail-fast - if we can't securely
 * delete the file, we don't delete it at all.
 *///from w w  w  .ja  v a 2  s.com
public static int deleteFileSecurely(Context context, Uri uri) throws IOException {

    ContentResolver resolver = context.getContentResolver();
    long lengthLeft = FileHelper.getFileSize(context, uri);

    if (lengthLeft == -1) {
        throw new IOException("Error opening file!");
    }

    SecureRandom random = new SecureRandom();
    byte[] randomData = new byte[1024];

    OutputStream out = resolver.openOutputStream(uri, "w");
    if (out == null) {
        throw new IOException("Error opening file!");
    }
    out = new BufferedOutputStream(out);
    while (lengthLeft > 0) {
        random.nextBytes(randomData);
        out.write(randomData, 0, lengthLeft > randomData.length ? randomData.length : (int) lengthLeft);
        lengthLeft -= randomData.length;
    }
    out.close();

    if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
        return new File(uri.getPath()).delete() ? 1 : 0;
    } else {
        return resolver.delete(uri, null, null);
    }

}

From source file:de.vanita5.twittnuker.activity.FiltersActivity.java

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    switch (requestCode) {
    case REQUEST_SELECT_USER: {
        final Fragment filter = mAdapter.getItem(mViewPager.getCurrentItem());
        if (resultCode != RESULT_OK || !(filter instanceof FilteredUsersFragment) || !data.hasExtra(EXTRA_USER))
            return;
        final ParcelableUser user = data.getParcelableExtra(EXTRA_USER);
        final ContentValues values = makeFilterdUserContentValues(user);
        final ContentResolver resolver = getContentResolver();
        resolver.delete(Filters.Users.CONTENT_URI, Where.equals(Filters.Users.USER_ID, user.id).getSQL(), null);
        resolver.insert(Filters.Users.CONTENT_URI, values);
        break;//from   w  w w .j a va2s.  c o m
    }
    }
}

From source file:com.antew.redditinpictures.library.reddit.AboutResponse.java

@Override
public void processHttpResponse(Context context) {
    Ln.v("About Subreddit complete! = %s", result.getJson());
    About aboutSubreddit = JsonDeserializer.deserialize(result.getJson(), About.class);

    if (aboutSubreddit == null) {
        Ln.e("Something went wrong on About Subreddit status code: %d json: %s", result.getHttpStatusCode(),
                result.getJson());//from   w w  w .j a  v a2  s .c  om
        return;
    }

    ContentResolver resolver = context.getContentResolver();
    //TODO: Once API-11 is sunset, replace with an update instead of delete/insert.
    // Updates with parameters aren't supported prior to API-11 (Honeycomb). So instead we are just deleting the record if it exists and recreating it.
    resolver.delete(RedditContract.Subreddits.CONTENT_URI, "subredditId = ?",
            new String[] { aboutSubreddit.getData().getId() });
    Ln.v("Deleted row");
    resolver.insert(RedditContract.Subreddits.CONTENT_URI, aboutSubreddit.getContentValues());
    Ln.v("Inserted row");

    LocalBroadcastManager.getInstance(context)
            .sendBroadcast(new Intent(Constants.Broadcast.BROADCAST_ABOUT_SUBREDDIT));
}

From source file:de.aw.awlib.gv.CalendarReminder.java

/**
 * Loescht einen Eintrag aus dem Kalender
 *
 * @param calendarItemID//from   www  . j a  v a2s. c o  m
 *         ID des Eintrages, der geloescht werden soll
 * @return true, wenn erfolgreich
 */
public boolean deleteEvent(long calendarItemID) {
    if (ContextCompat.checkSelfPermission(mContext,
            Manifest.permission.WRITE_CALENDAR) == PackageManager.PERMISSION_GRANTED) {
        ContentResolver cr = mContext.getContentResolver();
        Uri deleteUri = ContentUris.withAppendedId(Events.CONTENT_URI, calendarItemID);
        return (cr.delete(deleteUri, null, null) != -1);
    }
    return false;
}

From source file:org.zywx.wbpalmstar.plugin.uexcontacts.PFConcactMan.java

public static boolean deletesWithContactId(Context context, String contactId) {
    int result = -1;
    try {/*from   ww w.  j a  va2s .  c o m*/
        ContentResolver contentResolver = context.getContentResolver();
        int sdkVersion = Build.VERSION.SDK_INT;
        if (sdkVersion <= 8) {// ?2.2
            result = contentResolver.delete(android.provider.Contacts.People.CONTENT_URI,
                    android.provider.Contacts.People._ID + "=?", new String[] { contactId });
        } else if (sdkVersion < 14) {// ?4.0
            result = contentResolver.delete(
                    android.content.ContentUris.withAppendedId(
                            android.provider.ContactsContract.Contacts.CONTENT_URI, Long.parseLong(contactId)),
                    null, null);
        } else {
            result = contentResolver.delete(
                    android.content.ContentUris.withAppendedId(
                            android.provider.ContactsContract.Contacts.CONTENT_URI, Long.parseLong(contactId)),
                    null, null);
        }
        if (result > 0) {
            ToastShow(context, finder.getString(context, "plugin_contact_delete_succeed"));
        } else {
            ToastShow(context, finder.getString(context, "plugin_contact_delete_fail"));
        }
    } catch (Exception e) {
        ToastShow(context, finder.getString(context, "plugin_contact_delete_fail"));
    }
    return result > 0 ? true : false;
}

From source file:org.xwalk.runtime.extension.api.messaging.MessagingSmsManager.java

public void onSmsClear(int instanceID, JSONObject jsonMsg) {
    String promise_id = null, cmd = null;
    JSONObject eventBody = null;/*from   w w  w .j  av a  2 s  .  co m*/
    String serviceID = null;
    try {
        promise_id = jsonMsg.getString("_promise_id");
        cmd = jsonMsg.getString("cmd");
        eventBody = jsonMsg.getJSONObject("data");
        serviceID = eventBody.getString("serviceID");
    } catch (JSONException e) {
        e.printStackTrace();
        return;
    }

    ContentResolver cr = mMainActivity.getContentResolver();
    cr.delete(Uri.parse("content://sms"), null, null);

    JSONObject jsonMsgRet = null;
    try {
        jsonMsgRet = new JSONObject();
        jsonMsgRet.put("_promise_id", promise_id);
        jsonMsgRet.put("cmd", cmd + "_ret");
        JSONObject jsData = new JSONObject();
        jsonMsgRet.put("data", jsData);
        jsData.put("error", false);
        JSONObject jsBody = new JSONObject();
        jsData.put("body", jsBody);
        jsBody.put("value", serviceID);
    } catch (JSONException e) {
        e.printStackTrace();
        return;
    }

    mMessagingHandler.postMessage(instanceID, jsonMsgRet.toString());
}

From source file:com.filemanager.free.filesystem.FileUtil.java

public static boolean rmdir(final File file, Context context) {
    if (file == null)
        return false;
    if (!file.exists()) {
        return true;
    }/* www .  j av  a  2 s .co  m*/
    if (!file.isDirectory()) {
        return false;
    }
    String[] fileList = file.list();
    if (fileList != null && fileList.length > 0) {
        //  empty the folder.
        rmdir1(file, context);
    }
    String[] fileList1 = file.list();
    if (fileList1 != null && fileList1.length > 0) {
        // Delete only empty folder.
        return false;
    }
    // Try the normal way
    if (file.delete()) {
        return true;
    }

    // Try with Storage Access Framework.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        DocumentFile document = getDocumentFile(file, true, context);
        return document.delete();
    }

    // Try the Kitkat workaround.
    if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
        ContentResolver resolver = context.getContentResolver();
        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
        resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

        // Delete the created entry, such that content provider will delete the file.
        resolver.delete(MediaStore.Files.getContentUri("external"), MediaStore.MediaColumns.DATA + "=?",
                new String[] { file.getAbsolutePath() });
    }

    return !file.exists();
}

From source file:net.naonedbus.manager.impl.FavoriManager.java

public void removeFavori(final ContentResolver contentResolver, final int id) {
    contentResolver.delete(FavoriProvider.CONTENT_URI, FavoriTable._ID + "=?",
            new String[] { String.valueOf(id) });

    if (mIsImporting == false)
        notifyOnRemove(id);/*from w  w w .  j  a  v  a  2 s .c om*/
}

From source file:com.amaze.carbonfilemanager.services.DeleteTask.java

private void delete(final Context context, final String file) {
    final String where = MediaStore.MediaColumns.DATA + "=?";
    final String[] selectionArgs = new String[] { file };
    final ContentResolver contentResolver = context.getContentResolver();
    final Uri filesUri = MediaStore.Files.getContentUri("external");
    // Delete the entry from the media database. This will actually delete media files.
    contentResolver.delete(filesUri, where, selectionArgs);

}