Skip to content

Instantly share code, notes, and snippets.

@EugeneShapovalov94
Last active July 18, 2017 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EugeneShapovalov94/9944560e42a080d35d6ce06fb17e41c4 to your computer and use it in GitHub Desktop.
Save EugeneShapovalov94/9944560e42a080d35d6ce06fb17e41c4 to your computer and use it in GitHub Desktop.
public class AoAsyncQueryHandler extends AsyncQueryHandler {
private WeakReference<AsyncQueryListener> mListener;
public AoAsyncQueryHandler(ContentResolver resolver) {
super(resolver);
}
public AoAsyncQueryHandler(ContentResolver resolver, AsyncQueryListener listener) {
super(resolver);
setQueryListener(listener);
}
/**
* Assign the given {@link AsyncQueryListener} to receive query events from
* asynchronous calls. Will replace any existing listener.
*/
public void setQueryListener(AsyncQueryListener listener) {
mListener = new WeakReference<AsyncQueryListener>(listener);
}
/**
* Clear any {@link AsyncQueryListener} set through
* {@link #setQueryListener(AsyncQueryListener)}
*/
public void clearQueryListener() {
mListener = null;
}
/**
* Begin an asynchronous query with the given arguments. When finished,
* {@link AsyncQueryListener#onQueryComplete(int, Object, Cursor)} is called
* if a valid {@link AsyncQueryListener} is present.
*/
public void startQuery(Uri uri, String[] projection) {
startQuery(NULL, null, uri, projection, null, null, null);
}
/**
* Begin an asynchronous query with the given arguments. When finished,
* {@link AsyncQueryListener#onQueryComplete(int, Object, Cursor)} is called
* if a valid {@link AsyncQueryListener} is present.
*
* @param token Unique identifier passed through to
* {@link AsyncQueryListener#onQueryComplete(int, Object, Cursor)}
*/
public void startQuery(int token, Uri uri, String[] projection) {
startQuery(token, null, uri, projection, null, null, null);
}
/**
* Begin an asynchronous query with the given arguments. When finished,
* {@link AsyncQueryListener#onQueryComplete(int, Object, Cursor)} is called
* if a valid {@link AsyncQueryListener} is present.
*/
public void startQuery(Uri uri, String[] projection, String sortOrder) {
startQuery(NULL, null, uri, projection, null, null, sortOrder);
}
/**
* Begin an asynchronous query with the given arguments. When finished,
* {@link AsyncQueryListener#onQueryComplete(int, Object, Cursor)} is called
* if a valid {@link AsyncQueryListener} is present.
*/
public void startQuery(Uri uri, String[] projection, String selection, String[] selectionArgs,
String orderBy) {
startQuery(NULL, null, uri, projection, selection, selectionArgs, orderBy);
}
/**
* Begin an asynchronous update with the given arguments.
*/
public void startUpdate(Uri uri, ContentValues values) {
startUpdate(NULL, null, uri, values, null, null);
}
public void startUpdate(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
startUpdate(NULL, null, uri, values, selection, selectionArgs);
}
public void startInsert(Uri uri, ContentValues values) {
startInsert(NULL, null, uri, values);
}
public void startDelete(Uri uri) {
startDelete(NULL, null, uri, null, null);
}
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
final AsyncQueryListener listener = mListener == null ? null : mListener.get();
if (listener != null) {
listener.onQueryComplete(token, cookie, cursor);
} else if (cursor != null) {
cursor.close();
}
}
@Override
protected void onUpdateComplete(int token, Object cookie, int result) {
final AsyncQueryListener listener = mListener == null ? null : mListener.get();
if (listener != null) {
listener.onUpdateComplete(token, cookie, result);
}
}
@Override
protected void onDeleteComplete(int token, Object cookie, int result) {
final AsyncQueryListener listener = mListener == null ? null : mListener.get();
if (listener != null) {
listener.onDeleteComplete(token, cookie, result);
}
}
@Override
protected void onInsertComplete(int token, Object cookie, Uri uri) {
final AsyncQueryListener listener = mListener == null ? null : mListener.get();
if (listener != null) {
listener.onInsertComplete(token, cookie, uri);
}
}
}
///////////Usage//////////
AoAsyncQueryHandler asyncQueryHandler = new AoAsyncQueryHandler(
getActivity().getContentResolver(), new BaseAsyncQueryListener() {
@Override
public void onQueryComplete(int token, Object cookie, Cursor cursor) {
// Do here your logic.
}
});
asyncQueryHandler.startQuery(uri, ...);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment