-
Notifications
You must be signed in to change notification settings - Fork 928
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Traffic Quality Feature Flag (#5328)
Task/Issue URL: https://app.asana.com/0/1174433894299346/1208848089144698/f ### Description Implement the header request that sends one enabled feature ### Steps to test this PR See Test scenarios section in the description of the Asana task -> https://app.asana.com/0/1174433894299346/1208865134645715/f
- Loading branch information
Showing
10 changed files
with
518 additions
and
16 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
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
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
127 changes: 127 additions & 0 deletions
127
...in/java/com/duckduckgo/app/browser/trafficquality/remote/AndroidFeaturesHeaderProvider.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,127 @@ | ||
/* | ||
* 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.app.browser.trafficquality.remote | ||
|
||
import com.duckduckgo.appbuildconfig.api.AppBuildConfig | ||
import com.duckduckgo.autoconsent.api.Autoconsent | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.duckduckgo.mobile.android.app.tracking.AppTrackingProtection | ||
import com.duckduckgo.networkprotection.api.NetworkProtectionState | ||
import com.duckduckgo.privacy.config.api.Gpc | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import java.time.LocalDateTime | ||
import java.time.ZoneOffset | ||
import java.time.temporal.ChronoUnit | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.runBlocking | ||
|
||
interface AndroidFeaturesHeaderProvider { | ||
fun provide(): String? | ||
} | ||
|
||
@ContributesBinding(AppScope::class) | ||
class RealAndroidFeaturesHeaderProvider @Inject constructor( | ||
private val appBuildConfig: AppBuildConfig, | ||
private val featuresRequestHeaderStore: FeaturesRequestHeaderStore, | ||
private val autoconsent: Autoconsent, | ||
private val gpc: Gpc, | ||
private val appTrackingProtection: AppTrackingProtection, | ||
private val networkProtectionState: NetworkProtectionState, | ||
) : AndroidFeaturesHeaderProvider { | ||
|
||
override fun provide(): String? { | ||
val config = featuresRequestHeaderStore.getConfig() | ||
val versionConfig = config.find { it.appVersion == appBuildConfig.versionCode } | ||
return if (versionConfig != null && shouldLogValue(versionConfig)) { | ||
logFeature(versionConfig) | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
private fun shouldLogValue(versionConfig: TrafficQualityAppVersion): Boolean { | ||
val appBuildDateMillis = appBuildConfig.buildDateTimeMillis | ||
if (appBuildDateMillis == 0L) { | ||
return false | ||
} | ||
|
||
val appBuildDate = LocalDateTime.ofEpochSecond(appBuildDateMillis / 1000, 0, ZoneOffset.UTC) | ||
val now = LocalDateTime.now(ZoneOffset.UTC) | ||
|
||
val daysSinceBuild = ChronoUnit.DAYS.between(appBuildDate, now) | ||
val daysUntilLoggingStarts = versionConfig.daysUntilLoggingStarts | ||
val daysForAppVersionLogging = versionConfig.daysUntilLoggingStarts + versionConfig.daysLogging | ||
|
||
return daysSinceBuild in daysUntilLoggingStarts..daysForAppVersionLogging | ||
} | ||
|
||
private fun logFeature(versionConfig: TrafficQualityAppVersion): String? { | ||
val listOfFeatures = mutableListOf<String>() | ||
if (versionConfig.featuresLogged.cpm) { | ||
listOfFeatures.add(CPM_HEADER) | ||
} | ||
if (versionConfig.featuresLogged.gpc) { | ||
listOfFeatures.add(GPC_HEADER) | ||
} | ||
|
||
if (versionConfig.featuresLogged.appTP) { | ||
listOfFeatures.add(APP_TP_HEADER) | ||
} | ||
|
||
if (versionConfig.featuresLogged.netP) { | ||
listOfFeatures.add(NET_P_HEADER) | ||
} | ||
|
||
return if (listOfFeatures.isEmpty()) { | ||
null | ||
} else { | ||
val randomIndex = (0..<listOfFeatures.size).random() | ||
listOfFeatures[randomIndex].plus("=").plus(mapFeature(listOfFeatures[randomIndex])) | ||
} | ||
} | ||
|
||
private fun mapFeature(feature: String): String? { | ||
return runBlocking { | ||
when (feature) { | ||
CPM_HEADER -> { | ||
autoconsent.isAutoconsentEnabled().toString() | ||
} | ||
|
||
GPC_HEADER -> { | ||
gpc.isEnabled().toString() | ||
} | ||
|
||
APP_TP_HEADER -> { | ||
appTrackingProtection.isEnabled().toString() | ||
} | ||
|
||
NET_P_HEADER -> { | ||
networkProtectionState.isEnabled().toString() | ||
} | ||
|
||
else -> null | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
private const val CPM_HEADER = "cpm_enabled" | ||
private const val GPC_HEADER = "gpc_enabled" | ||
private const val APP_TP_HEADER = "atp_enabled" | ||
private const val NET_P_HEADER = "vpn_enabled" | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...ava/com/duckduckgo/app/browser/trafficquality/remote/FeaturesRequestHeaderSettingStore.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,67 @@ | ||
/* | ||
* 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.app.browser.trafficquality.remote | ||
|
||
import com.duckduckgo.app.pixels.remoteconfig.AndroidBrowserConfigFeature | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import com.squareup.moshi.JsonAdapter | ||
import com.squareup.moshi.Moshi | ||
import javax.inject.Inject | ||
|
||
interface FeaturesRequestHeaderStore { | ||
fun getConfig(): List<TrafficQualityAppVersion> | ||
} | ||
|
||
data class TrafficQualitySettingsJson( | ||
val versions: List<TrafficQualityAppVersion>, | ||
) | ||
|
||
data class TrafficQualityAppVersion( | ||
val appVersion: Int, | ||
val daysUntilLoggingStarts: Int, | ||
val daysLogging: Int, | ||
val featuresLogged: TrafficQualityAppVersionFeatures, | ||
) | ||
|
||
data class TrafficQualityAppVersionFeatures( | ||
val gpc: Boolean, | ||
val cpm: Boolean, | ||
val appTP: Boolean, | ||
val netP: Boolean, | ||
) | ||
|
||
@ContributesBinding(AppScope::class) | ||
class FeaturesRequestHeaderSettingStore @Inject constructor( | ||
private val androidBrowserConfigFeature: AndroidBrowserConfigFeature, | ||
private val moshi: Moshi, | ||
) : FeaturesRequestHeaderStore { | ||
|
||
private val jsonAdapter: JsonAdapter<TrafficQualitySettingsJson> by lazy { | ||
moshi.adapter(TrafficQualitySettingsJson::class.java) | ||
} | ||
|
||
override fun getConfig(): List<TrafficQualityAppVersion> { | ||
val config = androidBrowserConfigFeature.featuresRequestHeader().getSettings()?.let { | ||
runCatching { | ||
val configJson = jsonAdapter.fromJson(it) | ||
configJson?.versions | ||
}.getOrDefault(emptyList()) | ||
} ?: emptyList() | ||
return config | ||
} | ||
} |
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
Oops, something went wrong.