package com.axelby.podax;

import java.util.Vector;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;

public class GPodderReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		ContentResolver contentResolver = context.getContentResolver();
		if (intent.getAction().equals("com.axelby.gpodder.INITIALIZE")) {
			// update the gpodder db with all of our subscriptions
			Vector<String> in_podax = getPodaxUrls(contentResolver);
			for (String url : in_podax)
				contentResolver.insert(Constants.GPODDER_URI, makeUrlValues(url));
		} else if (intent.getAction().equals(
				"com.axelby.gpodder.SUBSCRIPTION_UPDATE")) {
			syncWithProvider(context);
		}
	}

	public static void syncWithProvider(Context context) {
		ContentResolver contentResolver = context.getContentResolver();
		Vector<String> at_gpodder = getGPodderUrls(contentResolver);
		Vector<String> in_podax = getPodaxUrls(contentResolver);

		for (String url : at_gpodder)
			if (!in_podax.contains(url))
				contentResolver.insert(SubscriptionProvider.URI, makeUrlValues(url));

		for (String url : in_podax)
			if (!at_gpodder.contains(url))
				contentResolver.delete(SubscriptionProvider.URI,
						SubscriptionProvider.COLUMN_URL + "=?",
						new String[] { url });
	}

	private static Vector<String> getPodaxUrls(ContentResolver contentResolver) {
		return retrieveUrls(contentResolver, SubscriptionProvider.URI);
	}

	private static Vector<String> getGPodderUrls(ContentResolver contentResolver) {
		return retrieveUrls(contentResolver, Constants.GPODDER_URI);
	}

	private static Vector<String> retrieveUrls(ContentResolver contentResolver, Uri uri) {
		Vector<String> in_podax = new Vector<String>();
		Cursor c = contentResolver.query(uri, new String[] { "url" }, null, null, null);
		while (c.moveToNext())
			in_podax.add(c.getString(0));
		c.close();
		return in_podax;
	}

	private static ContentValues makeUrlValues(String url) {
		ContentValues values = new ContentValues();
		values.put(SubscriptionProvider.COLUMN_URL, url);
		return values;
	}

}

@@ -1875,24 +1875,20 @@
 String%3E 
-in_podax
+urls
  = new V
@@ -1987,16 +1987,80 @@
  null);%0A
+%09%09// not sure why this happens%0A%09%09if (c == null)%0A%09%09%09return urls;%0A
 %09%09while 
@@ -2079,24 +2079,20 @@
 t())%0A%09%09%09
-in_podax
+urls
 .add(c.g
@@ -2127,24 +2127,20 @@
 %09return 
-in_podax
+urls
 ;%0A%09%7D%0A%0A%09p

fix crash bug in aggressive gpodder sync