Skip to content

Commit

Permalink
Added methods that take a Context in addition to the ones that take a…
Browse files Browse the repository at this point in the history
… ContentResolver.
  • Loading branch information
BoD committed May 9, 2015
1 parent 57ac828 commit 9724c1e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ ${header}
</#if>
package ${config.providerJavaPackage}.base;

import android.content.Context;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
Expand All @@ -24,10 +25,19 @@ public abstract class AbstractContentValues {

/**
* Inserts a row into a table using the values stored by this object.
*
*
* @param contentResolver The content resolver to use.
*/
public Uri insert(ContentResolver contentResolver) {
return contentResolver.insert(uri(), values());
}

/**
* Inserts a row into a table using the values stored by this object.
*
* @param context The context to use.
*/
public Uri insert(Context context) {
return context.getContentResolver().insert(uri(), values());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.content.ContentResolver;
import android.net.Uri;

Expand Down Expand Up @@ -298,6 +299,16 @@ public abstract class AbstractSelection<T extends AbstractSelection<?>> {
return contentResolver.delete(uri(), sel(), args());
}

/**
* Deletes row(s) specified by this selection.
*
* @param Context the context to use.
* @return The number of rows deleted.
*/
public int delete(Context context) {
return context.getContentResolver().delete(uri(), sel(), args());
}

@SuppressWarnings("unchecked")
public T notify(boolean notify) {
mNotify = notify;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package ${config.providerJavaPackage}.${entity.packageName};

import java.util.Date;

import android.content.Context;
import android.content.ContentResolver;
import android.net.Uri;
<#if config.useAnnotations>
Expand Down Expand Up @@ -32,6 +33,16 @@ public class ${entity.nameCamelCase}ContentValues extends AbstractContentValues
public int update(ContentResolver contentResolver, <#if config.useAnnotations>@Nullable</#if> ${entity.nameCamelCase}Selection where) {
return contentResolver.update(uri(), values(), where == null ? null : where.sel(), where == null ? null : where.args());
}

/**
* Update row(s) using the values stored by this object and the given selection.
*
* @param contentResolver The content resolver to use.
* @param where The selection to use (can be {@code null}).
*/
public int update(Context context, <#if config.useAnnotations>@Nullable</#if> ${entity.nameCamelCase}Selection where) {
return context.getContentResolver().update(uri(), values(), where == null ? null : where.sel(), where == null ? null : where.args());
}
<#list entity.fields as field>
<#if field.nameLowerCase != "_id">

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package ${config.providerJavaPackage}.${entity.packageName};

import java.util.Date;

import android.content.Context;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
Expand Down Expand Up @@ -52,6 +53,35 @@ public class ${entity.nameCamelCase}Selection extends AbstractSelection<${entity
return query(contentResolver, null, null);
}

/**
* Query the given content resolver using this selection.
*
* @param context The context to use for the query.
* @param projection A list of which columns to return. Passing null will return all columns, which is inefficient.
* @param sortOrder How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort
* order, which may be unordered.
* @return A {@code ${entity.nameCamelCase}Cursor} object, which is positioned before the first entry, or null.
*/
public ${entity.nameCamelCase}Cursor query(Context context, String[] projection, String sortOrder) {
Cursor cursor = context.getContentResolver().query(uri(), projection, sel(), args(), sortOrder);
if (cursor == null) return null;
return new ${entity.nameCamelCase}Cursor(cursor);
}

/**
* Equivalent of calling {@code query(context, projection, null)}.
*/
public ${entity.nameCamelCase}Cursor query(Context context, String[] projection) {
return query(context, projection, null);
}

/**
* Equivalent of calling {@code query(context, projection, null, null)}.
*/
public ${entity.nameCamelCase}Cursor query(Context context) {
return query(context, null, null);
}


public ${entity.nameCamelCase}Selection id(long... value) {
addEquals("${entity.nameLowerCase}." + ${entity.nameCamelCase}Columns._ID, toObjectArray(value));
Expand Down

0 comments on commit 9724c1e

Please sign in to comment.