package org.ccci.gto.android.common.support.v4.content;

import android.content.Context;
import android.content.IntentFilter;
import android.net.Uri;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.LocalBroadcastManager;

import java.util.ArrayList;
import java.util.List;

public class BroadcastReceiverCursorLoader extends CursorLoader {
    private final LocalBroadcastManager localBroadcastManager;
    private final LoaderBroadcastReceiver receiver = new LoaderBroadcastReceiver(this);
    private final List<IntentFilter> filters = new ArrayList<IntentFilter>();

    public BroadcastReceiverCursorLoader(final Context context) {
        super(context);
        this.localBroadcastManager = LocalBroadcastManager.getInstance(context);
    }

    public BroadcastReceiverCursorLoader(final Context context, final Uri uri, final String[] projection,
                                         final String selection, final String[] selectionArgs, final String sortOrder) {
        super(context, uri, projection, selection, selectionArgs, sortOrder);
        this.localBroadcastManager = LocalBroadcastManager.getInstance(context);
    }

    public final void addIntentFilter(final IntentFilter filter) {
        this.filters.add(filter);
    }

    @Override
    protected void onStartLoading() {
        super.onStartLoading();
        if (this.localBroadcastManager != null && this.filters.size() > 0) {
            for (final IntentFilter filter : this.filters) {
                localBroadcastManager.registerReceiver(this.receiver, filter);
            }
        }
    }

    @Override
    protected void onAbandon() {
        super.onAbandon();
        if (this.localBroadcastManager != null) {
            localBroadcastManager.unregisterReceiver(this.receiver);
        }
    }
}

@@ -138,15 +138,23 @@
 oid.
-net.Uri
+database.Cursor
 ;%0Aim
@@ -316,16 +316,25 @@
 %0Apublic 
+abstract 
 class Br
@@ -676,16 +676,47 @@
  context
+, final IntentFilter... filters
 ) %7B%0A    
@@ -824,58 +824,22 @@
     
-%7D%0A%0A
     
-public BroadcastReceiverCursorLoade
+fo
 r(final 
 Cont
@@ -838,346 +838,92 @@
 nal 
-Context context, final Uri uri, final String%5B%5D projection,%0A                                         final String selection, final String%5B%5D selectionArgs, final String sortOrder) %7B%0A        super(context, uri, projection, selection, selectionArgs, sortOrder);%0A        this.localBroadcastManager = LocalBroadcastManager.getInstance(context);
+IntentFilter filter : filters) %7B%0A            this.addIntentFilter(filter);%0A        %7D
 %0A   
@@ -1026,32 +1026,59 @@
 filter);%0A    %7D%0A%0A
+    /* BEGIN lifecycle */%0A%0A
     @Override%0A  
@@ -1596,16 +1596,272 @@
       %7D%0A
-
     %7D
+%0A%0A    /* END lifecycle */%0A%0A    @Override%0A    public final Cursor loadInBackground() %7B%0A        final Cursor c = this.getCursor();%0A        if(c != null) %7B%0A            c.getCount();%0A        %7D%0A        return c;%0A    %7D%0A%0A    protected abstract Cursor getCursor();
 %0A%7D%0A

make BroadcastReceiverCursorLoader and abstract class