summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/activity/ItemviewActivity.java
blob: eb8d71bbe55d98c96ca6698719e079a3771ac349 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package de.danoeh.antennapod.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.format.DateUtils;
import android.util.Log;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.dialog.DownloadRequestErrorDialogCreator;
import de.danoeh.antennapod.feed.EventDistributor;
import de.danoeh.antennapod.feed.FeedItem;
import de.danoeh.antennapod.fragment.ItemDescriptionFragment;
import de.danoeh.antennapod.fragment.ItemlistFragment;
import de.danoeh.antennapod.preferences.UserPreferences;
import de.danoeh.antennapod.storage.DBReader;
import de.danoeh.antennapod.storage.DownloadRequestException;
import de.danoeh.antennapod.util.QueueAccess;
import de.danoeh.antennapod.util.StorageUtils;
import de.danoeh.antennapod.util.menuhandler.FeedItemMenuHandler;

import java.text.DateFormat;

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

    private static final int EVENTS = EventDistributor.DOWNLOAD_HANDLED | EventDistributor.DOWNLOAD_QUEUED;

	private FeedItem item;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		setTheme(UserPreferences.getTheme());
		super.onCreate(savedInstanceState);
		StorageUtils.checkStorageAvailability(this);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		getSupportActionBar().setDisplayShowTitleEnabled(false);
        EventDistributor.getInstance().register(contentUpdate);

        long itemId = getIntent().getLongExtra(
                ItemlistFragment.EXTRA_SELECTED_FEEDITEM, -1);
        if (itemId == -1) {
            Log.e(TAG, "Received invalid selection of either feeditem or feed.");
        } else {
            loadData(itemId);
        }
	}

	@Override
	protected void onResume() {
		super.onResume();
		StorageUtils.checkStorageAvailability(this);

	}

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

    private void loadData(long itemId) {
        AsyncTask<Long, Void, FeedItem> loadTask = new AsyncTask<Long, Void, FeedItem>() {

            @Override
            protected FeedItem doInBackground(Long... longs) {
                return DBReader.getFeedItem(ItemviewActivity.this, longs[0]);
            }

            @Override
            protected void onPostExecute(FeedItem feedItem) {
                super.onPostExecute(feedItem);
                if (feedItem != null && feedItem.getFeed() != null) {
                    item = feedItem;
                    populateUI();
                    supportInvalidateOptionsMenu();
                } else {
                    if (feedItem == null) {
                        Log.e(TAG, "Error: FeedItem was null");
                    } else if (feedItem.getFeed() == null) {
                        Log.e(TAG, "Error: Feed was null");
                    }
                }
            }
        };
        loadTask.execute(itemId);
    }

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

		txtvPublished.setText(DateUtils.formatSameDayTime(item.getPubDate()
				.getTime(), System.currentTimeMillis(), DateFormat.MEDIUM,
				DateFormat.SHORT));
		txtvTitle.setText(item.getTitle());

		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentTransaction fragmentTransaction = fragmentManager
				.beginTransaction();
		ItemDescriptionFragment fragment = ItemDescriptionFragment
				.newInstance(item, false);
		fragmentTransaction.replace(R.id.description_fragment, fragment);
		fragmentTransaction.commit();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
        if (item != null) {
            MenuInflater inflater = getSupportMenuInflater();
            inflater.inflate(R.menu.feeditem, menu);
            return true;
        } else {
            return false;
        }
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem menuItem) {
		try {
			if (!FeedItemMenuHandler.onMenuItemClicked(this,
					menuItem.getItemId(), item)) {
				switch (menuItem.getItemId()) {
				case android.R.id.home:
					finish();
					break;
				}
			}
		} catch (DownloadRequestException e) {
			e.printStackTrace();
			DownloadRequestErrorDialogCreator.newRequestErrorDialog(this,
					e.getMessage());
		}
		supportInvalidateOptionsMenu();
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(final Menu menu) {
		return FeedItemMenuHandler.onPrepareMenu(
                new FeedItemMenuHandler.MenuInterface() {

                    @Override
                    public void setItemVisibility(int id, boolean visible) {
                        menu.findItem(id).setVisible(visible);
                    }
                }, item, true, QueueAccess.NotInQueueAccess());
	}

    private EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() {

        @Override
        public void update(EventDistributor eventDistributor, Integer arg) {
            if ((EVENTS & arg) != 0) {
                if (AppConfig.DEBUG)
                    Log.d(TAG, "Received contentUpdate Intent.");
                if (item != null) {
                    loadData(item.getId());
                }
            }
        }
    };


}