2011-09-09 11:58:11 +00:00
|
|
|
package org.fox.ttrss;
|
|
|
|
|
2011-11-25 09:10:56 +00:00
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Date;
|
2011-11-23 16:38:21 +00:00
|
|
|
|
2011-11-28 09:45:19 +00:00
|
|
|
import org.fox.ttrss.ArticleOps.RelativeArticle;
|
|
|
|
|
2011-09-09 11:58:11 +00:00
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.os.Bundle;
|
2011-11-25 11:03:14 +00:00
|
|
|
import android.preference.PreferenceManager;
|
2011-11-25 18:37:02 +00:00
|
|
|
import android.support.v4.app.Fragment;
|
2011-11-24 14:57:44 +00:00
|
|
|
import android.text.Html;
|
|
|
|
import android.text.method.LinkMovementMethod;
|
2011-11-28 10:01:04 +00:00
|
|
|
import android.view.GestureDetector;
|
|
|
|
import android.view.GestureDetector.SimpleOnGestureListener;
|
2011-09-09 11:58:11 +00:00
|
|
|
import android.view.LayoutInflater;
|
2011-11-28 10:01:04 +00:00
|
|
|
import android.view.MotionEvent;
|
2011-09-09 11:58:11 +00:00
|
|
|
import android.view.View;
|
2011-11-28 09:45:19 +00:00
|
|
|
import android.view.View.OnClickListener;
|
2011-11-28 10:01:04 +00:00
|
|
|
import android.view.ViewGroup;
|
2011-09-09 16:36:09 +00:00
|
|
|
import android.webkit.WebView;
|
2011-11-28 09:45:19 +00:00
|
|
|
import android.widget.ImageView;
|
2011-09-09 16:36:09 +00:00
|
|
|
import android.widget.TextView;
|
2011-09-09 11:58:11 +00:00
|
|
|
|
2011-11-27 16:04:25 +00:00
|
|
|
import com.google.ads.AdRequest;
|
|
|
|
import com.google.ads.AdView;
|
|
|
|
|
2011-11-28 09:45:19 +00:00
|
|
|
public class ArticleFragment extends Fragment implements OnClickListener {
|
2011-11-27 13:20:27 +00:00
|
|
|
@SuppressWarnings("unused")
|
2011-09-09 16:36:09 +00:00
|
|
|
private final String TAG = this.getClass().getSimpleName();
|
|
|
|
|
2011-11-25 11:03:14 +00:00
|
|
|
private SharedPreferences m_prefs;
|
2011-11-23 16:38:21 +00:00
|
|
|
private Article m_article;
|
2011-11-27 12:22:03 +00:00
|
|
|
private ArticleOps m_articleOps;
|
2011-11-28 09:45:19 +00:00
|
|
|
private Article m_nextArticle;
|
|
|
|
private Article m_prevArticle;
|
2011-11-28 10:01:04 +00:00
|
|
|
private GestureDetector m_gestureDetector;
|
|
|
|
private View.OnTouchListener m_gestureListener;
|
2011-09-09 11:58:11 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
|
|
|
|
|
if (savedInstanceState != null) {
|
2011-11-25 10:32:32 +00:00
|
|
|
m_article = savedInstanceState.getParcelable("article");
|
2011-09-09 11:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
View view = inflater.inflate(R.layout.article_fragment, container, false);
|
2011-11-28 09:45:19 +00:00
|
|
|
|
2011-11-28 10:01:04 +00:00
|
|
|
m_gestureDetector = new GestureDetector(new MyGestureDetector());
|
|
|
|
m_gestureListener = new View.OnTouchListener() {
|
|
|
|
public boolean onTouch(View v, MotionEvent aEvent) {
|
|
|
|
if (m_gestureDetector.onTouchEvent(aEvent))
|
|
|
|
return true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-11-28 09:45:19 +00:00
|
|
|
// TODO change to interface?
|
|
|
|
MainActivity activity = (MainActivity)getActivity();
|
|
|
|
|
|
|
|
if (activity != null) {
|
|
|
|
int orientation = activity.getWindowManager().getDefaultDisplay().getOrientation();
|
|
|
|
|
|
|
|
if (!activity.isSmallScreen()) {
|
|
|
|
if (orientation % 2 == 0) {
|
|
|
|
view.findViewById(R.id.splitter_horizontal).setVisibility(View.GONE);
|
|
|
|
} else {
|
|
|
|
view.findViewById(R.id.splitter_vertical).setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
view.findViewById(R.id.splitter_vertical).setVisibility(View.GONE);
|
|
|
|
view.findViewById(R.id.splitter_horizontal).setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
view.findViewById(R.id.splitter_horizontal).setVisibility(View.GONE);
|
|
|
|
}
|
2011-09-09 16:36:09 +00:00
|
|
|
|
2011-11-23 16:38:21 +00:00
|
|
|
if (m_article != null) {
|
|
|
|
|
|
|
|
TextView title = (TextView)view.findViewById(R.id.title);
|
|
|
|
|
|
|
|
if (title != null) {
|
2011-11-30 09:02:26 +00:00
|
|
|
|
|
|
|
String titleStr;
|
|
|
|
|
|
|
|
if (m_article.title.length() > 200)
|
|
|
|
titleStr = m_article.title.substring(0, 200) + "...";
|
|
|
|
else
|
|
|
|
titleStr = m_article.title;
|
|
|
|
|
2011-11-24 14:57:44 +00:00
|
|
|
title.setMovementMethod(LinkMovementMethod.getInstance());
|
2011-11-30 09:02:26 +00:00
|
|
|
title.setText(Html.fromHtml("<a href=\""+m_article.link.replace("\"", "\\\"")+"\">" + titleStr + "</a>"));
|
2011-11-23 16:38:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WebView web = (WebView)view.findViewById(R.id.content);
|
|
|
|
|
|
|
|
if (web != null) {
|
|
|
|
|
2011-11-25 09:10:56 +00:00
|
|
|
String content;
|
2011-11-29 04:03:38 +00:00
|
|
|
String cssOverride = "";
|
|
|
|
|
2011-11-30 16:04:31 +00:00
|
|
|
web.setBackgroundColor(0x00000000);
|
|
|
|
|
2011-11-29 04:03:38 +00:00
|
|
|
if (m_prefs.getString("theme", "THEME_DARK").equals("THEME_DARK")) {
|
2011-11-30 16:04:31 +00:00
|
|
|
cssOverride = "body { background : transparent; color : #e0e0e0}\n";
|
|
|
|
} else {
|
|
|
|
cssOverride = "body { background : transparent; }\n";
|
2011-11-25 09:10:56 +00:00
|
|
|
}
|
2011-11-23 16:38:21 +00:00
|
|
|
|
2011-11-29 04:03:38 +00:00
|
|
|
content =
|
|
|
|
"<html>" +
|
|
|
|
"<head>" +
|
|
|
|
"<meta content=\"text/html; charset=utf-8\" http-equiv=\"content-type\">" +
|
2011-11-30 16:04:31 +00:00
|
|
|
"<meta name=\"viewport\" content=\"target-densitydpi=device-dpi\" />" +
|
2011-11-29 04:03:38 +00:00
|
|
|
"<style type=\"text/css\">" +
|
|
|
|
cssOverride +
|
|
|
|
"img { max-width : 90%; }" +
|
|
|
|
"body { text-align : justify; }" +
|
|
|
|
"</style>" +
|
|
|
|
"</head>" +
|
|
|
|
"<body>" + m_article.content + "</body></html>";
|
|
|
|
|
2011-11-27 07:40:59 +00:00
|
|
|
web.loadDataWithBaseURL(null, content, "text/html", "utf-8", null);
|
2011-11-28 10:01:04 +00:00
|
|
|
|
|
|
|
if (activity.isSmallScreen())
|
|
|
|
web.setOnTouchListener(m_gestureListener);
|
2011-11-23 16:38:21 +00:00
|
|
|
}
|
2011-11-24 12:08:02 +00:00
|
|
|
|
2011-11-25 09:10:56 +00:00
|
|
|
TextView dv = (TextView)view.findViewById(R.id.date);
|
|
|
|
|
|
|
|
if (dv != null) {
|
|
|
|
Date d = new Date(m_article.updated * 1000L);
|
|
|
|
SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy, HH:mm");
|
|
|
|
dv.setText(df.format(d));
|
|
|
|
}
|
|
|
|
|
2011-11-28 09:45:19 +00:00
|
|
|
TextView tagv = (TextView)view.findViewById(R.id.tags);
|
|
|
|
|
|
|
|
if (tagv != null) {
|
|
|
|
if (m_article.tags != null) {
|
|
|
|
String tagsStr = "";
|
|
|
|
|
|
|
|
for (String tag : m_article.tags)
|
|
|
|
tagsStr += tag + ", ";
|
|
|
|
|
|
|
|
tagsStr = tagsStr.replaceAll(", $", "");
|
|
|
|
|
|
|
|
tagv.setText(tagsStr);
|
|
|
|
} else {
|
|
|
|
tagv.setVisibility(View.GONE);
|
|
|
|
}
|
2011-11-25 09:10:56 +00:00
|
|
|
}
|
2011-11-27 16:04:25 +00:00
|
|
|
|
|
|
|
AdView av = (AdView)view.findViewById(R.id.ad);
|
2011-11-28 05:20:29 +00:00
|
|
|
boolean enableAds = m_prefs.getBoolean("enable_ads", false);
|
2011-11-27 16:04:25 +00:00
|
|
|
|
|
|
|
if (av != null) {
|
2011-11-28 05:20:29 +00:00
|
|
|
if (enableAds) {
|
|
|
|
AdRequest request = new AdRequest();
|
|
|
|
request.addTestDevice(AdRequest.TEST_EMULATOR);
|
|
|
|
av.loadAd(request);
|
|
|
|
} else {
|
|
|
|
av.setVisibility(View.GONE);
|
|
|
|
}
|
2011-11-27 16:04:25 +00:00
|
|
|
}
|
2011-11-28 09:45:19 +00:00
|
|
|
|
|
|
|
ImageView next = (ImageView)view.findViewById(R.id.next_article);
|
|
|
|
|
|
|
|
if (next != null) {
|
|
|
|
if (m_nextArticle != null) {
|
|
|
|
next.setOnClickListener(this);
|
|
|
|
} else {
|
|
|
|
next.setImageResource(R.drawable.ic_next_article_disabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ImageView prev = (ImageView)view.findViewById(R.id.prev_article);
|
|
|
|
|
|
|
|
if (prev != null) {
|
|
|
|
if (m_prevArticle != null) {
|
|
|
|
prev.setOnClickListener(this);
|
|
|
|
} else {
|
|
|
|
prev.setImageResource(R.drawable.ic_prev_article_disabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-23 16:38:21 +00:00
|
|
|
}
|
|
|
|
|
2011-09-09 11:58:11 +00:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroy() {
|
2011-09-11 09:04:48 +00:00
|
|
|
super.onDestroy();
|
2011-09-09 11:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-09-09 16:36:09 +00:00
|
|
|
public void onSaveInstanceState (Bundle out) {
|
2011-09-09 11:58:11 +00:00
|
|
|
super.onSaveInstanceState(out);
|
2011-09-09 16:36:09 +00:00
|
|
|
|
2011-11-25 10:32:32 +00:00
|
|
|
out.putParcelable("article", m_article);
|
2011-09-09 11:58:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAttach(Activity activity) {
|
|
|
|
super.onAttach(activity);
|
2011-11-23 16:38:21 +00:00
|
|
|
|
2011-11-25 11:03:14 +00:00
|
|
|
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
|
2011-11-27 12:22:03 +00:00
|
|
|
m_articleOps = (ArticleOps)activity;
|
|
|
|
m_article = m_articleOps.getSelectedArticle();
|
2011-11-28 09:45:19 +00:00
|
|
|
|
|
|
|
m_prevArticle = m_articleOps.getRelativeArticle(m_article, RelativeArticle.BEFORE);
|
|
|
|
m_nextArticle = m_articleOps.getRelativeArticle(m_article, RelativeArticle.AFTER);
|
|
|
|
}
|
2011-11-27 12:22:03 +00:00
|
|
|
|
2011-11-28 09:45:19 +00:00
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
if (v.getId() == R.id.next_article) {
|
|
|
|
m_articleOps.openArticle(m_nextArticle, 0);
|
|
|
|
} else if (v.getId() == R.id.prev_article) {
|
|
|
|
m_articleOps.openArticle(m_prevArticle, R.anim.slide_right);
|
|
|
|
}
|
2011-09-09 11:58:11 +00:00
|
|
|
}
|
2011-11-28 10:01:04 +00:00
|
|
|
|
|
|
|
// http://blog.blackmoonit.com/2010/07/gesture-detection-swipe-detection_4292.html
|
|
|
|
class MyGestureDetector extends SimpleOnGestureListener {
|
|
|
|
private static final int SWIPE_MIN_DISTANCE = 150;
|
|
|
|
private static final int SWIPE_MAX_OFF_PATH = 100;
|
|
|
|
private static final int SWIPE_THRESHOLD_VELOCITY = 100;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
|
|
|
|
float dX = e2.getX()-e1.getX();
|
|
|
|
float dY = e1.getY()-e2.getY();
|
|
|
|
if (Math.abs(dY)<SWIPE_MAX_OFF_PATH &&
|
|
|
|
Math.abs(velocityX)>=SWIPE_THRESHOLD_VELOCITY &&
|
|
|
|
Math.abs(dX)>=SWIPE_MIN_DISTANCE ) {
|
|
|
|
if (dX>0) {
|
|
|
|
//Log.d(TAG, "Right swipe");
|
|
|
|
|
|
|
|
if (m_prevArticle != null)
|
|
|
|
m_articleOps.openArticle(m_prevArticle, R.anim.slide_right);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
//Log.d(TAG, "Left swipe");
|
|
|
|
|
|
|
|
if (m_nextArticle != null)
|
|
|
|
m_articleOps.openArticle(m_nextArticle, 0);
|
|
|
|
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
/* } else if (Math.abs(dX)<SWIPE_MAX_OFF_PATH &&
|
|
|
|
Math.abs(velocityY)>=SWIPE_THRESHOLD_VELOCITY &&
|
|
|
|
Math.abs(dY)>=SWIPE_MIN_DISTANCE ) {
|
|
|
|
if (dY>0) {
|
|
|
|
Log.d(TAG, "Up swipe");
|
|
|
|
} else {
|
|
|
|
Log.d(TAG, "Down swipe");
|
|
|
|
}
|
|
|
|
return true; */
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2011-09-09 11:58:11 +00:00
|
|
|
}
|