Avoid rare NPE

This commit is contained in:
tzugen 2023-07-06 18:05:16 +02:00
parent 96ac6fcac7
commit ecee57e166
No known key found for this signature in database
GPG Key ID: 61E9C34BC10EC930
3 changed files with 24 additions and 13 deletions

View File

@ -306,7 +306,7 @@ class NavigationActivity : AppCompatActivity() {
Storage.reset() Storage.reset()
lifecycleScope.launch(Dispatchers.IO) { lifecycleScope.launch(Dispatchers.IO) {
Storage.ensureRootIsAvailable() Storage.checkForErrorsWithCustomRoot()
} }
setMenuForServerCapabilities() setMenuForServerCapabilities()

View File

@ -344,7 +344,7 @@ class SettingsFragment :
// Clear download queue. // Clear download queue.
mediaPlayerManager.clear() mediaPlayerManager.clear()
Storage.reset() Storage.reset()
Storage.ensureRootIsAvailable() Storage.checkForErrorsWithCustomRoot()
} }
private fun setDebugLogToFile(writeLog: Boolean) { private fun setDebugLogToFile(writeLog: Boolean) {

View File

@ -22,19 +22,23 @@ import timber.log.Timber
object Storage { object Storage {
val mediaRoot: ResettableLazy<AbstractFile> = ResettableLazy { val mediaRoot: ResettableLazy<AbstractFile> = ResettableLazy {
getRoot()!! val ret = getRoot()
rootNotFoundError = ret.second
ret.first
} }
var rootNotFoundError: Boolean = false
fun reset() { fun reset() {
StorageFile.storageFilePathDictionary.clear() StorageFile.storageFilePathDictionary.clear()
StorageFile.notExistingPathDictionary.clear() StorageFile.notExistingPathDictionary.clear()
mediaRoot.reset() mediaRoot.reset()
rootNotFoundError = false
Timber.i("StorageFile caches were reset") Timber.i("StorageFile caches were reset")
} }
fun ensureRootIsAvailable() { fun checkForErrorsWithCustomRoot() {
val root = getRoot() if (rootNotFoundError) {
if (root == null) {
Settings.customCacheLocation = false Settings.customCacheLocation = false
Settings.cacheLocationUri = "" Settings.cacheLocationUri = ""
Util.toast(UApp.applicationContext(), R.string.settings_cache_location_error) Util.toast(UApp.applicationContext(), R.string.settings_cache_location_error)
@ -98,18 +102,25 @@ object Storage {
return success return success
} }
private fun getRoot(): AbstractFile? { private fun getRoot(): Pair<AbstractFile, Boolean> {
return if (Settings.customCacheLocation) { return if (Settings.customCacheLocation) {
if (Settings.cacheLocationUri.isBlank()) return null if (Settings.cacheLocationUri.isBlank()) return Pair(getDefaultRoot(), true)
val documentFile = DocumentFile.fromTreeUri( val documentFile = DocumentFile.fromTreeUri(
UApp.applicationContext(), UApp.applicationContext(),
Uri.parse(Settings.cacheLocationUri) Uri.parse(Settings.cacheLocationUri)
) ?: return null ) ?: return Pair(getDefaultRoot(), true)
if (!documentFile.exists()) return null if (!documentFile.exists()) return Pair(getDefaultRoot(), true)
StorageFile(null, documentFile.uri, documentFile.name!!, documentFile.isDirectory) Pair(
StorageFile(null, documentFile.uri, documentFile.name!!, documentFile.isDirectory),
false
)
} else { } else {
val file = File(FileUtil.defaultMusicDirectory.path) Pair(getDefaultRoot(), false)
JavaFile(null, file)
} }
} }
private fun getDefaultRoot(): JavaFile {
val file = File(FileUtil.defaultMusicDirectory.path)
return JavaFile(null, file)
}
} }