PlayerAndroid/app/src/main/java/code/name/monkey/retromusic/activities/MainActivity.kt

236 lines
9.7 KiB
Kotlin
Raw Normal View History

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.
*
*/
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
import android.os.Bundle
import android.provider.MediaStore
import androidx.lifecycle.lifecycleScope
2021-10-11 11:25:17 +00:00
import androidx.navigation.contains
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
import code.name.monkey.retromusic.extensions.*
import code.name.monkey.retromusic.fragments.base.AbsRecyclerViewFragment
import code.name.monkey.retromusic.fragments.home.HomeFragment
import code.name.monkey.retromusic.helper.MusicPlayerRemote
import code.name.monkey.retromusic.helper.SearchQueryHelper.getSongs
2020-09-25 17:38:59 +00:00
import code.name.monkey.retromusic.model.CategoryInfo
import code.name.monkey.retromusic.model.Song
import code.name.monkey.retromusic.repository.PlaylistSongsLoader
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
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch
import org.koin.android.ext.android.get
class MainActivity : AbsCastActivity(), OnSharedPreferenceChangeListener {
companion object {
const val TAG = "MainActivity"
const val EXPAND_PANEL = "expand_panel"
}
override fun createContentView(): SlidingMusicPanelLayoutBinding {
2020-10-02 18:05:09 +00:00
return wrapSlidingMusicPanel()
}
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
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
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)
// Scroll Fragment to top
2021-11-11 14:51:57 +00:00
bottomNavigationView.setOnItemReselectedListener {
currentFragment(R.id.fragment_container)
.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)
navController.addOnDestinationChangedListener { _, destination, _ ->
2021-11-17 15:48:52 +00:00
when (destination.id) {
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-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
}
}
}
private fun saveTab(id: Int) {
PreferenceUtil.lastTab = id
}
2020-08-11 18:29:44 +00:00
override fun onSupportNavigateUp(): Boolean =
findNavController(R.id.fragment_container).navigateUp()
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) {
expandPanel()
intent.removeExtra(EXPAND_PANEL)
}
}
2020-12-05 05:40:35 +00:00
override fun onDestroy() {
super.onDestroy()
PreferenceUtil.unregisterOnSharedPreferenceChangedListener(this)
}
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) {
postRecreate()
}
}
override fun onServiceConnected() {
super.onServiceConnected()
2020-12-05 05:40:35 +00:00
intent ?: return
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
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)
}
handled = true
}
if (uri != null && uri.toString().isNotEmpty()) {
MusicPlayerRemote.playFromUri(uri)
handled = true
} else if (MediaStore.Audio.Playlists.CONTENT_TYPE == mimeType) {
val id = parseLongFromIntent(intent, "playlistId", "playlist")
if (id >= 0L) {
2020-09-05 20:13:45 +00:00
val position: Int = intent.getIntExtra("position", 0)
val songs: List<Song> = PlaylistSongsLoader.getPlaylistSongList(get(), id)
MusicPlayerRemote.openQueue(songs, position, true)
handled = true
}
} else if (MediaStore.Audio.Albums.CONTENT_TYPE == mimeType) {
val id = parseLongFromIntent(intent, "albumId", "album")
if (id >= 0L) {
2020-09-05 20:13:45 +00:00
val position: Int = intent.getIntExtra("position", 0)
val songs = libraryViewModel.albumById(id).songs
MusicPlayerRemote.openQueue(
songs,
position,
true
)
handled = true
}
} else if (MediaStore.Audio.Artists.CONTENT_TYPE == mimeType) {
val id = parseLongFromIntent(intent, "artistId", "artist")
if (id >= 0L) {
2020-09-05 20:13:45 +00:00
val position: Int = intent.getIntExtra("position", 0)
val songs: List<Song> = libraryViewModel.artistById(id).songs
MusicPlayerRemote.openQueue(
songs,
position,
true
)
handled = true
}
}
if (handled) {
setIntent(Intent())
}
}
}
private fun parseLongFromIntent(
2020-10-06 08:46:04 +00:00
intent: Intent,
longKey: String,
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)
}
}
}
return id
}
2020-10-06 08:46:04 +00:00
}