2011-12-09 20:49:55 +00:00
|
|
|
package org.fox.ttrss;
|
|
|
|
|
2012-09-17 09:27:27 +00:00
|
|
|
import java.util.HashMap;
|
|
|
|
|
2012-06-19 10:18:00 +00:00
|
|
|
import org.fox.ttrss.types.Article;
|
2012-09-16 15:25:47 +00:00
|
|
|
import org.fox.ttrss.types.ArticleList;
|
2012-09-17 09:27:27 +00:00
|
|
|
import org.fox.ttrss.types.Feed;
|
|
|
|
import org.fox.ttrss.util.HeadlinesRequest;
|
|
|
|
|
2012-10-09 09:16:05 +00:00
|
|
|
import android.annotation.SuppressLint;
|
2011-12-09 20:49:55 +00:00
|
|
|
import android.app.Activity;
|
2012-10-09 09:16:05 +00:00
|
|
|
import android.content.SharedPreferences;
|
2012-11-22 11:18:19 +00:00
|
|
|
import android.os.BadParcelableException;
|
2011-12-09 20:49:55 +00:00
|
|
|
import android.os.Bundle;
|
2012-10-09 09:16:05 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2012-06-19 14:24:22 +00:00
|
|
|
import android.support.v4.app.Fragment;
|
|
|
|
import android.support.v4.app.FragmentManager;
|
|
|
|
import android.support.v4.app.FragmentStatePagerAdapter;
|
2011-12-09 20:49:55 +00:00
|
|
|
import android.support.v4.view.ViewPager;
|
2012-09-17 09:27:27 +00:00
|
|
|
import android.util.Log;
|
2011-12-09 20:49:55 +00:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
2013-01-05 08:33:06 +00:00
|
|
|
import android.view.WindowManager;
|
2011-12-09 20:49:55 +00:00
|
|
|
|
2012-09-18 11:45:22 +00:00
|
|
|
import com.google.gson.JsonElement;
|
|
|
|
|
2011-12-09 20:49:55 +00:00
|
|
|
public class ArticlePager extends Fragment {
|
|
|
|
|
2012-06-20 09:12:46 +00:00
|
|
|
private final String TAG = "ArticlePager";
|
2011-12-09 20:49:55 +00:00
|
|
|
private PagerAdapter m_adapter;
|
2012-09-18 13:11:18 +00:00
|
|
|
private HeadlinesEventListener m_listener;
|
2011-12-09 20:49:55 +00:00
|
|
|
private Article m_article;
|
2012-09-18 11:17:39 +00:00
|
|
|
private ArticleList m_articles = GlobalState.getInstance().m_loadedArticles;
|
2012-09-17 09:27:27 +00:00
|
|
|
private OnlineActivity m_activity;
|
|
|
|
private String m_searchQuery = "";
|
2012-09-19 08:49:10 +00:00
|
|
|
private Feed m_feed;
|
2012-10-09 09:16:05 +00:00
|
|
|
private SharedPreferences m_prefs;
|
2011-12-09 20:49:55 +00:00
|
|
|
|
|
|
|
private class PagerAdapter extends FragmentStatePagerAdapter {
|
|
|
|
|
2011-12-10 07:22:27 +00:00
|
|
|
public PagerAdapter(FragmentManager fm) {
|
2011-12-09 20:49:55 +00:00
|
|
|
super(fm);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Fragment getItem(int position) {
|
2012-09-16 15:25:47 +00:00
|
|
|
Article article = m_articles.get(position);
|
2011-12-09 20:49:55 +00:00
|
|
|
|
|
|
|
if (article != null) {
|
|
|
|
ArticleFragment af = new ArticleFragment(article);
|
2012-10-10 11:12:58 +00:00
|
|
|
|
2013-01-06 20:31:37 +00:00
|
|
|
if (m_prefs.getBoolean("dim_status_bar", false) && getView() != null && !m_activity.isCompatMode()) {
|
|
|
|
getView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
|
2012-10-10 11:12:58 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 20:49:55 +00:00
|
|
|
return af;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getCount() {
|
2012-09-16 15:25:47 +00:00
|
|
|
return m_articles.size();
|
2011-12-09 20:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public ArticlePager() {
|
|
|
|
super();
|
|
|
|
}
|
|
|
|
|
2012-09-19 08:49:10 +00:00
|
|
|
public ArticlePager(Article article, Feed feed) {
|
2011-12-09 20:49:55 +00:00
|
|
|
super();
|
2012-09-19 08:49:10 +00:00
|
|
|
|
2011-12-09 20:49:55 +00:00
|
|
|
m_article = article;
|
2012-09-19 08:49:10 +00:00
|
|
|
m_feed = feed;
|
2011-12-09 20:49:55 +00:00
|
|
|
}
|
2012-09-17 09:27:27 +00:00
|
|
|
|
|
|
|
public void setSearchQuery(String searchQuery) {
|
|
|
|
m_searchQuery = searchQuery;
|
|
|
|
}
|
2011-12-09 20:49:55 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
|
View view = inflater.inflate(R.layout.article_pager, container, false);
|
|
|
|
|
2012-09-16 15:25:47 +00:00
|
|
|
if (savedInstanceState != null) {
|
|
|
|
m_article = savedInstanceState.getParcelable("article");
|
2012-09-19 08:49:10 +00:00
|
|
|
m_feed = savedInstanceState.getParcelable("feed");
|
2012-09-16 15:25:47 +00:00
|
|
|
}
|
|
|
|
|
2012-06-19 14:24:22 +00:00
|
|
|
m_adapter = new PagerAdapter(getActivity().getSupportFragmentManager());
|
2011-12-09 20:49:55 +00:00
|
|
|
|
|
|
|
ViewPager pager = (ViewPager) view.findViewById(R.id.article_pager);
|
|
|
|
|
2012-09-16 15:25:47 +00:00
|
|
|
int position = m_articles.indexOf(m_article);
|
2011-12-09 20:49:55 +00:00
|
|
|
|
2012-09-19 19:39:46 +00:00
|
|
|
m_listener.onArticleSelected(m_article, false);
|
|
|
|
|
2012-12-09 11:06:03 +00:00
|
|
|
m_activity.setProgressBarVisibility(true);
|
|
|
|
|
2011-12-09 20:49:55 +00:00
|
|
|
pager.setAdapter(m_adapter);
|
|
|
|
pager.setCurrentItem(position);
|
|
|
|
pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPageScrollStateChanged(int arg0) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPageScrolled(int arg0, float arg1, int arg2) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onPageSelected(int position) {
|
2012-09-16 15:25:47 +00:00
|
|
|
Article article = m_articles.get(position);
|
2011-12-09 20:49:55 +00:00
|
|
|
|
|
|
|
if (article != null) {
|
2012-09-16 17:59:03 +00:00
|
|
|
m_article = article;
|
|
|
|
|
2012-09-19 12:59:08 +00:00
|
|
|
/* if (article.unread) {
|
2011-12-10 07:22:27 +00:00
|
|
|
article.unread = false;
|
2012-09-17 14:44:43 +00:00
|
|
|
m_activity.saveArticleUnread(article);
|
2012-09-19 12:59:08 +00:00
|
|
|
} */
|
|
|
|
|
2012-09-18 13:11:18 +00:00
|
|
|
m_listener.onArticleSelected(article, false);
|
2012-06-20 09:12:46 +00:00
|
|
|
|
|
|
|
//Log.d(TAG, "Page #" + position + "/" + m_adapter.getCount());
|
|
|
|
|
2012-12-09 11:06:03 +00:00
|
|
|
if ((m_activity.isSmallScreen() || m_activity.isPortrait()) && position == m_adapter.getCount() - 5) {
|
2012-09-17 09:27:27 +00:00
|
|
|
Log.d(TAG, "loading more articles...");
|
2012-09-19 08:49:10 +00:00
|
|
|
refresh(true);
|
2012-06-20 09:12:46 +00:00
|
|
|
}
|
2011-12-09 20:49:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2012-09-21 06:44:30 +00:00
|
|
|
@SuppressWarnings({ "unchecked", "serial" })
|
|
|
|
protected void refresh(boolean append) {
|
2012-09-17 09:27:27 +00:00
|
|
|
m_activity.setLoadingStatus(R.string.blank, true);
|
2012-09-19 12:01:31 +00:00
|
|
|
|
|
|
|
m_activity.setProgressBarVisibility(true);
|
2012-09-17 09:27:27 +00:00
|
|
|
|
2012-09-19 08:49:10 +00:00
|
|
|
if (!m_feed.equals(GlobalState.getInstance().m_activeFeed)) {
|
|
|
|
append = false;
|
|
|
|
}
|
|
|
|
|
2012-09-17 09:27:27 +00:00
|
|
|
HeadlinesRequest req = new HeadlinesRequest(getActivity().getApplicationContext(), m_activity) {
|
2012-09-19 12:01:31 +00:00
|
|
|
@Override
|
|
|
|
protected void onProgressUpdate(Integer... progress) {
|
|
|
|
m_activity.setProgress(progress[0] / progress[1] * 10000);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-09-17 09:27:27 +00:00
|
|
|
protected void onPostExecute(JsonElement result) {
|
2012-10-09 05:54:43 +00:00
|
|
|
if (isDetached()) return;
|
|
|
|
|
2012-09-19 12:01:31 +00:00
|
|
|
m_activity.setProgressBarVisibility(false);
|
|
|
|
|
2012-09-17 09:27:27 +00:00
|
|
|
super.onPostExecute(result);
|
2012-09-19 08:49:10 +00:00
|
|
|
|
2012-11-22 11:18:19 +00:00
|
|
|
if (result != null) {
|
|
|
|
try {
|
|
|
|
m_adapter.notifyDataSetChanged();
|
|
|
|
} catch (BadParcelableException e) {
|
|
|
|
if (getActivity() != null) {
|
|
|
|
getActivity().finish();
|
2013-02-11 18:06:40 +00:00
|
|
|
return;
|
2012-11-22 11:18:19 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-19 12:59:08 +00:00
|
|
|
|
2012-09-19 19:39:46 +00:00
|
|
|
if (m_article.id == 0 || m_articles.indexOf(m_article) == -1) {
|
2012-09-19 12:59:08 +00:00
|
|
|
if (m_articles.size() > 0) {
|
|
|
|
m_article = m_articles.get(0);
|
|
|
|
m_listener.onArticleSelected(m_article, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 08:49:10 +00:00
|
|
|
} else {
|
|
|
|
if (m_lastError == ApiError.LOGIN_FAILED) {
|
2012-09-21 06:44:30 +00:00
|
|
|
m_activity.login(true);
|
2012-09-19 08:49:10 +00:00
|
|
|
} else {
|
|
|
|
m_activity.toast(getErrorMessage());
|
|
|
|
//setLoadingStatus(getErrorMessage(), false);
|
|
|
|
}
|
|
|
|
}
|
2012-09-17 09:27:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-09-19 08:49:10 +00:00
|
|
|
final Feed feed = m_feed;
|
2012-09-17 09:27:27 +00:00
|
|
|
|
|
|
|
final String sessionId = m_activity.getSessionId();
|
|
|
|
final boolean showUnread = m_activity.getUnreadArticlesOnly();
|
|
|
|
int skip = 0;
|
|
|
|
|
2012-09-19 08:49:10 +00:00
|
|
|
if (append) {
|
|
|
|
for (Article a : m_articles) {
|
|
|
|
if (a.unread) ++skip;
|
|
|
|
}
|
2012-09-17 09:27:27 +00:00
|
|
|
|
2012-09-19 08:49:10 +00:00
|
|
|
if (skip == 0) skip = m_articles.size();
|
|
|
|
}
|
2012-09-17 09:27:27 +00:00
|
|
|
|
|
|
|
final int fskip = skip;
|
|
|
|
|
|
|
|
req.setOffset(skip);
|
|
|
|
|
|
|
|
HashMap<String,String> map = new HashMap<String,String>() {
|
|
|
|
{
|
|
|
|
put("op", "getHeadlines");
|
|
|
|
put("sid", sessionId);
|
|
|
|
put("feed_id", String.valueOf(feed.id));
|
|
|
|
put("show_content", "true");
|
|
|
|
put("include_attachments", "true");
|
|
|
|
put("limit", String.valueOf(HeadlinesFragment.HEADLINES_REQUEST_SIZE));
|
|
|
|
put("offset", String.valueOf(0));
|
|
|
|
put("view_mode", showUnread ? "adaptive" : "all_articles");
|
|
|
|
put("skip", String.valueOf(fskip));
|
2012-09-18 10:31:22 +00:00
|
|
|
put("include_nested", "true");
|
2012-09-17 09:27:27 +00:00
|
|
|
|
|
|
|
if (feed.is_cat) put("is_cat", "true");
|
|
|
|
|
|
|
|
if (m_searchQuery != null && m_searchQuery.length() != 0) {
|
|
|
|
put("search", m_searchQuery);
|
|
|
|
put("search_mode", "");
|
|
|
|
put("match_on", "both");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
req.execute(map);
|
|
|
|
}
|
|
|
|
|
2012-09-16 15:25:47 +00:00
|
|
|
@Override
|
|
|
|
public void onSaveInstanceState(Bundle out) {
|
|
|
|
super.onSaveInstanceState(out);
|
|
|
|
|
|
|
|
out.putParcelable("article", m_article);
|
2012-09-19 08:49:10 +00:00
|
|
|
out.putParcelable("feed", m_feed);
|
2012-09-16 15:25:47 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 20:49:55 +00:00
|
|
|
@Override
|
|
|
|
public void onAttach(Activity activity) {
|
|
|
|
super.onAttach(activity);
|
|
|
|
|
2012-09-18 13:11:18 +00:00
|
|
|
m_listener = (HeadlinesEventListener)activity;
|
2012-09-17 09:27:27 +00:00
|
|
|
m_activity = (OnlineActivity)activity;
|
2012-10-09 09:16:05 +00:00
|
|
|
|
|
|
|
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
|
2012-09-16 16:46:54 +00:00
|
|
|
}
|
|
|
|
|
2012-10-09 09:16:05 +00:00
|
|
|
@SuppressLint("NewApi")
|
2012-09-16 16:46:54 +00:00
|
|
|
@Override
|
|
|
|
public void onResume() {
|
|
|
|
super.onResume();
|
2012-09-16 15:25:47 +00:00
|
|
|
|
2012-09-19 08:49:10 +00:00
|
|
|
if (m_articles.size() == 0 || !m_feed.equals(GlobalState.getInstance().m_activeFeed)) {
|
|
|
|
refresh(false);
|
|
|
|
GlobalState.getInstance().m_activeFeed = m_feed;
|
|
|
|
}
|
|
|
|
|
2012-09-17 09:27:27 +00:00
|
|
|
m_activity.initMenu();
|
2012-10-09 09:16:05 +00:00
|
|
|
|
2013-01-05 08:33:06 +00:00
|
|
|
if (!m_activity.isCompatMode() && m_prefs.getBoolean("dim_status_bar", false)) {
|
2012-10-09 09:16:05 +00:00
|
|
|
getView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
|
|
|
|
}
|
2013-01-05 08:33:06 +00:00
|
|
|
|
|
|
|
if (m_prefs.getBoolean("full_screen_mode", false)) {
|
|
|
|
m_activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
|
|
|
|
WindowManager.LayoutParams.FLAG_FULLSCREEN);
|
|
|
|
|
|
|
|
/* if (!m_activity.isCompatMode()) {
|
|
|
|
m_activity.getActionBar().hide();
|
|
|
|
} */
|
|
|
|
}
|
2012-09-16 16:46:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public Article getSelectedArticle() {
|
|
|
|
return m_article;
|
2011-12-09 20:49:55 +00:00
|
|
|
}
|
|
|
|
|
2012-09-17 19:20:59 +00:00
|
|
|
public void setActiveArticle(Article article) {
|
2012-09-19 19:39:46 +00:00
|
|
|
if (m_article != article) {
|
|
|
|
m_article = article;
|
2012-09-17 19:20:59 +00:00
|
|
|
|
2012-09-19 19:39:46 +00:00
|
|
|
int position = m_articles.indexOf(m_article);
|
2012-09-17 19:20:59 +00:00
|
|
|
|
2012-09-19 19:39:46 +00:00
|
|
|
ViewPager pager = (ViewPager) getView().findViewById(R.id.article_pager);
|
2012-09-17 19:20:59 +00:00
|
|
|
|
2012-09-19 19:39:46 +00:00
|
|
|
pager.setCurrentItem(position);
|
|
|
|
}
|
2012-09-17 19:20:59 +00:00
|
|
|
}
|
2012-09-23 17:14:01 +00:00
|
|
|
|
|
|
|
public void selectArticle(boolean next) {
|
|
|
|
if (m_article != null) {
|
|
|
|
int position = m_articles.indexOf(m_article);
|
|
|
|
|
|
|
|
if (next)
|
|
|
|
position++;
|
|
|
|
else
|
|
|
|
position--;
|
|
|
|
|
|
|
|
try {
|
|
|
|
Article tmp = m_articles.get(position);
|
|
|
|
|
|
|
|
if (tmp != null) {
|
|
|
|
setActiveArticle(tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (IndexOutOfBoundsException e) {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-09 20:49:55 +00:00
|
|
|
}
|