summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/fragment/MiroGuideChannellistFragment.java
blob: 60546fdeb0ad90e5af4ff8b99a6f4566f68e8c55 (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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package de.danoeh.antennapod.fragment;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockListFragment;

import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MiroGuideChannelViewActivity;
import de.danoeh.antennapod.adapter.MiroGuideChannelListAdapter;
import de.danoeh.antennapod.miroguide.conn.MiroGuideException;
import de.danoeh.antennapod.miroguide.conn.MiroGuideService;
import de.danoeh.antennapod.miroguide.model.MiroGuideChannel;

/**
 * Displays a list of MiroGuideChannel objects that were results of a certain
 * MiroGuideService query. If the user reaches the bottom of the list, more
 * entries will be loaded until all entries have been loaded or the maximum
 * number of channels has been reached.
 * */
public class MiroGuideChannellistFragment extends SherlockListFragment {
	private static final String TAG = "MiroGuideChannellistFragment";

	private static final String ARG_FILTER = "filter";
	private static final String ARG_FILTER_VALUE = "filter_value";
	private static final String ARG_SORT = "sort";

	private static final int MAX_CHANNELS = 200;
	private static final int CHANNELS_PER_QUERY = MiroGuideService.DEFAULT_CHANNEL_LIMIT;

	private ArrayList<MiroGuideChannel> channels;
	private MiroGuideChannelListAdapter listAdapter;
	private int offset;

	private boolean isLoadingChannels;
	/**
	 * True if there are no more entries to load or if the maximum number of
	 * channels in the channellist has been reached
	 */
	private boolean stopLoading;

	private View footer;

	private String filter;
	private String filterValue;
	private String sort;

	private AsyncTask<Void, Void, List<MiroGuideChannel>> channelLoader;

	/**
	 * Creates a new instance of Channellist fragment.
	 * 
	 * @throws IllegalArgumentException
	 *             if filter, filterValue or sort is null
	 * */
	public static MiroGuideChannellistFragment newInstance(String filter,
			String filterValue, String sort) {
		if (filter == null) {
			throw new IllegalArgumentException("filter cannot be null");
		}
		if (filterValue == null) {
			throw new IllegalArgumentException("filter value cannot be null");
		}
		if (sort == null) {
			throw new IllegalArgumentException("sort cannot be null");
		}
		MiroGuideChannellistFragment cf = new MiroGuideChannellistFragment();
		Bundle args = new Bundle();
		args.putString(ARG_FILTER, filter);
		args.putString(ARG_FILTER_VALUE, filterValue);
		args.putString(ARG_SORT, sort);
		cf.setArguments(args);
		return cf;
	}

	private MiroGuideChannellistFragment() {
		super();
	}

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

		offset = 0;
		channels = new ArrayList<MiroGuideChannel>();

		Bundle args = getArguments();
		filter = args.getString(ARG_FILTER);
		filterValue = args.getString(ARG_FILTER_VALUE);
		sort = args.getString(ARG_SORT);

		LayoutInflater inflater = (LayoutInflater) getActivity()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		footer = inflater.inflate(R.layout.loading_footer, null);
		listAdapter = new MiroGuideChannelListAdapter(getActivity(), 0,
				channels);
	}

	@Override
	public void onResume() {
		super.onResume();
		if (channels.isEmpty()) {
			setListShown(false);
			loadChannels();
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		if (channelLoader != null) {
			channelLoader.cancel(true);
		}
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		getListView().addFooterView(footer); // footer has to be added before
												// the adapter has been set
		getListView().setAdapter(listAdapter);
		getListView().removeFooterView(footer);

		getListView().setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				int lastVisibleItem = firstVisibleItem + visibleItemCount;
				if (lastVisibleItem == totalItemCount) {
					if (AppConfig.DEBUG)
						loadChannels();
				}
			}

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
			}
		});
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		if (listAdapter != null) {
			MiroGuideChannel selection = listAdapter.getItem(position);
			Intent launchIntent = new Intent(getActivity(),
					MiroGuideChannelViewActivity.class);
			launchIntent.putExtra(MiroGuideChannelViewActivity.EXTRA_CHANNEL_ID,
					selection.getId());
			launchIntent.putExtra(MiroGuideChannelViewActivity.EXTRA_CHANNEL_URL,
					selection.getDownloadUrl());
			startActivity(launchIntent);
		}
	}

	@SuppressLint("NewApi")
	private void loadChannels() {
		if (!isLoadingChannels) {
			if (!stopLoading) {
				isLoadingChannels = true;
				channelLoader = new AsyncTask<Void, Void, List<MiroGuideChannel>>() {
					private MiroGuideException exception;

					@Override
					protected void onCancelled() {
						if (AppConfig.DEBUG)
							Log.d(TAG, "Channel loader was cancelled");
					}

					@Override
					protected void onPostExecute(List<MiroGuideChannel> result) {
						if (AppConfig.DEBUG)
							Log.d(TAG, "Channel loading finished");
						if (exception == null) {
							getListView().removeFooterView(footer);
							for (MiroGuideChannel channel : result) {
								channels.add(channel);
							}
							listAdapter.notifyDataSetChanged();
							offset += CHANNELS_PER_QUERY;
							// check if fragment should not send any more
							// queries
							if (result.size() < CHANNELS_PER_QUERY) {
								if (AppConfig.DEBUG)
									Log.d(TAG,
											"Query result was less than requested number of channels. Stopping to send any more queries");
								stopLoading = true;
							}
							if (offset >= MAX_CHANNELS) {
								if (AppConfig.DEBUG)
									Log.d(TAG,
											"Maximum number of feeds has been reached. Stopping to send any more queries");
								stopLoading = true;
							}

							setListShown(true);
						} else {
							AlertDialog.Builder dialog = new AlertDialog.Builder(
									getActivity());
							dialog.setTitle(R.string.error_label);
							dialog.setMessage(exception.getMessage());
							dialog.setNeutralButton(android.R.string.ok,
									new DialogInterface.OnClickListener() {

										@Override
										public void onClick(
												DialogInterface dialog,
												int which) {
											dialog.dismiss();
										}
									});
							dialog.create().show();
						}
						isLoadingChannels = false;
					}

					@Override
					protected void onPreExecute() {
						getListView().addFooterView(footer);
					}

					@Override
					protected List<MiroGuideChannel> doInBackground(Void... params) {
						if (AppConfig.DEBUG)
							Log.d(TAG, "Background channel loader started");
						MiroGuideService service = new MiroGuideService();
						try {
							return service.getChannelList(filter, filterValue,
									sort, CHANNELS_PER_QUERY, offset);
						} catch (MiroGuideException e) {
							exception = e;
							e.printStackTrace();
						} finally {
							// service.close();
						}
						return null;
					}
				};

				if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
					channelLoader
							.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
				} else {
					channelLoader.execute();
				}
			}
		} else {
			if (AppConfig.DEBUG)
				Log.d(TAG, "Channels are already being loaded");
		}
	}
}