Skip to content

Commit d608d9c

Browse files
committed
handle nullable current file
Signed-off-by: alperozturk <[email protected]>
1 parent 90ebd53 commit d608d9c

File tree

7 files changed

+67
-49
lines changed

7 files changed

+67
-49
lines changed

app/src/main/java/com/owncloud/android/ui/activity/FileActivity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ protected void onSaveInstanceState(@NonNull Bundle outState) {
334334
*
335335
* @return Main {@link OCFile} handled by the activity.
336336
*/
337+
@Nullable
337338
public OCFile getFile() {
338339
return mFile;
339340
}
@@ -654,6 +655,7 @@ public FileUploadHelper getFileUploaderHelper() {
654655
return fileUploadHelper;
655656
}
656657

658+
@Nullable
657659
public OCFile getCurrentDir() {
658660
OCFile file = getFile();
659661
if (file != null) {

app/src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.kt

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,7 @@ class FileDisplayActivity :
10701070
localBasePath = "$localBasePath/"
10711071
}
10721072

1073-
val remotePathBase = getCurrentDir().remotePath
1073+
val remotePathBase = getCurrentDir()?.remotePath
10741074
val decryptedRemotePaths = getRemotePaths(remotePathBase, filePaths, localBasePath)
10751075

10761076
val behaviour = when (resultCode) {
@@ -1081,8 +1081,8 @@ class FileDisplayActivity :
10811081

10821082
connectivityService.isNetworkAndServerAvailable { result: Boolean? ->
10831083
if (result == true) {
1084-
val isValidFolderPath = checkFolderPath(remotePathBase, capabilities, this)
1085-
if (!isValidFolderPath) {
1084+
val isValidFolderPath = remotePathBase?.let { checkFolderPath(it, capabilities, this) }
1085+
if (isValidFolderPath == false) {
10861086
DisplayUtils.showSnackMessage(
10871087
this,
10881088
R.string.file_name_validator_error_contains_reserved_names_or_invalid_characters
@@ -1493,9 +1493,8 @@ class FileDisplayActivity :
14931493
return
14941494
}
14951495

1496-
var currentFile = if (file == null) null else storageManager.getFileByPath(file.remotePath)
1497-
val currentDir =
1498-
if (getCurrentDir() == null) null else storageManager.getFileByPath(getCurrentDir().remotePath)
1496+
var currentFile = file?.remotePath?.let { storageManager.getFileByPath(it) }
1497+
val currentDir = getCurrentDir()?.remotePath?.let { storageManager.getFileByPath(it) }
14991498
val isSyncFolderRemotePathRoot = OCFile.ROOT_PATH == syncFolderRemotePath
15001499

15011500
if (currentDir == null && !isSyncFolderRemotePathRoot) {
@@ -1519,7 +1518,7 @@ class FileDisplayActivity :
15191518
}
15201519

15211520
private fun handleRemovedFileFromServer(currentFile: OCFile?, currentDir: OCFile?): OCFile? {
1522-
if (currentFile == null && !file.isFolder) {
1521+
if (currentFile == null && file?.isFolder == false) {
15231522
resetTitleBarAndScrolling()
15241523
return currentDir
15251524
}
@@ -1668,8 +1667,8 @@ class FileDisplayActivity :
16681667
var sameFile = false
16691668
if (file != null) {
16701669
renamedInUpload =
1671-
file.remotePath == intent.getStringExtra(FileUploadWorker.EXTRA_OLD_REMOTE_PATH)
1672-
sameFile = file.remotePath == uploadedRemotePath || renamedInUpload
1670+
file?.remotePath == intent.getStringExtra(FileUploadWorker.EXTRA_OLD_REMOTE_PATH)
1671+
sameFile = file?.remotePath == uploadedRemotePath || renamedInUpload
16731672
}
16741673

16751674
if (sameAccount && sameFile && this@FileDisplayActivity.leftFragment is FileDetailFragment) {
@@ -1689,21 +1688,21 @@ class FileDisplayActivity :
16891688
)
16901689
}
16911690

1692-
if (uploadWasFine || file != null && file.fileExists()) {
1691+
if (uploadWasFine || file?.fileExists() == true) {
16931692
fileDetailFragment.updateFileDetails(false, true)
16941693
} else {
16951694
onBackPressedDispatcher.onBackPressed()
16961695
}
16971696

16981697
// Force the preview if the file is an image or text file
16991698
if (uploadWasFine) {
1700-
val ocFile = file
1701-
if (PreviewImageFragment.canBePreviewed(ocFile)) {
1702-
startImagePreview(file, true)
1703-
} else if (PreviewTextFileFragment.canBePreviewed(ocFile)) {
1704-
startTextPreview(ocFile, true)
1699+
file?.let {
1700+
if (PreviewImageFragment.canBePreviewed(it)) {
1701+
startImagePreview(it, true)
1702+
} else if (PreviewTextFileFragment.canBePreviewed(it)) {
1703+
startTextPreview(it, true)
1704+
}
17051705
}
1706-
// TODO what about other kind of previews?
17071706
}
17081707
}
17091708
}
@@ -1961,7 +1960,7 @@ class FileDisplayActivity :
19611960
if (getCurrentDir() !=
19621961
null
19631962
) {
1964-
storageManager.getFileByDecryptedRemotePath(getCurrentDir().remotePath)
1963+
storageManager.getFileByDecryptedRemotePath(getCurrentDir()?.remotePath)
19651964
} else {
19661965
null
19671966
}
@@ -2131,17 +2130,17 @@ class FileDisplayActivity :
21312130
val file = getFile()
21322131

21332132
// delete old local copy
2134-
if (file.isDown) {
2133+
if (file?.isDown == true) {
21352134
val list: MutableList<OCFile?> = ArrayList()
21362135
list.add(file)
21372136
fileOperationsHelper.removeFiles(list, true, true)
21382137

21392138
// download new version, only if file was previously download
2140-
showSyncLoadingDialog(file.isFolder)
2139+
showSyncLoadingDialog(file.isFolder == true)
21412140
fileOperationsHelper.syncFile(file)
21422141
}
21432142

2144-
val parent = storageManager.getFileById(file.parentId)
2143+
val parent = file?.let { storageManager.getFileById(it.parentId) }
21452144
startSyncFolderOperation(parent, ignoreETag = true, ignoreFocus = true)
21462145

21472146
val leftFragment = this.leftFragment
@@ -2974,11 +2973,17 @@ class FileDisplayActivity :
29742973
}
29752974

29762975
fun performUnifiedSearch(query: String, listOfHiddenFiles: ArrayList<String>?) {
2976+
val path = currentDir?.decryptedRemotePath
2977+
?: run {
2978+
Log_OC.w(TAG, "currentDir is null, using ROOT_PATH")
2979+
OCFile.ROOT_PATH
2980+
}
2981+
29772982
val unifiedSearchFragment =
29782983
UnifiedSearchFragment.Companion.newInstance(
29792984
query,
29802985
listOfHiddenFiles,
2981-
currentDir.decryptedRemotePath
2986+
path
29822987
)
29832988
setLeftFragment(unifiedSearchFragment, false)
29842989
}

app/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,10 @@ open class FolderPickerActivity :
409409
file?.isFolder != true -> true
410410

411411
// all of the target files are already in the selected directory
412-
targetFilePaths?.all { PathUtils.isDirectParent(file.remotePath, it) } == true -> false
412+
targetFilePaths?.all { PathUtils.isDirectParent(file?.remotePath ?: "", it) } == true -> false
413413

414414
// some of the target files are parents of the selected folder
415-
targetFilePaths?.any { PathUtils.isAncestor(it, file.remotePath) } == true -> false
415+
targetFilePaths?.any { PathUtils.isAncestor(it, file?.remotePath ?: "") } == true -> false
416416
else -> true
417417
}
418418

@@ -429,7 +429,7 @@ open class FolderPickerActivity :
429429
}
430430

431431
private fun getSelectedFolderPathTitle(): String? {
432-
val atRoot = (currentDir == null || currentDir.parentId == 0L)
432+
val atRoot = (currentDir == null || currentDir?.parentId == 0L)
433433
return if (atRoot) captionText ?: "" else currentDir?.fileName
434434
}
435435

@@ -555,7 +555,7 @@ open class FolderPickerActivity :
555555
if (currentDir == null) {
556556
browseRootForRemovedFolder()
557557
} else {
558-
if (currentFile == null && !file.isFolder) {
558+
if (currentFile == null && file?.isFolder == false) {
559559
// currently selected file was removed in the server, and now we know it
560560
currentFile = currentDir
561561
}
@@ -580,8 +580,7 @@ open class FolderPickerActivity :
580580
}
581581

582582
private fun getCurrentFileAndDirectory(): Pair<OCFile?, OCFile?> {
583-
val currentFile =
584-
if (file == null) null else storageManager.getFileByEncryptedRemotePath(file.remotePath)
583+
val currentFile = file?.let { storageManager.getFileByEncryptedRemotePath(it.remotePath) }
585584

586585
val currentDir = if (currentFolder == null) {
587586
null

app/src/main/java/com/owncloud/android/ui/activity/RichDocumentsEditorWebView.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ class RichDocumentsEditorWebView : EditorWebView() {
185185
renameString ?: return
186186
val renameJson = JSONObject(renameString)
187187
val newName = renameJson.getString(NEW_NAME)
188-
file.fileName = newName
188+
file?.fileName = newName
189189
} catch (e: JSONException) {
190190
Log_OC.e(this, "Failed to parse rename json message: $e")
191191
}

app/src/main/java/com/owncloud/android/ui/activity/TextEditorWebView.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class TextEditorWebView : EditorWebView() {
4343
finish()
4444
}
4545

46-
val editor = editorUtils.getEditor(user.get(), file.mimeType)
46+
val editor = editorUtils.getEditor(user.get(), file?.mimeType)
4747

4848
if (editor != null && editor.id == "onlyoffice") {
4949
webView.settings.userAgentString = generateOnlyOfficeUserAgent()

app/src/main/java/com/owncloud/android/ui/preview/PreviewImageActivity.kt

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class PreviewImageActivity :
177177
)
178178
} else {
179179
// get parent from path
180-
var parentFolder = storageManager.getFileById(file.parentId)
180+
var parentFolder = file?.let { storageManager.getFileById(it.parentId) }
181181

182182
if (parentFolder == null) {
183183
// should not be necessary
@@ -197,7 +197,13 @@ class PreviewImageActivity :
197197

198198
viewPager = findViewById(R.id.fragmentPager)
199199

200-
var position = if (savedPosition != null) savedPosition else previewImagePagerAdapter?.getFilePosition(file)
200+
var position = if (savedPosition !=
201+
null
202+
) {
203+
savedPosition
204+
} else {
205+
file?.let { previewImagePagerAdapter?.getFilePosition(it) }
206+
}
201207
position = position?.toDouble()?.let { max(it, 0.0).toInt() }
202208

203209
viewPager?.adapter = previewImagePagerAdapter
@@ -210,7 +216,7 @@ class PreviewImageActivity :
210216
viewPager?.setCurrentItem(position, false)
211217
}
212218

213-
if (position == 0 && !file.isDown) {
219+
if (position == 0 && file?.isDown == false) {
214220
// this is necessary because mViewPager.setCurrentItem(0) just after setting the
215221
// adapter does not result in a call to #onPageSelected(0)
216222
screenState = PreviewImageActivityState.WaitingForBinder
@@ -275,7 +281,7 @@ class PreviewImageActivity :
275281
if (file != null) {
276282
// / Refresh the activity according to the Account and OCFile set
277283
setFile(file) // reset after getting it fresh from storageManager
278-
updateActionBarTitle(getFile().fileName)
284+
updateActionBarTitle(getFile()?.fileName)
279285
// if (!stateWasRecovered) {
280286
initViewPager(optionalUser.get())
281287

@@ -353,8 +359,10 @@ class PreviewImageActivity :
353359
savedPosition?.let { position ->
354360

355361
previewImagePagerAdapter?.run {
356-
updateFile(position, file)
357-
notifyItemChanged(position)
362+
file?.let {
363+
updateFile(position, it)
364+
notifyItemChanged(position)
365+
}
358366
}
359367

360368
if (user.isPresent) {
@@ -378,7 +386,9 @@ class PreviewImageActivity :
378386
dismissLoadingDialog()
379387
screenState = PreviewImageActivityState.Idle
380388
file = downloadedFile
381-
startEditImageActivity(file)
389+
file?.let {
390+
startEditImageActivity(it)
391+
}
382392
}
383393

384394
override fun onResume() {

app/src/main/java/com/owncloud/android/ui/preview/PreviewMediaActivity.kt

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -384,18 +384,19 @@ class PreviewMediaActivity :
384384

385385
@Suppress("TooGenericExceptionCaught")
386386
private fun playAudio() {
387-
if (file.isDown) {
388-
prepareAudioPlayer(file.storageUri)
387+
if (file?.isDown == true) {
388+
prepareAudioPlayer(file?.storageUri)
389389
} else {
390390
try {
391-
LoadStreamUrl(this, user, clientFactory).execute(file.localId)
391+
LoadStreamUrl(this, user, clientFactory).execute(file?.localId)
392392
} catch (e: Exception) {
393393
Log_OC.e(TAG, "Loading stream url for Audio not possible: $e")
394394
}
395395
}
396396
}
397397

398-
private fun prepareAudioPlayer(uri: Uri) {
398+
private fun prepareAudioPlayer(uri: Uri?) {
399+
uri ?: return
399400
audioMediaController?.let { audioPlayer ->
400401
audioPlayer.addListener(object : Player.Listener {
401402

@@ -434,7 +435,7 @@ class PreviewMediaActivity :
434435
})
435436
val mediaItem = MediaItem.Builder()
436437
.setUri(uri)
437-
.setMediaMetadata(MediaMetadata.Builder().setTitle(file.fileName).build())
438+
.setMediaMetadata(MediaMetadata.Builder().setTitle(file?.fileName).build())
438439
.build()
439440
audioPlayer.setMediaItem(mediaItem)
440441
audioPlayer.playWhenReady = autoplay
@@ -563,16 +564,16 @@ class PreviewMediaActivity :
563564

564565
R.id.action_remove_file -> {
565566
videoPlayer?.pause()
566-
val dialog = RemoveFilesDialogFragment.newInstance(file)
567-
dialog.show(supportFragmentManager, ConfirmationDialogFragment.FTAG_CONFIRMATION)
567+
val dialog = file?.let { RemoveFilesDialogFragment.newInstance(it) }
568+
dialog?.show(supportFragmentManager, ConfirmationDialogFragment.FTAG_CONFIRMATION)
568569
}
569570

570571
R.id.action_see_details -> {
571572
seeDetails()
572573
}
573574

574575
R.id.action_sync_file -> {
575-
showSyncLoadingDialog(file.isFolder)
576+
showSyncLoadingDialog(file?.isFolder == true)
576577
fileOperationsHelper.syncFile(file)
577578
}
578579

@@ -586,7 +587,7 @@ class PreviewMediaActivity :
586587

587588
R.id.action_export_file -> {
588589
val list = ArrayList<OCFile>()
589-
list.add(file)
590+
file?.let { list.add(it) }
590591
fileOperationsHelper.exportFiles(
591592
list,
592593
this,
@@ -673,18 +674,19 @@ class PreviewMediaActivity :
673674
private fun playVideo() {
674675
setupVideoView()
675676

676-
if (file.isDown) {
677-
prepareVideoPlayer(file.storageUri)
677+
if (file?.isDown == true) {
678+
prepareVideoPlayer(file?.storageUri)
678679
} else {
679680
try {
680-
LoadStreamUrl(this, user, clientFactory).execute(file.localId)
681+
LoadStreamUrl(this, user, clientFactory).execute(file?.localId)
681682
} catch (e: Exception) {
682683
Log_OC.e(TAG, "Loading stream url for Video not possible: $e")
683684
}
684685
}
685686
}
686687

687-
private fun prepareVideoPlayer(uri: Uri) {
688+
private fun prepareVideoPlayer(uri: Uri?) {
689+
uri ?: return
688690
binding.progress.visibility = View.GONE
689691
val videoMediaItem = MediaItem.fromUri(uri)
690692
videoPlayer?.run {

0 commit comments

Comments
 (0)