package com.newsblur.database;

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

/**
 * A partial copy of android.content.CursorLoader with the bits related to ContentProviders
 * gutted out so plain old SQLiteDatabase queries can be used where a ContentProvider is
 * contraindicated. (Why this isn't in core Android I will never understand)
 */
public abstract class QueryCursorLoader extends AsyncTaskLoader<Cursor> {

    private Cursor cursor;
    private CancellationSignal cancellationSignal;

    public QueryCursorLoader(Context context) {
        super(context);
    }

    protected abstract Cursor createCursor();

    @Override
    public Cursor loadInBackground() {
       synchronized (this) {
            if (isLoadInBackgroundCanceled()) {
                throw new OperationCanceledException();
            }
            cancellationSignal = new CancellationSignal();
        }
        try {
            Cursor c = createCursor();
            if (c != null) {
                c.getCount();
            }
            return c;
        } finally {
            synchronized (this) {
                cancellationSignal = null;
            }
        }
    }

    @Override
    public void cancelLoadInBackground() {
        super.cancelLoadInBackground();
        synchronized (this) {
            if (cancellationSignal != null) {
                cancellationSignal.cancel();
            }
        }
    }

    @Override
    public void deliverResult(Cursor cursor) {
        if (isReset()) {
            if (cursor != null) {
                cursor.close();
            }
            return;
        }
        Cursor oldCursor = cursor;
        cursor = cursor;
        if (isStarted()) {
            super.deliverResult(cursor);
        }
        if (oldCursor != null && oldCursor != cursor && !oldCursor.isClosed()) {
            oldCursor.close();
        }
    } 

    @Override
    protected void onStartLoading() {
        if (cursor != null) {
            deliverResult(cursor);
        }
        if (takeContentChanged() || cursor == null) {
            forceLoad();
        }
    }

    @Override
    protected void onStopLoading() {
        cancelLoad();
    }

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

    @Override
    protected void onReset() {
        super.onReset();
        onStopLoading();
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
        cursor = null;
    }

}

@@ -167,16 +167,41 @@
 Signal;%0A
+import android.os.Build;%0A
 import a
@@ -859,32 +859,111 @@
 onized (this) %7B%0A
+            if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.JELLY_BEAN) %7B%0A    
             if (
@@ -1010,16 +1010,20 @@
         
+    
 throw ne
@@ -1058,34 +1058,42 @@
 ();%0A            
+   
+ 
 %7D%0A
+    
             canc
@@ -1131,24 +1131,38 @@
 onSignal();%0A
+            %7D%0A
         %7D%0A  

Fix crash on pre-4.1 devices.