summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/activity/MiroGuideChannelViewActivity.java
blob: eae54f7129f570b49d64a735bb88bb68e87e67dc (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
package de.danoeh.antennapod.activity;

import java.util.Date;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

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

import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.adapter.MiroGuideItemlistAdapter;
import de.danoeh.antennapod.dialog.DownloadRequestErrorDialogCreator;
import de.danoeh.antennapod.feed.Feed;
import de.danoeh.antennapod.miroguide.conn.MiroGuideException;
import de.danoeh.antennapod.miroguide.conn.MiroGuideService;
import de.danoeh.antennapod.miroguide.model.MiroGuideChannel;
import de.danoeh.antennapod.preferences.UserPreferences;
import de.danoeh.antennapod.storage.DBReader;
import de.danoeh.antennapod.storage.DownloadRequestException;
import de.danoeh.antennapod.storage.DownloadRequester;

/**
 * Displays information about one channel and lets the user add this channel to
 * his library.
 */
public class MiroGuideChannelViewActivity extends SherlockActivity {
    private static final String TAG = "MiroGuideChannelViewActivity";

    public static final String EXTRA_CHANNEL_ID = "id";
    public static final String EXTRA_CHANNEL_URL = "url";

    private RelativeLayout layoutContent;
    private ProgressBar progLoading;
    private TextView txtvTitle;
    private TextView txtVDescription;
    private ListView listEntries;

    private long channelId;
    private String channelUrl;
    private MiroGuideChannel channel;
    private volatile List<Feed> feeds;

    @Override
    protected void onPause() {
        super.onPause();
        channelLoader.cancel(true);
    }

    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(UserPreferences.getTheme());
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setContentView(R.layout.miroguide_channelview);

        layoutContent = (RelativeLayout) findViewById(R.id.layout_content);
        progLoading = (ProgressBar) findViewById(R.id.progLoading);
        txtvTitle = (TextView) findViewById(R.id.txtvTitle);
        txtVDescription = (TextView) findViewById(R.id.txtvDescription);
        listEntries = (ListView) findViewById(R.id.itemlist);

        channelId = getIntent().getLongExtra(EXTRA_CHANNEL_ID, -1);
        channelUrl = getIntent().getStringExtra(EXTRA_CHANNEL_URL);

        if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
            channelLoader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        } else {
            channelLoader.execute();
        }

    }

    /**
     * Is used to load channel information asynchronously.
     */
    private AsyncTask<Void, Void, Void> channelLoader = new AsyncTask<Void, Void, Void>() {
        private static final String TAG = "ChannelLoader";
        private Exception exception;

        @Override
        protected Void doInBackground(Void... params) {
            if (AppConfig.DEBUG)
                Log.d(TAG, "Starting background task");
            feeds = DBReader.getFeedList(MiroGuideChannelViewActivity.this);
            MiroGuideService service = new MiroGuideService();
            try {
                channel = service.getChannel(channelId);
            } catch (MiroGuideException e) {
                e.printStackTrace();
                exception = e;
            }
            return null;
        }

        @SuppressLint("NewApi")
        @Override
        protected void onPostExecute(Void result) {
            if (AppConfig.DEBUG)
                Log.d(TAG, "Loading finished");
            if (exception == null) {
                txtvTitle.setText(channel.getName());
                txtVDescription.setText(channel.getDescription());

                MiroGuideItemlistAdapter listAdapter = new MiroGuideItemlistAdapter(
                        MiroGuideChannelViewActivity.this, 0,
                        channel.getItems());
                listEntries.setAdapter(listAdapter);
                progLoading.setVisibility(View.GONE);
                layoutContent.setVisibility(View.VISIBLE);
                supportInvalidateOptionsMenu();
            } else {
                finish();
            }
        }

    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = new MenuInflater(this);
        inflater.inflate(R.menu.channelview, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        boolean channelLoaded = channel != null;
        boolean beingDownloaded = channelLoaded
                && DownloadRequester.getInstance().isDownloadingFile(
                channel.getDownloadUrl());
        boolean notAdded = channelLoaded
                && !((feedExists(
                channel.getDownloadUrl()) || beingDownloaded));
        menu.findItem(R.id.add_feed).setVisible(notAdded);
        menu.findItem(R.id.visit_website_item).setVisible(
                channelLoaded && channel.getWebsiteUrl() != null);
        return true;
    }

    @SuppressLint("NewApi")
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
            case R.id.visit_website_item:
                Uri uri = Uri.parse(channel.getWebsiteUrl());
                startActivity(new Intent(Intent.ACTION_VIEW, uri));
                return true;
            case R.id.add_feed:
                try {
                    DownloadRequester.getInstance().downloadFeed(
                            this,
                            new Feed(channel.getDownloadUrl(), new Date(), channel
                                    .getName()));
                } catch (DownloadRequestException e) {
                    e.printStackTrace();
                    DownloadRequestErrorDialogCreator.newRequestErrorDialog(this,
                            e.getMessage());
                }
                Toast toast = Toast.makeText(this, R.string.miro_feed_added,
                        Toast.LENGTH_LONG);
                toast.show();
                supportInvalidateOptionsMenu();
                return true;
            default:
                return false;
        }
    }

    private boolean feedExists(String downloadUrl) {
        if (feeds == null) {
            return false;
        }

        for (Feed feed : feeds) {
            if (feed.getDownload_url().equals(downloadUrl)) {
                return true;
            }
        }
        return false;
    }

}