mirror of
https://gitlab.com/ultrasonic/ultrasonic.git
synced 2025-04-14 00:17:15 +03:00
Merge branch 'root' into 'develop'
Avoid rare NPE See merge request ultrasonic/ultrasonic!1073
This commit is contained in:
commit
7209779b64
@ -306,7 +306,7 @@ class NavigationActivity : AppCompatActivity() {
|
||||
Storage.reset()
|
||||
|
||||
lifecycleScope.launch(Dispatchers.IO) {
|
||||
Storage.ensureRootIsAvailable()
|
||||
Storage.checkForErrorsWithCustomRoot()
|
||||
}
|
||||
|
||||
setMenuForServerCapabilities()
|
||||
|
@ -344,7 +344,7 @@ class SettingsFragment :
|
||||
// Clear download queue.
|
||||
mediaPlayerManager.clear()
|
||||
Storage.reset()
|
||||
Storage.ensureRootIsAvailable()
|
||||
Storage.checkForErrorsWithCustomRoot()
|
||||
}
|
||||
|
||||
private fun setDebugLogToFile(writeLog: Boolean) {
|
||||
|
@ -22,19 +22,23 @@ import timber.log.Timber
|
||||
object Storage {
|
||||
|
||||
val mediaRoot: ResettableLazy<AbstractFile> = ResettableLazy {
|
||||
getRoot()!!
|
||||
val ret = getRoot()
|
||||
rootNotFoundError = ret.second
|
||||
ret.first
|
||||
}
|
||||
|
||||
var rootNotFoundError: Boolean = false
|
||||
|
||||
fun reset() {
|
||||
StorageFile.storageFilePathDictionary.clear()
|
||||
StorageFile.notExistingPathDictionary.clear()
|
||||
mediaRoot.reset()
|
||||
rootNotFoundError = false
|
||||
Timber.i("StorageFile caches were reset")
|
||||
}
|
||||
|
||||
fun ensureRootIsAvailable() {
|
||||
val root = getRoot()
|
||||
if (root == null) {
|
||||
fun checkForErrorsWithCustomRoot() {
|
||||
if (rootNotFoundError) {
|
||||
Settings.customCacheLocation = false
|
||||
Settings.cacheLocationUri = ""
|
||||
Util.toast(UApp.applicationContext(), R.string.settings_cache_location_error)
|
||||
@ -98,18 +102,25 @@ object Storage {
|
||||
return success
|
||||
}
|
||||
|
||||
private fun getRoot(): AbstractFile? {
|
||||
private fun getRoot(): Pair<AbstractFile, Boolean> {
|
||||
return if (Settings.customCacheLocation) {
|
||||
if (Settings.cacheLocationUri.isBlank()) return null
|
||||
if (Settings.cacheLocationUri.isBlank()) return Pair(getDefaultRoot(), true)
|
||||
val documentFile = DocumentFile.fromTreeUri(
|
||||
UApp.applicationContext(),
|
||||
Uri.parse(Settings.cacheLocationUri)
|
||||
) ?: return null
|
||||
if (!documentFile.exists()) return null
|
||||
StorageFile(null, documentFile.uri, documentFile.name!!, documentFile.isDirectory)
|
||||
) ?: return Pair(getDefaultRoot(), true)
|
||||
if (!documentFile.exists()) return Pair(getDefaultRoot(), true)
|
||||
Pair(
|
||||
StorageFile(null, documentFile.uri, documentFile.name!!, documentFile.isDirectory),
|
||||
false
|
||||
)
|
||||
} else {
|
||||
val file = File(FileUtil.defaultMusicDirectory.path)
|
||||
JavaFile(null, file)
|
||||
Pair(getDefaultRoot(), false)
|
||||
}
|
||||
}
|
||||
|
||||
private fun getDefaultRoot(): JavaFile {
|
||||
val file = File(FileUtil.defaultMusicDirectory.path)
|
||||
return JavaFile(null, file)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user