forked from QuantumBadger/RedReader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.java
1141 lines (912 loc) · 30.9 KB
/
MainActivity.java
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* This file is part of RedReader.
*
* RedReader is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* RedReader is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RedReader. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package org.quantumbadger.redreader.activities;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.FrameLayout;
import android.widget.Spinner;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import org.apache.commons.lang3.StringUtils;
import org.quantumbadger.redreader.R;
import org.quantumbadger.redreader.RedReader;
import org.quantumbadger.redreader.account.RedditAccount;
import org.quantumbadger.redreader.account.RedditAccountChangeListener;
import org.quantumbadger.redreader.account.RedditAccountManager;
import org.quantumbadger.redreader.adapters.MainMenuSelectionListener;
import org.quantumbadger.redreader.common.AndroidCommon;
import org.quantumbadger.redreader.common.Constants;
import org.quantumbadger.redreader.common.DialogUtils;
import org.quantumbadger.redreader.common.FeatureFlagHandler;
import org.quantumbadger.redreader.common.General;
import org.quantumbadger.redreader.common.LinkHandler;
import org.quantumbadger.redreader.common.PrefsUtility;
import org.quantumbadger.redreader.common.SharedPrefsWrapper;
import org.quantumbadger.redreader.common.UriString;
import org.quantumbadger.redreader.common.collections.CollectionStream;
import org.quantumbadger.redreader.common.time.TimestampUTC;
import org.quantumbadger.redreader.fragments.AccountListDialog;
import org.quantumbadger.redreader.fragments.ChangelogDialog;
import org.quantumbadger.redreader.fragments.CommentListingFragment;
import org.quantumbadger.redreader.fragments.MainMenuFragment;
import org.quantumbadger.redreader.fragments.PostListingFragment;
import org.quantumbadger.redreader.fragments.SessionListDialog;
import org.quantumbadger.redreader.listingcontrollers.CommentListingController;
import org.quantumbadger.redreader.listingcontrollers.PostListingController;
import org.quantumbadger.redreader.reddit.PostCommentSort;
import org.quantumbadger.redreader.reddit.PostSort;
import org.quantumbadger.redreader.reddit.RedditSubredditHistory;
import org.quantumbadger.redreader.reddit.UserCommentSort;
import org.quantumbadger.redreader.reddit.api.RedditOAuth;
import org.quantumbadger.redreader.reddit.api.RedditSubredditSubscriptionManager;
import org.quantumbadger.redreader.reddit.api.SubredditSubscriptionState;
import org.quantumbadger.redreader.reddit.prepared.RedditPreparedPost;
import org.quantumbadger.redreader.reddit.things.InvalidSubredditNameException;
import org.quantumbadger.redreader.reddit.things.RedditSubreddit;
import org.quantumbadger.redreader.reddit.things.SubredditCanonicalId;
import org.quantumbadger.redreader.reddit.url.PostCommentListingURL;
import org.quantumbadger.redreader.reddit.url.PostListingURL;
import org.quantumbadger.redreader.reddit.url.RedditURLParser;
import org.quantumbadger.redreader.reddit.url.SearchPostListURL;
import org.quantumbadger.redreader.reddit.url.SubredditPostListURL;
import org.quantumbadger.redreader.reddit.url.UserPostListingURL;
import org.quantumbadger.redreader.reddit.url.UserProfileURL;
import org.quantumbadger.redreader.views.RedditPostView;
import java.util.ArrayList;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicReference;
public class MainActivity extends RefreshableActivity
implements MainMenuSelectionListener,
RedditAccountChangeListener,
RedditPostView.PostSelectionListener,
OptionsMenuUtility.OptionsMenuSubredditsListener,
OptionsMenuUtility.OptionsMenuPostsListener,
OptionsMenuUtility.OptionsMenuCommentsListener,
SessionChangeListener,
RedditSubredditSubscriptionManager.SubredditSubscriptionStateChangeListener {
private static final String TAG = "MainActivity";
private boolean twoPane;
private MainMenuFragment mainMenuFragment;
private PostListingController postListingController;
private PostListingFragment postListingFragment;
private CommentListingController commentListingController;
private CommentListingFragment commentListingFragment;
private View mainMenuView;
private View postListingView;
private View commentListingView;
private FrameLayout mLeftPane;
private FrameLayout mRightPane;
private boolean isMenuShown = true;
private final AtomicReference<RedditSubredditSubscriptionManager.ListenerContext>
mSubredditSubscriptionListenerContext = new AtomicReference<>(null);
@Override
protected boolean baseActivityIsActionBarBackEnabled() {
return false;
}
@Override
protected boolean baseActivityAllowToolbarHideOnScroll() {
return !General.isTablet(this);
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
PrefsUtility.applyTheme(this);
super.onCreate(savedInstanceState);
if(!isTaskRoot()
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
&& getIntent().getAction() != null
&& getIntent().getAction().equals(Intent.ACTION_MAIN)) {
// Workaround for issue where a new MainActivity is created despite
// the app already running
finish();
return;
}
if(!PrefsUtility.isRedditUserAgreementAccepted()
&& !PrefsUtility.isRedditUserAgreementDeclined()) {
RedditTermsActivity.launch(this, true);
finish();
return;
}
final SharedPrefsWrapper sharedPreferences = General.getSharedPrefs(this);
twoPane = General.isTablet(this);
setTitle(R.string.app_name);
RedditAccountManager.getInstance(this).addUpdateListener(this);
final AndroidCommon.PackageInfo pInfo = RedReader.getInstance(this).getPackageInfo();
final int appVersion = pInfo.getVersionCode();
Log.i(TAG, "[Migration] App version: " + appVersion);
if(!sharedPreferences.contains(FeatureFlagHandler.PREF_FIRST_RUN_MESSAGE_SHOWN)) {
Log.i(TAG, "[Migration] Showing first run message");
FeatureFlagHandler.handleFirstInstall(sharedPreferences);
new MaterialAlertDialogBuilder(this)
.setTitle(R.string.firstrun_login_title)
.setMessage(R.string.firstrun_login_message)
.setPositiveButton(
R.string.firstrun_login_button_now,
(dialog, which) -> AccountListDialog.show(this))
.setNegativeButton(R.string.firstrun_login_button_later, null)
.show();
sharedPreferences.edit()
.putString(FeatureFlagHandler.PREF_FIRST_RUN_MESSAGE_SHOWN, "true")
.putInt(FeatureFlagHandler.PREF_LAST_VERSION, appVersion)
.apply();
} else if(sharedPreferences.contains(FeatureFlagHandler.PREF_LAST_VERSION)) {
FeatureFlagHandler.handleLegacyUpgrade(this, appVersion, pInfo.getVersionName());
} else {
Log.i(TAG, "[Migration] Last version not set.");
sharedPreferences.edit()
.putInt(FeatureFlagHandler.PREF_LAST_VERSION, appVersion)
.apply();
ChangelogDialog.newInstance().show(getSupportFragmentManager(), null);
}
FeatureFlagHandler.handleUpgrade(this);
if(RedditOAuth.anyNeedRelogin(this)) {
General.showMustReloginDialog(this);
} else {
AndroidCommon.promptForNotificationPermission(this, null);
}
recreateSubscriptionListener();
doRefresh(RefreshableFragment.MAIN_RELAYOUT, false, null);
if(savedInstanceState == null
&& PrefsUtility.pref_behaviour_skiptofrontpage()) {
onSelected(SubredditPostListURL.getFrontPage());
}
}
private void recreateSubscriptionListener() {
final RedditSubredditSubscriptionManager.ListenerContext oldContext
= mSubredditSubscriptionListenerContext.getAndSet(
RedditSubredditSubscriptionManager
.getSingleton(
this,
RedditAccountManager.getInstance(this)
.getDefaultAccount())
.addListener(this));
if(oldContext != null) {
oldContext.removeListener();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
final RedditSubredditSubscriptionManager.ListenerContext listenerContext
= mSubredditSubscriptionListenerContext.get();
if(listenerContext != null) {
listenerContext.removeListener();
}
}
@Override
public void onSelected(final @MainMenuFragment.MainMenuAction int type) {
final String username = RedditAccountManager.getInstance(this)
.getDefaultAccount().username;
switch(type) {
case MainMenuFragment.MENU_MENU_ACTION_FRONTPAGE:
onSelected(SubredditPostListURL.getFrontPage());
break;
case MainMenuFragment.MENU_MENU_ACTION_POPULAR:
onSelected(SubredditPostListURL.getPopular());
break;
case MainMenuFragment.MENU_MENU_ACTION_ALL:
onSelected(SubredditPostListURL.getAll());
break;
case MainMenuFragment.MENU_MENU_ACTION_SUBMITTED:
onSelected(UserPostListingURL.getSubmitted(username));
break;
case MainMenuFragment.MENU_MENU_ACTION_SUBMITTED_COMMENTS:
LinkHandler.onLinkClicked(
this,
Constants.Reddit.getUri("/user/" + username + "/comments.json"),
false
);
break;
case MainMenuFragment.MENU_MENU_ACTION_SAVED:
onSelected(UserPostListingURL.getSaved(username));
break;
case MainMenuFragment.MENU_MENU_ACTION_HIDDEN:
onSelected(UserPostListingURL.getHidden(username));
break;
case MainMenuFragment.MENU_MENU_ACTION_UPVOTED:
onSelected(UserPostListingURL.getLiked(username));
break;
case MainMenuFragment.MENU_MENU_ACTION_DOWNVOTED:
onSelected(UserPostListingURL.getDisliked(username));
break;
case MainMenuFragment.MENU_MENU_ACTION_PROFILE:
LinkHandler.onLinkClicked(this, new UserProfileURL(username).toUriString());
break;
case MainMenuFragment.MENU_MENU_ACTION_CUSTOM: {
final MaterialAlertDialogBuilder alertBuilder
= new MaterialAlertDialogBuilder(this);
final View root = getLayoutInflater().inflate(
R.layout.dialog_mainmenu_custom,
null);
final Spinner destinationType
= root.findViewById(R.id.dialog_mainmenu_custom_type);
final AutoCompleteTextView editText
= root.findViewById(R.id.dialog_mainmenu_custom_value);
final String[] typeReturnValues = getResources().getStringArray(
R.array.mainmenu_custom_destination_type_return);
if(PrefsUtility.pref_menus_mainmenu_shortcutitems().contains(
MainMenuFragment.MainMenuShortcutItems.SUBREDDIT_SEARCH)) {
for(int i = 0; i < typeReturnValues.length; i++) {
if(typeReturnValues[i].equals("user")) {
destinationType.setSelection(i);
break;
}
}
}
final ArrayList<SubredditCanonicalId> subredditHistory
= RedditSubredditHistory.getSubredditsSorted(
RedditAccountManager.getInstance(this).getDefaultAccount());
final ArrayAdapter<String> autocompleteAdapter = new ArrayAdapter<>(
this,
android.R.layout.simple_dropdown_item_1line,
new CollectionStream<>(subredditHistory)
.map(SubredditCanonicalId::getDisplayNameLowercase)
.collect(new ArrayList<>()));
editText.setAdapter(autocompleteAdapter);
editText.setOnEditorActionListener((v, actionId, event) -> {
boolean handled = false;
if(actionId == EditorInfo.IME_ACTION_GO
|| event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
openCustomLocation(
typeReturnValues,
destinationType,
editText);
handled = true;
}
return handled;
});
alertBuilder.setView(root);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(
final CharSequence s,
final int start,
final int count,
final int after) {}
@Override
public void onTextChanged(
final CharSequence s,
final int start,
final int before,
final int count) {
if(typeReturnValues[destinationType.getSelectedItemPosition()]
.equals("search")) {
return;
}
final String value = s.toString();
String type = null;
if(value.startsWith("http://") || value.startsWith("https://")) {
type = "url";
} else if(value.startsWith("/r/") || value.startsWith("r/")) {
type = "subreddit";
} else if(value.startsWith("/u/") || value.startsWith("u/")) {
type = "user";
}
if(type != null) {
for(int i = 0; i < typeReturnValues.length; i++) {
if(typeReturnValues[i].equals(type)) {
destinationType.setSelection(i);
break;
}
}
}
}
@Override
public void afterTextChanged(final Editable s) {}
});
destinationType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(
@Nullable final AdapterView<?> adapterView,
@Nullable final View view,
final int i,
final long l) {
final String typeName
= typeReturnValues[destinationType.getSelectedItemPosition()];
if("subreddit".equals(typeName)) {
editText.setAdapter(autocompleteAdapter);
} else {
editText.setAdapter(null);
}
}
@Override
public void onNothingSelected(final AdapterView<?> adapterView) {
editText.setAdapter(null);
}
});
alertBuilder.setPositiveButton(
R.string.dialog_go,
(dialog, which) -> openCustomLocation(
typeReturnValues,
destinationType,
editText));
alertBuilder.setNegativeButton(R.string.dialog_cancel, null);
final AlertDialog alertDialog = alertBuilder.create();
alertDialog.getWindow()
.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE
| WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
alertDialog.show();
break;
}
case MainMenuFragment.MENU_MENU_ACTION_INBOX:
startActivity(new Intent(this, InboxListingActivity.class));
break;
case MainMenuFragment.MENU_MENU_ACTION_SENT_MESSAGES: {
final Intent intent = new Intent(this, InboxListingActivity.class);
intent.putExtra("inboxType", "sent");
startActivity(intent);
break;
}
case MainMenuFragment.MENU_MENU_ACTION_MODMAIL: {
final Intent intent = new Intent(this, InboxListingActivity.class);
intent.putExtra("inboxType", "modmail");
startActivity(intent);
break;
}
case MainMenuFragment.MENU_MENU_ACTION_FIND_SUBREDDIT: {
startActivity(new Intent(this, SubredditSearchActivity.class));
}
}
}
private void openCustomLocation(
final String[] typeReturnValues,
final Spinner destinationType,
final AutoCompleteTextView editText) {
final String typeName
= typeReturnValues[destinationType.getSelectedItemPosition()];
switch(typeName) {
case "subreddit": {
final String subredditInput = editText.getText()
.toString()
.trim()
.replace(" ", "");
try {
final String normalizedName = RedditSubreddit.stripRPrefix(
subredditInput);
final RedditURLParser.RedditURL redditURL
= SubredditPostListURL.getSubreddit(normalizedName);
if(redditURL == null
|| redditURL.pathType()
!= RedditURLParser.SUBREDDIT_POST_LISTING_URL) {
General.quickToast(this, R.string.mainmenu_custom_invalid_name);
} else {
onSelected(redditURL.asSubredditPostListURL());
}
} catch(final InvalidSubredditNameException e) {
General.quickToast(this, R.string.mainmenu_custom_invalid_name);
}
break;
}
case "user":
String userInput = editText.getText().toString().trim().replace(" ", "");
if(!userInput.startsWith("/u/")
&& !userInput.startsWith("/user/")) {
if(userInput.startsWith("u/")
|| userInput.startsWith("user/")) {
userInput = "/" + userInput;
} else {
userInput = "/u/" + userInput;
}
}
LinkHandler.onLinkClicked(this, new UriString(userInput));
break;
case "url": {
LinkHandler.onLinkClicked(
this,
new UriString(editText.getText().toString().trim()));
break;
}
case "search": {
final String query = editText.getText().toString().trim();
if(StringUtils.isEmpty(query)) {
General.quickToast(this, R.string.mainmenu_custom_empty_search_query);
break;
}
final SearchPostListURL url = SearchPostListURL.build(null, query);
final Intent intent = new Intent(this, PostListingActivity.class);
intent.setData(url.generateJsonUri());
this.startActivity(intent);
break;
}
}
}
@Override
public void onSelected(final PostListingURL url) {
if(url == null) {
return;
}
if(twoPane) {
postListingController = new PostListingController(url, this);
requestRefresh(RefreshableFragment.POSTS, false);
} else {
final Intent intent = new Intent(this, PostListingActivity.class);
intent.setData(url.generateJsonUri());
startActivityForResult(intent, 1);
}
}
@Override
public void onRedditAccountChanged() {
recreateSubscriptionListener();
postInvalidateOptionsMenu();
requestRefresh(RefreshableFragment.ALL, false);
}
@Override
protected void doRefresh(
final RefreshableFragment which,
final boolean force,
final Bundle savedInstanceState) {
if(which == RefreshableFragment.MAIN_RELAYOUT) {
mainMenuFragment = null;
postListingFragment = null;
commentListingFragment = null;
mainMenuView = null;
postListingView = null;
commentListingView = null;
if(mLeftPane != null) {
mLeftPane.removeAllViews();
}
if(mRightPane != null) {
mRightPane.removeAllViews();
}
twoPane = General.isTablet(this);
if(twoPane) {
final View layout = getLayoutInflater().inflate(R.layout.main_double, null);
mLeftPane = layout.findViewById(R.id.main_left_frame);
mRightPane = layout.findViewById(R.id.main_right_frame);
setBaseActivityListing(layout);
} else {
mLeftPane = null;
mRightPane = null;
}
invalidateOptionsMenu();
requestRefresh(RefreshableFragment.ALL, false);
return;
}
if(twoPane) {
final FrameLayout postContainer = isMenuShown ? mRightPane : mLeftPane;
if(isMenuShown && (which == RefreshableFragment.ALL
|| which == RefreshableFragment.MAIN)) {
mainMenuFragment = new MainMenuFragment(this, null, force);
mainMenuView = mainMenuFragment.createCombinedListingAndOverlayView();
mLeftPane.removeAllViews();
mLeftPane.addView(mainMenuView);
}
if(postListingController != null && (which == RefreshableFragment.ALL
|| which == RefreshableFragment.POSTS)) {
if(force && postListingFragment != null) {
postListingFragment.cancel();
}
postListingFragment = postListingController.get(this, force, null);
postListingView = postListingFragment.createCombinedListingAndOverlayView();
postContainer.removeAllViews();
postContainer.addView(postListingView);
}
if(commentListingController != null && (which == RefreshableFragment.ALL
|| which
== RefreshableFragment.COMMENTS)) {
commentListingFragment = commentListingController.get(this, force, null);
commentListingView = commentListingFragment.createCombinedListingAndOverlayView();
mRightPane.removeAllViews();
mRightPane.addView(commentListingView);
}
} else {
if(which == RefreshableFragment.ALL || which == RefreshableFragment.MAIN) {
mainMenuFragment = new MainMenuFragment(this, null, force);
mainMenuFragment.setBaseActivityContent(this);
}
}
invalidateOptionsMenu();
}
@Override
public void onBackPressed() {
if(!General.onBackPressed()) {
return;
}
if(!twoPane || isMenuShown) {
super.onBackPressed();
return;
}
isMenuShown = true;
mainMenuFragment = new MainMenuFragment(
this,
null,
false); // TODO preserve position
mainMenuView = mainMenuFragment.createCombinedListingAndOverlayView();
commentListingFragment = null;
commentListingView = null;
mLeftPane.removeAllViews();
mRightPane.removeAllViews();
mLeftPane.addView(mainMenuView);
mRightPane.addView(postListingView);
showBackButton(false);
invalidateOptionsMenu();
}
@Override
public void onPostCommentsSelected(final RedditPreparedPost post) {
if(twoPane) {
commentListingController
= new CommentListingController(
PostCommentListingURL.forPostId(post.src
.getIdAlone()));
showBackButton(true);
if(isMenuShown) {
commentListingFragment = commentListingController.get(this, false, null);
commentListingView = commentListingFragment.createCombinedListingAndOverlayView();
mLeftPane.removeAllViews();
mRightPane.removeAllViews();
mLeftPane.addView(postListingView);
mRightPane.addView(commentListingView);
mainMenuFragment = null;
mainMenuView = null;
isMenuShown = false;
invalidateOptionsMenu();
} else {
requestRefresh(RefreshableFragment.COMMENTS, false);
}
} else {
LinkHandler.onLinkClicked(
this,
PostCommentListingURL.forPostId(post.src.getIdAlone()).toUriString(),
false);
}
}
@Override
public void onPostSelected(final RedditPreparedPost post) {
if(post.isSelf()) {
onPostCommentsSelected(post);
} else {
LinkHandler.onLinkClicked(this, post.src.getUrl(), false, post.src.getSrc());
}
}
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
final boolean postsVisible = postListingFragment != null;
final boolean commentsVisible = commentListingFragment != null;
final boolean postsSortable = postListingController != null
&& postListingController.isSortable();
final boolean commentsSortable = commentListingController != null
&& commentListingController.isSortable();
final boolean isFrontPage = postListingController != null && postListingController
.isFrontPage();
final RedditAccount user = RedditAccountManager.getInstance(this)
.getDefaultAccount();
final SubredditSubscriptionState
subredditSubscriptionState;
final RedditSubredditSubscriptionManager subredditSubscriptionManager
= RedditSubredditSubscriptionManager.getSingleton(this, user);
Boolean subredditPinState = null;
Boolean subredditBlockedState = null;
if(postsVisible
&& !user.isAnonymous()
&& postListingController.isSubreddit()
&& subredditSubscriptionManager.areSubscriptionsReady()
&& postListingFragment != null
&& postListingFragment.getSubreddit() != null) {
subredditSubscriptionState
= subredditSubscriptionManager.getSubscriptionState(
postListingController.subredditCanonicalName());
} else {
subredditSubscriptionState = null;
}
if(postsVisible
&& postListingController.isSubreddit()
&& postListingFragment != null
&& postListingFragment.getSubreddit() != null) {
try {
subredditPinState = PrefsUtility.pref_pinned_subreddits_check(
postListingFragment.getSubreddit().getCanonicalId());
subredditBlockedState = PrefsUtility.pref_blocked_subreddits_check(
postListingFragment.getSubreddit().getCanonicalId());
} catch(final InvalidSubredditNameException e) {
subredditPinState = null;
subredditBlockedState = null;
}
}
final String subredditDescription = postListingFragment != null
&& postListingFragment.getSubreddit() != null
? postListingFragment.getSubreddit().description_html
: null;
OptionsMenuUtility.prepare(
this,
menu,
isMenuShown,
postsVisible,
commentsVisible,
false,
false,
false,
postsSortable,
commentsSortable,
isFrontPage,
subredditSubscriptionState,
postsVisible
&& subredditDescription != null
&& !subredditDescription.isEmpty(),
true,
subredditPinState,
subredditBlockedState);
if(commentListingFragment != null) {
commentListingFragment.onCreateOptionsMenu(menu);
}
return true;
}
@Override
public void onRefreshComments() {
commentListingController.setSession(null);
requestRefresh(RefreshableFragment.COMMENTS, true);
}
@Override
public void onPastComments() {
final SessionListDialog sessionListDialog = SessionListDialog.newInstance(
commentListingController.getUri(),
commentListingController.getSession(),
SessionChangeListener.SessionChangeType.COMMENTS);
sessionListDialog.show(getSupportFragmentManager(), null);
}
@Override
public void onSortSelected(final PostCommentSort order) {
commentListingController.setSort(order);
requestRefresh(RefreshableFragment.COMMENTS, false);
}
@Override
public void onSortSelected(final UserCommentSort order) {
commentListingController.setSort(order);
requestRefresh(RefreshableFragment.COMMENTS, false);
}
@Override
public void onSearchComments() {
DialogUtils.showSearchDialog(
this,
R.string.action_search_comments,
query -> {
final Intent searchIntent
= new Intent(this, CommentListingActivity.class);
searchIntent.setData(commentListingController.getUri());
searchIntent.putExtra(
CommentListingActivity.EXTRA_SEARCH_STRING,
query);
startActivity(searchIntent);
});
}
@Override
public void onRefreshPosts() {
postListingController.setSession(null);
requestRefresh(RefreshableFragment.POSTS, true);
}
@Override
public void onPastPosts() {
final SessionListDialog sessionListDialog = SessionListDialog.newInstance(
postListingController.getUri(),
postListingController.getSession(),
SessionChangeListener.SessionChangeType.POSTS);
sessionListDialog.show(getSupportFragmentManager(), null);
}
@Override
public void onSubmitPost() {
final Intent intent = new Intent(this, PostSubmitActivity.class);
if(postListingController.isSubreddit()) {
intent.putExtra(
"subreddit",
postListingController.subredditCanonicalName().toString());
}
startActivity(intent);
}
@Override
public void onSortSelected(final PostSort order) {
postListingController.setSort(order);
requestRefresh(RefreshableFragment.POSTS, false);
}
@Override
public void onSearchPosts() {
PostListingActivity.onSearchPosts(postListingController, this);
}
@Override
public void onSubscribe() {
if(postListingFragment != null) {
postListingFragment.onSubscribe();
}
}
@Override
public void onUnsubscribe() {
if(postListingFragment != null) {
postListingFragment.onUnsubscribe();
}
}
@Override
public void onSidebar() {
postListingFragment.getSubreddit().showSidebarActivity(this);
}
@Override
public void onPin() {
if(postListingFragment == null) {
return;
}
try {
PrefsUtility.pref_pinned_subreddits_add(
this,
postListingFragment.getSubreddit().getCanonicalId());
} catch(final InvalidSubredditNameException e) {
throw new RuntimeException(e);
}
invalidateOptionsMenu();
}
@Override
public void onUnpin() {
if(postListingFragment == null) {
return;
}
try {
PrefsUtility.pref_pinned_subreddits_remove(
this,
postListingFragment.getSubreddit().getCanonicalId());
} catch(final InvalidSubredditNameException e) {
throw new RuntimeException(e);
}
invalidateOptionsMenu();
}
@Override
public void onBlock() {
if(postListingFragment == null) {
return;
}
try {
PrefsUtility.pref_blocked_subreddits_add(
this,
postListingFragment.getSubreddit().getCanonicalId());
} catch(final InvalidSubredditNameException e) {
throw new RuntimeException(e);
}
invalidateOptionsMenu();
}
@Override
public void onUnblock() {
if(postListingFragment == null) {
return;
}
try {
PrefsUtility.pref_blocked_subreddits_remove(
this,