2020-10-06 08:46:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Hemanth Savarla.
|
|
|
|
*
|
|
|
|
* Licensed under the GNU General Public License v3
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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-05-23 09:27:55 +00:00
|
|
|
package code.name.monkey.retromusic.activities
|
|
|
|
|
2020-08-11 18:29:44 +00:00
|
|
|
import android.content.Intent
|
|
|
|
import android.content.SharedPreferences
|
2020-08-13 08:24:36 +00:00
|
|
|
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
|
2020-09-05 20:13:45 +00:00
|
|
|
import android.net.Uri
|
2020-05-23 09:27:55 +00:00
|
|
|
import android.os.Bundle
|
|
|
|
import android.provider.MediaStore
|
2020-08-13 17:08:37 +00:00
|
|
|
import androidx.lifecycle.lifecycleScope
|
2021-10-11 11:25:17 +00:00
|
|
|
import androidx.navigation.contains
|
2021-09-08 18:30:20 +00:00
|
|
|
import androidx.navigation.ui.setupWithNavController
|
|
|
|
import code.name.monkey.retromusic.*
|
|
|
|
import code.name.monkey.retromusic.activities.base.AbsCastActivity
|
|
|
|
import code.name.monkey.retromusic.databinding.SlidingMusicPanelLayoutBinding
|
2021-10-13 11:23:53 +00:00
|
|
|
import code.name.monkey.retromusic.extensions.currentFragment
|
2020-12-05 05:40:35 +00:00
|
|
|
import code.name.monkey.retromusic.extensions.extra
|
2020-08-11 18:29:44 +00:00
|
|
|
import code.name.monkey.retromusic.extensions.findNavController
|
2021-09-08 18:30:20 +00:00
|
|
|
import code.name.monkey.retromusic.fragments.base.AbsRecyclerViewFragment
|
|
|
|
import code.name.monkey.retromusic.fragments.home.HomeFragment
|
2020-08-29 18:53:15 +00:00
|
|
|
import code.name.monkey.retromusic.helper.MusicPlayerRemote
|
2020-05-23 09:27:55 +00:00
|
|
|
import code.name.monkey.retromusic.helper.SearchQueryHelper.getSongs
|
2020-09-25 17:38:59 +00:00
|
|
|
import code.name.monkey.retromusic.model.CategoryInfo
|
2020-05-23 09:27:55 +00:00
|
|
|
import code.name.monkey.retromusic.model.Song
|
2020-08-29 18:53:15 +00:00
|
|
|
import code.name.monkey.retromusic.repository.PlaylistSongsLoader
|
2020-05-23 09:27:55 +00:00
|
|
|
import code.name.monkey.retromusic.service.MusicService
|
2020-09-12 19:39:46 +00:00
|
|
|
import code.name.monkey.retromusic.util.AppRater
|
2020-06-06 18:57:28 +00:00
|
|
|
import code.name.monkey.retromusic.util.PreferenceUtil
|
2020-08-29 18:53:15 +00:00
|
|
|
import kotlinx.coroutines.Dispatchers.IO
|
2020-08-13 17:08:37 +00:00
|
|
|
import kotlinx.coroutines.launch
|
2020-09-26 20:39:07 +00:00
|
|
|
import org.koin.android.ext.android.get
|
2020-05-23 09:27:55 +00:00
|
|
|
|
2021-09-08 18:30:20 +00:00
|
|
|
class MainActivity : AbsCastActivity(), OnSharedPreferenceChangeListener {
|
2020-05-23 09:27:55 +00:00
|
|
|
companion object {
|
|
|
|
const val TAG = "MainActivity"
|
|
|
|
const val EXPAND_PANEL = "expand_panel"
|
|
|
|
}
|
|
|
|
|
2021-09-08 18:30:20 +00:00
|
|
|
override fun createContentView(): SlidingMusicPanelLayoutBinding {
|
2020-10-02 18:05:09 +00:00
|
|
|
return wrapSlidingMusicPanel()
|
2020-05-23 09:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
setDrawUnderStatusBar()
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
setTaskDescriptionColorAuto()
|
|
|
|
hideStatusBar()
|
2020-05-23 23:27:19 +00:00
|
|
|
updateTabs()
|
2020-12-05 05:40:35 +00:00
|
|
|
AppRater.appLaunched(this)
|
2020-09-25 17:38:59 +00:00
|
|
|
|
2020-09-27 18:59:09 +00:00
|
|
|
setupNavigationController()
|
|
|
|
if (!hasPermissions()) {
|
|
|
|
findNavController(R.id.fragment_container).navigate(R.id.permissionFragment)
|
|
|
|
}
|
2020-09-25 17:38:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun setupNavigationController() {
|
|
|
|
val navController = findNavController(R.id.fragment_container)
|
|
|
|
val navInflater = navController.navInflater
|
|
|
|
val navGraph = navInflater.inflate(R.navigation.main_graph)
|
|
|
|
|
|
|
|
val categoryInfo: CategoryInfo = PreferenceUtil.libraryCategory.first { it.visible }
|
|
|
|
if (categoryInfo.visible) {
|
2021-10-11 11:25:17 +00:00
|
|
|
if (!navGraph.contains(PreferenceUtil.lastTab)) PreferenceUtil.lastTab =
|
|
|
|
categoryInfo.category.id
|
2021-09-08 18:30:20 +00:00
|
|
|
navGraph.setStartDestination(
|
|
|
|
if (PreferenceUtil.rememberLastTab) {
|
|
|
|
PreferenceUtil.lastTab.let {
|
|
|
|
if (it == 0) {
|
|
|
|
categoryInfo.category.id
|
|
|
|
} else {
|
|
|
|
it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else categoryInfo.category.id
|
|
|
|
)
|
2020-09-25 17:38:59 +00:00
|
|
|
}
|
|
|
|
navController.graph = navGraph
|
2021-11-11 14:51:57 +00:00
|
|
|
bottomNavigationView.setupWithNavController(navController)
|
2021-09-08 18:30:20 +00:00
|
|
|
// Scroll Fragment to top
|
2021-11-11 14:51:57 +00:00
|
|
|
bottomNavigationView.setOnItemReselectedListener {
|
2021-10-13 11:23:53 +00:00
|
|
|
currentFragment(R.id.fragment_container)
|
2021-09-08 18:30:20 +00:00
|
|
|
.also {
|
|
|
|
if (it is AbsRecyclerViewFragment<*, *>) {
|
|
|
|
it.scrollToTop()
|
|
|
|
}
|
|
|
|
if (it is HomeFragment) {
|
|
|
|
it.scrollToTop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-17 15:48:52 +00:00
|
|
|
// This is more like a work-around as for start destination of navGraph
|
|
|
|
// enterTransition won't work as expected
|
|
|
|
navGraph.setStartDestination(R.id.libraryFragment)
|
2021-09-08 18:30:20 +00:00
|
|
|
navController.addOnDestinationChangedListener { _, destination, _ ->
|
2021-11-17 15:48:52 +00:00
|
|
|
when (destination.id) {
|
2021-09-08 18:30:20 +00:00
|
|
|
R.id.action_home, R.id.action_song, R.id.action_album, R.id.action_artist, R.id.action_folder, R.id.action_playlist, R.id.action_genre -> {
|
|
|
|
// Save the last tab
|
|
|
|
if (PreferenceUtil.rememberLastTab) {
|
|
|
|
saveTab(destination.id)
|
|
|
|
}
|
|
|
|
// Show Bottom Navigation Bar
|
2021-10-25 12:38:10 +00:00
|
|
|
setBottomNavVisibility(visible = true, animate = true)
|
2021-09-08 18:30:20 +00:00
|
|
|
}
|
2021-09-24 04:37:52 +00:00
|
|
|
R.id.playing_queue_fragment -> {
|
2021-10-25 12:38:10 +00:00
|
|
|
setBottomNavVisibility(visible = false)
|
|
|
|
hideBottomSheet(true)
|
2021-09-24 04:37:52 +00:00
|
|
|
}
|
2021-10-25 12:38:10 +00:00
|
|
|
else -> setBottomNavVisibility(visible = false, animate = true) // Hide Bottom Navigation Bar
|
2021-09-08 18:30:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun saveTab(id: Int) {
|
|
|
|
PreferenceUtil.lastTab = id
|
2020-05-23 09:27:55 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 18:29:44 +00:00
|
|
|
override fun onSupportNavigateUp(): Boolean =
|
|
|
|
findNavController(R.id.fragment_container).navigateUp()
|
|
|
|
|
2020-05-23 09:27:55 +00:00
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
2020-06-06 18:57:28 +00:00
|
|
|
PreferenceUtil.registerOnSharedPreferenceChangedListener(this)
|
2020-12-05 05:40:35 +00:00
|
|
|
val expand = extra<Boolean>(EXPAND_PANEL).value ?: false
|
|
|
|
if (expand && PreferenceUtil.isExpandPanel) {
|
2021-10-25 12:38:10 +00:00
|
|
|
setBottomNavVisibility(false)
|
2021-09-08 18:30:20 +00:00
|
|
|
fromNotification = true
|
2020-10-11 17:15:27 +00:00
|
|
|
expandPanel()
|
2020-05-23 09:27:55 +00:00
|
|
|
intent.removeExtra(EXPAND_PANEL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-05 05:40:35 +00:00
|
|
|
override fun onDestroy() {
|
|
|
|
super.onDestroy()
|
|
|
|
PreferenceUtil.unregisterOnSharedPreferenceChangedListener(this)
|
|
|
|
}
|
|
|
|
|
2020-05-23 09:27:55 +00:00
|
|
|
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
|
2021-10-30 04:29:52 +00:00
|
|
|
if (key == GENERAL_THEME || key == MATERIAL_YOU || key == BLACK_THEME || key == ADAPTIVE_COLOR_APP || key == USER_NAME || key == TOGGLE_FULL_SCREEN || key == TOGGLE_VOLUME || key == ROUND_CORNERS || key == CAROUSEL_EFFECT || key == NOW_PLAYING_SCREEN_ID || key == TOGGLE_GENRE || key == BANNER_IMAGE_PATH || key == PROFILE_IMAGE_PATH || key == CIRCULAR_ALBUM_ART || key == KEEP_SCREEN_ON || key == TOGGLE_SEPARATE_LINE || key == TOGGLE_HOME_BANNER || key == TOGGLE_ADD_CONTROLS || key == ALBUM_COVER_STYLE || key == HOME_ARTIST_GRID_STYLE || key == ALBUM_COVER_TRANSFORM || key == DESATURATED_COLOR || key == EXTRA_SONG_INFO || key == TAB_TEXT_MODE || key == LANGUAGE_NAME || key == LIBRARY_CATEGORIES) {
|
2020-05-23 09:27:55 +00:00
|
|
|
postRecreate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onServiceConnected() {
|
|
|
|
super.onServiceConnected()
|
2020-12-05 05:40:35 +00:00
|
|
|
intent ?: return
|
2020-08-29 18:53:15 +00:00
|
|
|
handlePlaybackIntent(intent)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun handlePlaybackIntent(intent: Intent) {
|
|
|
|
lifecycleScope.launch(IO) {
|
2020-09-05 20:13:45 +00:00
|
|
|
val uri: Uri? = intent.data
|
|
|
|
val mimeType: String? = intent.type
|
2020-08-29 18:53:15 +00:00
|
|
|
var handled = false
|
|
|
|
if (intent.action != null &&
|
|
|
|
intent.action == MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH
|
|
|
|
) {
|
|
|
|
val songs: List<Song> = getSongs(intent.extras!!)
|
|
|
|
if (MusicPlayerRemote.shuffleMode == MusicService.SHUFFLE_MODE_SHUFFLE) {
|
|
|
|
MusicPlayerRemote.openAndShuffleQueue(songs, true)
|
|
|
|
} else {
|
|
|
|
MusicPlayerRemote.openQueue(songs, 0, true)
|
|
|
|
}
|
2020-05-23 09:27:55 +00:00
|
|
|
handled = true
|
|
|
|
}
|
2020-08-29 18:53:15 +00:00
|
|
|
if (uri != null && uri.toString().isNotEmpty()) {
|
|
|
|
MusicPlayerRemote.playFromUri(uri)
|
|
|
|
handled = true
|
|
|
|
} else if (MediaStore.Audio.Playlists.CONTENT_TYPE == mimeType) {
|
2020-09-17 21:25:41 +00:00
|
|
|
val id = parseLongFromIntent(intent, "playlistId", "playlist")
|
|
|
|
if (id >= 0L) {
|
2020-09-05 20:13:45 +00:00
|
|
|
val position: Int = intent.getIntExtra("position", 0)
|
2020-09-26 20:39:07 +00:00
|
|
|
val songs: List<Song> = PlaylistSongsLoader.getPlaylistSongList(get(), id)
|
2020-08-29 18:53:15 +00:00
|
|
|
MusicPlayerRemote.openQueue(songs, position, true)
|
2020-08-13 17:08:37 +00:00
|
|
|
handled = true
|
|
|
|
}
|
2020-08-29 18:53:15 +00:00
|
|
|
} else if (MediaStore.Audio.Albums.CONTENT_TYPE == mimeType) {
|
2020-09-17 21:25:41 +00:00
|
|
|
val id = parseLongFromIntent(intent, "albumId", "album")
|
|
|
|
if (id >= 0L) {
|
2020-09-05 20:13:45 +00:00
|
|
|
val position: Int = intent.getIntExtra("position", 0)
|
2020-09-26 20:39:07 +00:00
|
|
|
val songs = libraryViewModel.albumById(id).songs
|
2020-08-29 18:53:15 +00:00
|
|
|
MusicPlayerRemote.openQueue(
|
2020-09-26 20:39:07 +00:00
|
|
|
songs,
|
2020-08-29 18:53:15 +00:00
|
|
|
position,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
handled = true
|
|
|
|
}
|
|
|
|
} else if (MediaStore.Audio.Artists.CONTENT_TYPE == mimeType) {
|
2020-09-17 21:25:41 +00:00
|
|
|
val id = parseLongFromIntent(intent, "artistId", "artist")
|
|
|
|
if (id >= 0L) {
|
2020-09-05 20:13:45 +00:00
|
|
|
val position: Int = intent.getIntExtra("position", 0)
|
2020-09-26 20:39:07 +00:00
|
|
|
val songs: List<Song> = libraryViewModel.artistById(id).songs
|
2020-08-29 18:53:15 +00:00
|
|
|
MusicPlayerRemote.openQueue(
|
2020-09-26 20:39:07 +00:00
|
|
|
songs,
|
2020-08-29 18:53:15 +00:00
|
|
|
position,
|
|
|
|
true
|
|
|
|
)
|
2020-08-13 17:08:37 +00:00
|
|
|
handled = true
|
|
|
|
}
|
2020-05-23 09:27:55 +00:00
|
|
|
}
|
2020-08-29 18:53:15 +00:00
|
|
|
if (handled) {
|
|
|
|
setIntent(Intent())
|
|
|
|
}
|
2020-05-23 09:27:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-17 21:25:41 +00:00
|
|
|
private fun parseLongFromIntent(
|
2020-10-06 08:46:04 +00:00
|
|
|
intent: Intent,
|
|
|
|
longKey: String,
|
2020-05-23 09:27:55 +00:00
|
|
|
stringKey: String
|
|
|
|
): Long {
|
|
|
|
var id = intent.getLongExtra(longKey, -1)
|
|
|
|
if (id < 0) {
|
|
|
|
val idString = intent.getStringExtra(stringKey)
|
|
|
|
if (idString != null) {
|
|
|
|
try {
|
|
|
|
id = idString.toLong()
|
|
|
|
} catch (e: NumberFormatException) {
|
2020-08-24 16:27:08 +00:00
|
|
|
println(e.message)
|
2020-05-23 09:27:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|
2020-10-06 08:46:04 +00:00
|
|
|
}
|