-
Notifications
You must be signed in to change notification settings - Fork 871
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove listener when VPN is disabled
- Loading branch information
1 parent
d659f4d
commit 1379028
Showing
3 changed files
with
127 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
...ction-impl/src/main/java/com/duckduckgo/networkprotection/impl/snooze/VpnDisableOnCall.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* 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.telephony.PhoneStateListener | ||
import android.telephony.TelephonyManager | ||
import com.duckduckgo.app.di.AppCoroutineScope | ||
import com.duckduckgo.common.utils.DispatcherProvider | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.duckduckgo.mobile.android.vpn.Vpn | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import dagger.SingleInstanceIn | ||
import javax.inject.Inject | ||
import kotlin.properties.Delegates | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import logcat.LogPriority | ||
import logcat.asLog | ||
import logcat.logcat | ||
|
||
interface VpnDisableOnCall { | ||
fun enable() | ||
fun disable() | ||
fun isCallInProgress(): 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 vpn: Vpn, | ||
) : VpnDisableOnCall { | ||
private var _callInProgress: Boolean = false | ||
private val telephonyManager by lazy { | ||
context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager? | ||
} | ||
private val _listener: PhoneStateListener = | ||
object : PhoneStateListener() { | ||
@Deprecated("Deprecated in Java") | ||
override fun onCallStateChanged( | ||
state: Int, | ||
phoneNumber: String?, | ||
) { | ||
appCoroutineScope.launch(dispatcherProvider.io()) { | ||
logcat { "Call state: $state" } | ||
if (state == TelephonyManager.CALL_STATE_IDLE) { | ||
_callInProgress = false | ||
vpn.start() | ||
} else { | ||
_callInProgress = true | ||
vpn.stop() | ||
} | ||
} | ||
} | ||
} | ||
private var currentListener by Delegates.observable<PhoneStateListener?>(null) { _, old, new -> | ||
logcat { "CALL_STATE listener registered" } | ||
old?.let { | ||
telephonyManager?.listen(it, PhoneStateListener.LISTEN_NONE) | ||
} | ||
new?.let { | ||
telephonyManager?.listen(new, PhoneStateListener.LISTEN_CALL_STATE) | ||
} | ||
} | ||
|
||
override fun enable() { | ||
appCoroutineScope.launch(dispatcherProvider.io()) { | ||
_callInProgress = false | ||
registerListener() | ||
} | ||
} | ||
|
||
override fun disable() { | ||
appCoroutineScope.launch(dispatcherProvider.io()) { | ||
_callInProgress = false | ||
unregisterListener() | ||
} | ||
} | ||
|
||
override fun isCallInProgress(): Boolean = _callInProgress | ||
|
||
private fun registerListener() { | ||
runCatching { | ||
currentListener = _listener | ||
}.onFailure { t -> | ||
logcat(LogPriority.ERROR) { "CALL_STATE error registering: ${t.asLog()}" } | ||
} | ||
} | ||
|
||
private fun unregisterListener() { | ||
currentListener = null | ||
} | ||
} |