Example usage for android.database DatabaseUtils concatenateWhere

List of usage examples for android.database DatabaseUtils concatenateWhere

Introduction

In this page you can find the example usage for android.database DatabaseUtils concatenateWhere.

Prototype

public static String concatenateWhere(String a, String b) 

Source Link

Document

Concatenates two SQL WHERE clauses, handling empty or null values.

Usage

From source file:com.google.android.apps.muzei.provider.MuzeiProvider.java

private int updateSource(@NonNull final Uri uri, final ContentValues values, final String selection,
        final String[] selectionArgs) {
    Context context = getContext();
    if (context == null) {
        return 0;
    }/*from   ww w .j  ava 2s  .  co  m*/

    // Only Muzei can set the IS_SELECTED field
    if (values.containsKey(MuzeiContract.Sources.COLUMN_NAME_IS_SELECTED)) {
        if (!context.getPackageName().equals(getCallingPackage())) {
            Log.w(TAG, "Only Muzei can set the " + MuzeiContract.Sources.COLUMN_NAME_IS_SELECTED
                    + " column. Ignoring the value in " + values);
            values.remove(MuzeiContract.Sources.COLUMN_NAME_IS_SELECTED);
        }
    }

    final SQLiteDatabase db = databaseHelper.getWritableDatabase();
    String finalWhere = selection;
    String[] finalSelectionArgs = selectionArgs;
    if (MuzeiProvider.uriMatcher.match(uri) == SOURCE_ID) {
        // If the incoming URI matches a single source ID, does the update based on the incoming data, but
        // modifies the where clause to restrict it to the particular source ID.
        finalWhere = DatabaseUtils.concatenateWhere(finalWhere,
                BaseColumns._ID + " = " + uri.getLastPathSegment());
    }
    String callingPackageName = getCallingPackage();
    if (!context.getPackageName().equals(callingPackageName)) {
        // Only allow other apps to update their own source
        finalWhere = DatabaseUtils.concatenateWhere(finalWhere,
                MuzeiContract.Sources.COLUMN_NAME_COMPONENT_NAME + " LIKE ?");
        finalSelectionArgs = DatabaseUtils.appendSelectionArgs(finalSelectionArgs,
                new String[] { callingPackageName + "/%" });
    }
    int count = db.update(MuzeiContract.Sources.TABLE_NAME, values, finalWhere, finalSelectionArgs);
    if (count > 0) {
        notifyChange(uri);
    } else if (values.containsKey(MuzeiContract.Sources.COLUMN_NAME_COMPONENT_NAME)) {
        insertSource(MuzeiContract.Sources.CONTENT_URI, values);
        count = 1;
    }
    return count;
}