Commit 3b89410b authored by Ricki Hirner's avatar Ricki Hirner

WiFi restriction: allow multiple SSIDs (comma-separated)

parent f94d2f7a
Pipeline #10686749 passed with stage
in 3 minutes and 55 seconds
......@@ -31,6 +31,7 @@ import at.bitfire.ical4android.CalendarStorageException
import at.bitfire.vcard4android.ContactsStorageException
import at.bitfire.vcard4android.GroupMethod
import okhttp3.HttpUrl
import org.apache.commons.lang3.StringUtils
import java.util.*
import java.util.logging.Level
......@@ -46,8 +47,8 @@ class AccountSettings @Throws(InvalidAccountException::class) constructor(
val KEY_USERNAME = "user_name"
val KEY_WIFI_ONLY = "wifi_only" // sync on WiFi only (default: false)
val KEY_WIFI_ONLY_SSID = "wifi_only_ssid" // restrict sync to specific WiFi SSID
val KEY_WIFI_ONLY = "wifi_only" // sync on WiFi only (default: false)
val KEY_WIFI_ONLY_SSIDS = "wifi_only_ssids" // restrict sync to specific WiFi SSIDs
/** Time range limitation to the past [in days]
value = null default value (DEFAULT_TIME_RANGE_PAST_DAYS)
......@@ -133,8 +134,10 @@ class AccountSettings @Throws(InvalidAccountException::class) constructor(
fun setSyncWiFiOnly(wiFiOnly: Boolean) =
accountManager.setUserData(account, KEY_WIFI_ONLY, if (wiFiOnly) "1" else null)
fun getSyncWifiOnlySSID(): String? = accountManager.getUserData(account, KEY_WIFI_ONLY_SSID)
fun setSyncWifiOnlySSID(ssid: String?) = accountManager.setUserData(account, KEY_WIFI_ONLY_SSID, ssid)
fun getSyncWifiOnlySSIDs(): List<String>? =
accountManager.getUserData(account, KEY_WIFI_ONLY_SSIDS)?.split(',')
fun setSyncWifiOnlySSIDs(ssids: List<String>?) =
accountManager.setUserData(account, KEY_WIFI_ONLY_SSIDS, StringUtils.trimToNull(ssids?.joinToString(",")))
// CalDAV settings
......@@ -191,9 +194,18 @@ class AccountSettings @Throws(InvalidAccountException::class) constructor(
@Suppress("unused")
private fun update_6_7() {
val accountManager = AccountManager.get(context)
val accounts = accountManager.getAccountsByType(context.getString(R.string.account_type))
// add calendar colors
context.contentResolver.acquireContentProviderClient(CalendarContract.AUTHORITY)?.use { provider ->
for (account in accountManager.getAccountsByType(context.getString(R.string.account_type)))
AndroidCalendar.insertColors(provider, account)
accounts.forEach { AndroidCalendar.insertColors(provider, it) }
}
// update allowed WiFi settings key
for (account in accounts) {
val onlySSID = accountManager.getUserData(account, "wifi_only_ssid")
accountManager.setUserData(account, KEY_WIFI_ONLY_SSIDS, onlySSID)
accountManager.setUserData(account, "wifi_only_ssid", null)
}
}
......
......@@ -78,12 +78,11 @@ abstract class SyncAdapterService: Service() {
return false
}
settings.getSyncWifiOnlySSID()?.let { onlySSID ->
val quotedSSID = "\"$onlySSID\""
settings.getSyncWifiOnlySSIDs()?.let { onlySSIDs ->
val wifi = context.applicationContext.getSystemService(WIFI_SERVICE) as WifiManager
val info = wifi.connectionInfo
if (info == null || info.ssid != quotedSSID) {
Logger.log.info("Connected to wrong WiFi network (${info.ssid}, required: $quotedSSID), ignoring")
if (info == null || !onlySSIDs.contains(info.ssid.trim('"'))) {
Logger.log.info("Connected to wrong WiFi network (${info.ssid}), ignoring")
return false
}
}
......
......@@ -161,15 +161,15 @@ class AccountSettingsActivity: AppCompatActivity() {
false
}
val prefWifiOnlySSID = findPreference("sync_wifi_only_ssid") as EditTextPreference
val onlySSID = settings.getSyncWifiOnlySSID()
prefWifiOnlySSID.text = onlySSID
if (onlySSID != null)
prefWifiOnlySSID.summary = getString(R.string.settings_sync_wifi_only_ssid_on, onlySSID)
val prefWifiOnlySSIDs = findPreference("sync_wifi_only_ssids") as EditTextPreference
val onlySSIDs = settings.getSyncWifiOnlySSIDs()?.joinToString(", ")
prefWifiOnlySSIDs.text = onlySSIDs
if (onlySSIDs != null)
prefWifiOnlySSIDs.summary = getString(R.string.settings_sync_wifi_only_ssids_on, onlySSIDs)
else
prefWifiOnlySSID.setSummary(R.string.settings_sync_wifi_only_ssid_off)
prefWifiOnlySSID.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
settings.setSyncWifiOnlySSID(StringUtils.trimToNull(newValue as String))
prefWifiOnlySSIDs.setSummary(R.string.settings_sync_wifi_only_ssids_off)
prefWifiOnlySSIDs.onPreferenceChangeListener = Preference.OnPreferenceChangeListener { _, newValue ->
settings.setSyncWifiOnlySSIDs((newValue as String).split(',').map { StringUtils.trimToNull(it) }.filterNotNull().distinct())
loaderManager.restartLoader(0, arguments, this)
false
}
......
......@@ -226,8 +226,8 @@ class DebugInfoActivity: AppCompatActivity(), LoaderManager.LoaderCallbacks<Stri
" Calendar sync. interval: ${syncStatus(settings, CalendarContract.AUTHORITY)}\n" +
" OpenTasks sync. interval: ${syncStatus(settings, TaskProvider.ProviderName.OpenTasks.authority)}\n" +
" WiFi only: ").append(settings.getSyncWifiOnly())
settings.getSyncWifiOnlySSID()?.let {
report.append(", SSID: ${settings.getSyncWifiOnlySSID()}")
settings.getSyncWifiOnlySSIDs()?.let {
report.append(", SSIDs: ${settings.getSyncWifiOnlySSIDs()}")
}
report.append("\n [CardDAV] Contact group method: ${settings.getGroupMethod()}")
.append("\n [CalDAV] Time range (past days): ${settings.getTimeRangePastDays()}")
......
......@@ -194,10 +194,10 @@
<string name="settings_sync_wifi_only">Sync over WiFi only</string>
<string name="settings_sync_wifi_only_on">Synchronization is restricted to WiFi connections</string>
<string name="settings_sync_wifi_only_off">Connection type is not taken into consideration</string>
<string name="settings_sync_wifi_only_ssid">WiFi SSID restriction</string>
<string name="settings_sync_wifi_only_ssid_on">Will only synchronize over %s</string>
<string name="settings_sync_wifi_only_ssid_off">All WiFi connections may be used</string>
<string name="settings_sync_wifi_only_ssid_message">Enter the name of a WiFi network (SSID) to restrict synchronization to this network, or leave blank for all WiFi connections.</string>
<string name="settings_sync_wifi_only_ssids">WiFi SSID restriction</string>
<string name="settings_sync_wifi_only_ssids_on">Will only sync over %s</string>
<string name="settings_sync_wifi_only_ssids_off">All WiFi connections will be used</string>
<string name="settings_sync_wifi_only_ssids_message">Comma-separated names (SSIDs) of allowed WiFi networks (leave blank for all)</string>
<string name="settings_carddav">CardDAV</string>
<string name="settings_contact_group_method">Contact group method</string>
<string-array name="settings_contact_group_method_values">
......
......@@ -59,11 +59,11 @@
/>
<EditTextPreference
android:key="sync_wifi_only_ssid"
android:key="sync_wifi_only_ssids"
android:dependency="sync_wifi_only"
android:persistent="false"
android:title="@string/settings_sync_wifi_only_ssid"
android:dialogMessage="@string/settings_sync_wifi_only_ssid_message"/>
android:title="@string/settings_sync_wifi_only_ssids"
android:dialogMessage="@string/settings_sync_wifi_only_ssids_message"/>
</PreferenceCategory>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment