summaryrefslogtreecommitdiff
path: root/orgfoxttrss/src/main/java/org/fox/ttrss/share/SubscribeActivity.java
blob: bd7e964f3d99a4b6579b03c0f6e28105e20d4563 (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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package org.fox.ttrss.share;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

import org.fox.ttrss.ApiRequest;
import org.fox.ttrss.R;
import org.fox.ttrss.types.FeedCategory;
import org.fox.ttrss.types.FeedCategoryList;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

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

public class SubscribeActivity extends CommonShareActivity {
	private final String TAG = this.getClass().getSimpleName();
	
	private Button m_postButton;
	private Button m_catButton;
	private CatListAdapter m_adapter;
	private FeedCategoryList m_cats = new FeedCategoryList();
	
	private static final int REQ_CATS = 1;
	private static final int REQ_POST = 2;
	
	class CatTitleComparator implements Comparator<FeedCategory> {

		@Override
		public int compare(FeedCategory a, FeedCategory b) {
			if (a.id >= 0 && b.id >= 0)
				return a.title.compareTo(b.title);
			else
				return a.id - b.id;
		}
		
	}
	
	public void sortCats() {
		Comparator<FeedCategory> cmp = new CatTitleComparator();
		
		Collections.sort(m_cats, cmp);
		try {
			m_adapter.notifyDataSetChanged();
		} catch (NullPointerException e) {
			// adapter missing
		}
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_LEFT_ICON);

		String urlValue = getIntent().getDataString();
		
		if (urlValue == null) 
			urlValue = getIntent().getStringExtra(Intent.EXTRA_TEXT); 
		
		if (savedInstanceState != null) {
			urlValue = savedInstanceState.getString("url");
			
			ArrayList<FeedCategory> list = savedInstanceState.getParcelableArrayList("cats");
			
			for (FeedCategory c : list)
				m_cats.add(c);
		}

		setContentView(R.layout.subscribe);
		
		getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.icon);
		
		setSmallScreen(false); 
		
		Spinner catList = (Spinner) findViewById(R.id.category_spinner);

		if (m_cats.size() == 0) m_cats.add(new FeedCategory(0, "Uncategorized", 0));
		
		m_adapter = new CatListAdapter(this, android.R.layout.simple_spinner_dropdown_item, m_cats);
		catList.setAdapter(m_adapter);
		
		EditText feedUrl = (EditText) findViewById(R.id.feed_url);
		feedUrl.setText(urlValue);
		
		m_postButton = (Button) findViewById(R.id.subscribe_button);
		
		m_postButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				login(REQ_POST);
			} 
		});
		
		m_catButton = (Button) findViewById(R.id.cats_button);
		
		m_catButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				login(REQ_CATS);
			} 
		});
		
		login(REQ_CATS);
	}

	@Override
	public void onSaveInstanceState(Bundle out) {
		super.onSaveInstanceState(out);

		EditText url = (EditText) findViewById(R.id.url);

		if (url != null) {
			out.putString("url", url.getText().toString());
		}
		
		out.putParcelableArrayList("cats", m_cats);

	}
	
	private void subscribeToFeed() {
		m_postButton.setEnabled(false);
		
		ApiRequest req = new ApiRequest(getApplicationContext()) {
			protected void onPostExecute(JsonElement result) {
				setProgressBarIndeterminateVisibility(false);

				if (m_lastError != ApiError.NO_ERROR) {
					toast(getErrorMessage());
				} else {
					try {
						int rc = -1;
						
						try {
							rc = result.getAsJsonObject().get("status").getAsJsonObject().get("code").getAsInt();
						} catch (Exception e) {
							e.printStackTrace();
						}

						switch (rc) {
						case -1:
							toast(R.string.error_api_unknown);
							//finish();
							break;
						case 0:
							toast(R.string.error_feed_already_exists_);
							//finish();
							break;
						case 1:
							toast(R.string.subscribed_to_feed);
							finish();
							break;
						case 2:
							toast(R.string.error_invalid_url);
							break;
						case 3:
							toast(R.string.error_url_is_an_html_page_no_feeds_found);
							break;
						case 4:
							toast(R.string.error_url_contains_multiple_feeds);
							break;
						case 5:
							toast(R.string.error_could_not_download_url);
							break;						
						}
						
					} catch (Exception e) {
						toast(R.string.error_while_subscribing);
						e.printStackTrace();
					}
				}
				
				m_postButton.setEnabled(true);
			}
		};
		
		Spinner catSpinner = (Spinner) findViewById(R.id.category_spinner);
		
		final FeedCategory cat = (FeedCategory) m_adapter.getCategory(catSpinner.getSelectedItemPosition());		
		final EditText feedUrl = (EditText) findViewById(R.id.feed_url);

		if (feedUrl != null ) {
			HashMap<String, String> map = new HashMap<String, String>() {
				{
					put("sid", m_sessionId);
					put("op", "subscribeToFeed");
					put("feed_url", feedUrl.getText().toString());

					if (cat != null) {
						put("category_id", String.valueOf(cat.id));
					}
				}
			};
	
			setProgressBarIndeterminateVisibility(true);
			
			req.execute(map);
		}
	}
	
	@Override
	public void onLoggingIn(int requestId) {
		switch (requestId) {
		case REQ_CATS:
			m_catButton.setEnabled(false);
			break;
		case REQ_POST:
			m_postButton.setEnabled(false);
			break;
		}
	}
	
	private void updateCats() {
		ApiRequest req = new ApiRequest(getApplicationContext()) {
			protected void onPostExecute(JsonElement result) {
				setProgressBarIndeterminateVisibility(false);

				if (m_lastError != ApiError.NO_ERROR) {
					toast(getErrorMessage());
				} else {
					JsonArray content = result.getAsJsonArray();
					
					if (content != null) {
						Type listType = new TypeToken<List<FeedCategory>>() {}.getType();
						final List<FeedCategory> cats = new Gson().fromJson(content, listType);
						
						m_cats.clear();
												
						for (FeedCategory c : cats) {
							if (c.id > 0)
								m_cats.add(c);							
						}
						
						sortCats();
						
						m_cats.add(0, new FeedCategory(0, "Uncategorized", 0));
						
						m_adapter.notifyDataSetChanged();
												
						toast(R.string.category_list_updated);
					}
				}
				
				m_catButton.setEnabled(true);
			}
		};

		HashMap<String, String> map = new HashMap<String, String>() {
			{
				put("sid", m_sessionId);
				put("op", "getCategories");
			}
		};
	
		setProgressBarIndeterminateVisibility(true);
			
		req.execute(map);
	}
	
	@Override
	protected void onLoggedIn(int requestId) {
		switch (requestId) {
		case REQ_CATS:
			updateCats();			
			break;
		case REQ_POST:			
			m_postButton.setEnabled(true);			
			if (m_apiLevel < 5) {
				toast(R.string.api_too_low);									
			} else {
				subscribeToFeed();									
			}		
			break;
		}	
	}

	private class CatListAdapter extends ArrayAdapter<String> {
		private List<FeedCategory> m_items;
		
		public CatListAdapter(Context context, int resource,
				List<FeedCategory> items) {
			super(context, resource);
			
			m_items = items;
		}

		@Override
		public String getItem(int item) {
			return m_items.get(item).title;
		}
		
		public FeedCategory getCategory(int item) {
			try {
				return m_items.get(item);
			} catch (ArrayIndexOutOfBoundsException e) {
				return null;
			}
		}
		
		@Override
		public int getCount() {
			return m_items.size();
		}
	}
	
}