Skip to content

Commit

Permalink
Merge pull request #3159 from element-hq/feature/bma/elementCallPip
Browse files Browse the repository at this point in the history
Add support for Picture In Picture for Element Call
  • Loading branch information
bmarty authored Jul 8, 2024
2 parents 4640233 + 214c9d2 commit b0c9091
Show file tree
Hide file tree
Showing 19 changed files with 517 additions and 26 deletions.
2 changes: 2 additions & 0 deletions features/call/impl/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@ dependencies {
testImplementation(projects.libraries.push.test)
testImplementation(projects.services.analytics.test)
testImplementation(projects.tests.testutils)
testImplementation(libs.androidx.compose.ui.test.junit)
testReleaseImplementation(libs.androidx.compose.ui.test.manifest)
}
15 changes: 9 additions & 6 deletions features/call/impl/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@
<application>
<activity
android:name=".ui.ElementCallActivity"
android:configChanges="screenSize|screenLayout|orientation|keyboardHidden|keyboard|navigation|uiMode"
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation|keyboardHidden|keyboard|navigation|uiMode"
android:exported="true"
android:label="@string/element_call"
android:launchMode="singleTask"
android:supportsPictureInPicture="true"
android:taskAffinity="io.element.android.features.call">

<intent-filter android:autoVerify="true">
Expand Down Expand Up @@ -77,10 +78,11 @@

</activity>

<activity android:name=".ui.IncomingCallActivity"
<activity
android:name=".ui.IncomingCallActivity"
android:configChanges="screenSize|screenLayout|orientation|keyboardHidden|keyboard|navigation|uiMode"
android:exported="false"
android:excludeFromRecents="true"
android:exported="false"
android:launchMode="singleTask"
android:taskAffinity="io.element.android.features.call" />

Expand All @@ -90,9 +92,10 @@
android:exported="false"
android:foregroundServiceType="phoneCall" />

<receiver android:name=".receivers.DeclineCallBroadcastReceiver"
android:exported="false"
android:enabled="true" />
<receiver
android:name=".receivers.DeclineCallBroadcastReceiver"
android:enabled="true"
android:exported="false" />

</application>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) 2024 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.element.android.features.call.impl.pip

sealed interface PictureInPictureEvents {
data object EnterPictureInPicture : PictureInPictureEvents
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2024 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.element.android.features.call.impl.pip

import android.app.Activity
import android.app.PictureInPictureParams
import android.os.Build
import android.util.Rational
import androidx.annotation.RequiresApi
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import io.element.android.libraries.architecture.Presenter
import io.element.android.libraries.core.log.logger.LoggerTag
import timber.log.Timber
import java.lang.ref.WeakReference
import javax.inject.Inject

private val loggerTag = LoggerTag("PiP")

class PictureInPicturePresenter @Inject constructor(
pipSupportProvider: PipSupportProvider,
) : Presenter<PictureInPictureState> {
private val isPipSupported = pipSupportProvider.isPipSupported()
private var isInPictureInPicture = mutableStateOf(false)
private var hostActivity: WeakReference<Activity>? = null

@Composable
override fun present(): PictureInPictureState {
fun handleEvent(event: PictureInPictureEvents) {
when (event) {
PictureInPictureEvents.EnterPictureInPicture -> switchToPip()
}
}

return PictureInPictureState(
supportPip = isPipSupported,
isInPictureInPicture = isInPictureInPicture.value,
eventSink = ::handleEvent,
)
}

fun onCreate(activity: Activity) {
if (isPipSupported) {
Timber.tag(loggerTag.value).d("onCreate: Setting PiP params")
hostActivity = WeakReference(activity)
hostActivity?.get()?.setPictureInPictureParams(getPictureInPictureParams())
} else {
Timber.tag(loggerTag.value).d("onCreate: PiP is not supported")
}
}

fun onDestroy() {
Timber.tag(loggerTag.value).d("onDestroy")
hostActivity?.clear()
hostActivity = null
}

@RequiresApi(Build.VERSION_CODES.O)
private fun getPictureInPictureParams(): PictureInPictureParams {
return PictureInPictureParams.Builder()
// Portrait for calls seems more appropriate
.setAspectRatio(Rational(3, 5))
.apply {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
setAutoEnterEnabled(true)
}
}
.build()
}

/**
* Enters Picture-in-Picture mode.
*/
private fun switchToPip() {
if (isPipSupported) {
Timber.tag(loggerTag.value).d("Switch to PiP mode")
hostActivity?.get()?.enterPictureInPictureMode(getPictureInPictureParams())
?.also { Timber.tag(loggerTag.value).d("Switch to PiP mode result: $it") }
}
}

fun onPictureInPictureModeChanged(isInPictureInPictureMode: Boolean) {
Timber.tag(loggerTag.value).d("onPictureInPictureModeChanged: $isInPictureInPictureMode")
isInPictureInPicture.value = isInPictureInPictureMode
}

fun onUserLeaveHint() {
Timber.tag(loggerTag.value).d("onUserLeaveHint")
switchToPip()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2024 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.element.android.features.call.impl.pip

data class PictureInPictureState(
val supportPip: Boolean,
val isInPictureInPicture: Boolean,
val eventSink: (PictureInPictureEvents) -> Unit,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2024 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.element.android.features.call.impl.pip

fun aPictureInPictureState(
supportPip: Boolean = false,
isInPictureInPicture: Boolean = false,
eventSink: (PictureInPictureEvents) -> Unit = {},
): PictureInPictureState {
return PictureInPictureState(
supportPip = supportPip,
isInPictureInPicture = isInPictureInPicture,
eventSink = eventSink,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2024 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.element.android.features.call.impl.pip

import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import androidx.annotation.ChecksSdkIntAtLeast
import com.squareup.anvil.annotations.ContributesBinding
import io.element.android.libraries.core.bool.orFalse
import io.element.android.libraries.di.AppScope
import io.element.android.libraries.di.ApplicationContext
import javax.inject.Inject

interface PipSupportProvider {
@ChecksSdkIntAtLeast(Build.VERSION_CODES.O)
fun isPipSupported(): Boolean
}

@ContributesBinding(AppScope::class)
class DefaultPipSupportProvider @Inject constructor(
@ApplicationContext private val context: Context,
) : PipSupportProvider {
override fun isPipSupported(): Boolean {
val hasSystemFeaturePip = context.packageManager?.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE).orFalse()
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && hasSystemFeaturePip
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ open class CallScreenStateProvider : PreviewParameterProvider<CallScreenState> {
override val values: Sequence<CallScreenState>
get() = sequenceOf(
aCallScreenState(),
aCallScreenState(urlState = AsyncData.Loading()),
aCallScreenState(urlState = AsyncData.Failure(Exception("An error occurred"))),
)
}

private fun aCallScreenState(
internal fun aCallScreenState(
urlState: AsyncData<String> = AsyncData.Success("https://call.element.io/some-actual-call?with=parameters"),
userAgent: String = "",
isInWidgetMode: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.viewinterop.AndroidView
import io.element.android.compound.tokens.generated.CompoundIcons
import io.element.android.features.call.impl.R
import io.element.android.features.call.impl.pip.PictureInPictureEvents
import io.element.android.features.call.impl.pip.PictureInPictureState
import io.element.android.features.call.impl.pip.aPictureInPictureState
import io.element.android.features.call.impl.utils.WebViewWidgetMessageInterceptor
import io.element.android.libraries.architecture.AsyncData
import io.element.android.libraries.designsystem.components.ProgressDialog
Expand All @@ -58,25 +61,36 @@ interface CallScreenNavigator {
@Composable
internal fun CallScreenView(
state: CallScreenState,
pipState: PictureInPictureState,
requestPermissions: (Array<String>, RequestPermissionCallback) -> Unit,
modifier: Modifier = Modifier,
) {
fun handleBack() {
if (pipState.supportPip) {
pipState.eventSink.invoke(PictureInPictureEvents.EnterPictureInPicture)
} else {
state.eventSink(CallScreenEvents.Hangup)
}
}

Scaffold(
modifier = modifier,
topBar = {
TopAppBar(
title = { Text(stringResource(R.string.element_call)) },
navigationIcon = {
BackButton(
imageVector = CompoundIcons.Close(),
onClick = { state.eventSink(CallScreenEvents.Hangup) }
)
}
)
if (!pipState.isInPictureInPicture) {
TopAppBar(
title = { Text(stringResource(R.string.element_call)) },
navigationIcon = {
BackButton(
imageVector = CompoundIcons.Close(),
onClick = ::handleBack,
)
}
)
}
}
) { padding ->
BackHandler {
state.eventSink(CallScreenEvents.Hangup)
handleBack()
}
CallWebView(
modifier = Modifier
Expand Down Expand Up @@ -177,6 +191,7 @@ internal fun CallScreenViewPreview(
) = ElementPreview {
CallScreenView(
state = state,
pipState = aPictureInPictureState(),
requestPermissions = { _, _ -> },
)
}
Loading

0 comments on commit b0c9091

Please sign in to comment.