summaryrefslogtreecommitdiff
path: root/src/org/fox/ttrss/FeedsFragment.java
blob: 7c192b488bb3ffd4b9cc62c96fb8aadf876974cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package org.fox.ttrss;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.BaseColumns;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;

public class FeedsFragment extends Fragment implements OnItemClickListener {
	private final String TAG = this.getClass().getSimpleName();

//	protected ArrayList<Feed> m_feeds = new ArrayList<Feed>();
	protected FeedsListAdapter m_adapter;
	protected SharedPreferences m_prefs;
	protected int m_activeFeedId;
	protected long m_lastUpdate;
	protected Gson m_gson = new Gson();
	protected Cursor m_cursor;

	private Timer m_timer;
	private TimerTask m_updateTask = new TimerTask() {
		@Override
		public void run() {

			getActivity().runOnUiThread(new Runnable() {
				@Override
				public void run() {
					updateSelf();
				}				
			});			
		}		
	};
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {    	

		if (savedInstanceState != null) {
			m_activeFeedId = savedInstanceState.getInt("activeFeedId");
			m_lastUpdate = savedInstanceState.getLong("lastUpdate");
		}
		
		View view = inflater.inflate(R.layout.feeds_fragment, container, false);

		DatabaseHelper helper = new DatabaseHelper(getActivity());
		m_cursor = helper.getReadableDatabase().query("feeds_unread", null, null, null, null, null, "unread DESC");
		
		m_adapter = new FeedsListAdapter(getActivity(), R.layout.feeds_row, m_cursor,
				new String[] { "title", "unread" }, new int[] { R.id.title, R.id.unread_counter }, 0);
		
		ListView list = (ListView) view.findViewById(R.id.feeds);
		
		if (list != null) {
			list.setAdapter(m_adapter);		
			list.setOnItemClickListener(this);
			list.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
		}

		//updateSelf();
		
		m_timer = new Timer("UpdateFeeds");
		m_timer.schedule(m_updateTask, 1000L, 60*1000L);

		return view;    	
	}

	@Override
	public void onDestroy() {
		super.onDestroy();

		m_timer.cancel();
		m_timer = null;
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);		
		m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
	}

	private void updateSelf() {
		String sessionId = ((MainActivity)getActivity()).getSessionId();
		
		ApiRequest task = new ApiRequest(sessionId, 
				m_prefs.getString("ttrss_url", null),
				m_prefs.getString("login", null),
				m_prefs.getString("password", null)) {
			@Override
			protected void onPostExecute(JsonElement result) {
				if (result != null && getAuthStatus() == STATUS_OK) {
					try {
						((MainActivity)getActivity()).setSessionId(getSessionId());
						
						JsonArray feeds_object = (JsonArray) result.getAsJsonArray();
						
						Type listType = new TypeToken<List<Feed>>() {}.getType();
						List<Feed> feeds = m_gson.fromJson(feeds_object, listType);

						DatabaseHelper dh = new DatabaseHelper(getActivity());
						SQLiteDatabase db = dh.getWritableDatabase();

						/* db.execSQL("DELETE FROM FEEDS"); */
						
						/* SQLiteStatement stmFind = db.compileStatement("SELECT "+BaseColumns._ID+" FROM feeds WHERE "+
								BaseColumns._ID+" = ?"); */

						SQLiteStatement stmtUpdate = db.compileStatement("UPDATE feeds SET " +
								"title = ?, feed_url = ?, has_icon = ?, cat_id = ?, last_updated = ? WHERE " +
								BaseColumns._ID + " = ?");

						SQLiteStatement stmtInsert = db.compileStatement("INSERT INTO feeds " +
								"("+BaseColumns._ID+", title, feed_url, has_icon, cat_id, last_updated) " +
								"VALUES (?, ?, ?, ?, ?, ?);");

						for (Feed feed : feeds) {
							Cursor c = db.query("feeds", new String[] { BaseColumns._ID } , BaseColumns._ID + "=?", 
									new String[] { String.valueOf(feed.id) }, null, null, null);
							
							if (c.getCount() != 0) {
								stmtUpdate.bindString(1, feed.title);
								stmtUpdate.bindString(2, feed.feed_url);								
								stmtUpdate.bindLong(3, feed.has_icon ? 1 : 0);
								stmtUpdate.bindLong(4, feed.cat_id);
								stmtUpdate.bindLong(5, feed.last_updated);
								stmtUpdate.bindLong(6, feed.id);
								stmtUpdate.execute();								
								
							} else {
								stmtInsert.bindLong(1, feed.id);
								stmtInsert.bindString(2, feed.title);
								stmtInsert.bindString(3, feed.feed_url);
								stmtInsert.bindLong(4, feed.has_icon ? 1 : 0);
								stmtInsert.bindLong(5, feed.cat_id);
								stmtInsert.bindLong(6, feed.last_updated);
								stmtInsert.execute();
							}
							
							c.close();
						} 
						
						// TODO delete not returned feeds which has no data here
						
						db.close();
						
						m_cursor.requery();
						m_adapter.notifyDataSetChanged();						
					
					} catch (Exception e) {
						e.printStackTrace();
					}										
				}
				
			} 
		};
		
		task.execute(new HashMap<String,String>() {   
			{
				put("sid", ((MainActivity)getActivity()).getSessionId());
				put("op", "getFeeds");
				put("cat_id", "-3");
				put("unread_only", "true");
			}			 
		});

	}

	@Override
	public void onSaveInstanceState (Bundle out) {
		super.onSaveInstanceState(out);
		
		out.putInt("activeFeedId", m_activeFeedId);
		out.putLong("lastUpdate", m_lastUpdate);
	}
	
	class FeedsListAdapter extends SimpleCursorAdapter {

		private Context context;
		private int layout;
		
		public FeedsListAdapter(Context context, int layout, Cursor c,
				String[] from, int[] to, int flags) {
			super(context, layout, c, from, to, flags);
			
	        this.context = context;
	        this.layout = layout;
		}		
		
	}

	@Override
	public void onItemClick(AdapterView<?> av, View view, int position, long id) {
		ListView list = (ListView)getActivity().findViewById(R.id.feeds);
		
		if (list != null) {
			Cursor cursor = (Cursor) list.getItemAtPosition(position);
			
			if (cursor != null) {
				int feedId = (int) cursor.getLong(0);

				Log.d(TAG, "clicked on feed " + feedId);
				
				viewFeed(feedId);				
			}			
		}		
	} 

	private void viewFeed(int feedId) {
		m_activeFeedId = feedId;
		
		FragmentTransaction ft = getFragmentManager().beginTransaction();			
		HeadlinesFragment frag = new HeadlinesFragment();
		
		frag.initialize(feedId);
		
		ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
		ft.replace(R.id.headlines_container, frag);
		ft.commit();
		
		m_adapter.notifyDataSetChanged();

	}	

}