2011-09-08 12:45:08 +00:00
|
|
|
package org.fox.ttrss;
|
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
import java.lang.reflect.Type;
|
2011-11-24 10:01:51 +00:00
|
|
|
import java.text.DateFormat;
|
|
|
|
import java.text.SimpleDateFormat;
|
2011-11-23 10:55:05 +00:00
|
|
|
import java.util.ArrayList;
|
2011-11-24 10:01:51 +00:00
|
|
|
import java.util.Date;
|
2011-11-23 10:55:05 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
2011-11-25 08:47:07 +00:00
|
|
|
import java.util.TimeZone;
|
2011-11-23 10:55:05 +00:00
|
|
|
|
|
|
|
import org.jsoup.Jsoup;
|
|
|
|
|
2011-09-08 12:45:08 +00:00
|
|
|
import android.app.Activity;
|
2011-11-23 10:55:05 +00:00
|
|
|
import android.content.Context;
|
2011-11-30 13:05:03 +00:00
|
|
|
import android.content.SharedPreferences;
|
2011-12-04 07:50:49 +00:00
|
|
|
import android.graphics.drawable.BitmapDrawable;
|
|
|
|
import android.graphics.drawable.Drawable;
|
2011-09-08 12:45:08 +00:00
|
|
|
import android.os.Bundle;
|
2011-11-30 13:05:03 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2011-11-25 18:37:02 +00:00
|
|
|
import android.support.v4.app.Fragment;
|
2011-11-27 07:32:01 +00:00
|
|
|
import android.text.Html;
|
2011-12-04 07:50:49 +00:00
|
|
|
import android.text.Html.ImageGetter;
|
2011-11-30 18:44:36 +00:00
|
|
|
import android.text.method.LinkMovementMethod;
|
2011-11-24 11:51:23 +00:00
|
|
|
import android.util.Log;
|
2011-11-29 17:00:37 +00:00
|
|
|
import android.view.ContextMenu;
|
2011-12-03 18:34:40 +00:00
|
|
|
import android.view.ContextMenu.ContextMenuInfo;
|
2011-09-08 12:45:08 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
2011-11-24 14:57:44 +00:00
|
|
|
import android.view.View.OnClickListener;
|
2011-09-08 12:45:08 +00:00
|
|
|
import android.view.ViewGroup;
|
2011-11-30 11:45:23 +00:00
|
|
|
import android.widget.AbsListView;
|
|
|
|
import android.widget.AbsListView.OnScrollListener;
|
2011-09-09 11:58:11 +00:00
|
|
|
import android.widget.AdapterView;
|
2011-11-29 17:00:37 +00:00
|
|
|
import android.widget.AdapterView.AdapterContextMenuInfo;
|
2011-09-09 11:58:11 +00:00
|
|
|
import android.widget.AdapterView.OnItemClickListener;
|
2011-11-23 10:55:05 +00:00
|
|
|
import android.widget.ArrayAdapter;
|
2011-11-24 11:51:23 +00:00
|
|
|
import android.widget.CheckBox;
|
2011-11-24 14:57:44 +00:00
|
|
|
import android.widget.ImageView;
|
2011-09-09 09:50:15 +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 12:45:08 +00:00
|
|
|
|
2011-11-30 11:45:23 +00:00
|
|
|
public class HeadlinesFragment extends Fragment implements OnItemClickListener, OnScrollListener {
|
2011-11-29 11:09:27 +00:00
|
|
|
public static enum ArticlesSelection { ALL, NONE, UNREAD };
|
|
|
|
|
2011-11-30 11:45:23 +00:00
|
|
|
public static final int HEADLINES_REQUEST_SIZE = 30;
|
|
|
|
public static final int HEADLINES_BUFFER_MAX = 500;
|
|
|
|
|
2011-09-08 12:45:08 +00:00
|
|
|
private final String TAG = this.getClass().getSimpleName();
|
2011-09-09 09:50:15 +00:00
|
|
|
|
2011-11-23 16:38:21 +00:00
|
|
|
private Feed m_feed;
|
2011-11-27 13:11:39 +00:00
|
|
|
private int m_activeArticleId;
|
2011-11-30 11:45:23 +00:00
|
|
|
private boolean m_refreshInProgress = false;
|
|
|
|
private boolean m_canLoadMore = false;
|
2011-11-30 13:05:03 +00:00
|
|
|
private boolean m_combinedMode = true;
|
2012-01-19 10:05:48 +00:00
|
|
|
private String m_searchQuery = "";
|
2011-11-30 13:05:03 +00:00
|
|
|
|
|
|
|
private SharedPreferences m_prefs;
|
2011-11-23 10:55:05 +00:00
|
|
|
|
|
|
|
private ArticleListAdapter m_adapter;
|
2011-11-25 10:32:32 +00:00
|
|
|
private ArticleList m_articles = new ArticleList();
|
2011-11-25 11:03:14 +00:00
|
|
|
private ArticleList m_selectedArticles = new ArticleList();
|
|
|
|
|
2011-12-06 12:52:33 +00:00
|
|
|
private OnlineServices m_onlineServices;
|
2011-11-23 10:55:05 +00:00
|
|
|
|
2011-12-04 07:50:49 +00:00
|
|
|
private ImageGetter m_dummyGetter = new ImageGetter() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Drawable getDrawable(String source) {
|
|
|
|
return new BitmapDrawable();
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2011-11-27 13:11:39 +00:00
|
|
|
public ArticleList getSelectedArticles() {
|
|
|
|
return m_selectedArticles;
|
2011-11-23 16:38:21 +00:00
|
|
|
}
|
2011-11-25 10:32:32 +00:00
|
|
|
|
2011-11-29 17:00:37 +00:00
|
|
|
@Override
|
|
|
|
public void onCreateContextMenu(ContextMenu menu, View v,
|
|
|
|
ContextMenuInfo menuInfo) {
|
|
|
|
|
|
|
|
getActivity().getMenuInflater().inflate(R.menu.headlines_menu, menu);
|
|
|
|
|
|
|
|
if (m_selectedArticles.size() > 0) {
|
|
|
|
menu.setHeaderTitle(R.string.headline_context_multiple);
|
2011-11-30 18:44:36 +00:00
|
|
|
menu.setGroupVisible(R.id.menu_group_single_article, false);
|
2011-11-29 17:00:37 +00:00
|
|
|
} else {
|
2011-11-30 08:54:42 +00:00
|
|
|
AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo;
|
|
|
|
Article article = getArticleAtPosition(info.position);
|
|
|
|
menu.setHeaderTitle(article.title);
|
2011-11-30 18:44:36 +00:00
|
|
|
menu.setGroupVisible(R.id.menu_group_single_article, true);
|
2011-11-29 17:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
super.onCreateContextMenu(menu, v, menuInfo);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-09-08 12:45:08 +00:00
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
2011-11-30 06:30:19 +00:00
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
if (savedInstanceState != null) {
|
2011-11-25 10:13:10 +00:00
|
|
|
m_feed = savedInstanceState.getParcelable("feed");
|
2011-11-25 10:32:32 +00:00
|
|
|
m_articles = savedInstanceState.getParcelable("articles");
|
2011-11-27 13:11:39 +00:00
|
|
|
m_activeArticleId = savedInstanceState.getInt("activeArticleId");
|
2011-11-25 11:03:14 +00:00
|
|
|
m_selectedArticles = savedInstanceState.getParcelable("selectedArticles");
|
2011-11-30 13:05:03 +00:00
|
|
|
m_canLoadMore = savedInstanceState.getBoolean("canLoadMore");
|
|
|
|
m_combinedMode = savedInstanceState.getBoolean("combinedMode");
|
2012-01-19 10:46:28 +00:00
|
|
|
m_searchQuery = (String) savedInstanceState.getCharSequence("searchQuery");
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
|
|
|
|
2011-09-08 12:45:08 +00:00
|
|
|
View view = inflater.inflate(R.layout.headlines_fragment, container, false);
|
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
ListView list = (ListView)view.findViewById(R.id.headlines);
|
|
|
|
m_adapter = new ArticleListAdapter(getActivity(), R.layout.headlines_row, (ArrayList<Article>)m_articles);
|
|
|
|
list.setAdapter(m_adapter);
|
|
|
|
list.setOnItemClickListener(this);
|
2011-11-30 11:45:23 +00:00
|
|
|
list.setOnScrollListener(this);
|
2011-12-06 11:29:57 +00:00
|
|
|
//list.setEmptyView(view.findViewById(R.id.no_headlines));
|
2011-11-29 17:00:37 +00:00
|
|
|
registerForContextMenu(list);
|
2011-11-27 12:22:03 +00:00
|
|
|
|
2011-11-25 10:13:10 +00:00
|
|
|
Log.d(TAG, "onCreateView, feed=" + m_feed);
|
|
|
|
|
2011-11-25 10:32:32 +00:00
|
|
|
if (m_feed != null && (m_articles == null || m_articles.size() == 0))
|
2011-11-26 06:52:50 +00:00
|
|
|
refresh(false);
|
2011-11-24 06:06:47 +00:00
|
|
|
else
|
2011-11-27 07:18:54 +00:00
|
|
|
view.findViewById(R.id.loading_progress).setVisibility(View.GONE);
|
2011-11-23 20:18:18 +00:00
|
|
|
|
2011-09-08 12:45:08 +00:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAttach(Activity activity) {
|
|
|
|
super.onAttach(activity);
|
2011-11-30 13:05:03 +00:00
|
|
|
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
|
2011-12-06 12:52:33 +00:00
|
|
|
m_onlineServices = (OnlineServices) activity;
|
|
|
|
m_feed = m_onlineServices.getActiveFeed();
|
|
|
|
|
2011-11-30 13:05:03 +00:00
|
|
|
m_combinedMode = m_prefs.getBoolean("combined_mode", false);
|
2011-09-08 12:45:08 +00:00
|
|
|
}
|
|
|
|
|
2011-09-09 11:58:11 +00:00
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView<?> av, View view, int position, long id) {
|
2011-11-23 12:51:15 +00:00
|
|
|
ListView list = (ListView)av;
|
|
|
|
|
2011-11-30 18:44:36 +00:00
|
|
|
Log.d(TAG, "onItemClick=" + position);
|
|
|
|
|
|
|
|
if (list != null) {
|
2011-11-23 12:51:15 +00:00
|
|
|
Article article = (Article)list.getItemAtPosition(position);
|
2011-11-30 11:45:23 +00:00
|
|
|
if (article.id >= 0) {
|
2011-11-30 18:44:36 +00:00
|
|
|
if (m_combinedMode) {
|
|
|
|
article.unread = false;
|
2011-12-06 12:52:33 +00:00
|
|
|
m_onlineServices.saveArticleUnread(article);
|
2011-11-30 18:44:36 +00:00
|
|
|
} else {
|
2011-12-06 12:52:33 +00:00
|
|
|
m_onlineServices.openArticle(article, 0);
|
2011-11-30 18:44:36 +00:00
|
|
|
}
|
|
|
|
|
2011-11-30 11:45:23 +00:00
|
|
|
m_activeArticleId = article.id;
|
|
|
|
m_adapter.notifyDataSetChanged();
|
|
|
|
}
|
2011-11-23 12:51:15 +00:00
|
|
|
}
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
|
|
|
|
2011-11-27 13:20:27 +00:00
|
|
|
@SuppressWarnings({ "unchecked", "serial" })
|
2011-11-26 06:52:50 +00:00
|
|
|
public void refresh(boolean append) {
|
2011-11-30 11:45:23 +00:00
|
|
|
m_refreshInProgress = true;
|
|
|
|
|
2011-11-27 10:18:04 +00:00
|
|
|
HeadlinesRequest req = new HeadlinesRequest(getActivity().getApplicationContext());
|
2011-09-09 11:58:11 +00:00
|
|
|
|
2011-12-06 12:52:33 +00:00
|
|
|
final String sessionId = m_onlineServices.getSessionId();
|
|
|
|
final boolean showUnread = m_onlineServices.getUnreadArticlesOnly();
|
2011-11-29 10:02:16 +00:00
|
|
|
final boolean isCat = m_feed.is_cat;
|
2011-11-26 06:52:50 +00:00
|
|
|
int skip = 0;
|
|
|
|
|
|
|
|
if (append) {
|
|
|
|
for (Article a : m_articles) {
|
|
|
|
if (a.unread) ++skip;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (skip == 0) skip = m_articles.size();
|
2011-11-30 11:45:23 +00:00
|
|
|
} else {
|
|
|
|
setLoadingStatus(R.string.blank, true);
|
2011-11-26 06:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
final int fskip = skip;
|
|
|
|
|
|
|
|
req.setOffset(skip);
|
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
HashMap<String,String> map = new HashMap<String,String>() {
|
|
|
|
{
|
|
|
|
put("op", "getHeadlines");
|
2011-11-24 13:04:58 +00:00
|
|
|
put("sid", sessionId);
|
2011-11-23 16:38:21 +00:00
|
|
|
put("feed_id", String.valueOf(m_feed.id));
|
2011-11-23 10:55:05 +00:00
|
|
|
put("show_content", "true");
|
2011-11-30 11:45:23 +00:00
|
|
|
put("limit", String.valueOf(HEADLINES_REQUEST_SIZE));
|
2011-11-23 10:55:05 +00:00
|
|
|
put("offset", String.valueOf(0));
|
2011-11-26 08:46:17 +00:00
|
|
|
put("view_mode", showUnread ? "adaptive" : "all_articles");
|
2011-11-26 06:52:50 +00:00
|
|
|
put("skip", String.valueOf(fskip));
|
2011-11-29 10:02:16 +00:00
|
|
|
|
|
|
|
if (isCat) put("is_cat", "true");
|
2012-01-19 10:05:48 +00:00
|
|
|
|
|
|
|
if (m_searchQuery.length() != 0) {
|
|
|
|
put("search", m_searchQuery);
|
|
|
|
put("search_mode", "");
|
|
|
|
put("match_on", "both");
|
|
|
|
}
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
req.execute(map);
|
2011-09-09 11:58:11 +00:00
|
|
|
}
|
|
|
|
|
2011-09-10 08:16:59 +00:00
|
|
|
@Override
|
2011-11-23 10:55:05 +00:00
|
|
|
public void onSaveInstanceState (Bundle out) {
|
2011-09-10 08:16:59 +00:00
|
|
|
super.onSaveInstanceState(out);
|
|
|
|
|
2011-11-25 10:13:10 +00:00
|
|
|
out.putParcelable("feed", m_feed);
|
2011-11-25 10:32:32 +00:00
|
|
|
out.putParcelable("articles", m_articles);
|
2011-11-27 13:11:39 +00:00
|
|
|
out.putInt("activeArticleId", m_activeArticleId);
|
2011-11-25 11:03:14 +00:00
|
|
|
out.putParcelable("selectedArticles", m_selectedArticles);
|
2011-11-30 11:45:23 +00:00
|
|
|
out.putBoolean("canLoadMore", m_canLoadMore);
|
2011-11-30 13:05:03 +00:00
|
|
|
out.putBoolean("combinedMode", m_combinedMode);
|
2012-01-19 10:46:28 +00:00
|
|
|
out.putCharSequence("searchQuery", m_searchQuery);
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
|
|
|
|
2011-11-27 07:18:54 +00:00
|
|
|
public 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
View pb = getView().findViewById(R.id.loading_progress);
|
|
|
|
|
|
|
|
if (pb != null) {
|
|
|
|
pb.setVisibility(showProgress ? View.VISIBLE : View.GONE);
|
|
|
|
}
|
2011-11-27 07:18:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
private class HeadlinesRequest extends ApiRequest {
|
2011-11-26 06:52:50 +00:00
|
|
|
int m_offset = 0;
|
2011-11-23 10:55:05 +00:00
|
|
|
|
2011-11-27 10:18:04 +00:00
|
|
|
public HeadlinesRequest(Context context) {
|
|
|
|
super(context);
|
|
|
|
}
|
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
protected void onPostExecute(JsonElement result) {
|
|
|
|
if (result != null) {
|
|
|
|
try {
|
2011-11-29 04:03:38 +00:00
|
|
|
JsonArray content = result.getAsJsonArray();
|
|
|
|
if (content != null) {
|
|
|
|
Type listType = new TypeToken<List<Article>>() {}.getType();
|
|
|
|
final List<Article> articles = new Gson().fromJson(content, listType);
|
|
|
|
|
2011-11-30 11:45:23 +00:00
|
|
|
while (m_articles.size() > HEADLINES_BUFFER_MAX)
|
|
|
|
m_articles.remove(0);
|
|
|
|
|
2011-11-29 04:03:38 +00:00
|
|
|
if (m_offset == 0)
|
|
|
|
m_articles.clear();
|
2011-11-30 11:45:23 +00:00
|
|
|
else
|
|
|
|
m_articles.remove(m_articles.size()-1); // remove previous placeholder
|
2011-11-29 04:03:38 +00:00
|
|
|
|
|
|
|
for (Article f : articles)
|
|
|
|
m_articles.add(f);
|
2011-11-30 11:45:23 +00:00
|
|
|
|
|
|
|
if (articles.size() == HEADLINES_REQUEST_SIZE) {
|
|
|
|
Article placeholder = new Article(-1);
|
|
|
|
m_articles.add(placeholder);
|
|
|
|
|
|
|
|
m_canLoadMore = true;
|
|
|
|
} else {
|
|
|
|
m_canLoadMore = false;
|
|
|
|
}
|
|
|
|
|
2011-11-29 04:03:38 +00:00
|
|
|
m_adapter.notifyDataSetChanged();
|
2011-11-30 11:45:23 +00:00
|
|
|
|
2011-12-06 11:29:57 +00:00
|
|
|
if (m_articles.size() == 0)
|
|
|
|
setLoadingStatus(R.string.no_headlines_to_display, false);
|
|
|
|
else
|
2011-11-29 04:03:38 +00:00
|
|
|
setLoadingStatus(R.string.blank, false);
|
|
|
|
|
2011-11-30 11:45:23 +00:00
|
|
|
m_refreshInProgress = false;
|
|
|
|
|
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-23 10:55:05 +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) {
|
2011-12-06 12:52:33 +00:00
|
|
|
m_onlineServices.login();
|
2011-11-29 04:03:38 +00:00
|
|
|
} else {
|
|
|
|
setLoadingStatus(getErrorMessage(), false);
|
|
|
|
}
|
2011-11-30 11:45:23 +00:00
|
|
|
m_refreshInProgress = false;
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
2011-11-26 06:52:50 +00:00
|
|
|
|
|
|
|
public void setOffset(int skip) {
|
|
|
|
m_offset = skip;
|
|
|
|
}
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private class ArticleListAdapter extends ArrayAdapter<Article> {
|
|
|
|
private ArrayList<Article> items;
|
2011-11-24 10:01:51 +00:00
|
|
|
|
|
|
|
public static final int VIEW_NORMAL = 0;
|
|
|
|
public static final int VIEW_UNREAD = 1;
|
|
|
|
public static final int VIEW_SELECTED = 2;
|
2011-11-30 11:45:23 +00:00
|
|
|
public static final int VIEW_LOADMORE = 3;
|
2011-11-24 10:01:51 +00:00
|
|
|
|
2011-11-30 11:45:23 +00:00
|
|
|
public static final int VIEW_COUNT = VIEW_LOADMORE+1;
|
2011-11-24 11:51:23 +00:00
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
public ArticleListAdapter(Context context, int textViewResourceId, ArrayList<Article> 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) {
|
|
|
|
Article a = items.get(position);
|
|
|
|
|
2011-11-30 11:45:23 +00:00
|
|
|
if (a.id == -1) {
|
|
|
|
return VIEW_LOADMORE;
|
|
|
|
} else if (a.id == m_activeArticleId) {
|
2011-11-24 10:01:51 +00:00
|
|
|
return VIEW_SELECTED;
|
|
|
|
} else if (a.unread) {
|
|
|
|
return VIEW_UNREAD;
|
|
|
|
} else {
|
|
|
|
return VIEW_NORMAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
@Override
|
|
|
|
public View getView(int position, View convertView, ViewGroup parent) {
|
|
|
|
|
|
|
|
View v = convertView;
|
|
|
|
|
2011-11-24 11:51:23 +00:00
|
|
|
final Article article = items.get(position);
|
2011-12-03 18:34:40 +00:00
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
if (v == null) {
|
2011-11-30 10:17:03 +00:00
|
|
|
int layoutId = R.layout.headlines_row;
|
2011-11-24 10:01:51 +00:00
|
|
|
|
|
|
|
switch (getItemViewType(position)) {
|
2011-11-30 11:45:23 +00:00
|
|
|
case VIEW_LOADMORE:
|
|
|
|
layoutId = R.layout.headlines_row_loadmore;
|
|
|
|
break;
|
2011-11-24 10:01:51 +00:00
|
|
|
case VIEW_UNREAD:
|
2011-11-30 10:17:03 +00:00
|
|
|
layoutId = R.layout.headlines_row_unread;
|
2011-11-24 10:01:51 +00:00
|
|
|
break;
|
|
|
|
case VIEW_SELECTED:
|
2011-11-30 10:17:03 +00:00
|
|
|
layoutId = R.layout.headlines_row_selected;
|
2011-11-24 10:01:51 +00:00
|
|
|
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-30 18:44:36 +00:00
|
|
|
|
|
|
|
// http://code.google.com/p/android/issues/detail?id=3414
|
|
|
|
((ViewGroup)v).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
|
|
|
|
2011-11-24 14:57:44 +00:00
|
|
|
TextView tt = (TextView)v.findViewById(R.id.title);
|
2011-11-23 10:55:05 +00:00
|
|
|
|
|
|
|
if (tt != null) {
|
2011-11-30 18:44:36 +00:00
|
|
|
if (m_combinedMode) {
|
|
|
|
tt.setMovementMethod(LinkMovementMethod.getInstance());
|
|
|
|
tt.setText(Html.fromHtml("<a href=\""+article.link.replace("\"", "\\\"")+"\">" + article.title + "</a>"));
|
|
|
|
} else {
|
|
|
|
tt.setText(Html.fromHtml(article.title));
|
|
|
|
}
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
|
|
|
|
2011-11-24 14:57:44 +00:00
|
|
|
ImageView marked = (ImageView)v.findViewById(R.id.marked);
|
|
|
|
|
|
|
|
if (marked != null) {
|
|
|
|
marked.setImageResource(article.marked ? android.R.drawable.star_on : android.R.drawable.star_off);
|
|
|
|
|
|
|
|
marked.setOnClickListener(new OnClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
article.marked = !article.marked;
|
|
|
|
m_adapter.notifyDataSetChanged();
|
2011-11-24 16:11:10 +00:00
|
|
|
|
2011-12-06 12:52:33 +00:00
|
|
|
m_onlineServices.saveArticleMarked(article);
|
2011-11-24 14:57:44 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-11-27 12:47:38 +00:00
|
|
|
ImageView published = (ImageView)v.findViewById(R.id.published);
|
|
|
|
|
|
|
|
if (published != null) {
|
|
|
|
published.setImageResource(article.published ? R.drawable.ic_rss : R.drawable.ic_rss_bw);
|
|
|
|
|
|
|
|
published.setOnClickListener(new OnClickListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
article.published = !article.published;
|
|
|
|
m_adapter.notifyDataSetChanged();
|
|
|
|
|
2011-12-06 12:52:33 +00:00
|
|
|
m_onlineServices.saveArticlePublished(article);
|
2011-11-27 12:47:38 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-11-24 14:57:44 +00:00
|
|
|
TextView te = (TextView)v.findViewById(R.id.excerpt);
|
2011-11-23 10:55:05 +00:00
|
|
|
|
2011-12-13 12:46:58 +00:00
|
|
|
String articleContent = article.content != null ? article.content : "";
|
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
if (te != null) {
|
2011-11-30 13:05:03 +00:00
|
|
|
if (!m_combinedMode) {
|
2011-12-13 12:46:21 +00:00
|
|
|
String excerpt = Jsoup.parse(articleContent).text();
|
2011-11-23 10:55:05 +00:00
|
|
|
|
2011-11-30 13:05:03 +00:00
|
|
|
if (excerpt.length() > 100)
|
|
|
|
excerpt = excerpt.substring(0, 100) + "...";
|
2011-11-23 10:55:05 +00:00
|
|
|
|
2011-11-30 13:05:03 +00:00
|
|
|
te.setText(excerpt);
|
|
|
|
} else {
|
|
|
|
te.setVisibility(View.GONE);
|
|
|
|
}
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
|
|
|
|
2011-12-04 07:50:49 +00:00
|
|
|
TextView content = (TextView)v.findViewById(R.id.content);
|
2011-11-30 13:05:03 +00:00
|
|
|
|
2011-12-04 07:50:49 +00:00
|
|
|
if (content != null) {
|
2011-11-30 13:05:03 +00:00
|
|
|
if (m_combinedMode) {
|
2011-12-04 07:50:49 +00:00
|
|
|
content.setMovementMethod(LinkMovementMethod.getInstance());
|
2011-12-03 18:34:40 +00:00
|
|
|
|
2011-12-04 07:50:49 +00:00
|
|
|
//content.setText(Html.fromHtml(article.content, new URLImageGetter(content, getActivity()), null));
|
2011-12-13 12:46:58 +00:00
|
|
|
content.setText(Html.fromHtml(articleContent, m_dummyGetter, null));
|
2011-11-30 13:05:03 +00:00
|
|
|
|
|
|
|
} else {
|
2011-12-04 07:50:49 +00:00
|
|
|
content.setVisibility(View.GONE);
|
2011-11-30 13:05:03 +00:00
|
|
|
}
|
2011-12-04 07:50:49 +00:00
|
|
|
|
2011-11-30 13:05:03 +00:00
|
|
|
}
|
|
|
|
|
2011-11-24 10:01:51 +00:00
|
|
|
TextView dv = (TextView) v.findViewById(R.id.date);
|
|
|
|
|
|
|
|
if (dv != null) {
|
|
|
|
Date d = new Date((long)article.updated * 1000);
|
|
|
|
DateFormat df = new SimpleDateFormat("MMM dd, HH:mm");
|
2011-11-25 08:47:07 +00:00
|
|
|
df.setTimeZone(TimeZone.getDefault());
|
2011-11-24 10:01:51 +00:00
|
|
|
dv.setText(df.format(d));
|
|
|
|
}
|
|
|
|
|
2011-11-24 11:51:23 +00:00
|
|
|
CheckBox cb = (CheckBox) v.findViewById(R.id.selected);
|
|
|
|
|
|
|
|
if (cb != null) {
|
|
|
|
cb.setChecked(m_selectedArticles.contains(article));
|
2011-11-29 11:09:27 +00:00
|
|
|
cb.setOnClickListener(new OnClickListener() {
|
|
|
|
|
2011-11-24 11:51:23 +00:00
|
|
|
@Override
|
2011-11-29 11:09:27 +00:00
|
|
|
public void onClick(View view) {
|
|
|
|
CheckBox cb = (CheckBox)view;
|
|
|
|
|
|
|
|
if (cb.isChecked()) {
|
|
|
|
if (!m_selectedArticles.contains(article))
|
|
|
|
m_selectedArticles.add(article);
|
2011-11-24 11:51:23 +00:00
|
|
|
} else {
|
|
|
|
m_selectedArticles.remove(article);
|
|
|
|
}
|
|
|
|
|
2011-12-06 12:52:33 +00:00
|
|
|
m_onlineServices.initMainMenu();
|
2011-11-30 07:22:27 +00:00
|
|
|
|
2011-11-24 11:51:23 +00:00
|
|
|
Log.d(TAG, "num selected: " + m_selectedArticles.size());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
return v;
|
|
|
|
}
|
2011-09-10 08:16:59 +00:00
|
|
|
}
|
|
|
|
|
2011-11-27 12:22:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
public void notifyUpdated() {
|
|
|
|
m_adapter.notifyDataSetChanged();
|
2011-12-09 20:49:55 +00:00
|
|
|
|
|
|
|
Article article = m_onlineServices.getSelectedArticle();
|
|
|
|
if (article != null) {
|
|
|
|
setActiveArticleId(article.id);
|
|
|
|
}
|
2011-11-27 12:22:03 +00:00
|
|
|
}
|
|
|
|
|
2011-11-28 09:45:19 +00:00
|
|
|
public ArticleList getAllArticles() {
|
|
|
|
return m_articles;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setActiveArticleId(int id) {
|
|
|
|
m_activeArticleId = id;
|
|
|
|
m_adapter.notifyDataSetChanged();
|
|
|
|
|
|
|
|
ListView list = (ListView)getView().findViewById(R.id.headlines);
|
|
|
|
|
|
|
|
if (list != null) {
|
2011-11-30 15:22:33 +00:00
|
|
|
int position = m_adapter.getPosition(getArticleById(id));
|
2011-11-28 09:45:19 +00:00
|
|
|
list.setSelection(position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-29 11:09:27 +00:00
|
|
|
public void setSelection(ArticlesSelection select) {
|
|
|
|
m_selectedArticles.clear();
|
|
|
|
|
|
|
|
if (select != ArticlesSelection.NONE) {
|
|
|
|
for (Article a : m_articles) {
|
|
|
|
if (select == ArticlesSelection.ALL || select == ArticlesSelection.UNREAD && a.unread) {
|
|
|
|
m_selectedArticles.add(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_adapter.notifyDataSetChanged();
|
|
|
|
}
|
|
|
|
|
2011-11-29 17:00:37 +00:00
|
|
|
public Article getArticleAtPosition(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
|
|
|
}
|
2011-11-30 15:22:33 +00:00
|
|
|
|
|
|
|
public Article getArticleById(int id) {
|
|
|
|
for (Article a : m_articles) {
|
|
|
|
if (a.id == id)
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2011-11-29 17:00:37 +00:00
|
|
|
|
|
|
|
public ArticleList getUnreadArticles() {
|
|
|
|
ArticleList tmp = new ArticleList();
|
|
|
|
for (Article a : m_articles) {
|
|
|
|
if (a.unread) tmp.add(a);
|
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
2011-11-30 11:45:23 +00:00
|
|
|
@Override
|
|
|
|
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
|
|
|
|
if (!m_refreshInProgress && m_canLoadMore && firstVisibleItem + visibleItemCount == m_articles.size()) {
|
|
|
|
refresh(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onScrollStateChanged(AbsListView view, int scrollState) {
|
2011-11-30 13:05:03 +00:00
|
|
|
// no-op
|
2011-11-30 11:45:23 +00:00
|
|
|
}
|
|
|
|
|
2011-11-30 15:22:33 +00:00
|
|
|
public int getActiveArticleId() {
|
|
|
|
return m_activeArticleId;
|
|
|
|
}
|
|
|
|
|
2011-12-09 20:49:55 +00:00
|
|
|
public int getArticlePosition(Article article) {
|
|
|
|
return m_adapter.getPosition(article);
|
|
|
|
}
|
|
|
|
|
2012-01-19 10:05:48 +00:00
|
|
|
public void setSearchQuery(String query) {
|
|
|
|
if (!m_searchQuery.equals(query)) {
|
|
|
|
m_searchQuery = query;
|
|
|
|
refresh(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-30 15:22:33 +00:00
|
|
|
|
2011-09-08 12:45:08 +00:00
|
|
|
}
|