summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/activity/FeedItemlistActivity.java
blob: 78d6bcec4c8b89ee92b97a94af306eba5b4abace (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package de.danoeh.antennapod.activity;

import android.annotation.SuppressLint;
import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.TypedArray;
import android.media.AudioManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.NavUtils;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.SearchView;
import android.util.Log;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.asynctask.FeedRemover;
import de.danoeh.antennapod.dialog.ConfirmationDialog;
import de.danoeh.antennapod.dialog.DownloadRequestErrorDialogCreator;
import de.danoeh.antennapod.feed.Feed;
import de.danoeh.antennapod.fragment.ExternalPlayerFragment;
import de.danoeh.antennapod.fragment.FeedlistFragment;
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.StorageUtils;
import de.danoeh.antennapod.util.menuhandler.FeedMenuHandler;

/**
 * Displays a List of FeedItems
 */
public class FeedItemlistActivity extends ActionBarActivity {
    private static final String TAG = "FeedItemlistActivity";

    /**
     * The feed which the activity displays
     */
    private Feed feed;
    private ItemlistFragment filf;
    private ExternalPlayerFragment externalPlayerFragment;

    private AsyncTask<?, ?, ?> currentLoadTask;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setTheme(UserPreferences.getTheme());
        super.onCreate(savedInstanceState);
        StorageUtils.checkStorageAvailability(this);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setContentView(R.layout.feeditemlist_activity);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);

        long feedId = getIntent().getLongExtra(
                FeedlistFragment.EXTRA_SELECTED_FEED, -1);
        if (feedId == -1) {
            Log.e(TAG, "Received invalid feed selection.");
        } else {
            loadData(feedId);
        }

    }

    private synchronized void loadData(long id) {
        if (currentLoadTask != null) {
            currentLoadTask.cancel(true);
        }
        AsyncTask<Long, Void, Feed> loadTask = new AsyncTask<Long, Void, Feed>() {

            @Override
            protected Feed doInBackground(Long... longs) {
                if (AppConfig.DEBUG)
                    Log.d(TAG, "Loading feed data in background");
                return DBReader.getFeed(FeedItemlistActivity.this, longs[0]);
            }

            @Override
            protected void onCancelled(Feed feed) {
                super.onCancelled(feed);
                if (AppConfig.DEBUG) Log.d(TAG, "load task was cancelled");
            }

            @Override
            protected void onPostExecute(Feed result) {
                super.onPostExecute(result);
                if (result != null) {
                    if (AppConfig.DEBUG) Log.d(TAG, "Finished loading feed data");
                    feed = result;
                    setTitle(feed.getTitle());

                    FragmentManager fragmentManager = getSupportFragmentManager();
                    FragmentTransaction fT = fragmentManager.beginTransaction();

                    filf = ItemlistFragment.newInstance(feed.getId());
                    fT.replace(R.id.feeditemlistFragment, filf);

                    externalPlayerFragment = new ExternalPlayerFragment();
                    fT.replace(R.id.playerFragment, externalPlayerFragment);
                    fT.commit();
                    supportInvalidateOptionsMenu();
                } else {
                    Log.e(TAG, "Error: Feed was null");
                }
            }
        };
        currentLoadTask = loadTask;
        loadTask.execute(id);
    }

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

    @Override
    protected void onStop() {
        super.onStop();
        if (currentLoadTask != null) {
            currentLoadTask.cancel(true);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        if (feed != null) {
            TypedArray drawables = obtainStyledAttributes(new int[]{R.attr.action_search});
            MenuItemCompat.setShowAsAction(menu.add(Menu.NONE, R.id.search_item, Menu.NONE, R.string.search_label)
                    .setIcon(drawables.getDrawable(0)),
                    MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
            MenuItemCompat.setActionView(menu.findItem(R.id.search_item), new SearchView(this));

            SearchManager searchManager =
                    (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.search_item));


            searchView.setIconifiedByDefault(true);

            searchView.setSearchableInfo(
                    searchManager.getSearchableInfo(getComponentName()));
            FeedMenuHandler
                    .onCreateOptionsMenu(getMenuInflater(), menu);
        }
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        return FeedMenuHandler.onPrepareOptionsMenu(menu, feed);
    }

    @SuppressLint("NewApi")
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        try {
            if (FeedMenuHandler.onOptionsItemClicked(this, item, feed)) {
                filf.getListAdapter().notifyDataSetChanged();
            } else {
                switch (item.getItemId()) {
                    case R.id.remove_item:
                        final FeedRemover remover = new FeedRemover(
                                FeedItemlistActivity.this, feed) {
                            @Override
                            protected void onPostExecute(Void result) {
                                super.onPostExecute(result);
                                finish();
                            }
                        };
                        ConfirmationDialog conDialog = new ConfirmationDialog(this,
                                R.string.remove_feed_label,
                                R.string.feed_delete_confirmation_msg) {

                            @Override
                            public void onConfirmButtonPressed(
                                    DialogInterface dialog) {
                                dialog.dismiss();
                                remover.executeAsync();
                            }
                        };
                        conDialog.createNewDialog().show();
                        break;
                    case android.R.id.home:
                        NavUtils.navigateUpFromSameTask(this);
                        break;
                }
            }
        } catch (DownloadRequestException e) {
            e.printStackTrace();
            DownloadRequestErrorDialogCreator.newRequestErrorDialog(this,
                    e.getMessage());
        }
        return true;
    }

    @Override
    public boolean onSearchRequested() {
        if (feed != null) {
            Bundle bundle = new Bundle();
            bundle.putLong(SearchActivity.EXTRA_FEED_ID, feed.getId());
            startSearch(null, false, bundle, false);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void startActivity(Intent intent) {
        if (intent.getAction() != null &&
                intent.getAction().equals(Intent.ACTION_SEARCH)) {
            addSearchInformation(intent);
        }
        super.startActivity(intent);
    }

    private void addSearchInformation(Intent startIntent) {
        startIntent.putExtra(SearchActivity.EXTRA_FEED_ID, feed.getId());
    }

}