-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
app: local: scanners: Move scanner code to a class
- Loading branch information
1 parent
eb2f4b9
commit 59b6b95
Showing
9 changed files
with
1,081 additions
and
1,008 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
app/src/main/java/com/dd3boh/outertune/models/DirectoryTree.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
package com.dd3boh.outertune.models | ||
|
||
import com.dd3boh.outertune.db.entities.Song | ||
import com.dd3boh.outertune.ui.utils.SCANNER_DEBUG | ||
import com.dd3boh.outertune.ui.utils.sdcardRoot | ||
import timber.log.Timber | ||
|
||
/** | ||
* A tree representation of local audio files | ||
* | ||
* @param path root directory start | ||
*/ | ||
class DirectoryTree(path: String) { | ||
companion object { | ||
const val TAG = "DirectoryTree" | ||
var directoryUID = 0 | ||
} | ||
|
||
/** | ||
* Directory name | ||
*/ | ||
var currentDir = path // file name | ||
|
||
/** | ||
* Full parent directory path | ||
*/ | ||
var parent: String = "" | ||
|
||
// folder contents | ||
var subdirs = ArrayList<DirectoryTree>() | ||
var files = ArrayList<Song>() | ||
|
||
val uid = directoryUID | ||
|
||
init { | ||
// increment uid | ||
directoryUID++ | ||
} | ||
|
||
/** | ||
* Instantiate a directory tree directly | ||
*/ | ||
constructor(path: String, files: ArrayList<Song>) : this(path) { | ||
this.files = files | ||
} | ||
|
||
fun insert(path: String, song: Song) { | ||
// println("curr path =" + path) | ||
|
||
// add a file | ||
if (path.indexOf('/') == -1) { | ||
files.add(song) | ||
if (SCANNER_DEBUG) | ||
Timber.tag(TAG).d("Adding song with path: $path") | ||
return | ||
} | ||
|
||
// there is still subdirs to process | ||
var tmpPath = path | ||
if (path[path.length - 1] == '/') { | ||
tmpPath = path.substring(0, path.length - 1) | ||
} | ||
|
||
// the first directory before the . | ||
val subdirPath = tmpPath.substringBefore('/') | ||
|
||
// create subdirs if they do not exist, then insert | ||
var existingSubdir: DirectoryTree? = null | ||
subdirs.forEach { subdir -> | ||
if (subdir.currentDir == subdirPath) { | ||
existingSubdir = subdir | ||
return@forEach | ||
} | ||
} | ||
|
||
if (existingSubdir == null) { | ||
val tree = DirectoryTree(subdirPath) | ||
tree.parent = "$parent/$currentDir" | ||
tree.insert(tmpPath.substringAfter('/'), song) | ||
subdirs.add(tree) | ||
|
||
} else { | ||
existingSubdir!!.insert(tmpPath.substringAfter('/'), song) | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Get the name of the file from full path, without any extensions | ||
*/ | ||
private fun getFileName(path: String?): String? { | ||
if (path == null) { | ||
return null | ||
} | ||
return path.substringAfterLast('/').substringBefore('.') | ||
} | ||
|
||
/** | ||
* Retrieves song object at path | ||
* | ||
* @return song at path, or null if it does not exist | ||
*/ | ||
fun getSong(path: String): Song? { | ||
Timber.tag(TAG).d("Searching for song, at path: $path") | ||
|
||
// search for song in current dir | ||
if (path.indexOf('/') == -1) { | ||
val foundSong: Song = files.first { getFileName(it.song.localPath) == getFileName(path) } | ||
Timber.tag(TAG).d("Searching for song, found?: ${foundSong.id} Name: ${foundSong.song.title}") | ||
return foundSong | ||
} | ||
|
||
// there is still subdirs to process | ||
var tmpPath = path | ||
if (path[path.length - 1] == '/') { | ||
tmpPath = path.substring(0, path.length - 1) | ||
} | ||
|
||
// the first directory before the . | ||
val subdirPath = tmpPath.substringBefore('/') | ||
|
||
// scan for matching subdirectory | ||
var existingSubdir: DirectoryTree? = null | ||
subdirs.forEach { subdir -> | ||
if (subdir.currentDir == subdirPath) { | ||
existingSubdir = subdir | ||
return@forEach | ||
} | ||
} | ||
|
||
// explore the subdirectory if it exists in | ||
if (existingSubdir == null) { | ||
return null | ||
} else { | ||
return existingSubdir!!.getSong(tmpPath.substringAfter('/')) | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Retrieve a list of all the songs | ||
*/ | ||
fun toList(): List<Song> { | ||
val songs = ArrayList<Song>() | ||
|
||
fun traverseTree(tree: DirectoryTree, result: ArrayList<Song>) { | ||
result.addAll(tree.files) | ||
tree.subdirs.forEach { traverseTree(it, result) } | ||
} | ||
|
||
traverseTree(this, songs) | ||
return songs | ||
} | ||
|
||
/** | ||
* Retrieves a modified version of this DirectoryTree. | ||
* All folders are recognized to be top level folders | ||
*/ | ||
fun toFlattenedTree(): DirectoryTree { | ||
val result = DirectoryTree(sdcardRoot) | ||
getSubdirsRecursive(this, result.subdirs) | ||
return result | ||
} | ||
|
||
/** | ||
* Crawl the directory tree, add the subdirectories with songs to the list | ||
* @param it | ||
* @param result | ||
*/ | ||
private fun getSubdirsRecursive(it: DirectoryTree, result: ArrayList<DirectoryTree>) { | ||
if (it.files.size > 0) { | ||
result.add(DirectoryTree(it.currentDir, it.files)) | ||
} | ||
|
||
if (it.subdirs.size > 0) { | ||
it.subdirs.forEach { getSubdirsRecursive(it, result) } | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
app/src/main/java/com/dd3boh/outertune/models/SongTempData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.dd3boh.outertune.models | ||
|
||
import com.dd3boh.outertune.db.entities.FormatEntity | ||
|
||
/** | ||
* For passing along song metadata | ||
*/ | ||
data class SongTempData( | ||
val id: String, val path: String, val title: String, val duration: Int, val artist: String?, | ||
val artistID: String?, val album: String?, val albumID: String?, val formatEntity: FormatEntity, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.