Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimise use of default() dispatcher #3747

Merged
merged 4 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -26,6 +26,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.DispatcherProvider
import com.duckduckgo.app.global.DuckDuckGoActivity
import com.duckduckgo.app.global.extensions.launchAlwaysOnSystemSettings
import com.duckduckgo.app.global.extensions.launchIgnoreBatteryOptimizationSettings
Expand Down Expand Up @@ -68,6 +69,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 @@ -228,7 +231,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 @@ -30,7 +30,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.R as commonR
Expand Down Expand Up @@ -86,6 +86,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 @@ -244,7 +246,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 @@ -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.feature.toggles.api.FeatureSettings
import com.duckduckgo.feature.toggles.api.RemoteFeatureStoreNamed
Expand All @@ -42,6 +43,7 @@ class ExceptionListsSettingStore @Inject constructor(
private val vpnDatabase: VpnDatabase,
@AppCoroutineScope private val appCoroutineScope: CoroutineScope,
private val vpnFeaturesRegistry: VpnFeaturesRegistry,
private val dispatcherProvider: DispatcherProvider,
) : FeatureSettings.Store {
private val jsonAdapter = Moshi.Builder().build().adapter(JsonConfigModel::class.java)

Expand All @@ -66,7 +68,7 @@ class ExceptionListsSettingStore @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 @@ -66,7 +66,7 @@ class AppTPCompanyTrackersViewModel @Inject constructor(
statsRepository
.getTrackersForAppFromDate(date, packageName)
.map { aggregateDataPerApp(it, packageName) }
.flowOn(dispatchers.default())
.flowOn(dispatchers.io())
.collectLatest { state ->
viewStateFlow.emit(state)
}
Expand Down 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 @@ -120,7 +120,7 @@ class DeviceShieldActivityFeedViewModel @Inject constructor(
}
TrackerFeedViewState(trackers + appDataItems, trackerIntermediateState.runningState)
}
.flowOn(dispatcherProvider.default())
.flowOn(dispatcherProvider.io())
.onStart {
startTickerRefresher()
emit(TrackerFeedViewState(listOf(TrackerLoadingSkeleton), VpnState(DISABLED, ERROR)))
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 @@ -25,10 +26,10 @@ 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.Before
import org.junit.Rule
import org.junit.Test
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
Expand All @@ -38,6 +39,9 @@ import org.mockito.kotlin.whenever
@ExperimentalCoroutinesApi
class ExceptionListsSettingStoreTest {

@get:Rule
var coroutineRule = CoroutineTestRule()

private lateinit var exceptionListsSettingStore: ExceptionListsSettingStore

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

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

whenever(mockVpnDatabase.vpnAppTrackerBlockingDao()).thenReturn(mockVpnAppTrackerBlockingDao)
whenever(mockVpnDatabase.vpnSystemAppsOverridesDao()).thenReturn(mockVpnAppTrackerSystemAppsOverridesDao)
Expand All @@ -93,7 +102,12 @@ class ExceptionListsSettingStoreTest {

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

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