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-09-08 12:45:08 +00:00
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.os.Bundle;
|
2011-11-25 10:32:32 +00:00
|
|
|
import android.os.Parcel;
|
|
|
|
import android.os.Parcelable;
|
2011-09-08 12:45:08 +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-11-24 11:51:23 +00:00
|
|
|
import android.util.Log;
|
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-09-09 11:58:11 +00:00
|
|
|
import android.widget.AdapterView;
|
|
|
|
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;
|
|
|
|
import android.widget.CompoundButton;
|
|
|
|
import android.widget.CompoundButton.OnCheckedChangeListener;
|
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.JsonObject;
|
|
|
|
import com.google.gson.reflect.TypeToken;
|
2011-09-08 12:45:08 +00:00
|
|
|
|
2011-09-09 11:58:11 +00:00
|
|
|
public class HeadlinesFragment extends Fragment implements OnItemClickListener {
|
2011-09-08 12:45:08 +00:00
|
|
|
private final String TAG = this.getClass().getSimpleName();
|
2011-11-25 11:03:14 +00:00
|
|
|
private SharedPreferences m_prefs;
|
2011-09-09 09:50:15 +00:00
|
|
|
|
2011-11-23 16:38:21 +00:00
|
|
|
private Feed m_feed;
|
2011-11-24 10:01:51 +00:00
|
|
|
private int m_selectedArticleId;
|
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-11-23 16:38:21 +00:00
|
|
|
private OnArticleSelectedListener m_articleSelectedListener;
|
2011-11-27 12:22:03 +00:00
|
|
|
private ArticleOps m_articleOps;
|
2011-11-23 10:55:05 +00:00
|
|
|
|
2011-11-23 16:38:21 +00:00
|
|
|
public interface OnArticleSelectedListener {
|
|
|
|
public void onArticleSelected(Article article);
|
|
|
|
}
|
2011-11-25 10:32:32 +00:00
|
|
|
|
2011-11-25 20:34:40 +00:00
|
|
|
|
2011-11-25 10:32:32 +00:00
|
|
|
|
2011-09-08 12:45:08 +00:00
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
|
|
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");
|
|
|
|
m_selectedArticleId = savedInstanceState.getInt("selectedArticleId");
|
2011-11-25 11:03:14 +00:00
|
|
|
m_selectedArticles = savedInstanceState.getParcelable("selectedArticles");
|
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-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;
|
|
|
|
}
|
|
|
|
|
2011-11-27 07:18:54 +00:00
|
|
|
/* public void showLoading(boolean show) {
|
2011-11-23 12:14:41 +00:00
|
|
|
View v = getView();
|
|
|
|
|
|
|
|
if (v != null) {
|
|
|
|
v = v.findViewById(R.id.loading_container);
|
|
|
|
|
|
|
|
if (v != null)
|
|
|
|
v.setVisibility(show ? View.VISIBLE : View.GONE);
|
|
|
|
}
|
2011-11-27 07:18:54 +00:00
|
|
|
} */
|
2011-11-23 12:14:41 +00:00
|
|
|
|
2011-09-08 12:45:08 +00:00
|
|
|
@Override
|
|
|
|
public void onAttach(Activity activity) {
|
|
|
|
super.onAttach(activity);
|
|
|
|
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
|
2011-11-23 16:38:21 +00:00
|
|
|
m_feed = ((MainActivity)activity).getActiveFeed();
|
|
|
|
m_articleSelectedListener = (OnArticleSelectedListener) activity;
|
2011-11-27 12:22:03 +00:00
|
|
|
m_articleOps = (ArticleOps) activity;
|
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;
|
|
|
|
|
|
|
|
if (list != null) {
|
|
|
|
Article article = (Article)list.getItemAtPosition(position);
|
2011-11-23 16:38:21 +00:00
|
|
|
m_articleSelectedListener.onArticleSelected(article);
|
2011-11-24 10:01:51 +00:00
|
|
|
|
|
|
|
article.unread = false;
|
|
|
|
m_selectedArticleId = article.id;
|
|
|
|
m_adapter.notifyDataSetChanged();
|
2011-11-24 12:16:07 +00:00
|
|
|
|
2011-11-27 12:22:03 +00:00
|
|
|
m_articleOps.saveArticleUnread(article);
|
2011-11-23 12:51:15 +00:00
|
|
|
}
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
|
|
|
|
2011-11-26 06:52:50 +00:00
|
|
|
public void refresh(boolean append) {
|
2011-11-27 10:18:04 +00:00
|
|
|
HeadlinesRequest req = new HeadlinesRequest(getActivity().getApplicationContext());
|
2011-09-09 11:58:11 +00:00
|
|
|
|
2011-11-27 10:18:04 +00:00
|
|
|
//req.setApi(m_prefs.getString("ttrss_url", null));
|
|
|
|
//req.setTrustAny(m_prefs.getBoolean("ssl_trust_any", false));
|
2011-11-24 13:04:58 +00:00
|
|
|
|
|
|
|
final String sessionId = ((MainActivity)getActivity()).getSessionId();
|
2011-11-26 08:46:17 +00:00
|
|
|
final boolean showUnread = ((MainActivity)getActivity()).getUnreadArticlesOnly();
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
final int fskip = skip;
|
|
|
|
|
|
|
|
req.setOffset(skip);
|
|
|
|
|
2011-11-27 09:57:34 +00:00
|
|
|
setLoadingStatus(R.string.blank, true);
|
|
|
|
|
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");
|
|
|
|
put("limit", String.valueOf(30));
|
|
|
|
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-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);
|
|
|
|
out.putInt("selectedArticleId", m_selectedArticleId);
|
2011-11-25 11:03:14 +00:00
|
|
|
out.putParcelable("selectedArticles", m_selectedArticles);
|
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 {
|
|
|
|
JsonObject rv = result.getAsJsonObject();
|
|
|
|
|
|
|
|
Gson gson = new Gson();
|
|
|
|
|
|
|
|
int status = rv.get("status").getAsInt();
|
|
|
|
|
|
|
|
if (status == 0) {
|
|
|
|
JsonArray content = rv.get("content").getAsJsonArray();
|
|
|
|
if (content != null) {
|
|
|
|
Type listType = new TypeToken<List<Article>>() {}.getType();
|
|
|
|
final List<Article> articles = gson.fromJson(content, listType);
|
|
|
|
|
2011-11-27 09:57:34 +00:00
|
|
|
if (m_offset == 0)
|
|
|
|
m_articles.clear();
|
|
|
|
|
|
|
|
int last_position = m_articles.size();
|
|
|
|
|
|
|
|
for (Article f : articles)
|
|
|
|
m_articles.add(f);
|
|
|
|
|
|
|
|
m_adapter.notifyDataSetChanged();
|
|
|
|
|
|
|
|
ListView list = (ListView)getView().findViewById(R.id.headlines);
|
|
|
|
|
|
|
|
if (list != null && m_offset != 0) {
|
|
|
|
list.setSelection(last_position+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
MainActivity activity = (MainActivity)getActivity();
|
|
|
|
activity.setCanLoadMore(articles.size() >= 30);
|
|
|
|
activity.initMainMenu();
|
|
|
|
|
|
|
|
setLoadingStatus(R.string.blank, false);
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
2011-11-24 13:04:58 +00:00
|
|
|
MainActivity activity = (MainActivity)getActivity();
|
|
|
|
activity.login();
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
2011-11-27 07:18:54 +00:00
|
|
|
setLoadingStatus(R.string.error_invalid_object, false);
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
2011-11-27 07:18:54 +00:00
|
|
|
setLoadingStatus(R.string.error_no_data, false);
|
2011-11-23 10:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
public static final int VIEW_COUNT = VIEW_SELECTED+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);
|
|
|
|
|
|
|
|
if (a.id == m_selectedArticleId) {
|
|
|
|
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-11-23 10:55:05 +00:00
|
|
|
if (v == null) {
|
2011-11-24 10:01:51 +00:00
|
|
|
int layoutId = R.layout.headlines_row;
|
|
|
|
|
|
|
|
switch (getItemViewType(position)) {
|
|
|
|
case VIEW_UNREAD:
|
|
|
|
layoutId = R.layout.headlines_row_unread;
|
|
|
|
break;
|
|
|
|
case VIEW_SELECTED:
|
|
|
|
layoutId = R.layout.headlines_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
|
|
|
}
|
|
|
|
|
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-27 07:32:01 +00:00
|
|
|
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-11-27 12:22:03 +00:00
|
|
|
m_articleOps.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();
|
|
|
|
|
|
|
|
m_articleOps.saveArticlePublished(article);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-11-24 14:57:44 +00:00
|
|
|
TextView te = (TextView)v.findViewById(R.id.excerpt);
|
2011-11-23 10:55:05 +00:00
|
|
|
|
|
|
|
if (te != null) {
|
|
|
|
String excerpt = Jsoup.parse(article.content).text();
|
|
|
|
|
|
|
|
if (excerpt.length() > 250)
|
2011-11-24 10:01:51 +00:00
|
|
|
excerpt = excerpt.substring(0, 100) + "...";
|
2011-11-23 10:55:05 +00:00
|
|
|
|
|
|
|
te.setText(excerpt);
|
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
|
|
|
|
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
|
|
|
|
@Override
|
|
|
|
public void onCheckedChanged(CompoundButton buttonView,
|
|
|
|
boolean isChecked) {
|
|
|
|
|
|
|
|
if (isChecked) {
|
|
|
|
m_selectedArticles.add(article);
|
|
|
|
} else {
|
|
|
|
m_selectedArticles.remove(article);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-09-08 12:45:08 +00:00
|
|
|
}
|