From 0eaf1a67aa4d5aea5b2901d1f2ec4ab302edb29b Mon Sep 17 00:00:00 2001 From: Albin Date: Wed, 28 Aug 2024 14:03:34 +0200 Subject: [PATCH] Cleanup --- .../mullvadvpn/service/MullvadVpnService.kt | 8 ++--- .../service/util/AssetToFilesDirExtractor.kt | 31 ------------------- .../service/util/ContextExtensions.kt | 23 ++++++++++++++ 3 files changed, 26 insertions(+), 36 deletions(-) delete mode 100644 android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/util/AssetToFilesDirExtractor.kt create mode 100644 android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/util/ContextExtensions.kt diff --git a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt index abee0d2c7532..cc8b7b2365e0 100644 --- a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt +++ b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/MullvadVpnService.kt @@ -31,7 +31,7 @@ import net.mullvad.mullvadvpn.service.migration.MigrateSplitTunneling import net.mullvad.mullvadvpn.service.notifications.ForegroundNotificationManager import net.mullvad.mullvadvpn.service.notifications.NotificationChannelFactory import net.mullvad.mullvadvpn.service.notifications.NotificationManager -import net.mullvad.mullvadvpn.service.util.AssetToFilesDirExtractor +import net.mullvad.mullvadvpn.service.util.extractAndOverwriteIfAssetMoreRecent import net.mullvad.talpid.TalpidVpnService import org.koin.android.ext.android.getKoin import org.koin.core.context.loadKoinModules @@ -231,10 +231,8 @@ class MullvadVpnService : TalpidVpnService() { } private fun Context.prepareFiles() { - AssetToFilesDirExtractor(this).apply { - extract(RELAY_LIST_ASSET_NAME, overwriteFileIfAssetMoreRecent = true) - extract(MAYBENOT_MACHINES_ASSET_NAME, overwriteFileIfAssetMoreRecent = true) - } + extractAndOverwriteIfAssetMoreRecent(RELAY_LIST_ASSET_NAME) + extractAndOverwriteIfAssetMoreRecent(MAYBENOT_MACHINES_ASSET_NAME) } companion object { diff --git a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/util/AssetToFilesDirExtractor.kt b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/util/AssetToFilesDirExtractor.kt deleted file mode 100644 index 038371522de5..000000000000 --- a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/util/AssetToFilesDirExtractor.kt +++ /dev/null @@ -1,31 +0,0 @@ -package net.mullvad.mullvadvpn.service.util - -import android.content.Context -import java.io.File -import java.io.FileOutputStream - -class AssetToFilesDirExtractor(val context: Context) { - fun extract(assetName: String, overwriteFileIfAssetMoreRecent: Boolean = false) { - val forceOverwrite = - if (overwriteFileIfAssetMoreRecent) { - context.lastUpdatedTime() > File(context.filesDir, assetName).lastModified() - } else false - - val destination = File(context.filesDir, assetName) - - if (!destination.exists() || forceOverwrite) { - extractFile(assetName, destination) - } - } - - private fun Context.lastUpdatedTime(): Long = - packageManager.getPackageInfo(packageName, 0).lastUpdateTime - - private fun extractFile(asset: String, destination: File) { - val destinationStream = FileOutputStream(destination) - - context.assets.open(asset).copyTo(destinationStream) - - destinationStream.close() - } -} diff --git a/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/util/ContextExtensions.kt b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/util/ContextExtensions.kt new file mode 100644 index 000000000000..51240fa16d62 --- /dev/null +++ b/android/service/src/main/kotlin/net/mullvad/mullvadvpn/service/util/ContextExtensions.kt @@ -0,0 +1,23 @@ +package net.mullvad.mullvadvpn.service.util + +import android.content.Context +import java.io.File +import java.io.FileOutputStream + +fun Context.extractAndOverwriteIfAssetMoreRecent(assetName: String) { + val forceOverwriteIfMoreRecent = lastUpdatedTime() > File(filesDir, assetName).lastModified() + val destination = File(filesDir, assetName) + + if (!destination.exists() || forceOverwriteIfMoreRecent) { + extractFile(assetName, destination) + } +} + +private fun Context.lastUpdatedTime(): Long = + packageManager.getPackageInfo(packageName, 0).lastUpdateTime + +private fun Context.extractFile(asset: String, destination: File) { + val destinationStream = FileOutputStream(destination) + assets.open(asset).copyTo(destinationStream) + destinationStream.close() +}