diff --git a/src/main/resources/org/jraf/androidcontentprovidergenerator/abstractcontentvalues.ftl b/src/main/resources/org/jraf/androidcontentprovidergenerator/abstractcontentvalues.ftl index 3bb16d0..80ab4a3 100644 --- a/src/main/resources/org/jraf/androidcontentprovidergenerator/abstractcontentvalues.ftl +++ b/src/main/resources/org/jraf/androidcontentprovidergenerator/abstractcontentvalues.ftl @@ -3,6 +3,7 @@ ${header} package ${config.providerJavaPackage}.base; +import android.content.Context; import android.content.ContentResolver; import android.content.ContentValues; import android.net.Uri; @@ -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()); + } } \ No newline at end of file diff --git a/src/main/resources/org/jraf/androidcontentprovidergenerator/abstractselection.ftl b/src/main/resources/org/jraf/androidcontentprovidergenerator/abstractselection.ftl index 42a752a..258d4a2 100644 --- a/src/main/resources/org/jraf/androidcontentprovidergenerator/abstractselection.ftl +++ b/src/main/resources/org/jraf/androidcontentprovidergenerator/abstractselection.ftl @@ -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; @@ -298,6 +299,16 @@ public abstract class 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; diff --git a/src/main/resources/org/jraf/androidcontentprovidergenerator/contentvalues.ftl b/src/main/resources/org/jraf/androidcontentprovidergenerator/contentvalues.ftl index 03b1288..78435c6 100644 --- a/src/main/resources/org/jraf/androidcontentprovidergenerator/contentvalues.ftl +++ b/src/main/resources/org/jraf/androidcontentprovidergenerator/contentvalues.ftl @@ -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> @@ -32,6 +33,16 @@ public class ${entity.nameCamelCase}ContentValues extends AbstractContentValues public int update(ContentResolver contentResolver, <#if config.useAnnotations>@Nullable ${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 ${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"> diff --git a/src/main/resources/org/jraf/androidcontentprovidergenerator/selection.ftl b/src/main/resources/org/jraf/androidcontentprovidergenerator/selection.ftl index 49253cf..dce1218 100644 --- a/src/main/resources/org/jraf/androidcontentprovidergenerator/selection.ftl +++ b/src/main/resources/org/jraf/androidcontentprovidergenerator/selection.ftl @@ -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; @@ -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));