|
126 | 126 | import java.util.List; |
127 | 127 | import java.util.Objects; |
128 | 128 | import java.util.Set; |
129 | | -import java.util.concurrent.CompletableFuture; |
130 | | -import java.util.concurrent.Executors; |
131 | | -import java.util.concurrent.Future; |
132 | 129 |
|
133 | 130 | import javax.inject.Inject; |
134 | 131 |
|
@@ -954,128 +951,72 @@ private void updateSortAndGridMenuItems() { |
954 | 951 | } |
955 | 952 | } |
956 | 953 |
|
957 | | - private boolean shouldNavigateWithoutFilter(OCFile topParent) { |
958 | | - int menuItemId = DrawerActivity.menuItemId; |
959 | | - return (menuItemId != R.id.nav_shared && menuItemId != R.id.nav_favorites) || |
960 | | - (menuItemId == R.id.nav_shared && topParent != null && topParent.isShared()) || |
961 | | - (menuItemId == R.id.nav_favorites && topParent != null && topParent.isFavorite()); |
962 | | - } |
963 | 954 |
|
964 | | - private boolean shouldNavigateWithFilter() { |
965 | | - int menuItemId = DrawerActivity.menuItemId; |
966 | | - return menuItemId == R.id.nav_shared || menuItemId == R.id.nav_favorites; |
| 955 | + /** |
| 956 | + * Call this, when the user presses the up button. |
| 957 | + * <p> |
| 958 | + * Tries to move up the current folder one level. If the parent folder was removed from the database, it continues |
| 959 | + * browsing up until finding an existing folders. |
| 960 | + * <p> |
| 961 | + * return Count of folder levels browsed up. |
| 962 | + */ |
| 963 | + public int onBrowseUp() { |
| 964 | + if (mFile == null) { |
| 965 | + return 0; |
| 966 | + } |
| 967 | + |
| 968 | + Pair<Integer, OCFile> result = getPreviousFile(); |
| 969 | + mFile = result.second; |
| 970 | + setFileDepth(mFile); |
| 971 | + |
| 972 | + // since on browse down sets it to the false, browse up should set back to true if current search type is not NO_SEARCH |
| 973 | + if (mFile.isRootDirectory() && currentSearchType != NO_SEARCH) { |
| 974 | + searchFragment = true; |
| 975 | + } |
| 976 | + |
| 977 | + updateFileList(); |
| 978 | + return result.first; |
967 | 979 | } |
968 | 980 |
|
969 | | - private Pair<Integer, OCFile> getPreviousFileWithoutFilter(FileDataStorageManager storageManager) { |
| 981 | + private Pair<Integer, OCFile> getPreviousFile() { |
| 982 | + if (mFile == null) { |
| 983 | + return new Pair<>(0, null); |
| 984 | + } |
| 985 | + |
| 986 | + FileDataStorageManager storageManager = mContainerActivity.getStorageManager(); |
970 | 987 | int moveCount = 0; |
971 | | - OCFile parentDir = null; |
972 | | - String parentPath = null; |
| 988 | + String parentPath; |
| 989 | + OCFile parentDir; |
973 | 990 |
|
974 | 991 | if (mFile.getParentId() != FileDataStorageManager.ROOT_PARENT_ID) { |
975 | 992 | parentPath = new File(mFile.getRemotePath()).getParent(); |
976 | | - |
977 | | - if (parentPath != null) { |
978 | | - parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR; |
979 | | - parentDir = storageManager.getFileByPath(parentPath); |
980 | | - moveCount++; |
981 | | - } |
| 993 | + parentPath = ensureTrailingSeparator(parentPath); |
| 994 | + parentDir = storageManager.getFileByPath(parentPath); |
| 995 | + moveCount++; |
982 | 996 | } else { |
983 | 997 | parentDir = storageManager.getFileByPath(ROOT_PATH); |
| 998 | + parentPath = ROOT_PATH; |
984 | 999 | } |
985 | 1000 |
|
986 | | - while (parentDir == null) { |
| 1001 | + // Keep going up until we find a valid folder |
| 1002 | + while (parentDir == null && !ROOT_PATH.equals(parentPath)) { |
987 | 1003 | parentPath = new File(parentPath).getParent(); |
988 | | - |
989 | | - if (parentPath != null) { |
990 | | - parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : |
991 | | - parentPath + OCFile.PATH_SEPARATOR; |
992 | | - parentDir = storageManager.getFileByPath(parentPath); |
993 | | - moveCount++; |
| 1004 | + if (parentPath == null) { |
| 1005 | + parentPath = ROOT_PATH; // fallback to root |
994 | 1006 | } |
| 1007 | + parentPath = ensureTrailingSeparator(parentPath); |
| 1008 | + parentDir = storageManager.getFileByPath(parentPath); |
| 1009 | + moveCount++; |
995 | 1010 | } |
996 | 1011 |
|
997 | 1012 | return new Pair<>(moveCount, parentDir); |
998 | 1013 | } |
999 | 1014 |
|
1000 | | - private OCFile getPreviousFileWithFilter(FileDataStorageManager storageManager, OCFile currentFile) { |
1001 | | - while (true) { |
1002 | | - OCFile parent = storageManager.getFileById(currentFile.getParentId()); |
1003 | | - if (parent == null) { |
1004 | | - return currentFile; |
1005 | | - } |
1006 | | - |
1007 | | - if (parent.isRootDirectory()) { |
1008 | | - return parent; |
1009 | | - } |
1010 | | - |
1011 | | - if ((DrawerActivity.menuItemId == R.id.nav_shared && parent.isShared()) || |
1012 | | - (DrawerActivity.menuItemId == R.id.nav_favorites && parent.isFavorite())) { |
1013 | | - return parent; |
1014 | | - } |
1015 | | - |
1016 | | - currentFile = parent; |
1017 | | - } |
1018 | | - } |
1019 | | - |
1020 | | - private Future<Pair<Integer, OCFile>> getPreviousFile() { |
1021 | | - CompletableFuture<Pair<Integer, OCFile>> completableFuture = new CompletableFuture<>(); |
1022 | | - |
1023 | | - Executors.newCachedThreadPool().execute(() -> { |
1024 | | - var result = new Pair<Integer, OCFile>(null, null); |
1025 | | - |
1026 | | - FileDataStorageManager storageManager = mContainerActivity.getStorageManager(); |
1027 | | - OCFile currentFile = getCurrentFile(); |
1028 | | - OCFile topParent = storageManager.getTopParent(currentFile); |
1029 | | - |
1030 | | - if (shouldNavigateWithoutFilter(topParent)) { |
1031 | | - result = getPreviousFileWithoutFilter(storageManager); |
1032 | | - } else if (shouldNavigateWithFilter()) { |
1033 | | - OCFile previousFileWithFilter = getPreviousFileWithFilter(storageManager, currentFile); |
1034 | | - result = new Pair<>(0, previousFileWithFilter); |
1035 | | - } |
1036 | | - |
1037 | | - completableFuture.complete(result); |
1038 | | - |
1039 | | - }); |
1040 | | - |
1041 | | - return completableFuture; |
1042 | | - } |
1043 | | - |
1044 | | - /** |
1045 | | - * Call this, when the user presses the up button. |
1046 | | - * <p> |
1047 | | - * Tries to move up the current folder one level. If the parent folder was removed from the database, it continues |
1048 | | - * browsing up until finding an existing folders. |
1049 | | - * <p> |
1050 | | - * return Count of folder levels browsed up. |
1051 | | - */ |
1052 | | - public int onBrowseUp() { |
1053 | | - if (mFile == null) { |
1054 | | - return 0; |
1055 | | - } |
1056 | | - |
1057 | | - try { |
1058 | | - Future<Pair<Integer, OCFile>> futureResult = getPreviousFile(); |
1059 | | - Pair<Integer, OCFile> result = futureResult.get(); |
1060 | | - mFile = result.second; |
1061 | | - setFileDepth(mFile); |
1062 | | - |
1063 | | - // since on browse down sets it to the false, browse up should set back to true if current search type is not NO_SEARCH |
1064 | | - if (mFile.isRootDirectory() && currentSearchType != NO_SEARCH) { |
1065 | | - searchFragment = true; |
1066 | | - } |
1067 | | - |
1068 | | - updateFileList(); |
1069 | | - return result.first; |
1070 | | - } catch (Exception e) { |
1071 | | - Log_OC.e(TAG,"Error caught in onBrowseUp " + e + " getPreviousFileWithoutFilter() used: "); |
1072 | | - |
1073 | | - FileDataStorageManager storageManager = mContainerActivity.getStorageManager(); |
1074 | | - var result = getPreviousFileWithoutFilter(storageManager); |
1075 | | - mFile = result.second; |
1076 | | - updateFileList(); |
1077 | | - return result.first; |
| 1015 | + private String ensureTrailingSeparator(String path) { |
| 1016 | + if (path == null) { |
| 1017 | + return ROOT_PATH; |
1078 | 1018 | } |
| 1019 | + return path.endsWith(OCFile.PATH_SEPARATOR) ? path : path + OCFile.PATH_SEPARATOR; |
1079 | 1020 | } |
1080 | 1021 |
|
1081 | 1022 | private void updateFileList() { |
|
0 commit comments