2019-03-03 09:20:15 +00:00
|
|
|
/*
|
2020-10-06 08:46:04 +00:00
|
|
|
* Copyright (c) 2020 Hemanth Savarla.
|
2019-03-03 09:20:15 +00:00
|
|
|
*
|
|
|
|
* Licensed under the GNU General Public License v3
|
|
|
|
*
|
2020-10-06 08:46:04 +00:00
|
|
|
* This is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
|
2019-03-03 09:20:15 +00:00
|
|
|
*
|
|
|
|
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
2020-10-06 08:46:04 +00:00
|
|
|
*
|
2019-03-03 09:20:15 +00:00
|
|
|
*/
|
2018-11-30 01:06:16 +00:00
|
|
|
package code.name.monkey.retromusic.helper
|
|
|
|
|
|
|
|
import android.app.SearchManager
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.provider.MediaStore
|
|
|
|
import code.name.monkey.retromusic.model.Song
|
2020-08-13 17:08:37 +00:00
|
|
|
import code.name.monkey.retromusic.repository.RealSongRepository
|
2021-09-20 06:29:25 +00:00
|
|
|
import org.koin.core.component.KoinComponent
|
|
|
|
import org.koin.core.component.inject
|
2021-09-08 18:30:20 +00:00
|
|
|
import java.util.*
|
2018-11-30 01:06:16 +00:00
|
|
|
|
2020-08-13 17:08:37 +00:00
|
|
|
object SearchQueryHelper : KoinComponent {
|
2019-03-04 03:55:09 +00:00
|
|
|
private const val TITLE_SELECTION = "lower(" + MediaStore.Audio.AudioColumns.TITLE + ") = ?"
|
|
|
|
private const val ALBUM_SELECTION = "lower(" + MediaStore.Audio.AudioColumns.ALBUM + ") = ?"
|
|
|
|
private const val ARTIST_SELECTION = "lower(" + MediaStore.Audio.AudioColumns.ARTIST + ") = ?"
|
|
|
|
private const val AND = " AND "
|
2020-08-13 17:08:37 +00:00
|
|
|
private val songRepository by inject<RealSongRepository>()
|
2018-11-30 01:06:16 +00:00
|
|
|
var songs = ArrayList<Song>()
|
2020-05-23 13:53:10 +00:00
|
|
|
|
2020-02-19 07:08:12 +00:00
|
|
|
@JvmStatic
|
2020-08-21 18:01:52 +00:00
|
|
|
fun getSongs(extras: Bundle): List<Song> {
|
2018-11-30 01:06:16 +00:00
|
|
|
val query = extras.getString(SearchManager.QUERY, null)
|
|
|
|
val artistName = extras.getString(MediaStore.EXTRA_MEDIA_ARTIST, null)
|
|
|
|
val albumName = extras.getString(MediaStore.EXTRA_MEDIA_ALBUM, null)
|
|
|
|
val titleName = extras.getString(MediaStore.EXTRA_MEDIA_TITLE, null)
|
|
|
|
|
2020-08-13 17:08:37 +00:00
|
|
|
var songs = listOf<Song>()
|
2018-11-30 01:06:16 +00:00
|
|
|
if (artistName != null && albumName != null && titleName != null) {
|
2020-08-13 17:08:37 +00:00
|
|
|
songs = songRepository.songs(
|
|
|
|
songRepository.makeSongCursor(
|
2020-02-19 07:08:12 +00:00
|
|
|
ARTIST_SELECTION + AND + ALBUM_SELECTION + AND + TITLE_SELECTION,
|
2020-02-26 16:49:24 +00:00
|
|
|
arrayOf(
|
2021-09-08 18:30:20 +00:00
|
|
|
artistName.lowercase(),
|
|
|
|
albumName.lowercase(),
|
|
|
|
titleName.lowercase()
|
2020-02-26 16:49:24 +00:00
|
|
|
)
|
2020-02-19 07:08:12 +00:00
|
|
|
)
|
|
|
|
)
|
2018-11-30 01:06:16 +00:00
|
|
|
}
|
2019-08-02 08:59:40 +00:00
|
|
|
if (songs.isNotEmpty()) {
|
2018-11-30 01:06:16 +00:00
|
|
|
return songs
|
|
|
|
}
|
|
|
|
if (artistName != null && titleName != null) {
|
2020-08-13 17:08:37 +00:00
|
|
|
songs = songRepository.songs(
|
|
|
|
songRepository.makeSongCursor(
|
2020-02-19 07:08:12 +00:00
|
|
|
ARTIST_SELECTION + AND + TITLE_SELECTION,
|
2020-08-21 18:01:52 +00:00
|
|
|
arrayOf(
|
2021-09-08 18:30:20 +00:00
|
|
|
artistName.lowercase(),
|
|
|
|
titleName.lowercase()
|
2020-08-21 18:01:52 +00:00
|
|
|
)
|
2020-02-19 07:08:12 +00:00
|
|
|
)
|
|
|
|
)
|
2018-11-30 01:06:16 +00:00
|
|
|
}
|
2019-08-02 08:59:40 +00:00
|
|
|
if (songs.isNotEmpty()) {
|
2018-11-30 01:06:16 +00:00
|
|
|
return songs
|
|
|
|
}
|
|
|
|
if (albumName != null && titleName != null) {
|
2020-08-13 17:08:37 +00:00
|
|
|
songs = songRepository.songs(
|
|
|
|
songRepository.makeSongCursor(
|
2020-02-19 07:08:12 +00:00
|
|
|
ALBUM_SELECTION + AND + TITLE_SELECTION,
|
2020-08-21 18:01:52 +00:00
|
|
|
arrayOf(
|
2021-09-08 18:30:20 +00:00
|
|
|
albumName.lowercase(),
|
|
|
|
titleName.lowercase()
|
2020-08-21 18:01:52 +00:00
|
|
|
)
|
2020-02-19 07:08:12 +00:00
|
|
|
)
|
|
|
|
)
|
2018-11-30 01:06:16 +00:00
|
|
|
}
|
2019-08-02 08:59:40 +00:00
|
|
|
if (songs.isNotEmpty()) {
|
2018-11-30 01:06:16 +00:00
|
|
|
return songs
|
|
|
|
}
|
|
|
|
if (artistName != null) {
|
2020-08-13 17:08:37 +00:00
|
|
|
songs = songRepository.songs(
|
|
|
|
songRepository.makeSongCursor(
|
2020-02-19 07:08:12 +00:00
|
|
|
ARTIST_SELECTION,
|
2021-09-08 18:30:20 +00:00
|
|
|
arrayOf(artistName.lowercase())
|
2020-02-19 07:08:12 +00:00
|
|
|
)
|
|
|
|
)
|
2018-11-30 01:06:16 +00:00
|
|
|
}
|
2019-08-02 08:59:40 +00:00
|
|
|
if (songs.isNotEmpty()) {
|
2018-11-30 01:06:16 +00:00
|
|
|
return songs
|
|
|
|
}
|
|
|
|
if (albumName != null) {
|
2020-08-13 17:08:37 +00:00
|
|
|
songs = songRepository.songs(
|
|
|
|
songRepository.makeSongCursor(
|
2020-02-19 07:08:12 +00:00
|
|
|
ALBUM_SELECTION,
|
2021-09-08 18:30:20 +00:00
|
|
|
arrayOf(albumName.lowercase())
|
2020-02-19 07:08:12 +00:00
|
|
|
)
|
|
|
|
)
|
2018-11-30 01:06:16 +00:00
|
|
|
}
|
2019-08-02 08:59:40 +00:00
|
|
|
if (songs.isNotEmpty()) {
|
2018-11-30 01:06:16 +00:00
|
|
|
return songs
|
|
|
|
}
|
|
|
|
if (titleName != null) {
|
2020-08-13 17:08:37 +00:00
|
|
|
songs = songRepository.songs(
|
|
|
|
songRepository.makeSongCursor(
|
2020-02-19 07:08:12 +00:00
|
|
|
TITLE_SELECTION,
|
2021-09-08 18:30:20 +00:00
|
|
|
arrayOf(titleName.lowercase())
|
2020-02-19 07:08:12 +00:00
|
|
|
)
|
|
|
|
)
|
2018-11-30 01:06:16 +00:00
|
|
|
}
|
2019-08-02 08:59:40 +00:00
|
|
|
if (songs.isNotEmpty()) {
|
2018-11-30 01:06:16 +00:00
|
|
|
return songs
|
|
|
|
}
|
2020-08-13 17:08:37 +00:00
|
|
|
songs = songRepository.songs(
|
|
|
|
songRepository.makeSongCursor(
|
|
|
|
ARTIST_SELECTION,
|
2021-09-08 18:30:20 +00:00
|
|
|
arrayOf(query.lowercase())
|
2020-02-26 16:49:24 +00:00
|
|
|
)
|
2020-08-13 17:08:37 +00:00
|
|
|
)
|
2018-11-30 01:06:16 +00:00
|
|
|
|
2019-08-02 08:59:40 +00:00
|
|
|
if (songs.isNotEmpty()) {
|
2018-11-30 01:06:16 +00:00
|
|
|
return songs
|
|
|
|
}
|
2020-08-13 17:08:37 +00:00
|
|
|
songs = songRepository.songs(
|
|
|
|
songRepository.makeSongCursor(
|
2020-02-26 16:49:24 +00:00
|
|
|
ALBUM_SELECTION,
|
2021-09-08 18:30:20 +00:00
|
|
|
arrayOf(query.lowercase())
|
2020-02-26 16:49:24 +00:00
|
|
|
)
|
|
|
|
)
|
2019-08-02 08:59:40 +00:00
|
|
|
if (songs.isNotEmpty()) {
|
2018-11-30 01:06:16 +00:00
|
|
|
return songs
|
|
|
|
}
|
2020-08-13 17:08:37 +00:00
|
|
|
songs = songRepository.songs(
|
|
|
|
songRepository.makeSongCursor(
|
2020-02-26 16:49:24 +00:00
|
|
|
TITLE_SELECTION,
|
2021-09-08 18:30:20 +00:00
|
|
|
arrayOf(query.lowercase())
|
2020-02-26 16:49:24 +00:00
|
|
|
)
|
|
|
|
)
|
2019-08-02 08:59:40 +00:00
|
|
|
return if (songs.isNotEmpty()) {
|
2018-11-30 01:06:16 +00:00
|
|
|
songs
|
|
|
|
} else ArrayList()
|
|
|
|
}
|
|
|
|
}
|