summaryrefslogtreecommitdiff
path: root/src/de/podfetcher/activity/ItemviewActivity.java
blob: 3844ee8e07c3c7503ef9fa7837b3421305c53c41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package de.podfetcher.activity;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DateFormat;

import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

import de.podfetcher.R;
import de.podfetcher.asynctask.DownloadObserver;
import de.podfetcher.asynctask.DownloadStatus;
import de.podfetcher.feed.Feed;
import de.podfetcher.feed.FeedItem;
import de.podfetcher.feed.FeedManager;
import de.podfetcher.feed.FeedMedia;
import de.podfetcher.fragment.FeedItemlistFragment;
import de.podfetcher.fragment.FeedlistFragment;
import de.podfetcher.service.PlaybackService;
import de.podfetcher.storage.DownloadRequester;
import de.podfetcher.util.FeedItemMenuHandler;

/** Displays a single FeedItem and provides various actions */
public class ItemviewActivity extends SherlockActivity {
	private static final String TAG = "ItemviewActivity";

	private FeedManager manager;
	private FeedItem item;

	// Widgets
	private TextView txtvTitle;
	private TextView txtvPublished;
	private WebView webvDescription;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		manager = FeedManager.getInstance();
		extractFeeditem();
		populateUI();

	}

	@Override
	public void onStop() {
		super.onStop();
		Log.d(TAG, "Stopping Activity");
	}

	/** Extracts FeedItem object the activity is supposed to display */
	private void extractFeeditem() {
		long itemId = getIntent().getLongExtra(
				FeedItemlistFragment.EXTRA_SELECTED_FEEDITEM, -1);
		long feedId = getIntent().getLongExtra(
				FeedlistFragment.EXTRA_SELECTED_FEED, -1);
		if (itemId == -1 || feedId == -1) {
			Log.e(TAG, "Received invalid selection of either feeditem or feed.");
		}
		Feed feed = manager.getFeed(feedId);
		item = manager.getFeedItem(itemId, feed);
		Log.d(TAG, "Title of item is " + item.getTitle());
		Log.d(TAG, "Title of feed is " + item.getFeed().getTitle());
	}

	private void populateUI() {
		setContentView(R.layout.feeditemview);
		txtvTitle = (TextView) findViewById(R.id.txtvItemname);
		txtvPublished = (TextView) findViewById(R.id.txtvPublished);
		webvDescription = (WebView) findViewById(R.id.webvDescription);
		setTitle(item.getFeed().getTitle());

		txtvPublished.setText(DateUtils.formatSameDayTime(item.getPubDate()
				.getTime(), System.currentTimeMillis(), DateFormat.MEDIUM,
				DateFormat.SHORT));
		txtvTitle.setText(item.getTitle());
		String url = "";
		try {
			url = URLEncoder.encode(item.getContentEncoded(), "utf-8")
					.replaceAll("\\+", " ");
		} catch (UnsupportedEncodingException e) {
			url = "Page could not be loaded";
			e.printStackTrace();
		}

		webvDescription.loadData(url, "text/html", "utf-8");

	}

	/*
	 * TODO implement final DownloadObserver downloadObserver = new
	 * DownloadObserver(this) {
	 * 
	 * @Override protected void onProgressUpdate( DownloadStatus... values) {
	 * 
	 * }
	 * 
	 * @Override protected void onPostExecute(Boolean result) { boolean r =
	 * getStatusList()[0].isSuccessful(); if (r) { //setDownloadedState(); }
	 * else { //setNotDownloadedState(); } } };
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		return FeedItemMenuHandler.onCreateMenu(new MenuInflater(this), menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem menuItem) {
		FeedItemMenuHandler.onMenuItemClicked(this, menuItem, item);
		invalidateOptionsMenu();
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		return FeedItemMenuHandler.onPrepareMenu(menu, item);
	}
}