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;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import org.jsoup.Jsoup;
|
|
|
|
|
2011-09-08 12:45:08 +00:00
|
|
|
import android.app.Activity;
|
|
|
|
import android.app.Fragment;
|
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;
|
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
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-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();
|
|
|
|
protected SharedPreferences m_prefs;
|
2011-09-09 09:50:15 +00:00
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
private String m_sessionId;
|
|
|
|
private int m_feedId;
|
|
|
|
|
|
|
|
private ArticleListAdapter m_adapter;
|
|
|
|
private List<Article> m_articles = new ArrayList<Article>();
|
|
|
|
|
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) {
|
|
|
|
m_sessionId = savedInstanceState.getString("sessionId");
|
|
|
|
m_feedId = savedInstanceState.getInt("feedId");
|
|
|
|
}
|
|
|
|
|
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-09-08 12:45:08 +00:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAttach(Activity activity) {
|
|
|
|
super.onAttach(activity);
|
|
|
|
|
|
|
|
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
|
|
|
|
}
|
|
|
|
|
2011-09-09 11:58:11 +00:00
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView<?> av, View view, int position, long id) {
|
2011-11-23 10:55:05 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void initialize(String sessionId, int feedId) {
|
|
|
|
m_sessionId = sessionId;
|
|
|
|
m_feedId = feedId;
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void refresh() {
|
|
|
|
HeadlinesRequest req = new HeadlinesRequest();
|
2011-09-09 11:58:11 +00:00
|
|
|
|
2011-11-23 10:55:05 +00:00
|
|
|
req.setApi(m_prefs.getString("ttrss_url", null));
|
|
|
|
|
|
|
|
HashMap<String,String> map = new HashMap<String,String>() {
|
|
|
|
{
|
|
|
|
put("op", "getHeadlines");
|
|
|
|
put("sid", m_sessionId);
|
|
|
|
put("feed_id", String.valueOf(m_feedId));
|
|
|
|
put("show_content", "true");
|
|
|
|
put("limit", String.valueOf(30));
|
|
|
|
put("offset", String.valueOf(0));
|
|
|
|
put("view_mode", "adaptive");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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-23 10:55:05 +00:00
|
|
|
out.putString("sessionId", m_sessionId);
|
|
|
|
out.putInt("feedId", m_feedId);
|
|
|
|
}
|
|
|
|
|
|
|
|
private class HeadlinesRequest extends ApiRequest {
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
getActivity().runOnUiThread(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
m_articles.clear();
|
|
|
|
|
|
|
|
for (Article f : articles)
|
|
|
|
m_articles.add(f);
|
|
|
|
|
|
|
|
m_adapter.notifyDataSetInvalidated();
|
|
|
|
|
|
|
|
View v = getView().findViewById(R.id.loading_container);
|
|
|
|
v.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
JsonObject content = rv.get("content").getAsJsonObject();
|
|
|
|
|
|
|
|
if (content != null) {
|
|
|
|
String error = content.get("error").getAsString();
|
|
|
|
|
|
|
|
/* m_sessionId = null;
|
|
|
|
|
|
|
|
if (error.equals("LOGIN_ERROR")) {
|
|
|
|
setLoadingStatus(R.string.login_wrong_password, false);
|
|
|
|
} else if (error.equals("API_DISABLED")) {
|
|
|
|
setLoadingStatus(R.string.login_api_disabled, false);
|
|
|
|
} else {
|
|
|
|
setLoadingStatus(R.string.login_failed, false);
|
|
|
|
} */
|
|
|
|
|
|
|
|
// TODO report error back to MainActivity
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
|
|
MainActivity ma = (MainActivity)getActivity();
|
|
|
|
ma.toast("Error parsing headlines: incorrect format");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
MainActivity ma = (MainActivity)getActivity();
|
|
|
|
ma.toast("Error parsing headlines: null object.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class ArticleListAdapter extends ArrayAdapter<Article> {
|
|
|
|
private ArrayList<Article> items;
|
|
|
|
|
|
|
|
public ArticleListAdapter(Context context, int textViewResourceId, ArrayList<Article> items) {
|
|
|
|
super(context, textViewResourceId, items);
|
|
|
|
this.items = items;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View getView(int position, View convertView, ViewGroup parent) {
|
|
|
|
|
|
|
|
View v = convertView;
|
|
|
|
|
|
|
|
Article article = items.get(position);
|
|
|
|
|
|
|
|
if (v == null) {
|
|
|
|
LayoutInflater vi = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
|
|
|
v = vi.inflate(R.layout.headlines_row, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
TextView tt = (TextView) v.findViewById(R.id.title);
|
|
|
|
|
|
|
|
if (tt != null) {
|
|
|
|
tt.setText(article.title);
|
|
|
|
//tt.setTextAppearance(getContext(), R.style.Connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
TextView te = (TextView) v.findViewById(R.id.excerpt);
|
|
|
|
|
|
|
|
if (te != null) {
|
|
|
|
String excerpt = Jsoup.parse(article.content).text();
|
|
|
|
|
|
|
|
if (excerpt.length() > 250)
|
|
|
|
excerpt = excerpt.substring(0, 250) + "...";
|
|
|
|
|
|
|
|
te.setText(excerpt);
|
|
|
|
}
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
2011-09-10 08:16:59 +00:00
|
|
|
}
|
|
|
|
|
2011-09-08 12:45:08 +00:00
|
|
|
}
|