Skip to content

Commit

Permalink
stop using default dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
cmonfortep committed Oct 25, 2023
1 parent 04fef84 commit 3768271
Show file tree
Hide file tree
Showing 81 changed files with 405 additions and 215 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ class AdClickModule {
@Provides
fun provideAdClickAttributionRepository(
database: AdClickDatabase,
@AppCoroutineScope coroutineScope: CoroutineScope,
@AppCoroutineScope appCoroutineScope: CoroutineScope,
dispatcherProvider: DispatcherProvider,
): AdClickAttributionRepository {
return RealAdClickAttributionRepository(database, coroutineScope, dispatcherProvider)
return RealAdClickAttributionRepository(database, appCoroutineScope, dispatcherProvider)
}

@SingleInstanceIn(AppScope::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class RealAppTrackingProtection @Inject constructor(
}

override fun restart() {
coroutineScope.launch {
coroutineScope.launch(dispatcherProvider.io()) {
vpnFeaturesRegistry.refreshFeature(AppTpVpnFeature.APPTP_VPN)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.duckduckgo.anvil.annotations.InjectWith
import com.duckduckgo.app.di.AppCoroutineScope
import com.duckduckgo.app.global.DuckDuckGoActivity
import com.duckduckgo.app.global.*
import com.duckduckgo.di.scopes.ActivityScope
import com.duckduckgo.mobile.android.ui.view.addClickableLink
import com.duckduckgo.mobile.android.ui.view.gone
Expand Down Expand Up @@ -63,6 +63,8 @@ class ManageRecentAppsProtectionActivity :
@AppCoroutineScope
lateinit var appCoroutineScope: CoroutineScope

@Inject lateinit var dispatcherProvider: DispatcherProvider

@Inject lateinit var vpnFeaturesRegistry: VpnFeaturesRegistry

@Inject lateinit var reportBreakageContract: Provider<ReportBreakageContract>
Expand Down Expand Up @@ -191,7 +193,7 @@ class ManageRecentAppsProtectionActivity :

private fun restartVpn() {
// we use the app coroutine scope to ensure this call outlives the Activity
appCoroutineScope.launch {
appCoroutineScope.launch(dispatcherProvider.io()) {
vpnFeaturesRegistry.refreshFeature(AppTpVpnFeature.APPTP_VPN)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.duckduckgo.anvil.annotations.InjectWith
import com.duckduckgo.app.di.AppCoroutineScope
import com.duckduckgo.app.global.DuckDuckGoActivity
import com.duckduckgo.app.global.*
import com.duckduckgo.browser.api.ui.WebViewActivityWithParams
import com.duckduckgo.di.scopes.ActivityScope
import com.duckduckgo.mobile.android.ui.menu.PopupMenu
Expand Down Expand Up @@ -82,6 +82,8 @@ class TrackingProtectionExclusionListActivity :
@Inject
lateinit var globalActivityStarter: GlobalActivityStarter

@Inject lateinit var dispatcherProvider: DispatcherProvider

private val binding: ActivityTrackingProtectionExclusionListBinding by viewBinding()

private val viewModel: ManageAppsProtectionViewModel by bindViewModel()
Expand Down Expand Up @@ -235,7 +237,7 @@ class TrackingProtectionExclusionListActivity :

private fun restartVpn() {
// we use the app coroutine scope to ensure this call outlives the Activity
appCoroutineScope.launch {
appCoroutineScope.launch(dispatcherProvider.io()) {
vpnFeaturesRegistry.refreshFeature(AppTpVpnFeature.APPTP_VPN)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class AppTpFeatureConfigImpl @Inject constructor(
@AppCoroutineScope private val coroutineScope: CoroutineScope,
private val appBuildConfig: AppBuildConfig,
vpnRemoteConfigDatabase: VpnRemoteConfigDatabase,
dispatcherProvider: DispatcherProvider,
private val dispatcherProvider: DispatcherProvider,
) : AppTpFeatureConfig, AppTpFeatureConfig.Editor {

private val togglesDao = vpnRemoteConfigDatabase.vpnConfigTogglesDao()
Expand Down Expand Up @@ -76,7 +76,7 @@ class AppTpFeatureConfigImpl @Inject constructor(
}

private fun persistToggle(toggle: VpnConfigToggle) {
coroutineScope.launch {
coroutineScope.launch(dispatcherProvider.io()) {
// Remote configs will not override any value that has isManualOverride = true
// But this is only for INTERNAL builds, because we have internal settings
// In any other build that is not internal isManualOverride is always false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.duckduckgo.mobile.android.vpn.feature.removal
import androidx.work.ExistingWorkPolicy.KEEP
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import com.duckduckgo.app.global.*
import com.duckduckgo.di.scopes.VpnScope
import com.duckduckgo.mobile.android.vpn.dao.VpnFeatureRemoverState
import com.duckduckgo.mobile.android.vpn.service.VpnServiceCallbacks
Expand All @@ -38,10 +39,11 @@ import logcat.logcat
class VpnFeatureRemoverStateListener @Inject constructor(
private val workManager: WorkManager,
private val vpnDatabase: VpnDatabase,
private val dispatcherProvider: DispatcherProvider,
) : VpnServiceCallbacks {

override fun onVpnStarted(coroutineScope: CoroutineScope) {
coroutineScope.launch() {
coroutineScope.launch(dispatcherProvider.io()) {
logcat { "FeatureRemoverVpnStateListener, new state ENABLED. Descheduling automatic feature removal" }
resetState()
}
Expand All @@ -60,7 +62,7 @@ class VpnFeatureRemoverStateListener @Inject constructor(
vpnStopReason: VpnStopReason,
) {
if (vpnStopReason == VpnStopReason.SELF_STOP) {
coroutineScope.launch() {
coroutineScope.launch(dispatcherProvider.io()) {
logcat { "FeatureRemoverVpnStateListener, new state DISABLED and it was MANUALLY. Scheduling automatic feature removal" }
scheduleFeatureRemoval()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.duckduckgo.mobile.android.vpn.feature.settings

import com.duckduckgo.app.di.AppCoroutineScope
import com.duckduckgo.app.global.DispatcherProvider
import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.mobile.android.vpn.AppTpVpnFeature
import com.duckduckgo.mobile.android.vpn.VpnFeaturesRegistry
Expand All @@ -42,6 +43,7 @@ class ExceptionListsSettingPlugin @Inject constructor(
private val vpnDatabase: VpnDatabase,
@AppCoroutineScope private val appCoroutineScope: CoroutineScope,
private val vpnFeaturesRegistry: VpnFeaturesRegistry,
private val dispatcherProvider: DispatcherProvider,
) : AppTpSettingPlugin {
private val jsonAdapter = Moshi.Builder().build().adapter(JsonConfigModel::class.java)

Expand All @@ -67,7 +69,7 @@ class ExceptionListsSettingPlugin @Inject constructor(
)

// Restart VPN now that the lists were updated
appCoroutineScope.launch {
appCoroutineScope.launch(dispatcherProvider.io()) {
vpnFeaturesRegistry.refreshFeature(AppTpVpnFeature.APPTP_VPN)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.duckduckgo.mobile.android.vpn.health

import android.content.Context
import android.os.PowerManager
import com.duckduckgo.app.global.DispatcherProvider
import com.duckduckgo.app.global.extensions.isAirplaneModeOn
import com.duckduckgo.app.global.plugins.PluginPoint
import com.duckduckgo.app.utils.ConflatedJob
Expand Down Expand Up @@ -51,12 +52,13 @@ class NetworkConnectivityHealthHandler @Inject constructor(
private val pixel: DeviceShieldPixels,
private val trackerBlockingVpnService: Provider<TrackerBlockingVpnService>,
private val vpnConnectivityLossListenerPluginPoint: PluginPoint<VpnConnectivityLossListenerPlugin>,
private val dispatcherProvider: DispatcherProvider,
) : VpnServiceCallbacks {
private val powerManager = context.applicationContext.getSystemService(Context.POWER_SERVICE) as PowerManager
private val job = ConflatedJob()

override fun onVpnStarted(coroutineScope: CoroutineScope) {
job += coroutineScope.launch {
job += coroutineScope.launch(dispatcherProvider.io()) {
while (isActive) {
delay(15_000)
if (powerManager.isInteractive && !context.isAirplaneModeOn() && !hasVpnConnectivity()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class VpnServiceHeartbeat @Inject constructor(

override fun onVpnStarted(coroutineScope: CoroutineScope) {
logcat { "onVpnStarted called" }
job += coroutineScope.launch {
job += coroutineScope.launch(dispatcherProvider.io()) {
while (true) {
storeHeartbeat(VpnServiceHeartbeatMonitor.DATA_HEART_BEAT_TYPE_ALIVE)
delay(TimeUnit.MINUTES.toMillis(HEART_BEAT_PERIOD_MINUTES))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class DeviceShieldTileService : TileService() {
@Inject lateinit var dispatcherProvider: DispatcherProvider

private var deviceShieldStatePollingJob = ConflatedJob()
private val serviceScope = CoroutineScope(Dispatchers.IO)
private val serviceScope = CoroutineScope(dispatcherProvider.io())

override fun onCreate() {
super.onCreate()
Expand Down Expand Up @@ -102,7 +102,7 @@ class DeviceShieldTileService : TileService() {

private fun pollDeviceShieldState() {
deviceShieldStatePollingJob +=
serviceScope.launch {
serviceScope.launch(dispatcherProvider.io()) {
while (isActive) {
val tile = qsTile
tile?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import com.duckduckgo.app.di.AppCoroutineScope
import com.duckduckgo.app.global.DispatcherProvider
import com.duckduckgo.appbuildconfig.api.AppBuildConfig
import com.duckduckgo.appbuildconfig.api.isInternalBuild
import com.duckduckgo.di.scopes.VpnScope
Expand All @@ -41,10 +42,11 @@ class RestartReceiver @Inject constructor(
@AppCoroutineScope private val coroutineScope: CoroutineScope,
private val context: Context,
private val appBuildConfig: AppBuildConfig,
private val dispatcherProvider: DispatcherProvider,
) : BroadcastReceiver(), VpnServiceCallbacks {
override fun onReceive(context: Context, intent: Intent) {
if (intent.getStringExtra("action")?.lowercase() == "restart") {
coroutineScope.launch {
coroutineScope.launch(dispatcherProvider.io()) {
TrackerBlockingVpnService.restartVpnService(context)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class AppTPReminderNotificationScheduler @Inject constructor(
private var isAppTPEnabled: AtomicReference<Boolean> = AtomicReference(false)

override fun onVpnStarted(coroutineScope: CoroutineScope) {
coroutineScope.launch {
coroutineScope.launch(dispatchers.io()) {
isAppTPEnabled.set(appTrackingProtection.isEnabled())
if (isAppTPEnabled.get()) {
// These are all relevant for when AppTP has been enabled.
Expand All @@ -77,7 +77,7 @@ class AppTPReminderNotificationScheduler @Inject constructor(
coroutineScope: CoroutineScope,
vpnStopReason: VpnStopReason,
) {
coroutineScope.launch {
coroutineScope.launch(dispatchers.io()) {
when (vpnStopReason) {
VpnStopReason.RESTART -> {} // no-op
VpnStopReason.SELF_STOP -> onVPNManuallyStopped()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ object DeviceShieldNotificationSchedulerModule {
@Provides
@IntoSet
fun provideDeviceShieldNotificationScheduler(
@AppCoroutineScope coroutineScope: CoroutineScope,
@AppCoroutineScope appCoroutineScope: CoroutineScope,
workManager: WorkManager,
vpnDatabase: VpnDatabase,
dispatchers: DispatcherProvider,
): MainProcessLifecycleObserver {
return DeviceShieldNotificationScheduler(coroutineScope, workManager, vpnDatabase, dispatchers)
return DeviceShieldNotificationScheduler(appCoroutineScope, workManager, vpnDatabase, dispatchers)
}

@Provides
Expand All @@ -72,7 +72,7 @@ class DeviceShieldNotificationScheduler(

private fun scheduleDailyNotification() {
val vpnNotificationsDao = vpnDatabase.vpnNotificationsDao()
coroutineScope.launch {
coroutineScope.launch(dispatchers.io()) {
val exists = withContext(dispatchers.io()) {
vpnNotificationsDao.exists(VPN_DAILY_NOTIFICATION_ID)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.Glide
import com.duckduckgo.anvil.annotations.InjectWith
import com.duckduckgo.app.di.AppCoroutineScope
import com.duckduckgo.app.global.DuckDuckGoActivity
import com.duckduckgo.app.global.*
import com.duckduckgo.app.global.extensions.safeGetApplicationIcon
import com.duckduckgo.browser.api.ui.WebViewActivityWithParams
import com.duckduckgo.di.scopes.ActivityScope
Expand Down Expand Up @@ -80,6 +80,8 @@ class AppTPCompanyTrackersActivity : DuckDuckGoActivity() {
@AppCoroutineScope
lateinit var appCoroutineScope: CoroutineScope

@Inject lateinit var dispatcherProvider: DispatcherProvider

@Inject
lateinit var globalActivityStarter: GlobalActivityStarter

Expand Down Expand Up @@ -225,7 +227,7 @@ class AppTPCompanyTrackersActivity : DuckDuckGoActivity() {

private fun restartVpn() {
// we use the app coroutine scope to ensure this call outlives the Activity
appCoroutineScope.launch {
appCoroutineScope.launch(dispatcherProvider.io()) {
vpnFeaturesRegistry.refreshFeature(AppTpVpnFeature.APPTP_VPN)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class AppTPCompanyTrackersViewModel @Inject constructor(
checked: Boolean,
packageName: String,
) {
viewModelScope.launch {
viewModelScope.launch(dispatchers.io()) {
withContext(dispatchers.io()) {
if (checked) {
deviceShieldPixels.didEnableAppProtectionFromDetail()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.duckduckgo.mobile.android.vpn.feature.settings

import com.duckduckgo.app.CoroutineTestRule
import com.duckduckgo.mobile.android.vpn.AppTpVpnFeature
import com.duckduckgo.mobile.android.vpn.VpnFeaturesRegistry
import com.duckduckgo.mobile.android.vpn.dao.VpnAppTrackerBlockingDao
Expand All @@ -26,11 +27,11 @@ import com.duckduckgo.mobile.android.vpn.trackers.AppTrackerExceptionRule
import com.duckduckgo.mobile.android.vpn.trackers.AppTrackerExcludedPackage
import com.duckduckgo.mobile.android.vpn.trackers.AppTrackerSystemAppOverridePackage
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import org.junit.Assert.*
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
Expand All @@ -40,6 +41,9 @@ import org.mockito.kotlin.whenever
@ExperimentalCoroutinesApi
class ExceptionListsSettingsPluginTest {

@get:Rule
var coroutineRule = CoroutineTestRule()

private lateinit var exceptionListsSettingPlugin: ExceptionListsSettingPlugin

private val mockVpnFeaturesRegistry: VpnFeaturesRegistry = mock()
Expand Down Expand Up @@ -79,7 +83,12 @@ class ExceptionListsSettingsPluginTest {

@Before
fun setup() {
exceptionListsSettingPlugin = ExceptionListsSettingPlugin(mockVpnDatabase, TestScope(), mockVpnFeaturesRegistry)
exceptionListsSettingPlugin = ExceptionListsSettingPlugin(
mockVpnDatabase,
coroutineRule.testScope,
mockVpnFeaturesRegistry,
coroutineRule.testDispatcherProvider,
)

whenever(mockVpnDatabase.vpnAppTrackerBlockingDao()).thenReturn(mockVpnAppTrackerBlockingDao)
whenever(mockVpnDatabase.vpnSystemAppsOverridesDao()).thenReturn(mockVpnAppTrackerSystemAppsOverridesDao)
Expand All @@ -99,7 +108,12 @@ class ExceptionListsSettingsPluginTest {

@Test
fun whenValidJSONUpdatesDB() = runTest {
exceptionListsSettingPlugin = ExceptionListsSettingPlugin(mockVpnDatabase, this, mockVpnFeaturesRegistry)
exceptionListsSettingPlugin = ExceptionListsSettingPlugin(
mockVpnDatabase,
coroutineRule.testScope,
mockVpnFeaturesRegistry,
coroutineRule.testDispatcherProvider,
)

val packageNames = listOf("com.subway.mobile.subwayapp03")
val trackerExceptionRules = ArrayList<AppTrackerExceptionRule>(1)
Expand Down
Loading

0 comments on commit 3768271

Please sign in to comment.