Skip to content

Commit

Permalink
Handle calls when Netp is enabled only
Browse files Browse the repository at this point in the history
  • Loading branch information
karlenDimla committed Apr 25, 2024
1 parent 307bd0c commit 7498b14
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.common.utils.extensions.registerExportedReceiver
import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.di.scopes.ReceiverScope
import com.duckduckgo.feature.toggles.api.Toggle
import com.duckduckgo.mobile.android.vpn.Vpn
import com.duckduckgo.networkprotection.impl.settings.NetPSettingsLocalConfig
import com.squareup.anvil.annotations.ContributesBinding
import com.duckduckgo.networkprotection.api.NetworkProtectionState
import com.squareup.anvil.annotations.ContributesMultibinding
import javax.inject.Inject
import kotlin.properties.Delegates
Expand All @@ -41,34 +39,25 @@ import logcat.LogPriority
import logcat.asLog
import logcat.logcat

interface VpnDisableOnCall {
fun enable()
fun disable()

suspend fun isEnabled(): Boolean
}

@InjectWith(ReceiverScope::class)
@ContributesMultibinding(
scope = AppScope::class,
boundType = MainProcessLifecycleObserver::class,
)
@ContributesBinding(
scope = AppScope::class,
boundType = VpnDisableOnCall::class,
)
class VpnCallStateReceiver @Inject constructor(
private val context: Context,
private val vpn: Vpn,
private val dispatcherProvider: DispatcherProvider,
@AppCoroutineScope private val appCoroutineScope: CoroutineScope,
private val netPSettingsLocalConfig: NetPSettingsLocalConfig,
@ProcessName private val processName: String,
) : BroadcastReceiver(), MainProcessLifecycleObserver, VpnDisableOnCall {
private val vpnDisableOnCall: VpnDisableOnCall,
private val networkProtectionState: NetworkProtectionState,
) : BroadcastReceiver(), MainProcessLifecycleObserver {

private val telephonyManager by lazy {
context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager?
}

private val _listener: PhoneStateListener =
object : PhoneStateListener() {
@Deprecated("Deprecated in Java")
Expand All @@ -78,10 +67,12 @@ class VpnCallStateReceiver @Inject constructor(
) {
appCoroutineScope.launch(dispatcherProvider.io()) {
logcat { "Call state: $state" }
if (state == TelephonyManager.CALL_STATE_IDLE) {
vpn.start()
} else {
vpn.stop()
if (networkProtectionState.isEnabled()) {
if (state == TelephonyManager.CALL_STATE_IDLE) {
vpn.start()
} else {
vpn.stop()
}
}
}
}
Expand All @@ -96,24 +87,6 @@ class VpnCallStateReceiver @Inject constructor(
}
}

override fun enable() {
appCoroutineScope.launch(dispatcherProvider.io()) {
netPSettingsLocalConfig.vpnPauseDuringCalls().setEnabled(Toggle.State(enable = true))
context.sendBroadcast(Intent(ACTION_REGISTER_STATE_CALL_LISTENER))
}
}

override fun disable() {
appCoroutineScope.launch(dispatcherProvider.io()) {
context.sendBroadcast(Intent(ACTION_UNREGISTER_STATE_CALL_LISTENER))
netPSettingsLocalConfig.vpnPauseDuringCalls().setEnabled(Toggle.State(enable = false))
}
}

override suspend fun isEnabled(): Boolean = withContext(dispatcherProvider.io()) {
return@withContext netPSettingsLocalConfig.vpnPauseDuringCalls().isEnabled()
}

override fun onReceive(
context: Context,
intent: Intent,
Expand Down Expand Up @@ -145,7 +118,7 @@ class VpnCallStateReceiver @Inject constructor(
override fun onCreate(owner: LifecycleOwner) {
register()
appCoroutineScope.launch(dispatcherProvider.io()) {
if (isEnabled()) {
if (vpnDisableOnCall.isEnabled()) {
registerListener()
} else {
logcat { "CALL_STATE listener feature is disabled" }
Expand Down Expand Up @@ -183,8 +156,8 @@ class VpnCallStateReceiver @Inject constructor(
}

companion object {
private const val ACTION_REGISTER_STATE_CALL_LISTENER = "com.duckduckgo.netp.feature.snooze.ACTION_REGISTER_STATE_CALL_LISTENER"
private const val ACTION_UNREGISTER_STATE_CALL_LISTENER = "com.duckduckgo.netp.feature.snooze.ACTION_UNREGISTER_STATE_CALL_LISTENER"
internal const val ACTION_REGISTER_STATE_CALL_LISTENER = "com.duckduckgo.netp.feature.snooze.ACTION_REGISTER_STATE_CALL_LISTENER"
internal const val ACTION_UNREGISTER_STATE_CALL_LISTENER = "com.duckduckgo.netp.feature.snooze.ACTION_UNREGISTER_STATE_CALL_LISTENER"
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) 2024 DuckDuckGo
*
* 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
*
* http://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 com.duckduckgo.networkprotection.impl.snooze

import android.content.Context
import android.content.Intent
import com.duckduckgo.app.di.AppCoroutineScope
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.feature.toggles.api.Toggle
import com.duckduckgo.networkprotection.impl.settings.NetPSettingsLocalConfig
import com.squareup.anvil.annotations.ContributesBinding
import dagger.SingleInstanceIn
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

interface VpnDisableOnCall {
fun enable()
fun disable()

suspend fun isEnabled(): Boolean
}

@ContributesBinding(AppScope::class)
@SingleInstanceIn(AppScope::class)
class RealVpnDisableOnCall @Inject constructor(
private val dispatcherProvider: DispatcherProvider,
@AppCoroutineScope private val appCoroutineScope: CoroutineScope,
private val context: Context,
private val netPSettingsLocalConfig: NetPSettingsLocalConfig,
) : VpnDisableOnCall {

override fun enable() {
appCoroutineScope.launch(dispatcherProvider.io()) {
netPSettingsLocalConfig.vpnPauseDuringCalls().setEnabled(Toggle.State(enable = true))
context.sendBroadcast(Intent(VpnCallStateReceiver.ACTION_REGISTER_STATE_CALL_LISTENER))
}
}

override fun disable() {
appCoroutineScope.launch(dispatcherProvider.io()) {
context.sendBroadcast(Intent(VpnCallStateReceiver.ACTION_UNREGISTER_STATE_CALL_LISTENER))
netPSettingsLocalConfig.vpnPauseDuringCalls().setEnabled(Toggle.State(enable = false))
}
}

override suspend fun isEnabled(): Boolean = withContext(dispatcherProvider.io()) {
return@withContext netPSettingsLocalConfig.vpnPauseDuringCalls().isEnabled()
}
}

0 comments on commit 7498b14

Please sign in to comment.