Skip to content

Commit 6f1ff21

Browse files
Merge pull request #15018 from nextcloud/backport/15016/stable-3.32
[stable-3.32] BugFix - Check recycler view nullability
2 parents d97218f + 6e38fb6 commit 6f1ff21

File tree

5 files changed

+90
-51
lines changed

5 files changed

+90
-51
lines changed

app/src/main/java/com/owncloud/android/ui/fragment/ExtendedListFragment.kt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -140,28 +140,28 @@ open class ExtendedListFragment :
140140
mRecyclerView?.setAdapter(recyclerViewAdapter)
141141
}
142142

143-
protected val recyclerView: RecyclerView
144-
get() = mRecyclerView!!
143+
protected val recyclerView: RecyclerView?
144+
get() = mRecyclerView
145145

146146
open fun setLoading(enabled: Boolean) {
147147
mRefreshListLayout?.isRefreshing = enabled
148148
}
149149

150150
open fun switchToGridView() {
151151
if (!isGridEnabled) {
152-
recyclerView.setLayoutManager(GridLayoutManager(context, columnsCount))
152+
recyclerView?.layoutManager = GridLayoutManager(context, columnsCount)
153153
}
154154
}
155155

156156
open fun switchToListView() {
157157
if (isGridEnabled) {
158-
recyclerView.setLayoutManager(LinearLayoutManager(context))
158+
recyclerView?.layoutManager = LinearLayoutManager(context)
159159
}
160160
}
161161

162162
val isGridEnabled: Boolean
163163
get() {
164-
return recyclerView.layoutManager is GridLayoutManager
164+
return recyclerView?.layoutManager is GridLayoutManager
165165
}
166166

167167
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
@@ -239,7 +239,7 @@ open class ExtendedListFragment :
239239

240240
@Suppress("ReturnCount")
241241
override fun onQueryTextSubmit(query: String): Boolean {
242-
val adapter = recyclerView.adapter
242+
val adapter = recyclerView?.adapter
243243
if (adapter is OCFileListAdapter) {
244244
val listOfHiddenFiles = adapter.listOfHiddenFiles
245245
performSearch(query, listOfHiddenFiles, false)
@@ -253,7 +253,7 @@ open class ExtendedListFragment :
253253
}
254254

255255
fun performSearch(query: String, listOfHiddenFiles: ArrayList<String?>?, isBackPressed: Boolean) {
256-
val adapter = recyclerView.adapter
256+
val adapter = recyclerView?.adapter
257257
val activity: Activity? = getActivity()
258258

259259
if (activity != null) {
@@ -296,7 +296,7 @@ open class ExtendedListFragment :
296296
}
297297

298298
override fun onClose(): Boolean {
299-
val adapter = recyclerView.adapter
299+
val adapter = recyclerView?.adapter
300300
if (adapter is OCFileListAdapter) {
301301
val listOfHiddenFiles = adapter.listOfHiddenFiles
302302
performSearch("", listOfHiddenFiles, true)
@@ -322,14 +322,14 @@ open class ExtendedListFragment :
322322
mRecyclerView?.setHasFooter(true)
323323
mRecyclerView?.setEmptyView(binding?.emptyList?.emptyListView)
324324
mRecyclerView?.setHasFixedSize(true)
325-
mRecyclerView?.setLayoutManager(LinearLayoutManager(context))
325+
mRecyclerView?.layoutManager = LinearLayoutManager(context)
326326

327327
mScale = preferences.getGridColumns()
328328
setGridViewColumns(1f)
329329

330330
mScaleGestureDetector = ScaleGestureDetector(MainApp.getAppContext(), ScaleListener())
331331

332-
recyclerView.setOnTouchListener(
332+
recyclerView?.setOnTouchListener(
333333
OnTouchListener { view: View, motionEvent: MotionEvent ->
334334
mScaleGestureDetector?.onTouchEvent(motionEvent)
335335
if (motionEvent.action == MotionEvent.ACTION_UP) {
@@ -370,7 +370,7 @@ open class ExtendedListFragment :
370370

371371
preferences.setGridColumns(mScale)
372372

373-
recyclerView.adapter?.notifyDataSetChanged()
373+
recyclerView?.adapter?.notifyDataSetChanged()
374374

375375
return true
376376
}
@@ -413,7 +413,7 @@ open class ExtendedListFragment :
413413
mHeightCell = savedInstanceState.getInt(KEY_HEIGHT_CELL)
414414
setMessageForEmptyList(savedInstanceState.getString(KEY_EMPTY_LIST_MESSAGE))
415415

416-
if (savedInstanceState.getBoolean(KEY_IS_GRID_VISIBLE, false) && recyclerView.adapter != null) {
416+
if (savedInstanceState.getBoolean(KEY_IS_GRID_VISIBLE, false) && recyclerView?.adapter != null) {
417417
switchToGridView()
418418
}
419419

@@ -714,7 +714,7 @@ open class ExtendedListFragment :
714714
}
715715

716716
if (isGridEnabled && columnsCount > maxColumnSize) {
717-
(recyclerView.layoutManager as GridLayoutManager).setSpanCount(maxColumnSize)
717+
(recyclerView?.layoutManager as GridLayoutManager).spanCount = maxColumnSize
718718
}
719719
}
720720

app/src/main/java/com/owncloud/android/ui/fragment/GalleryFragment.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,14 @@ public void onPause() {
140140
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
141141
View v = super.onCreateView(inflater, container, savedInstanceState);
142142

143-
getRecyclerView().addOnScrollListener(new RecyclerView.OnScrollListener() {
144-
@Override
145-
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
146-
loadMoreWhenEndReached(recyclerView, dy);
147-
}
148-
});
143+
if (getRecyclerView() != null) {
144+
getRecyclerView().addOnScrollListener(new RecyclerView.OnScrollListener() {
145+
@Override
146+
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
147+
loadMoreWhenEndReached(recyclerView, dy);
148+
}
149+
});
150+
}
149151

150152
Log_OC.i(this, "onCreateView() in GalleryFragment end");
151153
return v;
@@ -183,12 +185,14 @@ protected void setAdapter(Bundle args) {
183185
((EmptyRecyclerView) getRecyclerView()).setHasFooter(false);
184186
}
185187

186-
GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 1);
187-
mAdapter.setLayoutManager(layoutManager);
188-
getRecyclerView().setLayoutManager(layoutManager);
188+
if (getRecyclerView() != null) {
189+
GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 1);
190+
mAdapter.setLayoutManager(layoutManager);
191+
getRecyclerView().setLayoutManager(layoutManager);
189192

190-
if (lastMediaItemPosition != null) {
191-
layoutManager.scrollToPosition(lastMediaItemPosition);
193+
if (lastMediaItemPosition != null) {
194+
layoutManager.scrollToPosition(lastMediaItemPosition);
195+
}
192196
}
193197
}
194198

app/src/main/java/com/owncloud/android/ui/fragment/GroupfolderListFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class GroupfolderListFragment : OCFileListFragment(), Injectable, GroupfolderLis
6565
setRecyclerViewAdapter(adapter)
6666

6767
val layoutManager = LinearLayoutManager(context)
68-
recyclerView.layoutManager = layoutManager
68+
recyclerView?.layoutManager = layoutManager
6969
(recyclerView as EmptyRecyclerView).setHasFooter(false)
7070
}
7171

app/src/main/java/com/owncloud/android/ui/fragment/LocalFileListFragment.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@ public void sortFiles(FileSortOrder sortOrder) {
318318
* @param select <code>true</code> to select all, <code>false</code> to deselect all
319319
*/
320320
public void selectAllFiles(boolean select) {
321+
if (getRecyclerView() == null) {
322+
return;
323+
}
324+
321325
final var localFileListAdapter = (LocalFileListAdapter) getRecyclerView().getAdapter();
322326
if (localFileListAdapter == null) {
323327
return;
@@ -336,8 +340,12 @@ public void selectAllFiles(boolean select) {
336340

337341
@Override
338342
public void switchToGridView() {
343+
if (getRecyclerView() == null) {
344+
return;
345+
}
346+
339347
mAdapter.setGridView(true);
340-
/**
348+
/*
341349
* Set recyclerview adapter again to force new view for items. If this is not done
342350
* a few items keep their old view.
343351
*
@@ -365,8 +373,12 @@ public int getSpanSize(int position) {
365373

366374
@Override
367375
public void switchToListView() {
376+
if (getRecyclerView() == null) {
377+
return;
378+
}
379+
368380
mAdapter.setGridView(false);
369-
/** Same problem here, see switchToGridView() */
381+
/* Same problem here, see switchToGridView() */
370382
getRecyclerView().setAdapter(mAdapter);
371383
super.switchToListView();
372384
}

app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,9 @@ protected void setAdapter(Bundle args) {
462462

463463
setRecyclerViewAdapter(mAdapter);
464464

465-
fastScrollUtils.applyFastScroll(getRecyclerView());
465+
if (getRecyclerView() != null) {
466+
fastScrollUtils.applyFastScroll(getRecyclerView());
467+
}
466468
}
467469

468470
protected void prepareCurrentSearch(SearchEvent event) {
@@ -788,7 +790,7 @@ public void onDrawerStateChanged(int newState) {
788790
return;
789791
}
790792

791-
if (getRecyclerView().getAdapter() instanceof OCFileListAdapter fileListAdapter) {
793+
if (getRecyclerView() != null && getRecyclerView().getAdapter() instanceof OCFileListAdapter fileListAdapter) {
792794
mSelectionWhenActionModeClosedByDrawer.addAll(fileListAdapter.getCheckedItems());
793795
}
794796

@@ -1156,8 +1158,12 @@ private void folderOnItemClick(OCFile file, int position) {
11561158

11571159
if (ocCapability.getEndToEndEncryption().isFalse() ||
11581160
ocCapability.getEndToEndEncryption().isUnknown()) {
1159-
Snackbar.make(getRecyclerView(), R.string.end_to_end_encryption_not_enabled,
1160-
Snackbar.LENGTH_LONG).show();
1161+
1162+
if (getRecyclerView() != null) {
1163+
Snackbar.make(getRecyclerView(), R.string.end_to_end_encryption_not_enabled,
1164+
Snackbar.LENGTH_LONG).show();
1165+
}
1166+
11611167
return;
11621168
}
11631169
// check if keys are stored
@@ -1169,9 +1175,12 @@ private void folderOnItemClick(OCFile file, int position) {
11691175
if (mContainerActivity instanceof FolderPickerActivity &&
11701176
((FolderPickerActivity) mContainerActivity)
11711177
.isDoNotEnterEncryptedFolder()) {
1172-
Snackbar.make(getRecyclerView(),
1173-
R.string.copy_move_to_encrypted_folder_not_supported,
1174-
Snackbar.LENGTH_LONG).show();
1178+
1179+
if (getRecyclerView() != null) {
1180+
Snackbar.make(getRecyclerView(),
1181+
R.string.copy_move_to_encrypted_folder_not_supported,
1182+
Snackbar.LENGTH_LONG).show();
1183+
}
11751184
} else {
11761185
browseToFolder(file, position);
11771186
}
@@ -1211,7 +1220,7 @@ private Integer checkFileBeforeOpen(OCFile file) {
12111220

12121221
private void fileOnItemClick(OCFile file) {
12131222
Integer errorMessageId = checkFileBeforeOpen(file);
1214-
if (errorMessageId != null) {
1223+
if (errorMessageId != null && getRecyclerView() != null) {
12151224
Snackbar.make(getRecyclerView(),
12161225
errorMessageId,
12171226
Snackbar.LENGTH_LONG).show();
@@ -1583,10 +1592,10 @@ public void listDirectory(OCFile directory, OCFile file, boolean onlyOnDevice, b
15831592
if (file != null) {
15841593
mAdapter.setHighlightedItem(file);
15851594
int position = mAdapter.getItemPosition(file);
1586-
if (position != -1) {
1595+
if (position != -1 && getRecyclerView() != null) {
15871596
getRecyclerView().scrollToPosition(position);
15881597
}
1589-
} else if (previousDirectory == null || !previousDirectory.equals(directory)) {
1598+
} else if (getRecyclerView() != null && (previousDirectory == null || !previousDirectory.equals(directory))) {
15901599
getRecyclerView().scrollToPosition(0);
15911600
}
15921601
} else if (isSearchEventSet(searchEvent)) {
@@ -1682,10 +1691,11 @@ public void switchToGridView() {
16821691
}
16831692
}
16841693

1694+
@SuppressLint("NotifyDataSetChanged")
16851695
public void switchLayoutManager(boolean grid) {
16861696
int position = 0;
16871697

1688-
if (getRecyclerView().getLayoutManager() != null) {
1698+
if (getRecyclerView() != null && getRecyclerView().getLayoutManager() != null) {
16891699
position = ((LinearLayoutManager) getRecyclerView().getLayoutManager())
16901700
.findFirstCompletelyVisibleItemPosition();
16911701
}
@@ -1709,11 +1719,13 @@ public int getSpanSize(int position) {
17091719
layoutManager = new LinearLayoutManager(getContext());
17101720
}
17111721

1712-
getRecyclerView().setLayoutManager(layoutManager);
1713-
getRecyclerView().scrollToPosition(position);
1714-
getAdapter().setGridView(grid);
1715-
getRecyclerView().setAdapter(getAdapter());
1716-
getAdapter().notifyDataSetChanged();
1722+
if (getRecyclerView() != null) {
1723+
getRecyclerView().setLayoutManager(layoutManager);
1724+
getRecyclerView().scrollToPosition(position);
1725+
getAdapter().setGridView(grid);
1726+
getRecyclerView().setAdapter(getAdapter());
1727+
getAdapter().notifyDataSetChanged();
1728+
}
17171729
}
17181730

17191731
public CommonOCFileListAdapterInterface getCommonAdapter() {
@@ -2034,14 +2046,18 @@ private void encryptFolder(OCFile folder,
20342046
}
20352047

20362048
requireActivity().runOnUiThread(() -> mAdapter.setEncryptionAttributeForItemID(remoteId, shouldBeEncrypted));
2037-
} else if (remoteOperationResult.getHttpCode() == HttpStatus.SC_FORBIDDEN) {
2049+
} else if (remoteOperationResult.getHttpCode() == HttpStatus.SC_FORBIDDEN && getRecyclerView() != null) {
20382050
requireActivity().runOnUiThread(() -> Snackbar.make(getRecyclerView(),
20392051
R.string.end_to_end_encryption_folder_not_empty,
20402052
Snackbar.LENGTH_LONG).show());
20412053
} else {
2042-
requireActivity().runOnUiThread(() -> Snackbar.make(getRecyclerView(),
2043-
R.string.common_error_unknown,
2044-
Snackbar.LENGTH_LONG).show());
2054+
requireActivity().runOnUiThread(() -> {{
2055+
if (getRecyclerView() != null) {
2056+
Snackbar.make(getRecyclerView(),
2057+
R.string.common_error_unknown,
2058+
Snackbar.LENGTH_LONG).show();
2059+
}
2060+
}});
20452061
}
20462062

20472063
} catch (Throwable e) {
@@ -2062,17 +2078,20 @@ public void onMessageEvent(FileLockEvent event) {
20622078
if (result.isSuccess()) {
20632079
// TODO only refresh the modified file?
20642080
new Handler(Looper.getMainLooper()).post(this::onRefresh);
2065-
} else {
2081+
} else if (getRecyclerView() != null) {
20662082
Snackbar.make(getRecyclerView(),
20672083
R.string.error_file_lock,
20682084
Snackbar.LENGTH_LONG).show();
20692085
}
20702086

20712087
} catch (ClientFactory.CreationException e) {
20722088
Log_OC.e(TAG, "Cannot create client", e);
2073-
Snackbar.make(getRecyclerView(),
2074-
R.string.error_file_lock,
2075-
Snackbar.LENGTH_LONG).show();
2089+
2090+
if (getRecyclerView() != null) {
2091+
Snackbar.make(getRecyclerView(),
2092+
R.string.error_file_lock,
2093+
Snackbar.LENGTH_LONG).show();
2094+
}
20762095
} finally {
20772096
new Handler(Looper.getMainLooper()).post(() -> setLoading(false));
20782097
}
@@ -2148,6 +2167,10 @@ public boolean isSearchFragment() {
21482167
*/
21492168
@SuppressLint("NotifyDataSetChanged")
21502169
public void selectAllFiles(boolean select) {
2170+
if (getRecyclerView() == null) {
2171+
return;
2172+
}
2173+
21512174
final var adapter = getRecyclerView().getAdapter();
21522175
if (adapter instanceof CommonOCFileListAdapterInterface commonInterface) {
21532176
commonInterface.selectAll(select);

0 commit comments

Comments
 (0)