Skip to content

Commit

Permalink
Add support to play music while on and other fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
brahmkshatriya committed Sep 10, 2024
1 parent 56c3fc0 commit 1a755ce
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 33 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/dev/brahmkshatriya/echo/EchoApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import dev.brahmkshatriya.echo.plugger.TrackerExtension
import dev.brahmkshatriya.echo.plugger.TrackerExtensionRepo
import dev.brahmkshatriya.echo.ui.exception.ExceptionFragment.Companion.getDetails
import dev.brahmkshatriya.echo.ui.exception.ExceptionFragment.Companion.getTitle
import dev.brahmkshatriya.echo.ui.settings.AboutFragment.AboutPreference.Companion.applyLocale
import dev.brahmkshatriya.echo.ui.settings.LookFragment.Companion.AMOLED_KEY
import dev.brahmkshatriya.echo.ui.settings.LookFragment.Companion.COLOR_KEY
import dev.brahmkshatriya.echo.ui.settings.LookFragment.Companion.CUSTOM_THEME_KEY
Expand Down Expand Up @@ -93,6 +94,7 @@ class EchoApplication : Application() {
override fun onCreate() {
super.onCreate()
//UI
applyLocale(settings)
applyUiChanges(this, settings)

//Crash Handling
Expand Down
6 changes: 2 additions & 4 deletions app/src/main/java/dev/brahmkshatriya/echo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ import dev.brahmkshatriya.echo.common.models.User
import dev.brahmkshatriya.echo.databinding.ActivityMainBinding
import dev.brahmkshatriya.echo.ui.common.openFragment
import dev.brahmkshatriya.echo.ui.item.ItemFragment
import dev.brahmkshatriya.echo.ui.settings.AboutFragment.AboutPreference.Companion.applyLocale
import dev.brahmkshatriya.echo.ui.settings.LookFragment.Companion.NAVBAR_GRADIENT
import dev.brahmkshatriya.echo.utils.animateTranslation
import dev.brahmkshatriya.echo.utils.checkPermissions
import dev.brahmkshatriya.echo.utils.checkAudioPermissions
import dev.brahmkshatriya.echo.utils.collect
import dev.brahmkshatriya.echo.utils.createNavDrawable
import dev.brahmkshatriya.echo.utils.emit
Expand Down Expand Up @@ -67,8 +66,7 @@ class MainActivity : AppCompatActivity() {
else SystemBarStyle.light(TRANSPARENT, TRANSPARENT)
)

checkPermissions(this)
applyLocale(playerViewModel.settings)
checkAudioPermissions()

val navView = binding.navView as NavigationBarView

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ class DelayedSource(
private lateinit var actualSource: MediaSource
override fun prepareSourceInternal(mediaTransferListener: TransferListener?) {
super.prepareSourceInternal(mediaTransferListener)
println("prepareSourceInternal")
scope.launch(Dispatchers.IO) {
val new = runCatching { resolve(mediaItem) }.getOrElse {
throwableFlow.emit(it)
Expand All @@ -73,8 +72,6 @@ class DelayedSource(

private suspend fun onUrlResolved(new: MediaItem) = withContext(Dispatchers.Main) {
mediaItem = new
mediaItem.run { println("urlResolved : $audioIndex $videoIndex $subtitleIndex") }
println("video : ${new.video}")
val useVideoFactory = when (val video = new.video) {
is Streamable.Media.WithVideo.WithAudio -> new.videoStreamable == new.audioStreamable
is Streamable.Media.WithVideo.Only -> if (!video.looping) false else null
Expand Down Expand Up @@ -121,23 +118,18 @@ class DelayedSource(

override fun canUpdateMediaItem(mediaItem: MediaItem) = run {
this.mediaItem.apply {
println("item : $audioIndex $videoIndex $subtitleIndex")
mediaItem.run { println("new : $audioIndex $videoIndex $subtitleIndex") }

if (audioIndex != mediaItem.audioIndex) return@run false
if (videoIndex != mediaItem.videoIndex) return@run false
if (subtitleIndex != mediaItem.subtitleIndex) return@run false
}
actualSource.canUpdateMediaItem(mediaItem)
}.also { println("canUpdateMediaItem : $it") }

}
override fun updateMediaItem(mediaItem: MediaItem) {
this.mediaItem = mediaItem
actualSource.updateMediaItem(mediaItem)
}

private suspend fun resolve(mediaItem: MediaItem): MediaItem {
mediaItem.run { println("resolve : $audioIndex $videoIndex $subtitleIndex") }
val new = if (mediaItem.isLoaded) mediaItem
else MediaItemUtils.build(settings, mediaItem, loadTrack(mediaItem))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi
import dev.brahmkshatriya.echo.playback.MediaItemUtils.isLoaded

@UnstableApi
open class PlayerListener(val player: Player) : Player.Listener {
open fun onTrackStart(mediaItem: MediaItem) {}
open fun onTrackEnd(mediaItem: MediaItem) {}
Expand Down Expand Up @@ -55,7 +56,6 @@ open class PlayerListener(val player: Player) : Player.Listener {
}

@CallSuper
@UnstableApi
override fun onPositionDiscontinuity(
oldPosition: Player.PositionInfo,
newPosition: Player.PositionInfo,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import dev.brahmkshatriya.echo.R
import dev.brahmkshatriya.echo.common.clients.LibraryClient
import dev.brahmkshatriya.echo.common.models.EchoMediaItem.Companion.toMediaItem
import dev.brahmkshatriya.echo.databinding.FragmentLibraryBinding
import dev.brahmkshatriya.echo.ui.adapter.MediaContainerAdapter
import dev.brahmkshatriya.echo.ui.common.MainFragment
import dev.brahmkshatriya.echo.ui.common.MainFragment.Companion.first
import dev.brahmkshatriya.echo.ui.common.MainFragment.Companion.scrollTo
import dev.brahmkshatriya.echo.ui.common.configureFeedUI
import dev.brahmkshatriya.echo.ui.common.configureMainMenu
import dev.brahmkshatriya.echo.ui.common.openFragment
import dev.brahmkshatriya.echo.ui.adapter.MediaContainerAdapter
import dev.brahmkshatriya.echo.utils.autoCleared
import dev.brahmkshatriya.echo.utils.observe
import dev.brahmkshatriya.echo.utils.onAppBarChangeListener
Expand Down Expand Up @@ -56,7 +56,7 @@ class LibraryFragment : Fragment() {
}

configureFeedUI<LibraryClient>(
R.string.home,
R.string.library,
viewModel,
binding.recyclerView,
binding.swipeRefresh,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ sealed class QuickSearchViewHolder(itemView: View) : RecyclerView.ViewHolder(ite
override fun bind(item: QuickSearchItem) {
item as QuickSearchItem.SearchMediaItem
binding.query.text = item.mediaItem.title
transitionView.transitionName = ("quick" + item.mediaItem.id).hashCode().toString()
item.mediaItem.cover.loadInto(binding.cover, item.mediaItem.placeHolder())
}

Expand Down
62 changes: 45 additions & 17 deletions app/src/main/java/dev/brahmkshatriya/echo/utils/CheckPermissions.kt
Original file line number Diff line number Diff line change
@@ -1,36 +1,64 @@
package dev.brahmkshatriya.echo.utils

import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.provider.Settings
import android.widget.Toast
import androidx.activity.result.ActivityResultCaller
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import dev.brahmkshatriya.echo.R

fun checkPermissions(activity: AppCompatActivity) {

fun AppCompatActivity.checkAudioPermissions() {
val perm = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
Manifest.permission.READ_MEDIA_AUDIO
else
Manifest.permission.READ_EXTERNAL_STORAGE
val permStatus = ContextCompat.checkSelfPermission(activity, perm)
if (permStatus != PackageManager.PERMISSION_GRANTED)
activity.registerForActivityResult(ActivityResultContracts.RequestPermission()) {
if (!it)
AlertDialog.Builder(activity)
.setTitle("Permission Required")
.setMessage("This permission is required to access your music library")
.setPositiveButton("Ok") { _, _ ->
Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
data = Uri.fromParts("package", activity.packageName, null)
activity.startActivity(this)
}
}
.setNegativeButton("Cancel") { _, _ -> activity.finish() }
.show()
checkPermissions(
this,
perm,
R.string.permission_required,
R.string.music_permission_required_summary,
{ finish() }
)?.launch(perm)
}

}.launch(perm)
fun ActivityResultCaller.checkPermissions(
context: Context,
perm: String,
title: Int,
message: Int,
onCancel: () -> Unit,
onGranted: () -> Unit = {},
onRequest: () -> Unit = {
Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS).apply {
data = Uri.fromParts("package", context.packageName, null)
context.startActivity(this)
}
}
): ActivityResultLauncher<String>? = with(context) {
val permStatus = ContextCompat.checkSelfPermission(this, perm)
val contract = ActivityResultContracts.RequestPermission()
return if (permStatus != PackageManager.PERMISSION_GRANTED) registerForActivityResult(contract) {
if (!it) AlertDialog.Builder(this)
.setTitle(getString(title))
.setMessage(getString(message))
.setPositiveButton(getString(R.string.ok)) { _, _ -> onRequest() }
.setNegativeButton(getString(R.string.cancel)) { _, _ ->
Toast.makeText(
this, getString(R.string.permission_denied), Toast.LENGTH_SHORT
).show()
onCancel()
}
.show()
else onGranted()
} else null
}
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@
<string name="off">Off</string>
<string name="quality_number">Quality %1$d</string>
<string name="unknown">Unknown</string>
<string name="permission_required">Permission Required</string>
<string name="permission_denied">Permission Denied</string>
<string name="music_permission_required_summary">This permission is required to access your music library</string>
<string-array name="stream_qualities">
<item>Highest</item>
<item>Medium</item>
Expand Down

0 comments on commit 1a755ce

Please sign in to comment.