Repository: openhab/openhab-android (language: kotlin)

Issue:
Preference fragments: "This method should only be accessed from tests or within private scope"
There are a lot of "This method should only be accessed from tests or within private scope" issues in the child classes of `PreferencesActivity.AbstractSettingsFragment`.


Discussion hints:
nan

Pull request: Move AbstractSettingsFragment to a new file
Fixes #3121

Signed-off-by: mueller-ma <mueller-ma@users.noreply.github.com>

Test command: ./gradlew :mobile:testFossBetaDebugUnitTest --tests "org.openhab.habdroid.util.PreferencesUtilTest"

Fix patch:
diff --git a/mobile/src/main/java/org/openhab/habdroid/ui/preference/PreferencesActivity.kt b/mobile/src/main/java/org/openhab/habdroid/ui/preference/PreferencesActivity.kt
index 088b508dc9..bc43d1dee8 100644
--- a/mobile/src/main/java/org/openhab/habdroid/ui/preference/PreferencesActivity.kt
+++ b/mobile/src/main/java/org/openhab/habdroid/ui/preference/PreferencesActivity.kt
@@ -22,24 +22,19 @@ import android.os.Bundle
 import android.service.quicksettings.TileService
 import android.view.MenuItem
 import androidx.annotation.DrawableRes
-import androidx.annotation.StringRes
-import androidx.annotation.VisibleForTesting
 import androidx.appcompat.app.AlertDialog
 import androidx.core.os.bundleOf
 import androidx.fragment.app.DialogFragment
 import androidx.fragment.app.FragmentManager
 import androidx.fragment.app.commit
-import androidx.preference.Preference
-import androidx.preference.PreferenceFragmentCompat
-import java.util.BitSet
 import org.openhab.habdroid.R
 import org.openhab.habdroid.background.tiles.AbstractTileService
 import org.openhab.habdroid.ui.AbstractBaseActivity
+import org.openhab.habdroid.ui.preference.fragments.AbstractSettingsFragment
 import org.openhab.habdroid.ui.preference.fragments.MainSettingsFragment
 import org.openhab.habdroid.ui.preference.fragments.TileOverviewFragment
 import org.openhab.habdroid.ui.preference.fragments.TileSettingsFragment
 import org.openhab.habdroid.ui.preference.fragments.WidgetSettingsFragment
-import org.openhab.habdroid.util.getSecretPrefs
 import org.openhab.habdroid.util.parcelable
 
 /**
@@ -125,64 +120,6 @@ class PreferencesActivity : AbstractBaseActivity() {
         }
     }
 
-    @VisibleForTesting
-    abstract class AbstractSettingsFragment : PreferenceFragmentCompat() {
-        @get:StringRes
-        protected abstract val titleResId: Int
-
-        protected val parentActivity get() = activity as PreferencesActivity
-        protected val prefs get() = preferenceScreen.sharedPreferences!!
-        protected val secretPrefs get() = requireContext().getSecretPrefs()
-
-        override fun onStart() {
-            super.onStart()
-            parentActivity.supportActionBar?.setTitle(titleResId)
-        }
-
-        override fun onDisplayPreferenceDialog(preference: Preference) {
-            val showDialog: (DialogFragment) -> Unit = { fragment ->
-                @Suppress("DEPRECATION") // TODO: Replace deprecated function
-                fragment.setTargetFragment(this, 0)
-                fragment.show(parentFragmentManager, "SettingsFragment.DIALOG:${preference.key}")
-            }
-            if (preference is CustomDialogPreference) {
-                showDialog(preference.createDialog())
-            } else {
-                super.onDisplayPreferenceDialog(preference)
-            }
-        }
-
-        companion object {
-            /**
-             * Password is considered strong when it is at least 8 chars long and contains 3 from those
-             * 4 categories:
-             * * lowercase
-             * * uppercase
-             * * numerics
-             * * other
-             * @param password
-             */
-            @VisibleForTesting
-            fun isWeakPassword(password: String?): Boolean {
-                if (password == null || password.length < 8) {
-                    return true
-                }
-                val groups = BitSet()
-                password.forEach { c ->
-                    groups.set(
-                        when {
-                            Character.isLetter(c) && Character.isLowerCase(c) -> 0
-                            Character.isLetter(c) && Character.isUpperCase(c) -> 1
-                            Character.isDigit(c) -> 2
-                            else -> 3
-                        }
-                    )
-                }
-                return groups.cardinality() < 3
-            }
-        }
-    }
-
     class ConfirmationDialogFragment : DialogFragment() {
         interface Callback {
             fun onConfirmed(tag: String?)
diff --git a/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/AbstractSettingsFragment.kt b/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/AbstractSettingsFragment.kt
new file mode 100644
index 0000000000..0a08c63e21
--- /dev/null
+++ b/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/AbstractSettingsFragment.kt
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2010-2022 Contributors to the openHAB project
+ *
+ * See the NOTICE file(s) distributed with this work for additional
+ * information.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ */
+
+package org.openhab.habdroid.ui.preference.fragments
+
+import androidx.annotation.StringRes
+import androidx.fragment.app.DialogFragment
+import androidx.preference.Preference
+import androidx.preference.PreferenceFragmentCompat
+import java.util.BitSet
+import org.openhab.habdroid.ui.preference.CustomDialogPreference
+import org.openhab.habdroid.ui.preference.PreferencesActivity
+import org.openhab.habdroid.util.getSecretPrefs
+
+abstract class AbstractSettingsFragment : PreferenceFragmentCompat() {
+    @get:StringRes
+    protected abstract val titleResId: Int
+
+    protected val parentActivity get() = activity as PreferencesActivity
+    protected val prefs get() = preferenceScreen.sharedPreferences!!
+    protected val secretPrefs get() = requireContext().getSecretPrefs()
+
+    override fun onStart() {
+        super.onStart()
+        parentActivity.supportActionBar?.setTitle(titleResId)
+    }
+
+    override fun onDisplayPreferenceDialog(preference: Preference) {
+        val showDialog: (DialogFragment) -> Unit = { fragment ->
+            @Suppress("DEPRECATION") // TODO: Replace deprecated function
+            fragment.setTargetFragment(this, 0)
+            fragment.show(parentFragmentManager, "SettingsFragment.DIALOG:${preference.key}")
+        }
+        if (preference is CustomDialogPreference) {
+            showDialog(preference.createDialog())
+        } else {
+            super.onDisplayPreferenceDialog(preference)
+        }
+    }
+
+    companion object {
+        /**
+         * Password is considered strong when it is at least 8 chars long and contains 3 from those
+         * 4 categories:
+         * * lowercase
+         * * uppercase
+         * * numerics
+         * * other
+         * @param password
+         */
+        fun isWeakPassword(password: String?): Boolean {
+            if (password == null || password.length < 8) {
+                return true
+            }
+            val groups = BitSet()
+            password.forEach { c ->
+                groups.set(
+                    when {
+                        Character.isLetter(c) && Character.isLowerCase(c) -> 0
+                        Character.isLetter(c) && Character.isUpperCase(c) -> 1
+                        Character.isDigit(c) -> 2
+                        else -> 3
+                    }
+                )
+            }
+            return groups.cardinality() < 3
+        }
+    }
+}
diff --git a/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/ConnectionSettingsFragment.kt b/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/ConnectionSettingsFragment.kt
index 73daba3a89..cc3913feb5 100644
--- a/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/ConnectionSettingsFragment.kt
+++ b/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/ConnectionSettingsFragment.kt
@@ -25,10 +25,9 @@ import androidx.preference.Preference
 import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
 import org.openhab.habdroid.R
 import org.openhab.habdroid.model.ServerPath
-import org.openhab.habdroid.ui.preference.PreferencesActivity
 import org.openhab.habdroid.util.parcelable
 
-class ConnectionSettingsFragment : PreferencesActivity.AbstractSettingsFragment() {
+class ConnectionSettingsFragment : AbstractSettingsFragment() {
     override val titleResId: Int @StringRes get() = requireArguments().getInt("title")
 
     private lateinit var urlPreference: EditTextPreference
diff --git a/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/DeviceControlFragment.kt b/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/DeviceControlFragment.kt
index 896bf4f4bc..eca2b2cdf2 100644
--- a/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/DeviceControlFragment.kt
+++ b/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/DeviceControlFragment.kt
@@ -18,11 +18,10 @@ import android.os.Bundle
 import androidx.annotation.RequiresApi
 import androidx.annotation.StringRes
 import org.openhab.habdroid.R
-import org.openhab.habdroid.ui.preference.PreferencesActivity
 import org.openhab.habdroid.util.PrefKeys
 
 @RequiresApi(Build.VERSION_CODES.R)
-class DeviceControlFragment : PreferencesActivity.AbstractSettingsFragment() {
+class DeviceControlFragment : AbstractSettingsFragment() {
     override val titleResId: Int @StringRes get() = R.string.device_control
 
     override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
diff --git a/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/DrawerEntriesMenuFragment.kt b/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/DrawerEntriesMenuFragment.kt
index 6839d6e6b3..096face4ed 100644
--- a/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/DrawerEntriesMenuFragment.kt
+++ b/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/DrawerEntriesMenuFragment.kt
@@ -22,7 +22,7 @@ import org.openhab.habdroid.util.PrefKeys
 import org.openhab.habdroid.util.Util
 import org.openhab.habdroid.util.getPreference
 
-class DrawerEntriesMenuFragment : PreferencesActivity.AbstractSettingsFragment() {
+class DrawerEntriesMenuFragment : AbstractSettingsFragment() {
     override val titleResId: Int @StringRes get() = R.string.drawer_entries
 
     override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
diff --git a/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/MainSettingsFragment.kt b/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/MainSettingsFragment.kt
index 814283a5c9..d7f6231646 100644
--- a/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/MainSettingsFragment.kt
+++ b/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/MainSettingsFragment.kt
@@ -65,10 +65,8 @@ import org.openhab.habdroid.util.isInstalled
 import org.openhab.habdroid.util.isTaskerPluginEnabled
 import org.openhab.habdroid.util.parcelable
 
-class MainSettingsFragment : PreferencesActivity.AbstractSettingsFragment(), ConnectionFactory.UpdateListener {
+class MainSettingsFragment : AbstractSettingsFragment(), ConnectionFactory.UpdateListener {
     override val titleResId: Int @StringRes get() = R.string.action_settings
-    @ColorInt
-    var previousColor: Int = 0
 
     private var notificationPollingPref: NotificationPollingPreference? = null
     private var notificationStatusHint: Preference? = null
diff --git a/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/SendDeviceInfoSettingsFragment.kt b/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/SendDeviceInfoSettingsFragment.kt
index 0827534aac..13a1e91bb5 100644
--- a/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/SendDeviceInfoSettingsFragment.kt
+++ b/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/SendDeviceInfoSettingsFragment.kt
@@ -29,7 +29,7 @@ import org.openhab.habdroid.util.getPrefixForBgTasks
 import org.openhab.habdroid.util.isInstalled
 
 class SendDeviceInfoSettingsFragment :
-    PreferencesActivity.AbstractSettingsFragment(),
+    AbstractSettingsFragment(),
     SharedPreferences.OnSharedPreferenceChangeListener {
     override val titleResId: Int @StringRes get() = R.string.send_device_info_to_server_short
     private lateinit var phoneStatePref: ItemUpdatingPreference
diff --git a/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/ServerEditorFragment.kt b/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/ServerEditorFragment.kt
index c2a819c471..3fc0a54e31 100644
--- a/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/ServerEditorFragment.kt
+++ b/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/ServerEditorFragment.kt
@@ -43,7 +43,7 @@ import org.openhab.habdroid.util.putPrimaryServerId
 import org.openhab.habdroid.util.updateDefaultSitemap
 
 class ServerEditorFragment :
-    PreferencesActivity.AbstractSettingsFragment(),
+    AbstractSettingsFragment(),
     PreferencesActivity.ConfirmationDialogFragment.Callback,
     PreferencesActivity.ConfirmLeaveDialogFragment.Callback {
     private lateinit var config: ServerConfiguration
diff --git a/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/TileOverviewFragment.kt b/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/TileOverviewFragment.kt
index 9d187882af..c05cca2e40 100644
--- a/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/TileOverviewFragment.kt
+++ b/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/TileOverviewFragment.kt
@@ -23,10 +23,9 @@ import androidx.preference.forEachIndexed
 import org.openhab.habdroid.R
 import org.openhab.habdroid.background.tiles.AbstractTileService
 import org.openhab.habdroid.background.tiles.getTileData
-import org.openhab.habdroid.ui.preference.PreferencesActivity
 
 @RequiresApi(Build.VERSION_CODES.N)
-class TileOverviewFragment : PreferencesActivity.AbstractSettingsFragment() {
+class TileOverviewFragment : AbstractSettingsFragment() {
     override val titleResId: Int @StringRes get() = R.string.tiles_for_quick_settings
 
     override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
diff --git a/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/TileSettingsFragment.kt b/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/TileSettingsFragment.kt
index 83b9e9e18d..467d6bb597 100644
--- a/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/TileSettingsFragment.kt
+++ b/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/TileSettingsFragment.kt
@@ -45,7 +45,7 @@ import org.openhab.habdroid.util.parcelableArrayList
 
 @RequiresApi(Build.VERSION_CODES.N)
 class TileSettingsFragment :
-    PreferencesActivity.AbstractSettingsFragment(),
+    AbstractSettingsFragment(),
     PreferencesActivity.ConfirmLeaveDialogFragment.Callback {
     override val titleResId: Int @StringRes get() = R.string.tile
     private var tileId = 0
diff --git a/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/WidgetSettingsFragment.kt b/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/WidgetSettingsFragment.kt
index e1b67d9556..a651191d21 100644
--- a/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/WidgetSettingsFragment.kt
+++ b/mobile/src/main/java/org/openhab/habdroid/ui/preference/fragments/WidgetSettingsFragment.kt
@@ -42,7 +42,7 @@ import org.openhab.habdroid.util.CacheManager
 import org.openhab.habdroid.util.PrefKeys
 
 class WidgetSettingsFragment :
-    PreferencesActivity.AbstractSettingsFragment(),
+    AbstractSettingsFragment(),
     PreferencesActivity.ConfirmLeaveDialogFragment.Callback {
     override val titleResId: Int @StringRes get() = R.string.item_update_widget
     private var widgetId: Int = AppWidgetManager.INVALID_APPWIDGET_ID


Test patch:
diff --git a/mobile/src/test/java/org/openhab/habdroid/util/PreferencesUtilTest.kt b/mobile/src/test/java/org/openhab/habdroid/util/PreferencesUtilTest.kt
index d23fcb4566..b48a03d9a8 100644
--- a/mobile/src/test/java/org/openhab/habdroid/util/PreferencesUtilTest.kt
+++ b/mobile/src/test/java/org/openhab/habdroid/util/PreferencesUtilTest.kt
@@ -18,7 +18,7 @@ import org.junit.Assert.assertFalse
 import org.junit.Assert.assertTrue
 import org.junit.Test
 import org.openhab.habdroid.model.toWifiSsids
-import org.openhab.habdroid.ui.preference.PreferencesActivity.AbstractSettingsFragment.Companion.isWeakPassword
+import org.openhab.habdroid.ui.preference.fragments.AbstractSettingsFragment.Companion.isWeakPassword
 import org.openhab.habdroid.ui.preference.fragments.ServerEditorFragment.Companion.beautifyUrl
 
 class PreferencesUtilTest {