2011-09-08 11:28:38 +00:00
|
|
|
package org.fox.ttrss;
|
|
|
|
|
2011-11-28 13:00:36 +00:00
|
|
|
import java.io.BufferedInputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
2011-11-28 14:20:56 +00:00
|
|
|
import java.io.InputStream;
|
2011-11-23 10:55:05 +00:00
|
|
|
import java.lang.reflect.Type;
|
2012-09-18 09:08:57 +00:00
|
|
|
import java.net.HttpURLConnection;
|
2011-11-28 13:00:36 +00:00
|
|
|
import java.net.URL;
|
2011-11-23 10:55:05 +00:00
|
|
|
import java.util.ArrayList;
|
2011-11-23 17:07:13 +00:00
|
|
|
import java.util.Collections;
|
2011-11-26 06:31:14 +00:00
|
|
|
import java.util.Comparator;
|
2011-11-23 10:55:05 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
|
2012-06-19 10:18:00 +00:00
|
|
|
import org.fox.ttrss.types.Feed;
|
|
|
|
import org.fox.ttrss.types.FeedCategory;
|
2012-06-19 14:32:18 +00:00
|
|
|
import org.fox.ttrss.types.FeedList;
|
2011-11-28 14:20:56 +00:00
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
2011-11-28 14:20:56 +00:00
|
|
|
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
|
2011-11-28 13:00:36 +00:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.graphics.BitmapFactory;
|
2012-05-02 04:35:53 +00:00
|
|
|
import android.net.http.AndroidHttpClient;
|
2011-11-28 13:00:36 +00:00
|
|
|
import android.os.AsyncTask;
|
2011-09-08 11:28:38 +00:00
|
|
|
import android.os.Bundle;
|
2011-11-28 13:00:36 +00:00
|
|
|
import android.os.Environment;
|
2011-09-08 11:28:38 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2012-06-19 14:24:22 +00:00
|
|
|
import android.support.v4.app.Fragment;
|
2012-09-18 09:08:57 +00:00
|
|
|
import android.util.Base64;
|
2011-11-28 13:00:36 +00:00
|
|
|
import android.util.Log;
|
2011-11-29 12:40:23 +00:00
|
|
|
import android.view.ContextMenu;
|
2011-12-06 11:46:15 +00:00
|
|
|
import android.view.ContextMenu.ContextMenuInfo;
|
2011-09-08 11:28:38 +00:00
|
|
|
import android.view.LayoutInflater;
|
2012-09-16 09:25:28 +00:00
|
|
|
import android.view.MenuItem;
|
2011-09-08 11:28:38 +00:00
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2011-09-08 12:45:08 +00:00
|
|
|
import android.widget.AdapterView;
|
2011-11-29 12:40:23 +00:00
|
|
|
import android.widget.AdapterView.AdapterContextMenuInfo;
|
2011-09-08 12:45:08 +00:00
|
|
|
import android.widget.AdapterView.OnItemClickListener;
|
2011-11-23 10:55:05 +00:00
|
|
|
import android.widget.ArrayAdapter;
|
2011-11-28 11:25:55 +00:00
|
|
|
import android.widget.ImageView;
|
2011-09-08 11:28:38 +00:00
|
|
|
import android.widget.ListView;
|
2011-11-23 10:55:05 +00:00
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.JsonArray;
|
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
import com.google.gson.reflect.TypeToken;
|
2011-09-08 11:28:38 +00:00
|
|
|
|
2011-11-28 14:20:56 +00:00
|
|
|
public class FeedsFragment extends Fragment implements OnItemClickListener, OnSharedPreferenceChangeListener {
|
2011-09-08 11:28:38 +00:00
|
|
|
private final String TAG = this.getClass().getSimpleName();
|
2011-11-22 12:13:02 +00:00
|
|
|
private SharedPreferences m_prefs;
|
2011-11-23 10:55:05 +00:00
|
|
|
private FeedListAdapter m_adapter;
|
2011-11-25 10:13:10 +00:00
|
|
|
private FeedList m_feeds = new FeedList();
|
2012-09-16 09:25:28 +00:00
|
|
|
private FeedsActivity m_activity;
|
2012-06-19 15:01:28 +00:00
|
|
|
private Feed m_selectedFeed;
|
|
|
|
private FeedCategory m_activeCategory;
|
2012-10-14 12:37:20 +00:00
|
|
|
private static final String ICON_PATH = "/icons/";
|
2011-11-28 13:00:36 +00:00
|
|
|
private boolean m_enableFeedIcons;
|
2011-11-30 06:30:19 +00:00
|
|
|
private boolean m_feedIconsChecked = false;
|
2011-11-23 16:38:21 +00:00
|
|
|
|
2012-06-19 15:01:28 +00:00
|
|
|
public FeedsFragment() {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public FeedsFragment(FeedCategory cat) {
|
|
|
|
m_activeCategory = cat;
|
|
|
|
}
|
|
|
|
|
2011-11-26 06:31:14 +00:00
|
|
|
class FeedUnreadComparator implements Comparator<Feed> {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int compare(Feed a, Feed b) {
|
|
|
|
if (a.unread != b.unread)
|
|
|
|
return b.unread - a.unread;
|
|
|
|
else
|
|
|
|
return a.title.compareTo(b.title);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class FeedTitleComparator implements Comparator<Feed> {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int compare(Feed a, Feed b) {
|
2012-09-18 06:03:16 +00:00
|
|
|
if (a.is_cat && b.is_cat)
|
|
|
|
return a.title.compareTo(b.title);
|
|
|
|
else if (a.is_cat && !b.is_cat)
|
|
|
|
return -1;
|
|
|
|
else if (!a.is_cat && b.is_cat)
|
|
|
|
return 1;
|
|
|
|
else if (a.id >= 0 && b.id >= 0)
|
2011-11-28 06:14:52 +00:00
|
|
|
return a.title.compareTo(b.title);
|
|
|
|
else
|
2012-09-18 06:03:16 +00:00
|
|
|
return a.id - b.id;
|
2011-11-26 06:31:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2012-03-08 15:29:27 +00:00
|
|
|
|
|
|
|
class FeedOrderComparator implements Comparator<Feed> {
|
|
|
|
|
|
|
|
@Override
|
2012-09-18 06:03:16 +00:00
|
|
|
public int compare(Feed a, Feed b) {
|
2012-03-08 15:29:27 +00:00
|
|
|
if (a.id >= 0 && b.id >= 0)
|
2012-09-18 06:03:16 +00:00
|
|
|
if (a.is_cat && b.is_cat)
|
|
|
|
return a.title.compareTo(b.title);
|
|
|
|
else if (a.is_cat && !b.is_cat)
|
|
|
|
return -1;
|
|
|
|
else if (!a.is_cat && b.is_cat)
|
|
|
|
return 1;
|
|
|
|
else if (a.order_id != 0 && b.order_id != 0)
|
2012-03-08 15:29:27 +00:00
|
|
|
return a.order_id - b.order_id;
|
|
|
|
else
|
|
|
|
return a.title.compareTo(b.title);
|
|
|
|
else
|
|
|
|
return a.id - b.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2012-09-16 09:25:28 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onContextItemSelected(MenuItem item) {
|
|
|
|
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
|
|
|
|
.getMenuInfo();
|
|
|
|
switch (item.getItemId()) {
|
2012-09-19 12:59:08 +00:00
|
|
|
case R.id.browse_articles:
|
|
|
|
if (true) {
|
|
|
|
Feed feed = getFeedAtPosition(info.position);
|
|
|
|
if (feed != null) {
|
|
|
|
m_activity.openFeedArticles(feed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case R.id.browse_headlines:
|
|
|
|
if (true) {
|
|
|
|
Feed feed = getFeedAtPosition(info.position);
|
|
|
|
if (feed != null) {
|
|
|
|
m_activity.onFeedSelected(feed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2012-09-24 18:10:01 +00:00
|
|
|
case R.id.browse_feeds:
|
|
|
|
if (true) {
|
|
|
|
Feed feed = getFeedAtPosition(info.position);
|
|
|
|
if (feed != null) {
|
|
|
|
m_activity.onCatSelected(new FeedCategory(feed.id, feed.title, feed.unread), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2012-09-16 09:25:28 +00:00
|
|
|
case R.id.catchup_feed:
|
|
|
|
if (true) {
|
|
|
|
Feed feed = getFeedAtPosition(info.position);
|
|
|
|
if (feed != null) {
|
|
|
|
m_activity.catchupFeed(feed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
default:
|
|
|
|
Log.d(TAG, "onContextItemSelected, unhandled id=" + item.getItemId());
|
|
|
|
return super.onContextItemSelected(item);
|
|
|
|
}
|
|
|
|
}
|
2012-03-08 15:29:27 +00:00
|
|
|
|
2011-11-29 12:40:23 +00:00
|
|
|
@Override
|
|
|
|
public void onCreateContextMenu(ContextMenu menu, View v,
|
|
|
|
ContextMenuInfo menuInfo) {
|
|
|
|
|
|
|
|
getActivity().getMenuInflater().inflate(R.menu.feed_menu, menu);
|
2011-11-29 17:00:37 +00:00
|
|
|
|
|
|
|
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
|
|
|
|
Feed feed = m_adapter.getItem(info.position);
|
|
|
|
|
|
|
|
if (feed != null)
|
|
|
|
menu.setHeaderTitle(feed.title);
|
|
|
|
|
2012-09-19 12:59:08 +00:00
|
|
|
if (!m_activity.isSmallScreen()) {
|
2012-09-19 21:39:38 +00:00
|
|
|
menu.findItem(R.id.browse_articles).setVisible(false);
|
2012-09-19 12:59:08 +00:00
|
|
|
}
|
|
|
|
|
2012-09-24 18:10:01 +00:00
|
|
|
if (!feed.is_cat) {
|
|
|
|
menu.findItem(R.id.browse_feeds).setVisible(false);
|
|
|
|
}
|
|
|
|
|
2011-11-29 12:40:23 +00:00
|
|
|
super.onCreateContextMenu(menu, v, menuInfo);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
2011-11-23 16:38:21 +00:00
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
if (savedInstanceState != null) {
|
2012-06-19 15:01:28 +00:00
|
|
|
m_selectedFeed = savedInstanceState.getParcelable("selectedFeed");
|
2011-11-25 10:13:10 +00:00
|
|
|
m_feeds = savedInstanceState.getParcelable("feeds");
|
2011-11-30 06:30:19 +00:00
|
|
|
m_feedIconsChecked = savedInstanceState.getBoolean("feedIconsChecked");
|
2012-06-19 15:01:28 +00:00
|
|
|
m_activeCategory = savedInstanceState.getParcelable("activeCat");
|
2011-09-08 11:28:38 +00:00
|
|
|
}
|
2011-09-09 16:36:09 +00:00
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
View view = inflater.inflate(R.layout.feeds_fragment, container, false);
|
2011-11-22 14:30:09 +00:00
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
ListView list = (ListView)view.findViewById(R.id.feeds);
|
|
|
|
m_adapter = new FeedListAdapter(getActivity(), R.layout.feeds_row, (ArrayList<Feed>)m_feeds);
|
|
|
|
list.setAdapter(m_adapter);
|
2011-12-06 11:29:57 +00:00
|
|
|
//list.setEmptyView(view.findViewById(R.id.no_feeds));
|
2011-11-23 10:55:05 +00:00
|
|
|
list.setOnItemClickListener(this);
|
2011-11-25 10:13:10 +00:00
|
|
|
|
2011-11-29 17:00:37 +00:00
|
|
|
registerForContextMenu(list);
|
2011-11-29 12:40:23 +00:00
|
|
|
|
2011-11-28 14:20:56 +00:00
|
|
|
m_enableFeedIcons = m_prefs.getBoolean("download_feed_icons", false);
|
2011-11-28 13:00:36 +00:00
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2011-09-09 06:02:31 +00:00
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
|
|
|
super.onDestroy();
|
2011-09-08 12:45:08 +00:00
|
|
|
}
|
2011-09-09 16:36:09 +00:00
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
@Override
|
|
|
|
public void onAttach(Activity activity) {
|
2011-09-08 16:05:00 +00:00
|
|
|
super.onAttach(activity);
|
2011-11-23 16:38:21 +00:00
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
|
2011-11-28 14:20:56 +00:00
|
|
|
m_prefs.registerOnSharedPreferenceChangeListener(this);
|
|
|
|
|
2012-09-16 09:25:28 +00:00
|
|
|
m_activity = (FeedsActivity)activity;
|
2011-09-08 11:28:38 +00:00
|
|
|
}
|
|
|
|
|
2012-09-16 09:25:28 +00:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
|
|
|
|
|
|
|
refresh(false);
|
|
|
|
|
|
|
|
m_activity.initMenu();
|
|
|
|
}
|
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
@Override
|
2011-09-08 11:28:38 +00:00
|
|
|
public void onSaveInstanceState (Bundle out) {
|
|
|
|
super.onSaveInstanceState(out);
|
2011-09-09 16:36:09 +00:00
|
|
|
|
2012-06-19 15:01:28 +00:00
|
|
|
out.putParcelable("selectedFeed", m_selectedFeed);
|
2011-11-25 10:13:10 +00:00
|
|
|
out.putParcelable("feeds", m_feeds);
|
2011-11-30 06:30:19 +00:00
|
|
|
out.putBoolean("feedIconsChecked", m_feedIconsChecked);
|
2012-06-19 15:01:28 +00:00
|
|
|
out.putParcelable("activeCat", m_activeCategory);
|
2011-11-23 12:14:41 +00:00
|
|
|
}
|
|
|
|
|
2011-09-08 12:45:08 +00:00
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView<?> av, View view, int position, long id) {
|
2011-11-23 10:55:05 +00:00
|
|
|
ListView list = (ListView)av;
|
|
|
|
|
|
|
|
if (list != null) {
|
|
|
|
Feed feed = (Feed)list.getItemAtPosition(position);
|
2013-01-06 20:54:24 +00:00
|
|
|
|
2012-09-18 06:03:16 +00:00
|
|
|
if (feed.is_cat) {
|
2012-09-20 05:40:32 +00:00
|
|
|
if (m_activity.isSmallScreen() && "ARTICLES".equals(m_prefs.getString("default_view_mode", "HEADLINES")) &&
|
2012-09-20 05:14:34 +00:00
|
|
|
m_prefs.getBoolean("browse_cats_like_feeds", false)) {
|
|
|
|
|
|
|
|
m_activity.openFeedArticles(feed);
|
|
|
|
|
2012-09-20 05:40:32 +00:00
|
|
|
} else {
|
2012-09-24 18:10:01 +00:00
|
|
|
m_activity.onCatSelected(new FeedCategory(feed.id, feed.title, feed.unread));
|
2012-09-20 05:14:34 +00:00
|
|
|
}
|
2012-09-19 12:59:08 +00:00
|
|
|
} else {
|
|
|
|
if ("ARTICLES".equals(m_prefs.getString("default_view_mode", "HEADLINES"))) {
|
|
|
|
m_activity.openFeedArticles(feed);
|
|
|
|
} else {
|
|
|
|
m_activity.onFeedSelected(feed);
|
|
|
|
}
|
2012-09-18 06:03:16 +00:00
|
|
|
}
|
2012-06-22 11:05:50 +00:00
|
|
|
|
2012-09-16 09:25:28 +00:00
|
|
|
if (!m_activity.isSmallScreen())
|
2012-06-22 11:05:50 +00:00
|
|
|
m_selectedFeed = feed;
|
|
|
|
|
2011-11-24 10:01:51 +00:00
|
|
|
m_adapter.notifyDataSetChanged();
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
2011-11-22 14:30:09 +00:00
|
|
|
}
|
|
|
|
|
2011-11-27 13:20:27 +00:00
|
|
|
@SuppressWarnings({ "unchecked", "serial" })
|
2011-11-27 10:18:04 +00:00
|
|
|
public void refresh(boolean background) {
|
2012-06-19 15:01:28 +00:00
|
|
|
//FeedCategory cat = m_onlineServices.getActiveCategory();
|
2011-11-29 05:25:13 +00:00
|
|
|
|
2012-09-19 12:01:31 +00:00
|
|
|
m_activity.setProgressBarVisibility(true);
|
|
|
|
|
2012-06-19 15:01:28 +00:00
|
|
|
final int catId = (m_activeCategory != null) ? m_activeCategory.id : -4;
|
2011-11-22 14:30:09 +00:00
|
|
|
|
2012-09-16 09:25:28 +00:00
|
|
|
final String sessionId = m_activity.getSessionId();
|
|
|
|
final boolean unreadOnly = m_activity.getUnreadOnly();
|
2011-11-29 05:25:13 +00:00
|
|
|
|
|
|
|
FeedsRequest req = new FeedsRequest(getActivity().getApplicationContext(), catId);
|
2011-11-28 17:14:50 +00:00
|
|
|
|
2011-11-23 17:19:35 +00:00
|
|
|
if (sessionId != null) {
|
2012-09-21 06:56:49 +00:00
|
|
|
m_activity.setLoadingStatus(R.string.blank, true);
|
2011-11-27 09:57:34 +00:00
|
|
|
|
2011-11-23 17:19:35 +00:00
|
|
|
HashMap<String,String> map = new HashMap<String,String>() {
|
|
|
|
{
|
|
|
|
put("op", "getFeeds");
|
|
|
|
put("sid", sessionId);
|
2012-09-18 06:03:16 +00:00
|
|
|
put("include_nested", "true");
|
2011-11-28 17:14:50 +00:00
|
|
|
put("cat_id", String.valueOf(catId));
|
2011-11-24 05:59:11 +00:00
|
|
|
if (unreadOnly) {
|
|
|
|
put("unread_only", String.valueOf(unreadOnly));
|
|
|
|
}
|
2011-11-23 17:19:35 +00:00
|
|
|
}
|
|
|
|
};
|
2011-11-23 10:55:05 +00:00
|
|
|
|
2011-11-27 07:18:54 +00:00
|
|
|
req.execute(map);
|
2011-11-23 17:19:35 +00:00
|
|
|
|
|
|
|
}
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
|
|
|
|
2012-09-21 06:56:49 +00:00
|
|
|
/* private void setLoadingStatus(int status, boolean showProgress) {
|
2011-11-27 09:57:34 +00:00
|
|
|
if (getView() != null) {
|
|
|
|
TextView tv = (TextView)getView().findViewById(R.id.loading_message);
|
|
|
|
|
|
|
|
if (tv != null) {
|
|
|
|
tv.setText(status);
|
|
|
|
}
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
2012-06-19 20:44:37 +00:00
|
|
|
|
2012-06-20 09:05:09 +00:00
|
|
|
if (getActivity() != null)
|
|
|
|
getActivity().setProgressBarIndeterminateVisibility(showProgress);
|
2012-09-21 06:56:49 +00:00
|
|
|
} */
|
2011-11-23 10:55:05 +00:00
|
|
|
|
2011-11-28 13:00:36 +00:00
|
|
|
@SuppressWarnings({ "unchecked", "serial" })
|
|
|
|
public void getFeedIcons() {
|
|
|
|
|
|
|
|
ApiRequest req = new ApiRequest(getActivity().getApplicationContext()) {
|
|
|
|
protected void onPostExecute(JsonElement result) {
|
2012-10-09 05:54:43 +00:00
|
|
|
if (isDetached()) return;
|
|
|
|
|
2011-11-28 13:00:36 +00:00
|
|
|
if (result != null) {
|
|
|
|
|
|
|
|
try {
|
2011-11-29 04:03:38 +00:00
|
|
|
JsonElement iconsUrl = result.getAsJsonObject().get("icons_dir");
|
2011-11-28 13:00:36 +00:00
|
|
|
|
|
|
|
if (iconsUrl != null) {
|
|
|
|
String iconsStr = iconsUrl.getAsString();
|
|
|
|
String baseUrl = "";
|
|
|
|
|
|
|
|
if (!iconsStr.contains("://")) {
|
|
|
|
baseUrl = m_prefs.getString("ttrss_url", "") + "/" + iconsStr;
|
|
|
|
} else {
|
|
|
|
baseUrl = iconsStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
GetIconsTask git = new GetIconsTask(baseUrl);
|
|
|
|
git.execute(m_feeds);
|
2011-11-30 06:30:19 +00:00
|
|
|
|
|
|
|
m_feedIconsChecked = true;
|
2011-11-28 13:00:36 +00:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.d(TAG, "Error receiving icons configuration");
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-09-16 09:25:28 +00:00
|
|
|
final String sessionId = m_activity.getSessionId();
|
2011-11-28 13:00:36 +00:00
|
|
|
|
|
|
|
HashMap<String,String> map = new HashMap<String,String>() {
|
|
|
|
{
|
|
|
|
put("sid", sessionId);
|
|
|
|
put("op", "getConfig");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
req.execute(map);
|
|
|
|
}
|
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
private class FeedsRequest extends ApiRequest {
|
2011-11-29 05:25:13 +00:00
|
|
|
private int m_catId;
|
2011-11-23 10:55:05 +00:00
|
|
|
|
2011-11-29 05:25:13 +00:00
|
|
|
public FeedsRequest(Context context, int catId) {
|
2011-11-27 10:18:04 +00:00
|
|
|
super(context);
|
2011-11-29 05:25:13 +00:00
|
|
|
m_catId = catId;
|
2011-11-27 10:18:04 +00:00
|
|
|
}
|
2011-11-29 04:03:38 +00:00
|
|
|
|
2012-09-19 12:01:31 +00:00
|
|
|
@Override
|
|
|
|
protected void onProgressUpdate(Integer... progress) {
|
|
|
|
m_activity.setProgress(Math.round((((float)progress[0] / (float)progress[1]) * 10000)));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-11-27 10:18:04 +00:00
|
|
|
protected void onPostExecute(JsonElement result) {
|
2012-10-09 05:54:43 +00:00
|
|
|
if (isDetached()) return;
|
2013-02-27 06:39:23 +00:00
|
|
|
|
|
|
|
if (getView() != null) {
|
|
|
|
ListView list = (ListView)getView().findViewById(R.id.feeds);
|
|
|
|
|
|
|
|
if (list != null) {
|
|
|
|
list.setEmptyView(getView().findViewById(R.id.no_feeds));
|
|
|
|
}
|
|
|
|
}
|
2012-10-09 05:54:43 +00:00
|
|
|
|
2012-09-19 12:01:31 +00:00
|
|
|
m_activity.setProgressBarVisibility(false);
|
|
|
|
|
2011-11-27 10:18:04 +00:00
|
|
|
if (result != null) {
|
|
|
|
try {
|
2011-11-29 04:03:38 +00:00
|
|
|
JsonArray content = result.getAsJsonArray();
|
|
|
|
if (content != null) {
|
2011-11-27 10:18:04 +00:00
|
|
|
|
2011-11-29 04:03:38 +00:00
|
|
|
Type listType = new TypeToken<List<Feed>>() {}.getType();
|
|
|
|
final List<Feed> feeds = new Gson().fromJson(content, listType);
|
|
|
|
|
|
|
|
m_feeds.clear();
|
|
|
|
|
|
|
|
for (Feed f : feeds)
|
2011-11-29 05:25:13 +00:00
|
|
|
if (f.id > -10 || m_catId != -4) // skip labels for flat feedlist for now
|
2011-11-29 04:03:38 +00:00
|
|
|
m_feeds.add(f);
|
|
|
|
|
|
|
|
sortFeeds();
|
|
|
|
|
2012-09-21 06:56:49 +00:00
|
|
|
/*if (m_feeds.size() == 0)
|
2011-12-06 11:29:57 +00:00
|
|
|
setLoadingStatus(R.string.no_feeds_to_display, false);
|
2012-09-21 06:56:49 +00:00
|
|
|
else */
|
|
|
|
|
|
|
|
m_activity.setLoadingStatus(R.string.blank, false);
|
2013-02-27 06:45:12 +00:00
|
|
|
//m_adapter.notifyDataSetChanged(); (done by sortFeeds)
|
2013-02-27 06:39:23 +00:00
|
|
|
|
2011-12-07 11:59:30 +00:00
|
|
|
if (m_enableFeedIcons && !m_feedIconsChecked &&
|
|
|
|
Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
|
|
|
|
getFeedIcons();
|
2011-11-29 04:03:38 +00:00
|
|
|
|
|
|
|
return;
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
2011-11-29 04:03:38 +00:00
|
|
|
|
2011-11-27 10:18:04 +00:00
|
|
|
} catch (Exception e) {
|
2011-11-29 04:03:38 +00:00
|
|
|
e.printStackTrace();
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
2011-11-29 04:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_lastError == ApiError.LOGIN_FAILED) {
|
2012-09-21 06:44:30 +00:00
|
|
|
m_activity.login(true);
|
2011-11-27 10:18:04 +00:00
|
|
|
} else {
|
2012-09-21 06:56:49 +00:00
|
|
|
m_activity.setLoadingStatus(getErrorMessage(), false);
|
2011-11-27 10:18:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-11-23 10:55:05 +00:00
|
|
|
|
|
|
|
private class FeedListAdapter extends ArrayAdapter<Feed> {
|
|
|
|
private ArrayList<Feed> items;
|
|
|
|
|
2011-11-24 10:01:51 +00:00
|
|
|
public static final int VIEW_NORMAL = 0;
|
|
|
|
public static final int VIEW_SELECTED = 1;
|
|
|
|
|
|
|
|
public static final int VIEW_COUNT = VIEW_SELECTED+1;
|
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
public FeedListAdapter(Context context, int textViewResourceId, ArrayList<Feed> items) {
|
|
|
|
super(context, textViewResourceId, items);
|
|
|
|
this.items = items;
|
|
|
|
}
|
|
|
|
|
2011-11-24 10:01:51 +00:00
|
|
|
public int getViewTypeCount() {
|
|
|
|
return VIEW_COUNT;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getItemViewType(int position) {
|
|
|
|
Feed feed = items.get(position);
|
2011-11-23 16:38:21 +00:00
|
|
|
|
2012-09-16 09:25:28 +00:00
|
|
|
if (!m_activity.isSmallScreen() && m_selectedFeed != null && feed.id == m_selectedFeed.id) {
|
2011-11-24 10:01:51 +00:00
|
|
|
return VIEW_SELECTED;
|
|
|
|
} else {
|
|
|
|
return VIEW_NORMAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View getView(int position, View convertView, ViewGroup parent) {
|
2011-11-23 10:55:05 +00:00
|
|
|
View v = convertView;
|
|
|
|
|
|
|
|
Feed feed = items.get(position);
|
|
|
|
|
|
|
|
if (v == null) {
|
2011-11-24 10:01:51 +00:00
|
|
|
int layoutId = R.layout.feeds_row;
|
|
|
|
|
|
|
|
switch (getItemViewType(position)) {
|
|
|
|
case VIEW_SELECTED:
|
|
|
|
layoutId = R.layout.feeds_row_selected;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
LayoutInflater vi = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
2011-11-24 10:01:51 +00:00
|
|
|
v = vi.inflate(layoutId, null);
|
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TextView tt = (TextView) v.findViewById(R.id.title);
|
|
|
|
|
|
|
|
if (tt != null) {
|
|
|
|
tt.setText(feed.title);
|
|
|
|
}
|
|
|
|
|
|
|
|
TextView tu = (TextView) v.findViewById(R.id.unread_counter);
|
|
|
|
|
|
|
|
if (tu != null) {
|
|
|
|
tu.setText(String.valueOf(feed.unread));
|
|
|
|
tu.setVisibility((feed.unread > 0) ? View.VISIBLE : View.INVISIBLE);
|
2011-11-28 11:25:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ImageView icon = (ImageView)v.findViewById(R.id.icon);
|
|
|
|
|
|
|
|
if (icon != null) {
|
2011-11-28 13:00:36 +00:00
|
|
|
|
|
|
|
if (m_enableFeedIcons) {
|
|
|
|
|
2012-10-14 12:37:20 +00:00
|
|
|
File storage = m_activity.getExternalCacheDir();
|
2011-11-28 13:00:36 +00:00
|
|
|
|
|
|
|
File iconFile = new File(storage.getAbsolutePath() + ICON_PATH + feed.id + ".ico");
|
|
|
|
if (iconFile.exists()) {
|
|
|
|
Bitmap bmpOrig = BitmapFactory.decodeFile(iconFile.getAbsolutePath());
|
|
|
|
if (bmpOrig != null) {
|
2011-11-29 04:40:50 +00:00
|
|
|
icon.setImageBitmap(bmpOrig);
|
2011-11-28 13:00:36 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
icon.setImageResource(feed.unread > 0 ? R.drawable.ic_rss : R.drawable.ic_rss_bw);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
icon.setImageResource(feed.unread > 0 ? R.drawable.ic_rss : R.drawable.ic_rss_bw);
|
|
|
|
}
|
|
|
|
|
2011-11-28 11:25:55 +00:00
|
|
|
}
|
2011-11-23 10:55:05 +00:00
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
2011-11-22 14:30:09 +00:00
|
|
|
}
|
2011-11-26 06:31:14 +00:00
|
|
|
|
|
|
|
public void sortFeeds() {
|
|
|
|
Comparator<Feed> cmp;
|
|
|
|
|
|
|
|
if (m_prefs.getBoolean("sort_feeds_by_unread", false)) {
|
|
|
|
cmp = new FeedUnreadComparator();
|
|
|
|
} else {
|
2012-09-16 09:25:28 +00:00
|
|
|
if (m_activity.getApiLevel() >= 3) {
|
2012-03-08 15:29:27 +00:00
|
|
|
cmp = new FeedOrderComparator();
|
|
|
|
} else {
|
|
|
|
cmp = new FeedTitleComparator();
|
|
|
|
}
|
2011-11-26 06:31:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Collections.sort(m_feeds, cmp);
|
2012-09-26 05:51:16 +00:00
|
|
|
|
|
|
|
try {
|
2013-02-27 06:45:12 +00:00
|
|
|
m_adapter.notifyDataSetChanged();
|
2012-09-26 05:51:16 +00:00
|
|
|
} catch (NullPointerException e) {
|
|
|
|
// adapter missing
|
|
|
|
}
|
2011-11-26 06:31:14 +00:00
|
|
|
}
|
2011-11-28 13:00:36 +00:00
|
|
|
|
|
|
|
public class GetIconsTask extends AsyncTask<FeedList, Integer, Integer> {
|
|
|
|
|
|
|
|
private String m_baseUrl;
|
|
|
|
|
|
|
|
public GetIconsTask(String baseUrl) {
|
|
|
|
m_baseUrl = baseUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Integer doInBackground(FeedList... params) {
|
|
|
|
|
|
|
|
try {
|
2012-10-14 12:37:20 +00:00
|
|
|
File storage = m_activity.getExternalCacheDir();
|
2011-11-28 13:00:36 +00:00
|
|
|
final File iconPath = new File(storage.getAbsolutePath() + ICON_PATH);
|
2011-11-28 14:20:56 +00:00
|
|
|
if (!iconPath.exists()) iconPath.mkdirs();
|
2011-11-28 13:00:36 +00:00
|
|
|
|
|
|
|
if (iconPath.exists()) {
|
2011-11-28 14:20:56 +00:00
|
|
|
for (Feed feed : params[0]) {
|
2012-09-18 06:06:30 +00:00
|
|
|
if (feed.id > 0 && feed.has_icon && !feed.is_cat) {
|
2011-11-28 14:20:56 +00:00
|
|
|
File outputFile = new File(iconPath.getAbsolutePath() + "/" + feed.id + ".ico");
|
2011-11-28 13:00:36 +00:00
|
|
|
String fetchUrl = m_baseUrl + "/" + feed.id + ".ico";
|
|
|
|
|
2011-11-28 14:20:56 +00:00
|
|
|
if (!outputFile.exists()) {
|
|
|
|
downloadFile(fetchUrl, outputFile.getAbsolutePath());
|
|
|
|
Thread.sleep(2000);
|
2011-11-28 13:00:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.d(TAG, "Error while downloading feed icons");
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2011-11-28 14:20:56 +00:00
|
|
|
protected void downloadFile(String fetchUrl, String outputFile) {
|
2012-04-30 11:24:25 +00:00
|
|
|
AndroidHttpClient client = AndroidHttpClient.newInstance("Tiny Tiny RSS");
|
2011-11-28 14:20:56 +00:00
|
|
|
|
2012-09-25 09:47:04 +00:00
|
|
|
/* ApiRequest.disableConnectionReuseIfNecessary(); */
|
2012-09-18 09:08:57 +00:00
|
|
|
|
2012-09-25 09:47:04 +00:00
|
|
|
/* ApiRequest.trustAllHosts(m_prefs.getBoolean("ssl_trust_any", false),
|
|
|
|
m_prefs.getBoolean("ssl_trust_any_host", false)); */
|
2011-11-28 14:20:56 +00:00
|
|
|
|
2012-09-18 09:08:57 +00:00
|
|
|
try {
|
|
|
|
URL url = new URL(fetchUrl);
|
|
|
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
2011-11-28 14:20:56 +00:00
|
|
|
|
2012-09-18 09:08:57 +00:00
|
|
|
String httpLogin = m_prefs.getString("http_login", "");
|
|
|
|
String httpPassword = m_prefs.getString("http_password", "");
|
2011-11-28 14:20:56 +00:00
|
|
|
|
2012-09-18 09:08:57 +00:00
|
|
|
if (httpLogin.length() > 0) {
|
|
|
|
conn.setRequestProperty("Authorization", "Basic " +
|
2012-09-20 11:01:38 +00:00
|
|
|
Base64.encodeToString((httpLogin + ":" + httpPassword).getBytes("UTF-8"), Base64.NO_WRAP));
|
2012-09-18 09:08:57 +00:00
|
|
|
}
|
2011-11-28 14:20:56 +00:00
|
|
|
|
2012-09-18 09:08:57 +00:00
|
|
|
InputStream content = conn.getInputStream();
|
2011-11-28 14:20:56 +00:00
|
|
|
|
|
|
|
BufferedInputStream is = new BufferedInputStream(content, 1024);
|
|
|
|
FileOutputStream fos = new FileOutputStream(outputFile);
|
|
|
|
|
|
|
|
byte[] buffer = new byte[1024];
|
|
|
|
int len = 0;
|
|
|
|
while ((len = is.read(buffer)) != -1) {
|
|
|
|
fos.write(buffer, 0, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
fos.close();
|
|
|
|
is.close();
|
2012-05-02 04:35:53 +00:00
|
|
|
|
2011-11-28 14:20:56 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2012-05-05 18:07:40 +00:00
|
|
|
|
|
|
|
client.close();
|
2011-11-28 14:20:56 +00:00
|
|
|
}
|
|
|
|
|
2011-11-28 13:00:36 +00:00
|
|
|
protected void onPostExecute(Integer result) {
|
2012-10-09 05:54:43 +00:00
|
|
|
if (isDetached()) return;
|
|
|
|
|
2013-02-27 06:45:12 +00:00
|
|
|
m_adapter.notifyDataSetChanged();
|
2011-11-28 13:00:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2011-11-28 14:20:56 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
|
|
|
|
String key) {
|
|
|
|
|
|
|
|
sortFeeds();
|
|
|
|
m_enableFeedIcons = m_prefs.getBoolean("download_feed_icons", false);
|
|
|
|
|
|
|
|
}
|
2011-11-29 17:00:37 +00:00
|
|
|
|
|
|
|
public Feed getFeedAtPosition(int position) {
|
2012-01-19 05:44:27 +00:00
|
|
|
try {
|
|
|
|
return m_adapter.getItem(position);
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
return null;
|
|
|
|
}
|
2011-11-29 17:00:37 +00:00
|
|
|
}
|
2012-01-19 10:54:14 +00:00
|
|
|
|
2012-06-19 15:01:28 +00:00
|
|
|
public Feed getSelectedFeed() {
|
|
|
|
return m_selectedFeed;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setSelectedFeed(Feed feed) {
|
2012-06-24 11:17:26 +00:00
|
|
|
m_selectedFeed = feed;
|
|
|
|
m_adapter.notifyDataSetChanged();
|
2012-01-19 10:54:14 +00:00
|
|
|
}
|
|
|
|
|
2011-09-08 11:28:38 +00:00
|
|
|
}
|