From 01dbcf5f2f9ff3153fd8286c6e5dcf4c8eb1a047 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Fri, 3 May 2024 15:54:04 -0300 Subject: [PATCH] Move StorageProperties class into its own file --- .../seedvault/plugins/StoragePluginManager.kt | 28 --------------- .../seedvault/plugins/StorageProperties.kt | 36 +++++++++++++++++++ 2 files changed, 36 insertions(+), 28 deletions(-) create mode 100644 app/src/main/java/com/stevesoltys/seedvault/plugins/StorageProperties.kt diff --git a/app/src/main/java/com/stevesoltys/seedvault/plugins/StoragePluginManager.kt b/app/src/main/java/com/stevesoltys/seedvault/plugins/StoragePluginManager.kt index df12c6e0..67c6d573 100644 --- a/app/src/main/java/com/stevesoltys/seedvault/plugins/StoragePluginManager.kt +++ b/app/src/main/java/com/stevesoltys/seedvault/plugins/StoragePluginManager.kt @@ -6,8 +6,6 @@ package com.stevesoltys.seedvault.plugins import android.content.Context -import android.net.ConnectivityManager -import android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET import androidx.annotation.WorkerThread import com.stevesoltys.seedvault.getStorageContext import com.stevesoltys.seedvault.permitDiskReads @@ -18,32 +16,6 @@ import com.stevesoltys.seedvault.plugins.webdav.WebDavFactory import com.stevesoltys.seedvault.settings.SettingsManager import com.stevesoltys.seedvault.settings.StoragePluginEnum -abstract class StorageProperties { - abstract val config: T - abstract val name: String - abstract val isUsb: Boolean - abstract val requiresNetwork: Boolean - - @WorkerThread - abstract fun isUnavailableUsb(context: Context): Boolean - - /** - * Returns true if this is storage that requires network access, - * but it isn't available right now. - */ - fun isUnavailableNetwork(context: Context, allowMetered: Boolean): Boolean { - return requiresNetwork && !hasUnmeteredInternet(context, allowMetered) - } - - private fun hasUnmeteredInternet(context: Context, allowMetered: Boolean): Boolean { - val cm = context.getSystemService(ConnectivityManager::class.java) ?: return false - val isMetered = cm.isActiveNetworkMetered - val capabilities = cm.getNetworkCapabilities(cm.activeNetwork) ?: return false - return capabilities.hasCapability(NET_CAPABILITY_INTERNET) && - (allowMetered || !isMetered) - } -} - class StoragePluginManager( private val context: Context, private val settingsManager: SettingsManager, diff --git a/app/src/main/java/com/stevesoltys/seedvault/plugins/StorageProperties.kt b/app/src/main/java/com/stevesoltys/seedvault/plugins/StorageProperties.kt new file mode 100644 index 00000000..eca9fb7f --- /dev/null +++ b/app/src/main/java/com/stevesoltys/seedvault/plugins/StorageProperties.kt @@ -0,0 +1,36 @@ +/* + * SPDX-FileCopyrightText: 2024 The Calyx Institute + * SPDX-License-Identifier: Apache-2.0 + */ + +package com.stevesoltys.seedvault.plugins + +import android.content.Context +import android.net.ConnectivityManager +import android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET +import androidx.annotation.WorkerThread + +abstract class StorageProperties { + abstract val config: T + abstract val name: String + abstract val isUsb: Boolean + abstract val requiresNetwork: Boolean + + @WorkerThread + abstract fun isUnavailableUsb(context: Context): Boolean + + /** + * Returns true if this is storage that requires network access, + * but it isn't available right now. + */ + fun isUnavailableNetwork(context: Context, allowMetered: Boolean): Boolean { + return requiresNetwork && !hasUnmeteredInternet(context, allowMetered) + } + + private fun hasUnmeteredInternet(context: Context, allowMetered: Boolean): Boolean { + val cm = context.getSystemService(ConnectivityManager::class.java) ?: return false + val isMetered = cm.isActiveNetworkMetered + val capabilities = cm.getNetworkCapabilities(cm.activeNetwork) ?: return false + return capabilities.hasCapability(NET_CAPABILITY_INTERNET) && (allowMetered || !isMetered) + } +}