From c2c495c49df8e02c9939ba551a719c34b7cec2e2 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Wed, 24 Nov 2021 15:08:02 +0000 Subject: [PATCH] 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 --- .../im/vector/app/core/extensions/Activity.kt | 9 +++++++++ .../home/room/detail/RoomDetailActivity.kt | 15 +++++++++++++++ .../helper/VoiceMessagePlaybackTracker.kt | 15 +++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/vector/src/main/java/im/vector/app/core/extensions/Activity.kt b/vector/src/main/java/im/vector/app/core/extensions/Activity.kt index de469b9e3a..da58498238 100644 --- a/vector/src/main/java/im/vector/app/core/extensions/Activity.kt +++ b/vector/src/main/java/im/vector/app/core/extensions/Activity.kt @@ -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) +} diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailActivity.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailActivity.kt index 415ca7bc04..2588f99861 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailActivity.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailActivity.kt @@ -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() } diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/VoiceMessagePlaybackTracker.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/VoiceMessagePlaybackTracker.kt index 2e8f6d9336..cf00c3e636 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/VoiceMessagePlaybackTracker.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/helper/VoiceMessagePlaybackTracker.kt @@ -26,8 +26,17 @@ class VoiceMessagePlaybackTracker @Inject constructor() { private val mainHandler = Handler(Looper.getMainLooper()) private val listeners = mutableMapOf() + private val activityListeners = mutableMapOf() private val states = mutableMapOf() + 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) : State() } } + + fun interface ActivityListener { + fun onUpdate(isActive: Boolean) + } }