implement fetching of feeds

This commit is contained in:
Andrew Dolgov 2011-09-08 15:28:38 +04:00
parent a04f748014
commit a14e9cd14e
9 changed files with 294 additions and 48 deletions

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<ListView android:layout_weight="1" android:background="?feedlistBackground"
android:layout_width="match_parent" android:id="@+id/feeds" android:layout_height="match_parent"></ListView>
</LinearLayout>
<ProgressBar android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center" android:id="@+id/loading_progress"/>
</FrameLayout>

27
res/layout/feeds_row.xml Normal file
View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:orientation="horizontal"
android:gravity="center_vertical"
android:padding="6dip" android:id="@+id/feeds_row">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:paddingLeft="16dp"
android:text="{FEED}" android:textSize="16sp"/>
<TextView
android:id="@+id/unread_counter"
android:gravity="right"
android:textSize="12sp"
android:textStyle="bold"
android:textColor="?unreadCounterColor"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="{123}"/>
</LinearLayout>

View File

@ -1,7 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal">
<LinearLayout android:layout_weight="1.5" android:layout_height="match_parent" android:id="@+id/linearLayout4" android:layout_width="match_parent">
<LinearLayout android:layout_height="match_parent" android:id="@+id/feeds_container" android:layout_width="match_parent">
</LinearLayout>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="?feedlistDivider"
android:paddingLeft="2dip" android:paddingRight="2dip"
android:layout_width="wrap_content" android:layout_height="match_parent"></ImageView>
</LinearLayout>
<LinearLayout android:layout_weight="0.5" android:layout_height="match_parent" android:id="@+id/headlines_container" android:layout_width="match_parent">
</LinearLayout>
</LinearLayout>

View File

@ -1,4 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="placeholder" format="reference|color" />
<attr name="feedlistDivider" format="reference|color" />
<attr name="feedlistBackground" format="reference|color" />
<attr name="unreadCounterColor" format="reference|color" />
</resources>

View File

@ -1,8 +1,14 @@
<resources>
<style name="LightTheme" parent="android:Theme.Holo.Light">
<item name="feedlistDivider">@android:drawable/divider_horizontal_bright</item>
<item name="feedlistBackground">#fafafa</item>
<item name="unreadCounterColor">#0000ff</item>
</style>
<style name="DarkTheme" parent="android:Theme.Holo">
<item name="feedlistDivider">@android:drawable/divider_horizontal_dark</item>
<item name="feedlistBackground">#101010</item>
<item name="unreadCounterColor">#0000ff</item>
</style>
</resources>

View File

@ -1,30 +1,22 @@
package org.fox.ttrss;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.util.Log;
public class ApiRequest extends AsyncTask<HashMap<String,String>, Integer, JsonElement> {
private final String TAG = this.getClass().getSimpleName();

View File

@ -0,0 +1,196 @@
package org.fox.ttrss;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
public class FeedsFragment extends Fragment {
private final String TAG = this.getClass().getSimpleName();
protected ArrayList<Feed> m_feeds = new ArrayList<Feed>();
protected FeedsListAdapter m_adapter;
protected SharedPreferences m_prefs;
protected String m_sessionId;
protected Gson m_gson = new Gson();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
m_sessionId = savedInstanceState.getString("sessionId");
}
View view = inflater.inflate(R.layout.feeds_fragment, container, false);
m_adapter = new FeedsListAdapter(getActivity(), R.id.feeds_row, m_feeds);
ListView list = (ListView) view.findViewById(R.id.feeds);
if (list != null) {
list.setAdapter(m_adapter);
}
return view;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
refresh();
}
public void initialize(String sessionId) {
m_sessionId = sessionId;
}
@SuppressWarnings("unchecked")
private void refresh() {
ApiRequest task = new ApiRequest(null, m_prefs.getString("ttrss_url", null)) {
@Override
protected void onPostExecute(JsonElement result) {
if (result != null) {
try {
JsonObject rv = result.getAsJsonObject();
int status = rv.get("status").getAsInt();
if (status == 0) {
Type listType = new TypeToken<List<Feed>>() {}.getType();
List<Feed> feeds = m_gson.fromJson(rv.get("content"), listType);
Collections.sort(feeds);
if (feeds != null) {
m_feeds.clear();
for (Feed feed : feeds) {
if (feed.id == -4 || feed.id > 0)
m_feeds.add(feed);
}
m_adapter.notifyDataSetChanged();
View v = getView().findViewById(R.id.loading_progress);
if (v != null) v.setVisibility(View.GONE);
return;
}
} else {
JsonObject content = rv.get("content").getAsJsonObject();
if (content != null) {
String error = content.get("error").getAsString();
if (error.equals("NOT_LOGGED_IN")) {
MainActivity ma = (MainActivity)getActivity();
if (ma != null) ma.logout();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
task.execute(new HashMap<String,String>() {
{
put("sid", m_sessionId);
put("op", "getFeeds");
put("cat_id", "-4");
put("unread_only", "true");
}
});
}
@Override
public void onSaveInstanceState (Bundle out) {
super.onSaveInstanceState(out);
out.putString("sessionId", m_sessionId);
}
private class FeedsListAdapter extends ArrayAdapter<Feed> {
private ArrayList<Feed> items;
public FeedsListAdapter(Context context, int textViewResourceId, ArrayList<Feed> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
Feed item = items.get(position);
if (v == null) {
LayoutInflater vi = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.feeds_row, null);
}
TextView title = (TextView) v.findViewById(R.id.title);
if (title != null) {
title.setText(item.title);
}
TextView unread = (TextView) v.findViewById(R.id.unread_counter);
if (unread != null) {
unread.setText(String.valueOf(item.unread));
}
return v;
}
}
private class Feed implements Comparable<Feed> {
String feed_url;
String title;
int id;
int unread;
boolean has_icon;
int cat_id;
int last_updated;
@Override
public int compareTo(Feed feed) {
return feed.unread - this.unread;
}
}
}

View File

@ -1,46 +1,25 @@
package org.fox.ttrss;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import org.json.JSONTokener;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
public class LoginActivity extends Activity {
private final String TAG = this.getClass().getSimpleName();
private SharedPreferences m_prefs;
private String m_themeName = "";
private String m_sessionId = null;
@Override
public void onCreate(Bundle savedInstanceState) {
@ -57,6 +36,8 @@ public class LoginActivity extends Activity {
m_themeName = m_prefs.getString("theme", "THEME_DARK");
setContentView(R.layout.login);
performLogin();
}
protected void updateLoginStatus(int id) {

View File

@ -1,6 +1,7 @@
package org.fox.ttrss;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
@ -37,7 +38,17 @@ public class MainActivity extends Activity {
m_sessionId = savedInstanceState.getString("sessionId");
}
setContentView(R.layout.main);
setContentView(R.layout.main);
FragmentTransaction ft = getFragmentManager().beginTransaction();
FeedsFragment frag = new FeedsFragment();
frag.initialize(m_sessionId);
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
ft.replace(R.id.feeds_container, frag);
ft.commit();
}
@Override
@ -73,14 +84,18 @@ public class MainActivity extends Activity {
startActivityForResult(intent, 0);
return true;
case R.id.logout:
intent = new Intent(this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivityForResult(intent, 0);
finish();
logout();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
protected void logout() {
Intent intent = new Intent(this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivityForResult(intent, 0);
finish();
}
}