11package it .niedermann .owncloud .notes .main ;
22
3+ import static androidx .lifecycle .Transformations .distinctUntilChanged ;
4+ import static androidx .lifecycle .Transformations .map ;
5+ import static androidx .lifecycle .Transformations .switchMap ;
6+ import static java .net .HttpURLConnection .HTTP_NOT_MODIFIED ;
7+ import static it .niedermann .owncloud .notes .main .MainActivity .ADAPTER_KEY_RECENT ;
8+ import static it .niedermann .owncloud .notes .main .MainActivity .ADAPTER_KEY_STARRED ;
9+ import static it .niedermann .owncloud .notes .main .slots .SlotterUtil .fillListByCategory ;
10+ import static it .niedermann .owncloud .notes .main .slots .SlotterUtil .fillListByInitials ;
11+ import static it .niedermann .owncloud .notes .main .slots .SlotterUtil .fillListByTime ;
12+ import static it .niedermann .owncloud .notes .shared .model .CategorySortingMethod .SORT_MODIFIED_DESC ;
13+ import static it .niedermann .owncloud .notes .shared .model .ENavigationCategoryType .DEFAULT_CATEGORY ;
14+ import static it .niedermann .owncloud .notes .shared .model .ENavigationCategoryType .FAVORITES ;
15+ import static it .niedermann .owncloud .notes .shared .model .ENavigationCategoryType .RECENT ;
16+ import static it .niedermann .owncloud .notes .shared .model .ENavigationCategoryType .UNCATEGORIZED ;
17+ import static it .niedermann .owncloud .notes .shared .util .DisplayUtils .convertToCategoryNavigationItem ;
18+
319import android .accounts .NetworkErrorException ;
420import android .app .Application ;
521import android .content .Context ;
1935import com .nextcloud .android .sso .AccountImporter ;
2036import com .nextcloud .android .sso .exceptions .NextcloudFilesAppAccountNotFoundException ;
2137import com .nextcloud .android .sso .exceptions .NextcloudHttpRequestFailedException ;
38+ import com .nextcloud .android .sso .exceptions .UnknownErrorException ;
2239import com .nextcloud .android .sso .helper .SingleAccountHelper ;
23- import com .nextcloud .android .sso .model .SingleSignOnAccount ;
2440
2541import java .util .ArrayList ;
2642import java .util .Collection ;
2743import java .util .Collections ;
2844import java .util .List ;
45+ import java .util .Locale ;
2946import java .util .concurrent .ExecutorService ;
3047import java .util .concurrent .Executors ;
3148import java .util .stream .Collectors ;
5067import it .niedermann .owncloud .notes .shared .model .Item ;
5168import it .niedermann .owncloud .notes .shared .model .NavigationCategory ;
5269
53- import static androidx .lifecycle .Transformations .distinctUntilChanged ;
54- import static androidx .lifecycle .Transformations .map ;
55- import static androidx .lifecycle .Transformations .switchMap ;
56- import static it .niedermann .owncloud .notes .main .MainActivity .ADAPTER_KEY_RECENT ;
57- import static it .niedermann .owncloud .notes .main .MainActivity .ADAPTER_KEY_STARRED ;
58- import static it .niedermann .owncloud .notes .main .slots .SlotterUtil .fillListByCategory ;
59- import static it .niedermann .owncloud .notes .main .slots .SlotterUtil .fillListByInitials ;
60- import static it .niedermann .owncloud .notes .main .slots .SlotterUtil .fillListByTime ;
61- import static it .niedermann .owncloud .notes .shared .model .CategorySortingMethod .SORT_MODIFIED_DESC ;
62- import static it .niedermann .owncloud .notes .shared .model .ENavigationCategoryType .DEFAULT_CATEGORY ;
63- import static it .niedermann .owncloud .notes .shared .model .ENavigationCategoryType .FAVORITES ;
64- import static it .niedermann .owncloud .notes .shared .model .ENavigationCategoryType .RECENT ;
65- import static it .niedermann .owncloud .notes .shared .model .ENavigationCategoryType .UNCATEGORIZED ;
66- import static it .niedermann .owncloud .notes .shared .util .DisplayUtils .convertToCategoryNavigationItem ;
67- import static java .net .HttpURLConnection .HTTP_NOT_MODIFIED ;
68-
6970public class MainViewModel extends AndroidViewModel {
7071
7172 private static final String TAG = MainViewModel .class .getSimpleName ();
@@ -351,7 +352,7 @@ private static List<NavigationItem> fromCategoriesWithNotesCount(@NonNull Contex
351352 lastSecondaryCategory .icon = NavigationAdapter .ICON_SUB_MULTIPLE ;
352353 } else if (belongsToLastPrimaryCategory ) {
353354 if (isCategoryOpen ) {
354- if (currentSecondaryCategory == null ) {
355+ if (currentSecondaryCategory == null ) {
355356 throw new IllegalStateException ("Current secondary category is null. Last primary category: " + lastPrimaryCategory );
356357 }
357358 item .label = currentSecondaryCategory ;
@@ -621,4 +622,45 @@ public String collectNoteContents(@NonNull List<Long> noteIds) {
621622 }
622623 return noteContents .toString ();
623624 }
625+
626+ /**
627+ * @return <code>true</code> if {@param exceptions} contains at least one exception which is not caused by flaky infrastructure.
628+ * @see <a href="https://github.com/stefan-niedermann/nextcloud-notes/issues/1303">Issue #1303</a>
629+ */
630+ public boolean containsNonInfrastructureRelatedItems (@ Nullable Collection <Throwable > exceptions ) {
631+ if (exceptions == null || exceptions .isEmpty ()) {
632+ return false ;
633+ }
634+
635+ return exceptions .stream ().anyMatch (e -> !exceptionIsInfrastructureRelated (e ));
636+ }
637+
638+ private boolean exceptionIsInfrastructureRelated (@ Nullable Throwable e ) {
639+ if (e == null ) {
640+ return false ;
641+ }
642+
643+ if (e instanceof RuntimeException || e instanceof UnknownErrorException ) {
644+ if (isSoftwareCausedConnectionAbort (e .getMessage ()) || isNetworkUnreachable (e .getMessage ())) {
645+ return true ;
646+ }
647+ }
648+
649+ return exceptionIsInfrastructureRelated (e .getCause ());
650+ }
651+
652+ private boolean isSoftwareCausedConnectionAbort (@ Nullable String input ) {
653+ if (input == null ) {
654+ return false ;
655+ }
656+ return input .toLowerCase (Locale .ROOT ).contains ("software caused connection abort" );
657+ }
658+
659+ private boolean isNetworkUnreachable (@ Nullable String input ) {
660+ if (input == null ) {
661+ return false ;
662+ }
663+ final var lower = input .toLowerCase (Locale .ROOT );
664+ return lower .contains ("failed to connect" ) && lower .contains ("network is unreachable" );
665+ }
624666}
0 commit comments