From 2bf00931d5a8bf49033bc66eb2cc38e1a0d59b2f Mon Sep 17 00:00:00 2001 From: alperozturk96 Date: Wed, 9 Sep 2026 14:56:07 +0200 Subject: [PATCH 1/2] fix(e2ee): event listen Signed-off-by: alperozturk96 --- .../dialog/SetupEncryptionDialogFragmentIT.kt | 4 +- .../OCFileListFragmentExtensions.kt | 53 +++++++++++-------- .../ui/activity/SetupEncryptionActivity.kt | 2 +- .../SetupEncryptionDialogFragment.kt | 7 ++- .../android/ui/events/EncryptionEvent.kt | 2 +- .../fragment/EncryptedFolderClickHandler.kt | 16 ++++-- .../ui/fragment/OCFileListFragment.java | 2 +- 7 files changed, 55 insertions(+), 31 deletions(-) diff --git a/app/src/androidTest/java/com/owncloud/android/ui/dialog/SetupEncryptionDialogFragmentIT.kt b/app/src/androidTest/java/com/owncloud/android/ui/dialog/SetupEncryptionDialogFragmentIT.kt index 576b11515063..688f805f8eba 100644 --- a/app/src/androidTest/java/com/owncloud/android/ui/dialog/SetupEncryptionDialogFragmentIT.kt +++ b/app/src/androidTest/java/com/owncloud/android/ui/dialog/SetupEncryptionDialogFragmentIT.kt @@ -28,7 +28,7 @@ class SetupEncryptionDialogFragmentIT : AbstractIT() { launchActivity().use { scenario -> var sut: SetupEncryptionDialogFragment? = null scenario.onActivity { activity -> - sut = SetupEncryptionDialogFragment.newInstance(user, null) + sut = SetupEncryptionDialogFragment.newInstance(user, null, null) sut.show(activity.supportFragmentManager, "1") val keyWords = arrayListOf( "ability", @@ -64,7 +64,7 @@ class SetupEncryptionDialogFragmentIT : AbstractIT() { launchActivity().use { scenario -> var sut: SetupEncryptionDialogFragment? = null scenario.onActivity { activity -> - sut = SetupEncryptionDialogFragment.newInstance(user, null) + sut = SetupEncryptionDialogFragment.newInstance(user, null, null) sut.show(activity.supportFragmentManager, "1") sut.errorSavingKeys() } diff --git a/app/src/main/java/com/nextcloud/utils/extensions/OCFileListFragmentExtensions.kt b/app/src/main/java/com/nextcloud/utils/extensions/OCFileListFragmentExtensions.kt index 89519699de9e..0c78af56f303 100644 --- a/app/src/main/java/com/nextcloud/utils/extensions/OCFileListFragmentExtensions.kt +++ b/app/src/main/java/com/nextcloud/utils/extensions/OCFileListFragmentExtensions.kt @@ -9,11 +9,13 @@ package com.nextcloud.utils.extensions import android.os.Bundle import androidx.lifecycle.lifecycleScope +import com.nextcloud.utils.e2ee.model.E2EEAction import com.owncloud.android.R import com.owncloud.android.datamodel.OCFile import com.owncloud.android.lib.common.utils.Log_OC import com.owncloud.android.ui.activity.FileActivity import com.owncloud.android.ui.dialog.setupEncryption.SetupEncryptionDialogFragment +import com.owncloud.android.ui.dialog.setupEncryption.SetupEncryptionDialogFragment.Companion.ARG_ACTION import com.owncloud.android.ui.dialog.setupEncryption.SetupEncryptionDialogFragment.Companion.ARG_FILE_PATH import com.owncloud.android.ui.dialog.setupEncryption.SetupEncryptionDialogFragment.Companion.RESULT_REQUEST_KEY import com.owncloud.android.ui.dialog.setupEncryption.SetupEncryptionDialogFragment.Companion.SUCCESS @@ -23,7 +25,7 @@ import kotlinx.coroutines.launch private const val TAG = "OCFileListFragmentExtensions" -fun OCFileListFragment.showEncryptionDialog(remotePath: String?) { +fun OCFileListFragment.showEncryptionDialog(remotePath: String?, action: E2EEAction) { if (parentFragmentManager.findFragmentByTag(SetupEncryptionDialogFragment.SETUP_ENCRYPTION_DIALOG_TAG) != null) { return } @@ -31,8 +33,8 @@ fun OCFileListFragment.showEncryptionDialog(remotePath: String?) { val user = accountManager.user val connectivityService = typedActivity()?.connectivityService connectivityService?.isNetworkAndServerAvailable { result -> - if (result == true) { - SetupEncryptionDialogFragment.newInstance(user, remotePath) + if (result) { + SetupEncryptionDialogFragment.newInstance(user, remotePath, action) .show(parentFragmentManager, SetupEncryptionDialogFragment.SETUP_ENCRYPTION_DIALOG_TAG) return@isNetworkAndServerAvailable } @@ -55,30 +57,37 @@ fun OCFileListFragment.listenEncryptionDialogResult() { return@setFragmentResultListener } - val fileRemotePath = bundle.getString(ARG_FILE_PATH, null) - if (fileRemotePath == null) { - Log_OC.e(TAG, "file path is null") + val action = bundle.getSerializableArgument(ARG_ACTION, E2EEAction::class.java) + if (action == null) { + Log_OC.e(TAG, "no pending encryption action, nothing to continue with") return@setFragmentResultListener } - val file: OCFile? = mContainerActivity.getStorageManager().getFileByDecryptedRemotePath(fileRemotePath) - if (file == null) { - Log_OC.e(TAG, "file is null, cannot toggle encryption") - return@setFragmentResultListener - } + when (action) { + E2EEAction.NEW_FOLDER -> createFolder(true) - if (file.isRootDirectory) { - Log_OC.d( - TAG, - "result of setup encryption triggered in root directory, this call is for " + - "creating encrypted folder" - ) - createFolder(true) - return@setFragmentResultListener - } + E2EEAction.OPEN -> folderFromResult(bundle)?.let { clickHandler.openAfterKeySetup(it) } - lifecycleScope.launch { - folderEncryption.toggle(file.toEncryptionEvent(true)) + E2EEAction.ENCRYPT -> folderFromResult(bundle)?.let { file -> + lifecycleScope.launch { + folderEncryption.toggle(file.toEncryptionEvent(true)) + } + } } } } + +private fun OCFileListFragment.folderFromResult(bundle: Bundle): OCFile? { + val remotePath = bundle.getString(ARG_FILE_PATH, null) + if (remotePath == null) { + Log_OC.e(TAG, "file path is null") + return null + } + + val file = mContainerActivity.storageManager.getFileByEncryptedRemotePath(remotePath) + if (file == null) { + Log_OC.e(TAG, "file is null, cannot continue encryption action") + } + + return file +} diff --git a/app/src/main/java/com/owncloud/android/ui/activity/SetupEncryptionActivity.kt b/app/src/main/java/com/owncloud/android/ui/activity/SetupEncryptionActivity.kt index 9ad74e9edf49..6de75832ce1f 100644 --- a/app/src/main/java/com/owncloud/android/ui/activity/SetupEncryptionActivity.kt +++ b/app/src/main/java/com/owncloud/android/ui/activity/SetupEncryptionActivity.kt @@ -30,7 +30,7 @@ class SetupEncryptionActivity : AppCompatActivity() { finish() } - val setupEncryptionDialogFragment = SetupEncryptionDialogFragment.newInstance(user, null) + val setupEncryptionDialogFragment = SetupEncryptionDialogFragment.newInstance(user, null, null) supportFragmentManager.setFragmentResultListener( SetupEncryptionDialogFragment.RESULT_REQUEST_KEY, this diff --git a/app/src/main/java/com/owncloud/android/ui/dialog/setupEncryption/SetupEncryptionDialogFragment.kt b/app/src/main/java/com/owncloud/android/ui/dialog/setupEncryption/SetupEncryptionDialogFragment.kt index 1034a7d4fe2a..ec6ec83a13a4 100644 --- a/app/src/main/java/com/owncloud/android/ui/dialog/setupEncryption/SetupEncryptionDialogFragment.kt +++ b/app/src/main/java/com/owncloud/android/ui/dialog/setupEncryption/SetupEncryptionDialogFragment.kt @@ -21,7 +21,9 @@ import com.google.android.material.textfield.TextInputLayout import com.nextcloud.client.account.User import com.nextcloud.client.di.Injectable import com.nextcloud.client.network.ClientFactory +import com.nextcloud.utils.e2ee.model.E2EEAction import com.nextcloud.utils.extensions.getParcelableArgument +import com.nextcloud.utils.extensions.getSerializableArgument import com.owncloud.android.BuildConfig import com.owncloud.android.R import com.owncloud.android.databinding.SetupEncryptionDialogBinding @@ -243,6 +245,7 @@ class SetupEncryptionDialogFragment : return Bundle().apply { putBoolean(SUCCESS, true) putString(ARG_FILE_PATH, requireArguments().getString(ARG_FILE_PATH)) + putSerializable(ARG_ACTION, arguments.getSerializableArgument(ARG_ACTION, E2EEAction::class.java)) } } @@ -526,6 +529,7 @@ class SetupEncryptionDialogFragment : const val SETUP_ENCRYPTION_RESULT_CODE = 101 const val SETUP_ENCRYPTION_DIALOG_TAG = "SETUP_ENCRYPTION_DIALOG_TAG" const val ARG_FILE_PATH = "ARG_FILE_PATH" + const val ARG_ACTION = "ARG_ACTION" const val RESULT_REQUEST_KEY = "RESULT_REQUEST" const val RESULT_KEY_CANCELLED = "IS_CANCELLED" private const val NUMBER_OF_WORDS = 12 @@ -537,11 +541,12 @@ class SetupEncryptionDialogFragment : private const val KEY_GENERATE = "KEY_GENERATE" @JvmStatic - fun newInstance(user: User?, filePath: String?): SetupEncryptionDialogFragment = + fun newInstance(user: User?, filePath: String?, action: E2EEAction?): SetupEncryptionDialogFragment = SetupEncryptionDialogFragment().apply { arguments = Bundle().apply { putParcelable(ARG_USER, user) putString(ARG_FILE_PATH, filePath) + putSerializable(ARG_ACTION, action) } } } diff --git a/app/src/main/java/com/owncloud/android/ui/events/EncryptionEvent.kt b/app/src/main/java/com/owncloud/android/ui/events/EncryptionEvent.kt index 1a7dbc56c9a4..a5763af837d8 100644 --- a/app/src/main/java/com/owncloud/android/ui/events/EncryptionEvent.kt +++ b/app/src/main/java/com/owncloud/android/ui/events/EncryptionEvent.kt @@ -26,7 +26,7 @@ class EncryptionEvent(val localId: Long, val remoteId: String, val remotePath: S } E2EEKeyCheck.ONLY_ON_SERVER, E2EEKeyCheck.MISSING_EVERYWHERE -> { - fragment.showEncryptionDialog(remotePath) + fragment.showEncryptionDialog(remotePath, E2EEAction.ENCRYPT) } E2EEKeyCheck.ONLY_ON_DEVICE, E2EEKeyCheck.DIFFERS_FROM_SERVER -> { diff --git a/app/src/main/java/com/owncloud/android/ui/fragment/EncryptedFolderClickHandler.kt b/app/src/main/java/com/owncloud/android/ui/fragment/EncryptedFolderClickHandler.kt index e182094edc46..3366ce48fbba 100644 --- a/app/src/main/java/com/owncloud/android/ui/fragment/EncryptedFolderClickHandler.kt +++ b/app/src/main/java/com/owncloud/android/ui/fragment/EncryptedFolderClickHandler.kt @@ -48,7 +48,7 @@ class EncryptedFolderClickHandler(private val fragment: OCFileListFragment) { E2EEKeyCheck.ONLY_ON_SERVER, E2EEKeyCheck.MISSING_EVERYWHERE -> { Log_OC.d(TAG, "keys found on server but missing locally, redirecting to encryption setup") - fragment.showEncryptionDialog(OCFile.ROOT_PATH) + fragment.showEncryptionDialog(OCFile.ROOT_PATH, E2EEAction.NEW_FOLDER) } E2EEKeyCheck.SAME_AS_SERVER -> { @@ -86,7 +86,7 @@ class EncryptedFolderClickHandler(private val fragment: OCFileListFragment) { } E2EEKeyCheck.ONLY_ON_SERVER -> { - fragment.showEncryptionDialog(file.remotePath) + fragment.showEncryptionDialog(file.remotePath, E2EEAction.OPEN) } E2EEKeyCheck.MISSING_EVERYWHERE -> { @@ -114,6 +114,16 @@ class EncryptedFolderClickHandler(private val fragment: OCFileListFragment) { } } + fun openAfterKeySetup(file: OCFile) { + fragment.lifecycleScope.launch { + if (fragment.e2eeActionResolver.checkFolderMetadataKey(file)) { + onEncryptionSetupComplete(file, fragment.adapter.getItemPosition(file)) + } else { + DisplayUtils.showSnackMessage(fragment, R.string.encryption_open_key_mismatch) + } + } + } + private fun dismissCheckingSnackbar() { DisplayUtils.dismissSnackMessage(checkingKeysSnackbar) checkingKeysSnackbar = null @@ -131,7 +141,7 @@ class EncryptedFolderClickHandler(private val fragment: OCFileListFragment) { if (FileOperationsHelper.isEndToEndEncryptionSetup(fragment.context, user)) { onEncryptionSetupComplete(file, position) } else { - fragment.showEncryptionDialog(file.remotePath) + fragment.showEncryptionDialog(file.remotePath, E2EEAction.OPEN) } } diff --git a/app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java b/app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java index 2e40256f37b7..bbc6f2ec42d1 100644 --- a/app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java +++ b/app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java @@ -202,7 +202,7 @@ public class OCFileListFragment extends ExtendedListFragment implements @Inject ThumbnailGenerator thumbnailGenerator; @Inject public E2EEActionResolver e2eeActionResolver; public E2EEDialogPresenter e2eeDialogPresenter; - private EncryptedFolderClickHandler clickHandler; + public EncryptedFolderClickHandler clickHandler; public FolderEncryption folderEncryption; public FileFragment.ContainerActivity mContainerActivity; From 29089f27aeb8d2417239386b5773beed53d5cd89 Mon Sep 17 00:00:00 2001 From: alperozturk96 Date: Mon, 14 Sep 2026 11:41:31 +0200 Subject: [PATCH 2/2] fix spotbugs Signed-off-by: alperozturk96 --- .../OCFileListFragmentExtensions.kt | 4 +- .../fragment/EncryptedFolderClickHandler.kt | 4 +- .../android/ui/fragment/FolderEncryption.kt | 2 +- .../android/ui/fragment/GalleryFragment.kt | 8 +- .../ui/fragment/GroupfolderListFragment.kt | 2 +- .../ui/fragment/OCFileListFragment.java | 111 ++++++++++++------ .../android/ui/fragment/SharedListFragment.kt | 4 +- 7 files changed, 86 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/com/nextcloud/utils/extensions/OCFileListFragmentExtensions.kt b/app/src/main/java/com/nextcloud/utils/extensions/OCFileListFragmentExtensions.kt index 0c78af56f303..33fc9b1a5872 100644 --- a/app/src/main/java/com/nextcloud/utils/extensions/OCFileListFragmentExtensions.kt +++ b/app/src/main/java/com/nextcloud/utils/extensions/OCFileListFragmentExtensions.kt @@ -66,7 +66,7 @@ fun OCFileListFragment.listenEncryptionDialogResult() { when (action) { E2EEAction.NEW_FOLDER -> createFolder(true) - E2EEAction.OPEN -> folderFromResult(bundle)?.let { clickHandler.openAfterKeySetup(it) } + E2EEAction.OPEN -> folderFromResult(bundle)?.let { encryptedClickHandler.openAfterKeySetup(it) } E2EEAction.ENCRYPT -> folderFromResult(bundle)?.let { file -> lifecycleScope.launch { @@ -84,7 +84,7 @@ private fun OCFileListFragment.folderFromResult(bundle: Bundle): OCFile? { return null } - val file = mContainerActivity.storageManager.getFileByEncryptedRemotePath(remotePath) + val file = containerActivity.storageManager.getFileByEncryptedRemotePath(remotePath) if (file == null) { Log_OC.e(TAG, "file is null, cannot continue encryption action") } diff --git a/app/src/main/java/com/owncloud/android/ui/fragment/EncryptedFolderClickHandler.kt b/app/src/main/java/com/owncloud/android/ui/fragment/EncryptedFolderClickHandler.kt index 3366ce48fbba..3a69ec1b7866 100644 --- a/app/src/main/java/com/owncloud/android/ui/fragment/EncryptedFolderClickHandler.kt +++ b/app/src/main/java/com/owncloud/android/ui/fragment/EncryptedFolderClickHandler.kt @@ -131,7 +131,7 @@ class EncryptedFolderClickHandler(private val fragment: OCFileListFragment) { private fun onFolderKeyVerified(file: OCFile, position: Int, fileActivity: FileActivity) { val user = fileActivity.user.orElseThrow { RuntimeException() } - val capability = fragment.mContainerActivity.getStorageManager().getCapability(user.accountName) + val capability = fragment.containerActivity.getStorageManager().getCapability(user.accountName) if (capability.endToEndEncryption.isFalse || capability.endToEndEncryption.isUnknown) { DisplayUtils.showSnackMessage(fragment, R.string.end_to_end_encryption_not_enabled) @@ -149,7 +149,7 @@ class EncryptedFolderClickHandler(private val fragment: OCFileListFragment) { fragment.searchFragment = false fragment.mHideFab = false - val folderPickerActivity = fragment.mContainerActivity as? FolderPickerActivity + val folderPickerActivity = fragment.containerActivity as? FolderPickerActivity if (folderPickerActivity?.isDoNotEnterEncryptedFolder == true) { DisplayUtils.showSnackMessage(fragment, R.string.copy_move_to_encrypted_folder_not_supported) } else { diff --git a/app/src/main/java/com/owncloud/android/ui/fragment/FolderEncryption.kt b/app/src/main/java/com/owncloud/android/ui/fragment/FolderEncryption.kt index 9823ec26effd..fc461f90e7dd 100644 --- a/app/src/main/java/com/owncloud/android/ui/fragment/FolderEncryption.kt +++ b/app/src/main/java/com/owncloud/android/ui/fragment/FolderEncryption.kt @@ -38,7 +38,7 @@ class FolderEncryption(private val fragment: OCFileListFragment) { val shouldBeEncrypted = event.shouldBeEncrypted try { - val storageManager = fragment.mContainerActivity.storageManager + val storageManager = fragment.containerActivity.storageManager val folder = storageManager.getFileByRemoteId(remoteId) ?: run { Log_OC.e(TAG, "folder is null, cannot encrypt") return@withContext false diff --git a/app/src/main/java/com/owncloud/android/ui/fragment/GalleryFragment.kt b/app/src/main/java/com/owncloud/android/ui/fragment/GalleryFragment.kt index a2b6fe5ced4e..17f6054e6a61 100644 --- a/app/src/main/java/com/owncloud/android/ui/fragment/GalleryFragment.kt +++ b/app/src/main/java/com/owncloud/android/ui/fragment/GalleryFragment.kt @@ -211,7 +211,7 @@ class GalleryFragment : requireContext(), accountManager.user, this, - mContainerActivity, + containerActivity, viewThemeUtils, this.columnsCount, ThumbnailsCacheManager.getThumbnailDimension(), @@ -362,7 +362,7 @@ class GalleryFragment : } private fun runGallerySearchTask() { - if (mContainerActivity == null) { + if (containerActivity == null) { Log_OC.w(TAG, "container activity is null, can't run search task") return } @@ -372,7 +372,7 @@ class GalleryFragment : photoSearchTask = GallerySearchTask( this, accountManager.user, - mContainerActivity.getStorageManager(), + containerActivity.getStorageManager(), endDate, limit ).execute() @@ -445,7 +445,7 @@ class GalleryFragment : showGalleryJob?.cancel() showGalleryJob = lifecycleScope.launch(Dispatchers.Default) { val remotePath = preferences.getLastSelectedMediaFolder() - val items = mContainerActivity.storageManager.getGalleryItemsPageSuspended( + val items = containerActivity.storageManager.getGalleryItemsPageSuspended( remotePath, mimeFilter, loadedItemCount diff --git a/app/src/main/java/com/owncloud/android/ui/fragment/GroupfolderListFragment.kt b/app/src/main/java/com/owncloud/android/ui/fragment/GroupfolderListFragment.kt index c5a6716d4d01..68bb888306ec 100644 --- a/app/src/main/java/com/owncloud/android/ui/fragment/GroupfolderListFragment.kt +++ b/app/src/main/java/com/owncloud/android/ui/fragment/GroupfolderListFragment.kt @@ -67,7 +67,7 @@ class GroupfolderListFragment : GroupfoldersSearchTask( this, accountManager.user, - mContainerActivity.storageManager + containerActivity.storageManager ).execute() } diff --git a/app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java b/app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java index bbc6f2ec42d1..61d8df3fff7a 100644 --- a/app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java +++ b/app/src/main/java/com/owncloud/android/ui/fragment/OCFileListFragment.java @@ -110,7 +110,6 @@ import com.owncloud.android.ui.fragment.helper.ParentFolderFinder; import com.owncloud.android.ui.helpers.FileOperationsHelper; import com.owncloud.android.ui.interfaces.OCFileListFragmentInterface; -import com.owncloud.android.ui.preview.PreviewImageFragment; import com.owncloud.android.utils.DisplayUtils; import com.owncloud.android.utils.FileSortOrder; import com.owncloud.android.utils.FileStorageUtils; @@ -200,11 +199,12 @@ public class OCFileListFragment extends ExtendedListFragment implements @Inject SyncedFolderProvider syncedFolderProvider; @Inject AppScanOptionalFeature appScanOptionalFeature; @Inject ThumbnailGenerator thumbnailGenerator; - @Inject public E2EEActionResolver e2eeActionResolver; - public E2EEDialogPresenter e2eeDialogPresenter; - public EncryptedFolderClickHandler clickHandler; - public FolderEncryption folderEncryption; - public FileFragment.ContainerActivity mContainerActivity; + + @Inject E2EEActionResolver e2eeActionResolver; + private E2EEDialogPresenter e2eeDialogPresenter; + private EncryptedFolderClickHandler clickHandler; + private FolderEncryption folderEncryption; + private FileFragment.ContainerActivity mContainerActivity; protected OCFile mFile; protected OCFileListAdapter mAdapter; @@ -321,6 +321,26 @@ public void onAttach(@NonNull Context context) { } } + public FileFragment.ContainerActivity getContainerActivity() { + return mContainerActivity; + } + + public FolderEncryption getFolderEncryption() { + return folderEncryption; + } + + public EncryptedFolderClickHandler getEncryptedClickHandler() { + return clickHandler; + } + + public E2EEDialogPresenter getE2eeDialogPresenter() { + return e2eeDialogPresenter; + } + + public E2EEActionResolver getE2eeActionResolver() { + return e2eeActionResolver; + } + public void setSearchArgs(Bundle state) { SearchType argSearchType = NO_SEARCH; SearchEvent argSearchEvent = null; @@ -386,8 +406,9 @@ public void onPause() { mAdapter.cancelAllPendingTasks(); } - if (getActivity() != null) { - getActivity().getIntent().removeExtra(OCFileListFragment.SEARCH_EVENT); + final var activity = getActivity(); + if (activity != null) { + activity.getIntent().removeExtra(OCFileListFragment.SEARCH_EVENT); } } @@ -1165,8 +1186,9 @@ private Integer checkFileBeforeOpen(OCFile file) { private void fileOnItemClick(OCFile file) { Integer errorMessageId = checkFileBeforeOpen(file); - if (getRecyclerView() != null && errorMessageId != null) { - Snackbar.make(getRecyclerView(), errorMessageId, Snackbar.LENGTH_LONG).show(); + final var recyclerView = getRecyclerView(); + if (recyclerView != null && errorMessageId != null) { + Snackbar.make(recyclerView, errorMessageId, Snackbar.LENGTH_LONG).show(); return; } @@ -1205,15 +1227,21 @@ private void handlePendingDownloadFile(OCFile file) { return; } - boolean webViewAvailable = WebViewUtil.available(getContext()); + final var context = getContext(); + if (context == null) { + Log_OC.e(TAG, "context not available"); + return; + } + + boolean webViewAvailable = WebViewUtil.available(context); if (!file.isEncrypted() && mContainerActivity instanceof FileDisplayActivity fda && fda.canMediaPreviewed(file)) { setFabVisible(false); fda.startMediaPreview(file, true, true); } else if (webViewAvailable && editorUtils.getEditor(accountManager.getUser(), file.getMimeType()) != null && !file.isEncrypted()) { - TextEditorWebView.Companion.startTextEditor(file, getContext()); + TextEditorWebView.Companion.startTextEditor(file, context); } else if (supportsDirectEditing(file, webViewAvailable)) { - mContainerActivity.getFileOperationsHelper().openFileAsRichDocument(file, getContext()); + mContainerActivity.getFileOperationsHelper().openFileAsRichDocument(file, context); } else if (mContainerActivity instanceof FileDisplayActivity fda) { fda.startDownloadForPreview(file, mFile); @@ -1654,8 +1682,11 @@ protected void prepareActionBarItems(SearchEvent event) { } } - if (FILE_SEARCH != currentSearchType && getActivity() != null) { - getActivity().invalidateOptionsMenu(); + if (FILE_SEARCH != currentSearchType) { + final var activity = getActivity(); + if (activity != null) { + activity.invalidateOptionsMenu(); + } } } @@ -1875,20 +1906,21 @@ public void onMessageEvent(FileLockEvent event) { if (result.isSuccess()) { // TODO only refresh the modified file? new Handler(Looper.getMainLooper()).post(this::onRefresh); - } else if (getRecyclerView() != null) { - Snackbar.make(getRecyclerView(), - R.string.error_file_lock, - Snackbar.LENGTH_LONG).show(); + } else { + final var recyclerView = getRecyclerView(); + if (recyclerView == null) { + return; + } + Snackbar.make(recyclerView, R.string.error_file_lock, Snackbar.LENGTH_LONG).show(); } } catch (ClientFactory.CreationException e) { Log_OC.e(TAG, "Cannot create client", e); - - if (getRecyclerView() != null) { - Snackbar.make(getRecyclerView(), - R.string.error_file_lock, - Snackbar.LENGTH_LONG).show(); + final var recyclerView = getRecyclerView(); + if (recyclerView == null) { + return; } + Snackbar.make(recyclerView, R.string.error_file_lock, Snackbar.LENGTH_LONG).show(); } } @@ -1944,11 +1976,12 @@ public void cancelAndRetriggerSearch() { */ @SuppressLint("NotifyDataSetChanged") public void selectAllFiles(boolean select) { - if (getRecyclerView() == null) { + final var recyclerView = getRecyclerView(); + if (recyclerView == null) { return; } - final var adapter = getRecyclerView().getAdapter(); + final var adapter = recyclerView.getAdapter(); if (adapter instanceof CommonOCFileListAdapterInterface commonInterface) { commonInterface.selectAll(select); adapter.notifyDataSetChanged(); @@ -2112,22 +2145,26 @@ public void slideHideBottomBehaviourForBottomNavigationView(boolean visible) { * @param enabled Desired visibility for the FAB. */ public void setFabEnabled(final boolean enabled) { - if (mFabMain == null) { + final var fabMain = mFabMain; + if (fabMain == null) { // is not available in FolderPickerActivity return; } - if (getActivity() != null) { - getActivity().runOnUiThread(() -> { - if (enabled) { - mFabMain.setEnabled(true); - viewThemeUtils.material.themeFAB(mFabMain); - } else { - mFabMain.setEnabled(false); - viewThemeUtils.material.themeFAB(mFabMain); - } - }); + final var activity = getActivity(); + if (activity == null) { + return; } + + activity.runOnUiThread(() -> { + if (enabled) { + fabMain.setEnabled(true); + viewThemeUtils.material.themeFAB(fabMain); + } else { + fabMain.setEnabled(false); + viewThemeUtils.material.themeFAB(fabMain); + } + }); } /** diff --git a/app/src/main/java/com/owncloud/android/ui/fragment/SharedListFragment.kt b/app/src/main/java/com/owncloud/android/ui/fragment/SharedListFragment.kt index 30857c9665eb..2f9b050622cf 100644 --- a/app/src/main/java/com/owncloud/android/ui/fragment/SharedListFragment.kt +++ b/app/src/main/java/com/owncloud/android/ui/fragment/SharedListFragment.kt @@ -75,7 +75,7 @@ class SharedListFragment : val fetchResult = ReadFileRemoteOperation(partialFile.remotePath).execute(user, context) if (fetchResult.isSuccess) { val remoteFile = (fetchResult.data[0] as RemoteFile).apply { - val existingFile = mContainerActivity.storageManager.getFileByDecryptedRemotePath(remotePath) + val existingFile = containerActivity.storageManager.getFileByDecryptedRemotePath(remotePath) // Use previous eTag if exists to prevent break checkForChanges logic in RefreshFolderOperation. // Otherwise RefreshFolderOperation will show empty list @@ -88,7 +88,7 @@ class SharedListFragment : parentId = partialFile.parentId } FileStorageUtils.searchForLocalFileInDefaultPath(file, user.accountName) - val savedFile = mContainerActivity.storageManager.saveFileWithParent(file, context) + val savedFile = containerActivity.storageManager.saveFileWithParent(file, context) savedFile } else { logger.e(SHARED_TAG, "Error fetching file")