tt-rss-android/src/org/fox/ttrss/FeedsFragment.java

454 lines
12 KiB
Java
Raw Normal View History

2011-09-08 11:28:38 +00:00
package org.fox.ttrss;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
2011-11-23 10:55:05 +00:00
import java.lang.reflect.Type;
import java.net.URL;
2011-11-23 10:55:05 +00:00
import java.util.ArrayList;
2011-11-23 17:07:13 +00:00
import java.util.Collections;
2011-11-26 06:31:14 +00:00
import java.util.Comparator;
2011-11-23 10:55:05 +00:00
import java.util.HashMap;
import java.util.List;
2011-09-08 11:28:38 +00:00
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
2011-09-08 11:28:38 +00:00
import android.os.Bundle;
import android.os.Environment;
2011-09-08 11:28:38 +00:00
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.util.Log;
2011-09-08 11:28:38 +00:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
2011-11-23 10:55:05 +00:00
import android.widget.ArrayAdapter;
2011-11-28 11:25:55 +00:00
import android.widget.ImageView;
2011-09-08 11:28:38 +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 11:28:38 +00:00
public class FeedsFragment extends Fragment implements OnItemClickListener {
@SuppressWarnings("unused")
2011-09-08 11:28:38 +00:00
private final String TAG = this.getClass().getSimpleName();
2011-11-22 12:13:02 +00:00
private SharedPreferences m_prefs;
2011-11-23 10:55:05 +00:00
private FeedListAdapter m_adapter;
private FeedList m_feeds = new FeedList();
private OnFeedSelectedListener m_feedSelectedListener;
private int m_selectedFeedId;
private static final String ICON_PATH = "/org.fox.ttrss-icons/";
private boolean m_enableFeedIcons;
public interface OnFeedSelectedListener {
public void onFeedSelected(Feed feed);
}
2011-11-24 05:59:11 +00:00
2011-11-26 06:31:14 +00:00
class FeedUnreadComparator implements Comparator<Feed> {
@Override
public int compare(Feed a, Feed b) {
if (a.unread != b.unread)
return b.unread - a.unread;
else
return a.title.compareTo(b.title);
}
}
class FeedTitleComparator implements Comparator<Feed> {
@Override
public int compare(Feed a, Feed b) {
2011-11-28 06:14:52 +00:00
if (a.id >= 0 && b.id >= 0)
return a.title.compareTo(b.title);
else
return a.id - b.id;
2011-11-26 06:31:14 +00:00
}
}
2011-09-08 11:28:38 +00:00
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
2011-09-08 11:28:38 +00:00
if (savedInstanceState != null) {
m_selectedFeedId = savedInstanceState.getInt("selectedFeedId");
m_feeds = savedInstanceState.getParcelable("feeds");
2011-09-08 11:28:38 +00:00
}
2011-09-09 16:36:09 +00:00
2011-09-08 11:28:38 +00:00
View view = inflater.inflate(R.layout.feeds_fragment, container, false);
2011-11-22 14:30:09 +00:00
2011-11-23 10:55:05 +00:00
ListView list = (ListView)view.findViewById(R.id.feeds);
m_adapter = new FeedListAdapter(getActivity(), R.layout.feeds_row, (ArrayList<Feed>)m_feeds);
list.setAdapter(m_adapter);
list.setOnItemClickListener(this);
m_enableFeedIcons = m_prefs.getBoolean("enable_feed_icons", false);
if (m_feeds == null || m_feeds.size() == 0)
refresh(false);
else
2011-11-27 07:18:54 +00:00
view.findViewById(R.id.loading_progress).setVisibility(View.GONE);
2011-09-08 11:28:38 +00:00
return view;
}
@Override
public void onDestroy() {
super.onDestroy();
}
2011-09-09 16:36:09 +00:00
2011-09-08 11:28:38 +00:00
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
2011-09-08 11:28:38 +00:00
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
m_feedSelectedListener = (OnFeedSelectedListener) activity;
2011-09-08 11:28:38 +00:00
}
2011-11-23 10:55:05 +00:00
@Override
2011-09-08 11:28:38 +00:00
public void onSaveInstanceState (Bundle out) {
super.onSaveInstanceState(out);
2011-09-09 16:36:09 +00:00
out.putInt("selectedFeedId", m_selectedFeedId);
out.putParcelable("feeds", m_feeds);
2011-11-23 12:14:41 +00:00
}
@Override
public void onItemClick(AdapterView<?> av, View view, int position, long id) {
2011-11-23 10:55:05 +00:00
ListView list = (ListView)av;
if (list != null) {
Feed feed = (Feed)list.getItemAtPosition(position);
m_feedSelectedListener.onFeedSelected(feed);
m_selectedFeedId = feed.id;
m_adapter.notifyDataSetChanged();
2011-11-23 10:55:05 +00:00
}
2011-11-22 14:30:09 +00:00
}
@SuppressWarnings({ "unchecked", "serial" })
public void refresh(boolean background) {
FeedsRequest req = new FeedsRequest(getActivity().getApplicationContext());
2011-11-22 14:30:09 +00:00
2011-11-23 17:19:35 +00:00
final String sessionId = ((MainActivity)getActivity()).getSessionId();
2011-11-24 05:59:11 +00:00
final boolean unreadOnly = ((MainActivity)getActivity()).getUnreadOnly();
2011-11-23 17:19:35 +00:00
if (sessionId != null) {
if (!background) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
setLoadingStatus(R.string.blank, true);
}
});
}
2011-11-23 17:19:35 +00:00
HashMap<String,String> map = new HashMap<String,String>() {
{
put("op", "getFeeds");
put("sid", sessionId);
2011-11-28 06:14:52 +00:00
put("cat_id", "-4");
2011-11-24 05:59:11 +00:00
if (unreadOnly) {
put("unread_only", String.valueOf(unreadOnly));
}
2011-11-23 17:19:35 +00:00
}
};
2011-11-23 10:55:05 +00:00
2011-11-27 07:18:54 +00:00
req.execute(map);
2011-11-23 17:19:35 +00:00
}
2011-11-23 10:55:05 +00:00
}
public void setLoadingStatus(int status, boolean showProgress) {
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-23 10:55:05 +00:00
}
}
@SuppressWarnings({ "unchecked", "serial" })
public void getFeedIcons() {
ApiRequest req = new ApiRequest(getActivity().getApplicationContext()) {
protected void onPostExecute(JsonElement result) {
if (result != null) {
try {
JsonElement iconsUrl = result.getAsJsonObject().get("content").getAsJsonObject().get("icons_dir");
if (iconsUrl != null) {
String iconsStr = iconsUrl.getAsString();
String baseUrl = "";
if (!iconsStr.contains("://")) {
baseUrl = m_prefs.getString("ttrss_url", "") + "/" + iconsStr;
} else {
baseUrl = iconsStr;
}
GetIconsTask git = new GetIconsTask(baseUrl);
git.execute(m_feeds);
}
} catch (Exception e) {
Log.d(TAG, "Error receiving icons configuration");
e.printStackTrace();
}
}
}
};
final String sessionId = ((MainActivity)getActivity()).getSessionId();
HashMap<String,String> map = new HashMap<String,String>() {
{
put("sid", sessionId);
put("op", "getConfig");
}
};
req.execute(map);
}
2011-11-23 10:55:05 +00:00
private class FeedsRequest extends ApiRequest {
public FeedsRequest(Context context) {
super(context);
}
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<Feed>>() {}.getType();
final List<Feed> feeds = gson.fromJson(content, listType);
m_feeds.clear();
2011-11-28 06:14:52 +00:00
for (Feed f : feeds)
if (f.id > -10) // skip labels for now
m_feeds.add(f);
sortFeeds();
if (m_feeds.size() == 0)
setLoadingStatus(R.string.error_no_feeds, false);
else
setLoadingStatus(R.string.blank, false);
if (m_enableFeedIcons) getFeedIcons();
2011-11-23 10:55:05 +00:00
}
} else {
MainActivity activity = (MainActivity)getActivity();
activity.login();
2011-11-23 10:55:05 +00:00
}
} catch (Exception e) {
e.printStackTrace();
setLoadingStatus(R.string.error_invalid_object, false);
// report invalid object received
2011-11-23 10:55:05 +00:00
}
} else {
// report null object received, unless we've been awakened from sleep right in the right time
// so that current request failed
if (m_feeds.size() == 0) setLoadingStatus(R.string.error_no_data, false);
}
return;
}
}
2011-11-23 10:55:05 +00:00
private class FeedListAdapter extends ArrayAdapter<Feed> {
private ArrayList<Feed> items;
public static final int VIEW_NORMAL = 0;
public static final int VIEW_SELECTED = 1;
public static final int VIEW_COUNT = VIEW_SELECTED+1;
2011-11-23 10:55:05 +00:00
public FeedListAdapter(Context context, int textViewResourceId, ArrayList<Feed> items) {
super(context, textViewResourceId, items);
this.items = items;
}
public int getViewTypeCount() {
return VIEW_COUNT;
}
@Override
public int getItemViewType(int position) {
Feed feed = items.get(position);
if (feed.id == m_selectedFeedId) {
return VIEW_SELECTED;
} else {
return VIEW_NORMAL;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
2011-11-23 10:55:05 +00:00
View v = convertView;
Feed feed = items.get(position);
if (v == null) {
int layoutId = R.layout.feeds_row;
switch (getItemViewType(position)) {
case VIEW_SELECTED:
layoutId = R.layout.feeds_row_selected;
break;
}
2011-11-23 10:55:05 +00:00
LayoutInflater vi = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(layoutId, null);
2011-11-23 10:55:05 +00:00
}
TextView tt = (TextView) v.findViewById(R.id.title);
if (tt != null) {
tt.setText(feed.title);
}
TextView tu = (TextView) v.findViewById(R.id.unread_counter);
if (tu != null) {
tu.setText(String.valueOf(feed.unread));
tu.setVisibility((feed.unread > 0) ? View.VISIBLE : View.INVISIBLE);
2011-11-28 11:25:55 +00:00
}
ImageView icon = (ImageView)v.findViewById(R.id.icon);
if (icon != null) {
if (m_enableFeedIcons) {
File storage = Environment.getExternalStorageDirectory();
File iconFile = new File(storage.getAbsolutePath() + ICON_PATH + feed.id + ".ico");
if (iconFile.exists()) {
Bitmap bmpOrig = BitmapFactory.decodeFile(iconFile.getAbsolutePath());
if (bmpOrig != null) {
Bitmap bmp = Bitmap.createScaledBitmap(bmpOrig, 28, 28, true);
icon.setImageBitmap(bmp);
}
} else {
icon.setImageResource(feed.unread > 0 ? R.drawable.ic_rss : R.drawable.ic_rss_bw);
}
} else {
icon.setImageResource(feed.unread > 0 ? R.drawable.ic_rss : R.drawable.ic_rss_bw);
}
2011-11-28 11:25:55 +00:00
}
2011-11-23 10:55:05 +00:00
return v;
}
2011-11-22 14:30:09 +00:00
}
2011-11-26 06:31:14 +00:00
public void sortFeeds() {
Comparator<Feed> cmp;
if (m_prefs.getBoolean("sort_feeds_by_unread", false)) {
cmp = new FeedUnreadComparator();
} else {
cmp = new FeedTitleComparator();
}
Collections.sort(m_feeds, cmp);
m_adapter.notifyDataSetInvalidated();
}
public class GetIconsTask extends AsyncTask<FeedList, Integer, Integer> {
private String m_baseUrl;
public GetIconsTask(String baseUrl) {
m_baseUrl = baseUrl;
}
@Override
protected Integer doInBackground(FeedList... params) {
try {
File storage = Environment.getExternalStorageDirectory();
final File iconPath = new File(storage.getAbsolutePath() + ICON_PATH);
if (!iconPath.exists()) iconPath.mkdir();
final FeedList feeds = params[0];
if (iconPath.exists()) {
for (Feed feed : feeds) {
if (feed.id > 0 && feed.has_icon) {
File iconFile = new File(iconPath.getAbsolutePath() + "/" + feed.id + ".ico");
String fetchUrl = m_baseUrl + "/" + feed.id + ".ico";
if (!iconFile.exists()) {
//Log.d(TAG, "Downloading " + fetchUrl);
try {
BufferedInputStream is = new BufferedInputStream(new URL(fetchUrl).openStream(), 1024);
FileOutputStream fos = new FileOutputStream(iconFile);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.close();
is.close();
} catch (Exception e) {
Log.d(TAG, "Error downloading " + fetchUrl);
e.printStackTrace();
}
}
}
}
}
} catch (Exception e) {
Log.d(TAG, "Error while downloading feed icons");
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Integer result) {
m_adapter.notifyDataSetInvalidated();
}
}
2011-09-08 11:28:38 +00:00
}