keeping device screen on during any voice playback/recording within the room details activity

- Fixes playback and recording stopping due to device inactivity
- Adds a separate activity listener for monitoring if any audio is playing or being recording and keeping the screen on if so
This commit is contained in:
Adam Brown 2021-11-24 15:08:02 +00:00
parent 32441eb81b
commit c2c495c49d
3 changed files with 39 additions and 0 deletions

View file

@ -19,6 +19,7 @@ package im.vector.app.core.extensions
import android.app.Activity
import android.content.Intent
import android.os.Parcelable
import android.view.WindowManager
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultLauncher
@ -112,3 +113,11 @@ fun Activity.restart() {
startActivity(intent)
finish()
}
fun Activity.keepScreenOn() {
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
fun Activity.endKeepScreenOn() {
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}

View file

@ -30,12 +30,15 @@ import com.airbnb.mvrx.viewModel
import com.google.android.material.appbar.MaterialToolbar
import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.R
import im.vector.app.core.extensions.endKeepScreenOn
import im.vector.app.core.extensions.hideKeyboard
import im.vector.app.core.extensions.keepScreenOn
import im.vector.app.core.extensions.replaceFragment
import im.vector.app.core.platform.ToolbarConfigurable
import im.vector.app.core.platform.VectorBaseActivity
import im.vector.app.databinding.ActivityRoomDetailBinding
import im.vector.app.features.home.room.breadcrumbs.BreadcrumbsFragment
import im.vector.app.features.home.room.detail.timeline.helper.VoiceMessagePlaybackTracker
import im.vector.app.features.matrixto.MatrixToBottomSheet
import im.vector.app.features.navigation.Navigator
import im.vector.app.features.room.RequireActiveMembershipAction
@ -43,6 +46,9 @@ import im.vector.app.features.room.RequireActiveMembershipViewEvents
import im.vector.app.features.room.RequireActiveMembershipViewModel
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import javax.inject.Inject
private const val ROOM_DETAILS_SCREEN_ON_TRACKER = "room_details_screen_on"
@AndroidEntryPoint
class RoomDetailActivity :
@ -73,6 +79,7 @@ class RoomDetailActivity :
override fun getCoordinatorLayout() = views.coordinatorLayout
@Inject lateinit var playbackTracker: VoiceMessagePlaybackTracker
private lateinit var sharedActionViewModel: RoomDetailSharedActionViewModel
private val requireActiveMembershipViewModel: RequireActiveMembershipViewModel by viewModel()
@ -114,6 +121,13 @@ class RoomDetailActivity :
}
}
views.drawerLayout.addDrawerListener(drawerListener)
playbackTracker.trackActivity(ROOM_DETAILS_SCREEN_ON_TRACKER) { isActive ->
when (isActive) {
true -> keepScreenOn()
false -> endKeepScreenOn()
}
}
}
private fun handleRoomLeft(roomLeft: RequireActiveMembershipViewEvents.RoomLeft) {
@ -136,6 +150,7 @@ class RoomDetailActivity :
override fun onDestroy() {
supportFragmentManager.unregisterFragmentLifecycleCallbacks(fragmentLifecycleCallbacks)
views.drawerLayout.removeDrawerListener(drawerListener)
playbackTracker.unTrackActivity(ROOM_DETAILS_SCREEN_ON_TRACKER)
super.onDestroy()
}

View file

@ -26,8 +26,17 @@ class VoiceMessagePlaybackTracker @Inject constructor() {
private val mainHandler = Handler(Looper.getMainLooper())
private val listeners = mutableMapOf<String, Listener>()
private val activityListeners = mutableMapOf<String, ActivityListener>()
private val states = mutableMapOf<String, Listener.State>()
fun trackActivity(key: String, listener: ActivityListener) {
activityListeners[key] = listener
}
fun unTrackActivity(id: String) {
activityListeners.remove(id)
}
fun track(id: String, listener: Listener) {
listeners[id] = listener
@ -52,8 +61,10 @@ class VoiceMessagePlaybackTracker @Inject constructor() {
*/
private fun setState(key: String, state: Listener.State) {
states[key] = state
val isActive = states.values.any { it is Listener.State.Playing || it is Listener.State.Recording }
mainHandler.post {
listeners[key]?.onUpdate(state)
activityListeners.forEach { it.value.onUpdate(isActive) }
}
}
@ -125,4 +136,8 @@ class VoiceMessagePlaybackTracker @Inject constructor() {
data class Recording(val amplitudeList: List<Int>) : State()
}
}
fun interface ActivityListener {
fun onUpdate(isActive: Boolean)
}
}