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-05-23 09:27:55 +00:00
|
|
|
import android.os.Bundle
|
|
|
|
import android.provider.MediaStore
|
|
|
|
import android.util.Log
|
|
|
|
import android.view.View
|
2020-08-11 18:29:44 +00:00
|
|
|
import code.name.monkey.appthemehelper.ThemeStore
|
2020-05-23 21:47:23 +00:00
|
|
|
import code.name.monkey.appthemehelper.util.ATHUtil
|
2020-08-11 18:29:44 +00:00
|
|
|
import code.name.monkey.appthemehelper.util.VersionUtils
|
2020-05-23 09:27:55 +00:00
|
|
|
import code.name.monkey.retromusic.*
|
|
|
|
import code.name.monkey.retromusic.activities.base.AbsSlidingMusicPanelActivity
|
2020-08-11 18:29:44 +00:00
|
|
|
import code.name.monkey.retromusic.appshortcuts.DynamicShortcutManager
|
|
|
|
import code.name.monkey.retromusic.extensions.findNavController
|
2020-07-30 14:11:19 +00:00
|
|
|
import code.name.monkey.retromusic.fragments.LibraryViewModel
|
2020-05-23 09:27:55 +00:00
|
|
|
import code.name.monkey.retromusic.helper.MusicPlayerRemote.openAndShuffleQueue
|
|
|
|
import code.name.monkey.retromusic.helper.MusicPlayerRemote.openQueue
|
|
|
|
import code.name.monkey.retromusic.helper.MusicPlayerRemote.playFromUri
|
|
|
|
import code.name.monkey.retromusic.helper.MusicPlayerRemote.shuffleMode
|
|
|
|
import code.name.monkey.retromusic.helper.SearchQueryHelper.getSongs
|
|
|
|
import code.name.monkey.retromusic.interfaces.CabHolder
|
|
|
|
import code.name.monkey.retromusic.loaders.AlbumLoader.getAlbum
|
|
|
|
import code.name.monkey.retromusic.loaders.ArtistLoader.getArtist
|
|
|
|
import code.name.monkey.retromusic.loaders.PlaylistSongsLoader.getPlaylistSongList
|
|
|
|
import code.name.monkey.retromusic.model.Song
|
|
|
|
import code.name.monkey.retromusic.service.MusicService
|
|
|
|
import code.name.monkey.retromusic.util.AppRater.appLaunched
|
2020-06-06 18:57:28 +00:00
|
|
|
import code.name.monkey.retromusic.util.PreferenceUtil
|
2020-05-23 09:27:55 +00:00
|
|
|
import code.name.monkey.retromusic.util.RetroColorUtil
|
|
|
|
import com.afollestad.materialcab.MaterialCab
|
2020-08-11 18:29:44 +00:00
|
|
|
import com.afollestad.materialdialogs.color.ColorChooserDialog
|
2020-07-30 14:11:19 +00:00
|
|
|
import org.koin.android.ext.android.inject
|
2020-05-23 09:27:55 +00:00
|
|
|
import java.util.*
|
|
|
|
|
|
|
|
class MainActivity : AbsSlidingMusicPanelActivity(),
|
2020-08-11 18:29:44 +00:00
|
|
|
SharedPreferences.OnSharedPreferenceChangeListener, CabHolder,
|
|
|
|
ColorChooserDialog.ColorCallback {
|
2020-05-23 09:27:55 +00:00
|
|
|
companion object {
|
|
|
|
const val TAG = "MainActivity"
|
|
|
|
const val EXPAND_PANEL = "expand_panel"
|
|
|
|
const val APP_UPDATE_REQUEST_CODE = 9002
|
|
|
|
}
|
|
|
|
|
2020-08-11 18:29:44 +00:00
|
|
|
|
2020-07-30 14:11:19 +00:00
|
|
|
private val libraryViewModel: LibraryViewModel by inject()
|
2020-05-23 21:47:23 +00:00
|
|
|
private var cab: MaterialCab? = null
|
2020-05-23 09:27:55 +00:00
|
|
|
private var blockRequestPermissions = false
|
|
|
|
|
|
|
|
override fun createContentView(): View {
|
|
|
|
return wrapSlidingMusicPanel(R.layout.activity_main_content)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
setDrawUnderStatusBar()
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
setStatusbarColorAuto()
|
|
|
|
setNavigationbarColorAuto()
|
|
|
|
setLightNavigationBar(true)
|
|
|
|
setTaskDescriptionColorAuto()
|
|
|
|
hideStatusBar()
|
|
|
|
setBottomBarVisibility(View.VISIBLE)
|
|
|
|
appLaunched(this)
|
2020-08-11 18:29:44 +00:00
|
|
|
addMusicServiceEventListener(libraryViewModel)
|
2020-05-23 23:27:19 +00:00
|
|
|
updateTabs()
|
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-05-23 09:27:55 +00:00
|
|
|
if (intent.hasExtra(EXPAND_PANEL) &&
|
|
|
|
intent.getBooleanExtra(EXPAND_PANEL, false) &&
|
2020-06-06 18:57:28 +00:00
|
|
|
PreferenceUtil.isExpandPanel
|
2020-05-23 09:27:55 +00:00
|
|
|
) {
|
|
|
|
expandPanel()
|
|
|
|
intent.removeExtra(EXPAND_PANEL)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroy() {
|
|
|
|
super.onDestroy()
|
2020-06-06 18:57:28 +00:00
|
|
|
PreferenceUtil.unregisterOnSharedPreferenceChangedListener(this)
|
2020-05-23 09:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun requestPermissions() {
|
|
|
|
if (!blockRequestPermissions) {
|
|
|
|
super.requestPermissions()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data)
|
|
|
|
if (!hasPermissions()) {
|
|
|
|
requestPermissions()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
|
|
|
|
if (key == GENERAL_THEME || 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()
|
|
|
|
handlePlaybackIntent(intent)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun handlePlaybackIntent(intent: Intent?) {
|
|
|
|
if (intent == null) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
val uri = intent.data
|
|
|
|
val mimeType = intent.type
|
|
|
|
var handled = false
|
|
|
|
if (intent.action != null && (intent.action == MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH)
|
|
|
|
) {
|
|
|
|
val songs: List<Song> =
|
|
|
|
getSongs(this, intent.extras!!)
|
|
|
|
if (shuffleMode == MusicService.SHUFFLE_MODE_SHUFFLE) {
|
|
|
|
openAndShuffleQueue(songs, true)
|
|
|
|
} else {
|
|
|
|
openQueue(songs, 0, true)
|
|
|
|
}
|
|
|
|
handled = true
|
|
|
|
}
|
|
|
|
if (uri != null && uri.toString().isNotEmpty()) {
|
|
|
|
playFromUri(uri)
|
|
|
|
handled = true
|
|
|
|
} else if (MediaStore.Audio.Playlists.CONTENT_TYPE == mimeType) {
|
|
|
|
val id = parseIdFromIntent(intent, "playlistId", "playlist").toInt()
|
|
|
|
if (id >= 0) {
|
|
|
|
val position = intent.getIntExtra("position", 0)
|
|
|
|
val songs: List<Song> =
|
|
|
|
ArrayList(getPlaylistSongList(this, id))
|
|
|
|
openQueue(songs, position, true)
|
|
|
|
handled = true
|
|
|
|
}
|
|
|
|
} else if (MediaStore.Audio.Albums.CONTENT_TYPE == mimeType) {
|
|
|
|
val id = parseIdFromIntent(intent, "albumId", "album").toInt()
|
|
|
|
if (id >= 0) {
|
|
|
|
val position = intent.getIntExtra("position", 0)
|
|
|
|
openQueue(getAlbum(this, id).songs!!, position, true)
|
|
|
|
handled = true
|
|
|
|
}
|
|
|
|
} else if (MediaStore.Audio.Artists.CONTENT_TYPE == mimeType) {
|
|
|
|
val id = parseIdFromIntent(intent, "artistId", "artist").toInt()
|
|
|
|
if (id >= 0) {
|
|
|
|
val position = intent.getIntExtra("position", 0)
|
|
|
|
openQueue(getArtist(this, id).songs, position, true)
|
|
|
|
handled = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (handled) {
|
|
|
|
setIntent(Intent())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun parseIdFromIntent(
|
|
|
|
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-05-23 21:47:23 +00:00
|
|
|
Log.e(TAG, e.message)
|
2020-05-23 09:27:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun handleBackPress(): Boolean {
|
2020-05-23 21:47:23 +00:00
|
|
|
if (cab != null && cab!!.isActive) {
|
|
|
|
cab?.finish()
|
2020-05-23 09:27:55 +00:00
|
|
|
return true
|
|
|
|
}
|
2020-08-11 18:29:44 +00:00
|
|
|
return super.handleBackPress()
|
2020-05-23 09:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun openCab(menuRes: Int, callback: MaterialCab.Callback): MaterialCab {
|
2020-05-23 21:47:23 +00:00
|
|
|
cab?.let {
|
|
|
|
if (it.isActive) it.finish()
|
2020-05-23 09:27:55 +00:00
|
|
|
}
|
|
|
|
cab = MaterialCab(this, R.id.cab_stub)
|
|
|
|
.setMenu(menuRes)
|
2020-07-19 21:00:30 +00:00
|
|
|
.setCloseDrawableRes(R.drawable.ic_close)
|
2020-05-23 09:27:55 +00:00
|
|
|
.setBackgroundColor(
|
|
|
|
RetroColorUtil.shiftBackgroundColorForLightText(
|
2020-05-23 21:47:23 +00:00
|
|
|
ATHUtil.resolveColor(
|
|
|
|
this,
|
|
|
|
R.attr.colorSurface
|
|
|
|
)
|
2020-05-23 09:27:55 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
.start(callback)
|
2020-05-23 21:47:23 +00:00
|
|
|
return cab as MaterialCab
|
2020-05-23 09:27:55 +00:00
|
|
|
}
|
2020-08-11 18:29:44 +00:00
|
|
|
|
|
|
|
override fun onColorSelection(dialog: ColorChooserDialog, selectedColor: Int) {
|
|
|
|
when (dialog.title) {
|
|
|
|
R.string.accent_color -> {
|
|
|
|
ThemeStore.editTheme(this).accentColor(selectedColor).commit()
|
|
|
|
if (VersionUtils.hasNougatMR())
|
|
|
|
DynamicShortcutManager(this).updateDynamicShortcuts()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
recreate()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onColorChooserDismissed(dialog: ColorChooserDialog) {
|
|
|
|
|
|
|
|
}
|
2020-05-23 09:27:55 +00:00
|
|
|
}
|