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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
package org.fox.ttrss.offline;
import android.app.Activity;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.BaseColumns;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.viewpagerindicator.UnderlinePageIndicator;
import org.fox.ttrss.R;
public class OfflineArticlePager extends Fragment {
private final String TAG = this.getClass().getSimpleName();
private PagerAdapter m_adapter;
private OfflineActivity m_activity;
private OfflineHeadlinesEventListener m_listener;
private boolean m_isCat;
private int m_feedId;
private int m_articleId;
private String m_searchQuery = "";
private Cursor m_cursor;
private SharedPreferences m_prefs;
public int getFeedId() {
return m_feedId;
}
public boolean getFeedIsCat() {
return m_isCat;
}
public Cursor createCursor() {
String feedClause = null;
if (m_isCat) {
feedClause = "feed_id IN (SELECT "+BaseColumns._ID+" FROM feeds WHERE cat_id = ?)";
} else {
feedClause = "feed_id = ?";
}
String viewMode = m_activity.getViewMode();
if ("adaptive".equals(viewMode)) {
// TODO: implement adaptive
} else if ("marked".equals(viewMode)) {
feedClause += "AND (marked = 1)";
} else if ("published".equals(viewMode)) {
feedClause += "AND (published = 1)";
} else if ("unread".equals(viewMode)) {
feedClause += "AND (unread = 1)";
} else { // all_articles
//
}
String orderBy = (m_prefs.getBoolean("offline_oldest_first", false)) ? "updated" : "updated DESC";
if (m_searchQuery == null || m_searchQuery.equals("")) {
return m_activity.getReadableDb().query("articles LEFT JOIN feeds ON (feed_id = feeds."+BaseColumns._ID+")",
new String[] { "articles."+BaseColumns._ID, "feeds.title AS feed_title" }, feedClause,
new String[] { String.valueOf(m_feedId) }, null, null, orderBy);
} else {
return m_activity.getReadableDb().query("articles LEFT JOIN feeds ON (feed_id = feeds."+BaseColumns._ID+")",
new String[] { "articles."+BaseColumns._ID },
feedClause + " AND (articles.title LIKE '%' || ? || '%' OR content LIKE '%' || ? || '%')",
new String[] { String.valueOf(m_feedId), m_searchQuery, m_searchQuery }, null, null, orderBy);
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (m_cursor != null && !m_cursor.isClosed()) m_cursor.close();
}
private class PagerAdapter extends FragmentStatePagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Log.d(TAG, "getItem: " + position);
if (m_cursor.moveToPosition(position)) {
OfflineArticleFragment oaf = new OfflineArticleFragment();
oaf.initialize(m_cursor.getInt(m_cursor.getColumnIndex(BaseColumns._ID)));
return oaf;
}
return null;
}
@Override
public int getCount() {
return m_cursor.getCount();
}
}
@Override
public void onResume() {
super.onResume();
}
public void initialize(int articleId, int feedId, boolean isCat) {
m_feedId = feedId;
m_isCat = isCat;
m_articleId = articleId;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.article_pager, container, false);
if (savedInstanceState != null) {
m_articleId = savedInstanceState.getInt("articleId", 0);
m_feedId = savedInstanceState.getInt("feedId", 0);
m_isCat = savedInstanceState.getBoolean("isCat", false);
}
Log.d(TAG, "feed=" + m_feedId + "; iscat=" + m_isCat);
m_cursor = createCursor();
m_adapter = new PagerAdapter(getActivity().getSupportFragmentManager());
int position = 0;
Log.d(TAG, "maId=" + m_articleId);
if (m_articleId != 0) {
if (m_cursor.moveToFirst()) {
while (!m_cursor.isAfterLast()) {
if (m_cursor.getInt(m_cursor.getColumnIndex(BaseColumns._ID)) == m_articleId) {
position = m_cursor.getPosition();
break;
}
m_cursor.moveToNext();
}
Log.d(TAG, "(1)maId=" + m_articleId);
m_listener.onArticleSelected(m_articleId, false);
}
} else {
if (m_cursor.moveToFirst()) {
m_articleId = m_cursor.getInt(m_cursor.getColumnIndex(BaseColumns._ID));
m_listener.onArticleSelected(m_articleId, false);
Log.d(TAG, "(2)maId=" + m_articleId);
}
}
ViewPager pager = (ViewPager) view.findViewById(R.id.article_pager);
pager.setAdapter(m_adapter);
UnderlinePageIndicator indicator = (UnderlinePageIndicator)view.findViewById(R.id.article_titles);
indicator.setViewPager(pager);
pager.setCurrentItem(position);
indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int position) {
if (m_cursor.moveToPosition(position)) {
int articleId = m_cursor.getInt(m_cursor.getColumnIndex(BaseColumns._ID));
m_articleId = articleId;
m_listener.onArticleSelected(articleId, false);
}
}
});
return view;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
m_activity = (OfflineActivity)activity;
m_listener = (OfflineHeadlinesEventListener)activity;
m_prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
}
public void refresh() {
if (m_cursor != null && !m_cursor.isClosed()) m_cursor.close();
m_cursor = createCursor();
if (m_cursor != null) {
m_adapter.notifyDataSetChanged();
}
}
public int getSelectedArticleId() {
return m_articleId;
}
@Override
public void onSaveInstanceState(Bundle out) {
super.onSaveInstanceState(out);
out.putInt("articleId", m_articleId);
out.putInt("feedId", m_feedId);
out.putBoolean("isCat", m_isCat);
}
public void setSearchQuery(String searchQuery) {
m_searchQuery = searchQuery;
}
public void setArticleId(int articleId) {
m_articleId = articleId;
int position = getArticleIdPosition(articleId);
ViewPager pager = (ViewPager) getView().findViewById(R.id.article_pager);
pager.setCurrentItem(position);
}
public int getArticleIdPosition(int articleId) {
m_cursor.moveToFirst();
while (!m_cursor.isAfterLast()) {
if (m_cursor.getInt(m_cursor.getColumnIndex(BaseColumns._ID)) == articleId) {
return m_cursor.getPosition();
}
m_cursor.moveToNext();
}
return -1;
}
public void selectArticle(boolean next) {
int position = getArticleIdPosition(m_articleId);
if (position != -1) {
if (next)
position++;
else
position--;
Log.d(TAG, "pos=" + position);
if (m_cursor.moveToPosition(position)) {
setArticleId(m_cursor.getInt(m_cursor.getColumnIndex(BaseColumns._ID)));
}
}
}
}
|