-
Notifications
You must be signed in to change notification settings - Fork 910
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move system app categories to remote config
- Loading branch information
1 parent
587ad2c
commit c6ae3d9
Showing
6 changed files
with
186 additions
and
30 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
75 changes: 75 additions & 0 deletions
75
...ection-impl/src/main/java/com/duckduckgo/networkprotection/impl/VpnRemoteSettingsStore.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,75 @@ | ||
/* | ||
* 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 | ||
|
||
import com.duckduckgo.app.di.AppCoroutineScope | ||
import com.duckduckgo.common.utils.DispatcherProvider | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.duckduckgo.feature.toggles.api.FeatureSettings | ||
import com.duckduckgo.feature.toggles.api.RemoteFeatureStoreNamed | ||
import com.duckduckgo.networkprotection.api.NetworkProtectionState | ||
import com.duckduckgo.networkprotection.store.db.CategorizedSystemApp | ||
import com.duckduckgo.networkprotection.store.db.CategorizedSystemAppsDao | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import com.squareup.moshi.Json | ||
import com.squareup.moshi.Moshi | ||
import dagger.SingleInstanceIn | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.launch | ||
import logcat.LogPriority | ||
import logcat.asLog | ||
import logcat.logcat | ||
|
||
@ContributesBinding(AppScope::class) | ||
@RemoteFeatureStoreNamed(VpnRemoteFeatures::class) | ||
@SingleInstanceIn(AppScope::class) | ||
class VpnRemoteSettingsStore @Inject constructor( | ||
@AppCoroutineScope private val coroutineScope: CoroutineScope, | ||
private val dispatcherProvider: DispatcherProvider, | ||
private val networkProtectionState: NetworkProtectionState, | ||
private val categorizedSystemAppsDao: CategorizedSystemAppsDao, | ||
) : FeatureSettings.Store { | ||
|
||
private val jsonAdapter = Moshi.Builder().build().adapter(SettingsModel::class.java) | ||
|
||
override fun store(jsonString: String) { | ||
logcat { "Received configuration: $jsonString" } | ||
|
||
runCatching { | ||
jsonAdapter.fromJson(jsonString)?.let { model -> | ||
model.systemAppCategories.also { | ||
if (it.isNotEmpty()) { | ||
categorizedSystemAppsDao.upsertSystemAppCategories(it) | ||
} | ||
} | ||
|
||
// Restart VPN now that the lists were updated | ||
coroutineScope.launch(dispatcherProvider.io()) { | ||
networkProtectionState.restart() | ||
} | ||
} | ||
}.onFailure { | ||
logcat(LogPriority.WARN) { it.asLog() } | ||
} | ||
} | ||
|
||
data class SettingsModel( | ||
@field:Json(name = "systemAppCategories") | ||
val systemAppCategories: List<CategorizedSystemApp>, | ||
) | ||
} |
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
66 changes: 66 additions & 0 deletions
66
...store/src/main/java/com/duckduckgo/networkprotection/store/db/CategorizedSystemAppsDao.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,66 @@ | ||
/* | ||
* 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.store.db | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Entity | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.PrimaryKey | ||
import androidx.room.Query | ||
import androidx.room.Transaction | ||
|
||
@Dao | ||
interface CategorizedSystemAppsDao { | ||
@Query("SELECT * from netp_system_apps_categories where category = 'COMMUNICATION'") | ||
fun getCommunicationSystemApps(): List<CategorizedSystemApp> | ||
|
||
@Query("SELECT * from netp_system_apps_categories where category = 'NETWORKING'") | ||
fun getNetworkingSystemApps(): List<CategorizedSystemApp> | ||
|
||
@Query("SELECT * from netp_system_apps_categories where category = 'MEDIA'") | ||
fun getMediaSystemApps(): List<CategorizedSystemApp> | ||
|
||
@Transaction | ||
fun upsertSystemAppCategories( | ||
systemAppCategories: List<CategorizedSystemApp>, | ||
) { | ||
deleteSystemAppCategories() | ||
insertSystemAppOverrides(systemAppCategories) | ||
} | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
fun insertSystemAppOverrides( | ||
systemAppCategories: List<CategorizedSystemApp>, | ||
) | ||
|
||
@Query("DELETE from netp_system_apps_categories") | ||
fun deleteSystemAppCategories() | ||
} | ||
|
||
@Entity(tableName = "netp_system_apps_categories") | ||
data class CategorizedSystemApp( | ||
@PrimaryKey val packageName: String, | ||
val category: SystemAppCategory, | ||
) | ||
|
||
enum class SystemAppCategory { | ||
COMMUNICATION, | ||
NETWORKING, | ||
MEDIA, | ||
OTHERS, | ||
} |
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