Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.gawst.asyncdb.source.typed;
  
  import  android.content.ContentResolver;
  import  android.content.ContentValues;
  import  android.content.Context;
  import  android.database.Cursor;
  import  android.net.Uri;
  import  android.support.annotation.NonNull;
  import  android.support.annotation.Nullable;
 import  android.text.TextUtils;

A org.gawst.asyncdb.DataSource reading/writing data using a ContentProvider

Parameters:
<E> Type of the elements read from the CURSOR
<CURSOR> Wrapper around the raw Cursor read
Author(s):
Created by robUx4 on 11/01/2015.
 
 public abstract class TypedContentProviderDataSource<E, CURSOR extends Cursor> extends TypedCursorDataSource<E, Uri, Uri, CURSOR> {
 	public final Uri contentProviderUri;
 	private final Context context;
 	private final ContentResolver contentResolver;

Contructor.

Parameters:
contentResolver ContentResolver used to access the ContentProvider
contentProviderUri Uri to access the data from the ContentProvider
databaseElementHandler Handler to transform E elements to queries and Cursor to E elements.
 
 	public TypedContentProviderDataSource(@NonNull ContentResolver contentResolver, @NonNull Uri contentProviderUri, @NonNull TypedDatabaseElementHandler<E, CURSOR> databaseElementHandler) {
 		super(databaseElementHandler);
 		this. = null;
 		this. = contentResolver;
 		this. = contentProviderUri;
 	}

Contructor.

Parameters:
context Context used to get the ContentResolver used to access the ContentProvider
contentProviderUri Uri to access the data from the ContentProvider
databaseElementHandler Handler to transform E elements to queries and Cursor to E elements.
 
 	public TypedContentProviderDataSource(@NonNull Context context, @NonNull Uri contentProviderUri, @NonNull TypedDatabaseElementHandler<E, CURSOR> databaseElementHandler) {
 		super(databaseElementHandler);
 		this. = context.getApplicationContext();
 		this. = null;
 		this. = contentProviderUri;
 	}
 
 	public Uri getDatabaseId() {
 	}
 
 	private ContentResolver getContentResolver() {
 		if (null != )
 			return .getContentResolver();
 		return ;
 	}
 
 	public CURSOR query(String[] columnsString selectionString[] selectionArgsString groupByString havingString orderByString limit) {
 		Uri uri = ;
 		if (!TextUtils.isEmpty(groupBy)) {
 			orderBy = getGroupOrderBy(orderBygroupBy);
 			uri = getGroupUri(urigroupBy);
 		}
 		if (!TextUtils.isEmpty(having)) {
 			orderBy = getHavingOrderBy(orderByhaving);
 			uri = getHavingUri(urihaving);
 		}
 		if (!TextUtils.isEmpty(limit)) {
 			orderBy = getLimitOrderBy(orderBylimit);
 			uri = getLimitUri(urilimit);
 		}
 		return wrapCursor(getContentResolver().query(uricolumnsselectionselectionArgsorderBy));
 	}

Allow modifications on the ORDER_BY field to handle the GROUP BY field missing in Content-Provider query().

Returns:
modified ORDER_BY clause
 
 	protected String getGroupOrderBy(@Nullable String orderBy, @NonNull String groupBy) {
 		if (TextUtils.isEmpty(orderBy)) {
 			return "GROUP BY " + groupBy;
 		}
 
 		return orderBy + " GROUP BY " + groupBy;
 	}

Allow modifications on the Content-Provider Uri to handle the GROUP BY field missing in Content-Provider query().

Returns:
modified Uri
 
 	protected Uri getGroupUri(@NonNull Uri uri, @NonNull String groupBy) {
 		return uri;
 	}

Allow modifications on the ORDER_BY field to handle the HAVING field missing in Content-Provider query().

Returns:
modified ORDER_BY clause
	protected String getHavingOrderBy(@Nullable String orderBy, @NonNull String having) {
		if (TextUtils.isEmpty(orderBy)) {
			return "HAVING " + having;
		}
		return orderBy + " HAVING " + having;
	}

Allow modifications on the Content-Provider Uri to handle the HAVING field missing in Content-Provider query().

Returns:
modified Uri
	protected Uri getHavingUri(@NonNull Uri uri, @NonNull String having) {
		return uri;
	}

Allow modifications on the ORDER_BY field to handle the LIMIT field missing in Content-Provider query().

Returns:
modified ORDER_BY clause
	protected String getLimitOrderBy(@Nullable String orderBy, @NonNull String limit) {
		if (TextUtils.isEmpty(orderBy)) {
			return "LIMIT " + limit;
		}
		return orderBy + " LIMIT " + limit;
	}

Allow modifications on the Content-Provider Uri to handle the LIMIT field missing in Content-Provider query().

Returns:
modified Uri
	protected Uri getLimitUri(@NonNull Uri uri, @NonNull String limit) {
		return uri;
	}
	public int clearAllData() {
		return getContentResolver().delete("1"null);
	}
	public Uri insert(@NonNull ContentValues valuesthrows RuntimeException {
		return getContentResolver().insert(values);
	}
	public int update(@NonNull ContentValues updateValuesString whereString[] selectionArgs) {
		return getContentResolver().update(updateValueswhereselectionArgs);
	}
	public int delete(String selectionString[] selectionArgs) {
		return getContentResolver().delete(selectionselectionArgs);
	}

Does nothing for a android.content.ContentProvider
	public void eraseSource() {
		// TODO
	}
	protected String getToStringHeader() {
		return "TypedContentProvider:"+;
	}
New to GrepCode? Check out our FAQ X