package net.x4a42.volksempfaenger.ui;

import net.x4a42.volksempfaenger.R;
import net.x4a42.volksempfaenger.Utils;
import net.x4a42.volksempfaenger.data.Columns.Podcast;
import net.x4a42.volksempfaenger.data.VolksempfaengerContentProvider;
import android.app.Activity;
import android.content.ContentUris;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class DeleteSubscriptionActivity extends Activity implements
		OnClickListener {

	private long id;
	private Uri uri;

	private TextView textMessage;
	private Button buttonOk;
	private Button buttonCancel;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Check if there is an ID
		Bundle extras = getIntent().getExtras();
		if (extras == null) {
			finish();
			return;
		}
		id = extras.getLong("id");
		if (id <= 0) {
			finish();
			return;
		}
		uri = ContentUris.withAppendedId(
				VolksempfaengerContentProvider.PODCAST_URI, id);

		setContentView(R.layout.delete_subscription);

		LayoutParams params = getWindow().getAttributes();
		params.width = LayoutParams.MATCH_PARENT;
		getWindow().setAttributes(params);

		textMessage = (TextView) findViewById(R.id.text_message);
		buttonOk = (Button) findViewById(R.id.button_ok);
		buttonCancel = (Button) findViewById(R.id.button_cancel);
		buttonOk.setOnClickListener(this);
		buttonCancel.setOnClickListener(this);

		Cursor cursor = getContentResolver().query(uri, null, null, null, null);
		if (!cursor.moveToFirst()) {
			// ID does not exist
			finish();
			return;
		}
		textMessage.setText(getString(R.string.message_podcast_confirm_delete,
				cursor.getString(cursor.getColumnIndex(Podcast.TITLE))));
		cursor.close();

	}

	@Override
	protected void onResume() {
		super.onResume();
		ExternalStorageHelper.assertExternalStorageReadable(this);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button_ok:
			deletePodcast();
			setResult(RESULT_OK);
			finish();
			return;
		case R.id.button_cancel:
			setResult(RESULT_CANCELED);
			finish();
			return;
		}
	}

	public void deletePodcast() {
		int result = getContentResolver().delete(uri, null, null);
		if (result > 0) {
			// row was deleted
			Toast.makeText(this, R.string.message_podcast_successfully_deleted,
					Toast.LENGTH_LONG).show();
		}
		// delete podcast logo
		Utils.getPodcastLogoFile(this, id).delete();
	}
}

@@ -32,16 +32,124 @@
 er.ui;%0A%0A
+import android.app.DownloadManager;%0Aimport android.content.ContentResolver;%0Aimport android.content.Context;%0A
 import n
@@ -265,16 +265,169 @@
 umns
-.Podcast
+;%0Aimport net.x4a42.volksempfaenger.data.Columns.Podcast;%0Aimport net.x4a42.volksempfaenger.data.EpisodeCursor;%0Aimport net.x4a42.volksempfaenger.data.EpisodeHelper
 ;%0Aim
@@ -2595,16 +2595,726 @@
 ast() %7B%0A
+%0A%09%09ContentResolver resolver = this.getContentResolver();%0A%09%09Cursor cursor = resolver.query(%0A%09%09%09%09VolksempfaengerContentProvider.EPISODE_URI,%0A%09%09%09%09new String%5B%5D %7BColumns.Episode._ID, Columns.Episode.DOWNLOAD_BYTES_DOWNLOADED_SO_FAR %7D, Columns.Episode.PODCAST_ID + %22=%22 + String.valueOf(id), null, null);%0A%0A%09%09EpisodeCursor episodeCursor = new EpisodeCursor(cursor);%0A%0A%09%09if(episodeCursor.moveToFirst())%0A%09%09%7B%0A%09%09%09do%7B%0A%09%09%09%09if(episodeCursor.getDownloadDone() %3E 0)%0A%09%09%09%09%7B%0A%09%09%09%09%09EpisodeHelper%0A%09%09%09%09%09%09%09.deleteDownload(getContentResolver(),%0A%09%09%09%09%09%09%09%09%09(DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE),%0A%09%09%09%09%09%09%09%09%09episodeCursor.getId()%0A%09%09%09%09%09%09%09%09%09);%0A%09%09%09%09%7D%0A%09%09%09%7D while (episodeCursor.moveToNext());%0A%09%09%7D%0A%0A%09%09episodeCursor.close();%0A%0A
 %09%09int re
@@ -3362,24 +3362,25 @@
 ull, null);%0A
+%0A
 %09%09if (result

delete download when deleting a podcast