package org.monospace.smsfilter;

import android.content.AsyncTaskLoader;
import android.content.Context;
import android.database.Cursor;
import android.os.CancellationSignal;
import android.os.OperationCanceledException;

/**
 * SMSFilter
 * Author: rhgb
 */
public class DatabaseCursorLoader extends AsyncTaskLoader<Cursor> {
	final ForceLoadContentObserver mObserver;
	String mTable;
	String[] mColumns;
	String mSelection;
	String[] mSelectionArgs;
	String mOrderBy;
	DatabaseHelper mDatabaseHelper;
	Cursor mCursor;
	CancellationSignal mCancellationSignal;

	public DatabaseCursorLoader(Context context, String table, String[] columns, String selection,
	                    String[] selectionArgs, String orderBy) {
		super(context);
		mObserver = new ForceLoadContentObserver();
		mTable = table;
		mColumns = columns;
		mSelection = selection;
		mSelectionArgs = selectionArgs;
		mOrderBy = orderBy;
		mDatabaseHelper = new DatabaseHelper(context);
	}
	/* Runs on a worker thread */
	@Override
	public Cursor loadInBackground() {
		synchronized (this) {
			if (isLoadInBackgroundCanceled()) {
				throw new OperationCanceledException();
			}
			mCancellationSignal = new CancellationSignal();
		}
		try {
			Cursor cursor = mDatabaseHelper.getWritableDatabase().query(false, mTable, mColumns, mSelection, mSelectionArgs, null, null, mOrderBy, null, mCancellationSignal);
			if (cursor != null) {
				// Ensure the cursor window is filled
				cursor.getCount();
				cursor.registerContentObserver(mObserver);
			}
			return cursor;
		} finally {
			synchronized (this) {
				mCancellationSignal = null;
			}
		}
	}

	@Override
	public void cancelLoadInBackground() {
		super.cancelLoadInBackground();

		synchronized (this) {
			if (mCancellationSignal != null) {
				mCancellationSignal.cancel();
			}
		}
	}

	/* Runs on the UI thread */
	@Override
	public void deliverResult(Cursor cursor) {
		if (isReset()) {
			// An async query came in while the loader is stopped
			if (cursor != null) {
				cursor.close();
			}
			return;
		}
		Cursor oldCursor = mCursor;
		mCursor = cursor;

		if (isStarted()) {
			super.deliverResult(cursor);
		}

		if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) {
			oldCursor.close();
		}
	}

	/**
	 * Starts an asynchronous load of the contacts list data. When the result is ready the callbacks
	 * will be called on the UI thread. If a previous load has been completed and is still valid
	 * the result may be passed to the callbacks immediately.
	 *
	 * Must be called from the UI thread
	 */
	@Override
	protected void onStartLoading() {
		if (mCursor != null) {
			deliverResult(mCursor);
		}
		if (takeContentChanged() || mCursor == null) {
			forceLoad();
		}
	}

	/**
	 * Must be called from the UI thread
	 */
	@Override
	protected void onStopLoading() {
		// Attempt to cancel the current load task if possible.
		cancelLoad();
	}

	@Override
	public void onCanceled(Cursor cursor) {
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
	}

	@Override
	protected void onReset() {
		super.onReset();

		// Ensure the loader is stopped
		onStopLoading();

		if (mCursor != null && !mCursor.isClosed()) {
			mCursor.close();
		}
		mCursor = null;
	}

}

@@ -134,92 +134,8 @@
 sor;
-%0Aimport android.os.CancellationSignal;%0Aimport android.os.OperationCanceledException;
 %0A%0A/*
@@ -433,49 +433,8 @@
 sor;
-%0A%09CancellationSignal mCancellationSignal;
 %0A%0A%09p
@@ -910,186 +910,16 @@
 ) %7B%0A
-%09%09synchronized (this) %7B%0A%09%09%09if (isLoadInBackgroundCanceled()) %7B%0A%09%09%09%09throw new OperationCanceledException();%0A%09%09%09%7D%0A%09%09%09mCancellationSignal = new CancellationSignal();%0A%09%09%7D%0A%09%09try %7B%0A%09%09%09
+        
 Curs
@@ -1057,35 +1057,19 @@
 null
-, mCancellationSignal);%0A%09%09%09
+);%0A        
 if (
@@ -1086,20 +1086,28 @@
 null) %7B%0A
-%09%09%09%09
+            
 // Ensur
@@ -1136,20 +1136,28 @@
  filled%0A
-%09%09%09%09
+            
 cursor.g
@@ -1167,20 +1167,28 @@
 ount();%0A
-%09%09%09%09
+            
 cursor.r
@@ -1226,305 +1226,40 @@
 r);%0A
-%09%09%09%7D%0A%09%09%09return cursor;%0A%09%09%7D finally %7B%0A%09%09%09synchronized (this) %7B%0A%09%09%09%09mCancellationSignal = null;%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A%09@Override%0A%09public void cancelLoadInBackground() %7B%0A%09%09super.cancelLoadInBackground();%0A%0A%09%09synchronized (this) %7B%0A%09%09%09if (mCancellationSignal != null) %7B%0A%09%09%09%09mCancellationSignal.cancel();%0A%09%09%09%7D%0A%09%09%7D
+        %7D%0A        return cursor;
 %0A%09%7D%0A

Remove code to meet v13 compatibility