summaryrefslogtreecommitdiff
path: root/org.fox.ttrss/src/main/java/org/fox/ttrss/MasterActivity.java
blob: e94042685fbb45a06b11cf0336048419990b566c (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
package org.fox.ttrss;


import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.gson.JsonElement;

import org.fox.ttrss.types.Article;
import org.fox.ttrss.types.ArticleList;
import org.fox.ttrss.types.Feed;
import org.fox.ttrss.types.FeedCategory;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;

import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import icepick.State;

public class MasterActivity extends OnlineActivity implements HeadlinesEventListener {
	private final String TAG = this.getClass().getSimpleName();
	
	private static final int HEADLINES_REQUEST = 1;
	
	protected SharedPreferences m_prefs;
	protected long m_lastRefresh = 0;
	protected long m_lastWidgetRefresh = 0;
	
	@State protected boolean m_feedIsSelected = false;
    @State protected boolean m_userFeedSelected = false;

    private ActionBarDrawerToggle m_drawerToggle;
    private DrawerLayout m_drawerLayout;

	@SuppressLint("NewApi")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		m_prefs = PreferenceManager
				.getDefaultSharedPreferences(getApplicationContext());

		setAppTheme(m_prefs);

		super.onCreate(savedInstanceState);

		if (m_prefs.getBoolean("force_phone_layout", false)) {
			setContentView(R.layout.activity_master_phone);
		} else {
			setContentView(R.layout.activity_master);
		}

		setSmallScreen(findViewById(R.id.sw600dp_anchor) == null);

		Toolbar toolbar = findViewById(R.id.toolbar);
		setSupportActionBar(toolbar);

		Application.getInstance().load(savedInstanceState);

		m_lastWidgetRefresh = new Date().getTime();

        m_drawerLayout = findViewById(R.id.headlines_drawer);

        if (m_drawerLayout != null) {

			m_drawerToggle = new ActionBarDrawerToggle(this, m_drawerLayout, R.string.blank, R.string.blank) {
                @Override
                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);

                    getSupportActionBar().show();
                    invalidateOptionsMenu();

					Date date = new Date();
					if (date.getTime() - m_lastRefresh > 60*1000) {
						m_lastRefresh = date.getTime();
						refresh(false);
					}
                }

                @Override
                public void onDrawerClosed(View drawerView) {
                    super.onDrawerClosed(drawerView);

                    if (m_prefs.getBoolean("drawer_open_on_start", true)) {
                        SharedPreferences.Editor editor = m_prefs.edit();
                        editor.putBoolean("drawer_open_on_start", false);
                        editor.apply();
                    }

                    invalidateOptionsMenu();
                }
            };

            m_drawerLayout.setDrawerListener(m_drawerToggle);
            m_drawerToggle.setDrawerIndicatorEnabled(true);

            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
        }

        if (savedInstanceState == null) {
            if (m_drawerLayout != null && m_prefs.getBoolean("drawer_open_on_start", true)) {
                m_drawerLayout.openDrawer(Gravity.START);
            }

			final Intent i = getIntent();
			boolean shortcutMode = i.getBooleanExtra("shortcut_mode", false);
			
			Log.d(TAG, "is_shortcut_mode: " + shortcutMode);

			if (shortcutMode) {
				LoginRequest lr = new LoginRequest(this, false, new OnLoginFinishedListener() {
					
					@Override
					public void OnLoginSuccess() {
						int feedId = i.getIntExtra("feed_id", 0);
						boolean isCat = i.getBooleanExtra("feed_is_cat", false);
						String feedTitle = i.getStringExtra("feed_title");

						// app shortcuts are not allowed to pass string extras
						if (feedTitle == null) {
							switch (feedId) {
								case -1:
									feedTitle = getString(R.string.feed_starred_articles);
									break;
								case -3:
									feedTitle = getString(R.string.fresh_articles);
									break;
								case -4:
									feedTitle = getString(R.string.feed_all_articles);
									break;
							}
						}
						
						Feed tmpFeed = new Feed(feedId, feedTitle, isCat);
						
						onFeedSelected(tmpFeed, false);
					}
					
					@Override
					public void OnLoginFailed() {
						login();						
					}
				});
				
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("op", "login");
				map.put("user", m_prefs.getString("login", "").trim());
				map.put("password", m_prefs.getString("password", "").trim());

				lr.execute(map);
			}
			
			//m_pullToRefreshAttacher.setRefreshing(true);

			FragmentTransaction ft = getSupportFragmentManager().beginTransaction();

			if (m_prefs.getBoolean("enable_cats", false)) {
				ft.replace(R.id.feeds_fragment, new FeedCategoriesFragment(), FRAG_CATS);
			} else {
				ft.replace(R.id.feeds_fragment, new FeedsFragment(), FRAG_FEEDS);
			}

            if (!shortcutMode && m_prefs.getBoolean("open_fresh_on_startup", true)) {
                HeadlinesFragment hf = new HeadlinesFragment();

                if (BuildConfig.DEBUG) {
                    hf.initialize(new Feed(-1, getString(R.string.feed_starred_articles), false));
                } else {
                    hf.initialize(new Feed(-3, getString(R.string.fresh_articles), false));
                }

                ft.replace(R.id.headlines_fragment, hf, FRAG_HEADLINES);
            } else if (m_drawerLayout != null) {
                m_drawerLayout.openDrawer(Gravity.START);
            }

			ft.commit();

            m_feedIsSelected = true;

		} else { // savedInstanceState != null

			if (m_drawerLayout != null && !m_feedIsSelected) {
				m_drawerLayout.openDrawer(Gravity.START);
			}
		}

		FloatingActionButton fab = findViewById(R.id.master_fab);

        if (fab != null) {
        	fab.show();

        	fab.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);

					if (hf != null && hf.isAdded()) {
						hf.refresh(false);
					}
				}
			});
		}
	}

    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        if (m_drawerToggle != null) m_drawerToggle.syncState();
    }

	@Override
	protected void initMenu() {
		super.initMenu();
		
		if (m_menu != null && getSessionId() != null) {
			Fragment ff = getSupportFragmentManager().findFragmentByTag(FRAG_FEEDS);
			Fragment cf = getSupportFragmentManager().findFragmentByTag(FRAG_CATS);
			HeadlinesFragment hf = (HeadlinesFragment)getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);
			
			/* if (m_drawerLayout != null) {
                boolean isDrawerOpen = m_drawerLayout.isDrawerOpen(Gravity.START);

				m_menu.setGroupVisible(R.id.menu_group_feeds, isDrawerOpen);
				m_menu.setGroupVisible(R.id.menu_group_headlines, hf != null && hf.isAdded() && !isDrawerOpen);
			} else {
				m_menu.setGroupVisible(R.id.menu_group_feeds, (ff != null && ff.isAdded()) || (cf != null && cf.isAdded()));
				m_menu.setGroupVisible(R.id.menu_group_headlines, hf != null && hf.isAdded());
				
				m_menu.findItem(R.id.update_headlines).setVisible(false);
			} */

			m_menu.setGroupVisible(R.id.menu_group_feeds, (ff != null && ff.isAdded()) || (cf != null && cf.isAdded()));
			m_menu.setGroupVisible(R.id.menu_group_headlines, hf != null && hf.isAdded());

			//m_menu.findItem(R.id.headlines_toggle_sidebar).setVisible(false);
			
			/* MenuItem item = m_menu.findItem(R.id.show_feeds);

			if (getUnreadOnly()) {
				item.setTitle(R.string.menu_all_feeds);
			} else {
				item.setTitle(R.string.menu_unread_feeds);
			} */
		}		
	}

    public void onFeedSelected(Feed feed) {
        onFeedSelected(feed, true);
    }

	public void onFeedSelected(final Feed feed, final boolean selectedByUser) {

		FeedsFragment ff = (FeedsFragment) getSupportFragmentManager().findFragmentByTag(FRAG_FEEDS);

		if (ff != null && ff.isAdded()) {
			ff.setSelectedfeed(feed);
		}

		if (m_drawerLayout != null) {
			m_drawerLayout.closeDrawers();
		}

		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				FragmentTransaction ft = getSupportFragmentManager()
						.beginTransaction();

				HeadlinesFragment hf = new HeadlinesFragment();
				hf.initialize(feed);

				ft.replace(R.id.headlines_fragment, hf, FRAG_HEADLINES);

				ft.commit();

				m_feedIsSelected = true;
				m_userFeedSelected = selectedByUser;

			}
		}, 250);

        Date date = new Date();

        if (date.getTime() - m_lastRefresh > 30*1000) {
            m_lastRefresh = date.getTime();
            refresh(false);
        }
	}
	
	public void onCatSelected(FeedCategory cat, boolean openAsFeed) {
		FeedCategoriesFragment fc = (FeedCategoriesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_CATS);
		
		//m_pullToRefreshAttacher.setRefreshing(true);
		
		if (!openAsFeed) {
			
			if (fc != null && fc.isAdded()) {
				fc.setSelectedCategory(null);
			}

			FragmentTransaction ft = getSupportFragmentManager()
					.beginTransaction();

			FeedsFragment ff = new FeedsFragment();
			ff.initialize(cat, true);
			ft.replace(R.id.feeds_fragment, ff, FRAG_FEEDS);

			ft.addToBackStack(null);
			ft.commit();
			
			//getSupportActionBar().setDisplayHomeAsUpEnabled(true);
			//m_actionbarUpEnabled = true;
			//m_actionbarRevertDepth = m_actionbarRevertDepth + 1;

		} else {
			
			if (fc != null) {
				fc.setSelectedCategory(cat);
			}

			Feed feed = new Feed(cat.id, cat.title, true);
			onFeedSelected(feed);
		}
	}
	
	public void onCatSelected(FeedCategory cat) {
		onCatSelected(cat, m_prefs.getBoolean("browse_cats_like_feeds", false));		
	}

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

        finish();
    }

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
        if (m_drawerToggle != null && m_drawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        switch (item.getItemId()) {
        case R.id.headlines_toggle_sort_order:
            Dialog dialog = new Dialog(this);

            LinkedHashMap<String, String> sortModes = getSortModes();

			CharSequence[] sortTitles = sortModes.values().toArray(new CharSequence[0]);
			final CharSequence[] sortNames = sortModes.keySet().toArray(new CharSequence[0]);

			String currentMode = getSortMode();

			int i = 0;
			int selectedIndex = 0;

			for (CharSequence tmp : sortNames) {
				if (tmp.equals(currentMode)) {
					selectedIndex = i;
					break;
				}

				++i;
			}

            AlertDialog.Builder builder = new AlertDialog.Builder(this)
                    .setTitle(getString(R.string.headlines_sort_articles_title))
                    .setSingleChoiceItems(
							sortTitles,
                            selectedIndex, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog,
                                                    int which) {

                                	try {
//										Log.d(TAG, "sort selected index:" + which + ": " + sortNames[which]);

										setSortMode((String)sortNames[which]);

									} catch (IndexOutOfBoundsException e) {
                                		e.printStackTrace();
									}

                                    dialog.cancel();

                                    refresh();
                                }
                            });

            dialog = builder.create();
            dialog.show();

            return true;
        /* case R.id.show_feeds:
			setUnreadOnly(!getUnreadOnly());
			invalidateOptionsMenu();
			refresh();
			return true; */
		/*case R.id.update_feeds:
			//m_pullToRefreshAttacher.setRefreshing(true);
			refresh();
			return true;*/
		default:
			Log.d(TAG, "onOptionsItemSelected, unhandled id=" + item.getItemId());
			return super.onOptionsItemSelected(item);
		}
	}

    @Override
    public void onBackPressed() {
        if (m_drawerLayout != null && !m_drawerLayout.isDrawerOpen(Gravity.START) &&
                (getSupportFragmentManager().getBackStackEntryCount() > 0 || m_userFeedSelected)) {

            m_drawerLayout.openDrawer(Gravity.START);
        } else {
			try {
				super.onBackPressed();
			} catch (IllegalStateException e) {
				// java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
				e.printStackTrace();
			}
        }
    }

	@Override
	protected void loginSuccess(boolean refresh) {
		invalidateOptionsMenu();

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

		Application.getInstance().save(out);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		invalidateOptionsMenu();

	}

	@Override
	public void onArticleListSelectionChange(ArticleList m_selectedArticles) {
		invalidateOptionsMenu();
	}

	/* public void openFeedArticles(Feed feed) {
		//Application.getInstance().m_loadedArticles.clear();
		
		Intent intent = new Intent(MasterActivity.this, DetailActivity.class);
		intent.putExtra("feed", feed);
		intent.putExtra("article", (Article)null);
		intent.putExtra("searchQuery", (String)null);

		startActivityForResult(intent, HEADLINES_REQUEST);
		overridePendingTransition(R.anim.right_slide_in, 0);
	} */
	
	public void onArticleSelected(Article article, boolean open) {
		if (open) {
			boolean alwaysOpenUri = m_prefs.getBoolean("always_open_uri", false);
			if (alwaysOpenUri) {
				if (article.unread) {
					article.unread = false;
					saveArticleUnread(article);
				}

				HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);
				if (hf != null) {
					hf.setActiveArticle(article);
				}

				openUri(Uri.parse(article.link));
			}
			else {
				HeadlinesFragment hf = (HeadlinesFragment) getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);

				Intent intent = new Intent(MasterActivity.this, DetailActivity.class);
				intent.putExtra("feed", hf.getFeed());
				//intent.putExtra("article", article);
				intent.putExtra("searchQuery", hf.getSearchQuery());
				//intent.putExtra("articles", (Parcelable)hf.getAllArticles());
				Application.getInstance().tmpArticleList = hf.getAllArticles();
				Application.getInstance().tmpArticle = article;

				/* if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
					startActivityForResult(intent, HEADLINES_REQUEST, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
				} else {
					startActivityForResult(intent, HEADLINES_REQUEST);
				} */

				// mysterious crashes somewhere in gl layer (?) on some feeds if we use activitycompat transitions here on LP so welp
				startActivityForResult(intent, HEADLINES_REQUEST);
				overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
			}
		} else {
			invalidateOptionsMenu();

            if (article.unread) {
			    article.unread = false;
			    saveArticleUnread(article);
		    }
        }
	}

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

		Date date = new Date();

		if (isFinishing() || date.getTime() - m_lastWidgetRefresh > 60*1000) {
			m_lastWidgetRefresh = date.getTime();

			CommonActivity.requestWidgetUpdate(MasterActivity.this);
		}

    }

    @Override
	public void onArticleSelected(Article article) {
		onArticleSelected(article, true);		
	}

	@Override
	public void onHeadlinesLoaded(boolean appended) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == HEADLINES_REQUEST && data != null) {
			//Application.getInstance().m_activeArticle = null;

            //ArrayList<Article> tmp = data.getParcelableArrayListExtra("articles");
            Article article = data.getParcelableExtra("activeArticle");
            ArticleList articles = Application.getInstance().tmpArticleList;

            if (articles != null) {
                HeadlinesFragment hf = (HeadlinesFragment)getSupportFragmentManager().findFragmentByTag(FRAG_HEADLINES);

                if (hf != null) {
                    hf.setArticles(articles);
                    //hf.setActiveArticle(article); disable HL scrolling on resume for now
                }
            }


		}		
	}

	// TODO: remove; not supported on oreo
	public void createFeedShortcut(Feed feed) {
		final Intent shortcutIntent = new Intent(this, MasterActivity.class);
		shortcutIntent.putExtra("feed_id", feed.id);
		shortcutIntent.putExtra("feed_is_cat", feed.is_cat);
		shortcutIntent.putExtra("feed_title", feed.title);
		shortcutIntent.putExtra("shortcut_mode", true);
		
		Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
		
		intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, feed.title);
		intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
		intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher));
		intent.putExtra("duplicate", false);
		
		sendBroadcast(intent);
		
		toast(R.string.shortcut_has_been_placed_on_the_home_screen);
	}

	// TODO: remove; not supported on oreo
	public void createCategoryShortcut(FeedCategory cat) {
		createFeedShortcut(new Feed(cat.id, cat.title, true));
	}

	public void unsubscribeFeed(final Feed feed) {
		ApiRequest req = new ApiRequest(getApplicationContext()) {
			protected void onPostExecute(JsonElement result) {
				refresh();
			}
		};

		@SuppressWarnings("serial")
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("sid", getSessionId());
		map.put("op", "unsubscribeFeed");
		map.put("feed_id", String.valueOf(feed.id));
		
		req.execute(map);

	}

}