Search code examples
androidandroid-contentprovider

Android ContentProvider and ContentProviderOperation.newUpdate()


I'm trying to use overrided applyBatch() method on a custom contentprovider. I know it gets passed an ArrayList of ContentProviderOperations. In my case they're all update ContentProviderOperations. In my provider I want to update the data in a simple array (my stand-in DB for now) that was at first inserted into the ContentProviderOperation.Builder.

The important code bits so far:

Where I build my ContentProviderOperations with my data from the channels array:

public void setNumValues(Uri mindex, int[] channels)
        {//setting type to 0 first for everything
            //first is RPM, then Speed
            ContentValues[] values = new ContentValues[channels.length];        
            ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();

        for (int i = 0; i< channels.length-1; i++){
            values[i].put(NumberProvider.numColumnNames[NumberProvider.VALUE], channels[i]);
            values[i].put(NumberProvider.numColumnNames[NumberProvider.TYPE], 0);

        operations.add(ContentProviderOperation.newUpdate(NumberProvider.NUM_PROVIDER_MODEL_URI).withValues(values[i]).build());
        }
        //values.put(NumberProvider.numColumnNames[NumberProvider.VALUE], value);   
        //mContext.getContentResolver().update(mindex, values, null,null);
        try {
            mContext.getContentResolver().applyBatch(NumberProvider.NUM_AUTHORITY,operations);
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (OperationApplicationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Then my overridden methods in my provider:

@Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        int tableindex = (int) (ContentUris.parseId(uri));

    if ((tableindex > 0) && (tableindex <= numProviderDataList.size())) {
        numProviderDataType Data = numProviderDataList.get(tableindex - 1);

        Data.value = values.getAsInteger(numColumnNames[VALUE]);


        // Log.e("Provider1", Float.toString(Data.mValue1));
        // Log.e("Provider2", Float.toString(Data.mValue2));
    }

    getContext().getContentResolver().notifyChange(uri, null);

    return 1;
}

@Override
public ContentProviderResult[] applyBatch(
        ArrayList<ContentProviderOperation> ops)
        throws OperationApplicationException {
    // TODO Auto-generated method stub
    ContentProviderResult[] result;
    try{
    result = super.applyBatch(ops); //Does this call my provider's update() method?

    } catch(OperationApplicationException e){
        throw e;
    }
    //let observer know there's been a change at the URI
    getContext().getContentResolver().notifyChange(NUM_PROVIDER_MODEL_URI, null);
    return result;

How do I then get the data out of the ops ArrayList so I can put it in my storage structure? In this case it's a simple array. Does super.applyBatch() in the overrided applyBatch() recognize that they're all update ContentProviderOperations and then calls the overrided update method of my provider for ContentProviderOperation in my ops ArrayList? I couldn't find any examples using a custom provider.


Solution

  • The default implementation of applyBatch will iterate of the ContentProviderOperation array and call apply on each one, which in turn will call your provider's insert, update or delete methods.

    So yes it will by default call your overridden methods.

    There doesn't seem to be able to extract data from a ContentProviderOperation, only to call apply to it. Overriding the applyBatch should only be used to filter or order operations. Other than that you should just rely on your insert, update or delete implementations.