Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 1809f4a1 authored by cketti's avatar cketti Committed by GitHub
Browse files

Merge pull request #5383 from k9mail/idle_refresh_interval

Don't allow an IDLE refresh interval of 1 minute
parents 5a10d08f 7580c04d
Loading
Loading
Loading
Loading
+45 −1
Original line number Diff line number Diff line
@@ -101,7 +101,8 @@ public class AccountSettingsDescriptions {
                new V(1, new EnumSetting<>(FolderMode.class, FolderMode.NOT_SECOND_CLASS))
        ));
        s.put("idleRefreshMinutes", Settings.versions(
                new V(1, new IntegerResourceSetting(24, R.array.idle_refresh_period_values))
                new V(1, new IntegerArraySetting(24, new int[] { 1, 2, 3, 6, 12, 24, 36, 48, 60 })),
                new V(74, new IntegerResourceSetting(24, R.array.idle_refresh_period_values))
        ));
        s.put("led", Settings.versions(
                new V(1, new BooleanSetting(true))
@@ -268,6 +269,7 @@ public class AccountSettingsDescriptions {
        Map<Integer, SettingsUpgrader> u = new HashMap<>();
        u.put(53, new SettingsUpgraderV53());
        u.put(54, new SettingsUpgraderV54());
        u.put(74, new SettingsUpgraderV74());

        UPGRADERS = Collections.unmodifiableMap(u);
    }
@@ -327,6 +329,31 @@ public class AccountSettingsDescriptions {
        }
    }

    private static class IntegerArraySetting extends SettingsDescription<Integer> {
        private final int[] values;

        IntegerArraySetting(int defaultValue, int[] values) {
            super(defaultValue);
            this.values = values;
        }

        @Override
        public Integer fromString(String value) throws InvalidSettingValueException {
            try {
                int number = Integer.parseInt(value);
                for (int validValue : values) {
                    if (number == validValue) {
                        return number;
                    }
                }

                throw new InvalidSettingValueException();
            } catch (NumberFormatException e) {
                throw new InvalidSettingValueException();
            }
        }
    }

    private static class StringResourceSetting extends PseudoEnumSetting<String> {
        private final Context context = DI.get(Context.class);
        private final Map<String, String> mapping;
@@ -468,4 +495,21 @@ public class AccountSettingsDescriptions {
            return null;
        }
    }

    /**
     * Upgrades settings from version 73 to 74
     *
     * Rewrites 'idleRefreshMinutes' from '1' to '2' if necessary
     */
    private static class SettingsUpgraderV74 implements SettingsUpgrader {
        @Override
        public Set<String> upgrade(Map<String, Object> settings) {
            Integer idleRefreshMinutes = (Integer) settings.get("idleRefreshMinutes");
            if (idleRefreshMinutes == 1) {
                settings.put("idleRefreshMinutes", 2);
            }

            return null;
        }
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ public class Settings {
     *
     * @see SettingsExporter
     */
    public static final int VERSION = 73;
    public static final int VERSION = 74;

    static Map<String, Object> validate(int version, Map<String, TreeMap<Integer, SettingsDescription>> settings,
            Map<String, String> importedSettings, boolean useDefaultValues) {
+0 −1
Original line number Diff line number Diff line
@@ -157,7 +157,6 @@
    </string-array>

    <string-array name="idle_refresh_period_values" translatable="false">
        <item>1</item>
        <item>2</item>
        <item>3</item>
        <item>6</item>
+1 −1
Original line number Diff line number Diff line
@@ -21,7 +21,7 @@ import timber.log.Timber;


public class K9StoragePersister implements StoragePersister {
    private static final int DB_VERSION = 14;
    private static final int DB_VERSION = 15;
    private static final String DB_NAME = "preferences_storage";

    private final Context context;
+36 −0
Original line number Diff line number Diff line
package com.fsck.k9.preferences.migrations

import android.database.sqlite.SQLiteDatabase

private const val DEFAULT_IDLE_REFRESH_MINUTES = 24
private const val MINIMUM_IDLE_REFRESH_MINUTES = 2

/**
 * Rewrite 'idleRefreshMinutes' to make sure the minimum value is 2 minutes.
 */
class StorageMigrationTo15(
    private val db: SQLiteDatabase,
    private val migrationsHelper: StorageMigrationsHelper
) {
    fun rewriteIdleRefreshInterval() {
        val accountUuidsListValue = migrationsHelper.readValue(db, "accountUuids")
        if (accountUuidsListValue == null || accountUuidsListValue.isEmpty()) {
            return
        }

        val accountUuids = accountUuidsListValue.split(",")
        for (accountUuid in accountUuids) {
            rewriteIdleRefreshInterval(accountUuid)
        }
    }

    private fun rewriteIdleRefreshInterval(accountUuid: String) {
        val idleRefreshMinutes = migrationsHelper.readValue(db, "$accountUuid.idleRefreshMinutes")?.toIntOrNull()
            ?: DEFAULT_IDLE_REFRESH_MINUTES

        val newIdleRefreshMinutes = idleRefreshMinutes.coerceAtLeast(MINIMUM_IDLE_REFRESH_MINUTES)
        if (newIdleRefreshMinutes != idleRefreshMinutes) {
            migrationsHelper.writeValue(db, "$accountUuid.idleRefreshMinutes", newIdleRefreshMinutes.toString())
        }
    }
}
Loading