Skip to content

Commit

Permalink
Add for handling for new apps when auto exclude enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
karlenDimla committed Oct 2, 2024
1 parent ba5a112 commit efb282d
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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.autoexclude

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import com.duckduckgo.app.di.AppCoroutineScope
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.common.utils.extensions.registerExportedReceiver
import com.duckduckgo.di.scopes.VpnScope
import com.duckduckgo.mobile.android.vpn.service.VpnServiceCallbacks
import com.duckduckgo.mobile.android.vpn.state.VpnStateMonitor.VpnStopReason
import com.duckduckgo.networkprotection.api.NetworkProtectionState
import com.duckduckgo.networkprotection.impl.settings.NetPSettingsLocalConfig
import com.squareup.anvil.annotations.ContributesMultibinding
import dagger.SingleInstanceIn
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import logcat.logcat

@SingleInstanceIn(VpnScope::class)
@ContributesMultibinding(
scope = VpnScope::class,
boundType = VpnServiceCallbacks::class,
)
class AutoExcludeAppReceiver @Inject constructor(
private val networkProtectionState: NetworkProtectionState,
private val localConfig: NetPSettingsLocalConfig,
private val autoExcludeAppsRepository: AutoExcludeAppsRepository,
private val applicationContext: Context,
@AppCoroutineScope private var coroutineScope: CoroutineScope,
private val dispatcherProvider: DispatcherProvider,
) : BroadcastReceiver(), VpnServiceCallbacks {
override fun onReceive(
context: Context?,
intent: Intent?,
) {
when (intent?.action) {
Intent.ACTION_PACKAGE_ADDED -> intent.data?.schemeSpecificPart?.let {
coroutineScope.launch(dispatcherProvider.io()) {
restartVpn(it)
}
}
}
}

private suspend fun restartVpn(packageName: String) {
if (localConfig.autoExcludeBrokenApps().isEnabled()) {
logcat { "Auto exclude enabled, checking if $packageName is in auto exclude list" }
if (autoExcludeAppsRepository.isAppMarkedAsIncompatible(packageName)) {
logcat { "Newly installed package $packageName is in auto exclude list" }
networkProtectionState.restart()
} else {
logcat { "Newly installed package $packageName not in auto exclude list" }
}
}
}

override fun onVpnStarted(coroutineScope: CoroutineScope) {
logcat { "Auto exclude receiver started" }
kotlin.runCatching { applicationContext.unregisterReceiver(this) }

IntentFilter().apply {
addAction(Intent.ACTION_PACKAGE_ADDED)
addDataScheme("package")
}.run {
applicationContext.registerExportedReceiver(this@AutoExcludeAppReceiver, this)
}
}

override fun onVpnStopped(
coroutineScope: CoroutineScope,
vpnStopReason: VpnStopReason,
) {
logcat { "Auto exclude receiver stopped" }
kotlin.runCatching { applicationContext.unregisterReceiver(this) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.networkprotection.impl.VpnRemoteFeatures
import com.duckduckgo.networkprotection.impl.autoexclude.AutoExcludeAppsRepository
import com.duckduckgo.networkprotection.impl.autoexclude.VpnIncompatibleApp
import com.duckduckgo.networkprotection.impl.settings.NetPSettingsLocalConfig
Expand All @@ -41,7 +40,6 @@ class RealNetPExclusionListRepository @Inject constructor(
private val manualExclusionListRepository: NetPManualExclusionListRepository,
private val autoExcludeAppsRepository: AutoExcludeAppsRepository,
private val netPSettingsLocalConfig: NetPSettingsLocalConfig,
private val vpnRemoteFeatures: VpnRemoteFeatures,
private val dispatcherProvider: DispatcherProvider,
private val packageManager: PackageManager,
) : NetPExclusionListRepository {
Expand All @@ -63,7 +61,7 @@ class RealNetPExclusionListRepository @Inject constructor(
}

private fun isAutoExcludeEnabled(): Boolean {
return vpnRemoteFeatures.allowAutoExcludeBrokenApps().isEnabled() && netPSettingsLocalConfig.autoExcludeBrokenApps().isEnabled()
return netPSettingsLocalConfig.autoExcludeBrokenApps().isEnabled()
}

private fun isExcludedFromVpn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ class NetpAppExclusionListViewModel @Inject constructor(
netPManualExclusionListRepository.restoreDefaultProtectedList()
systemAppsExclusionRepository.restoreDefaults()
localConfig.autoExcludeBrokenApps().setRawStoredState(State(enable = false))
delay(100)
forceRestart = true
refreshSnapshot.refresh()
forceRefreshList.refresh()
Expand Down

0 comments on commit efb282d

Please sign in to comment.