From 694c6ea853a6a5e86bb78d8d7aed708164f088c9 Mon Sep 17 00:00:00 2001 From: Callum Stott Date: Wed, 30 Oct 2024 14:43:43 +0000 Subject: [PATCH 1/6] Block settings that restart the app when in form entry --- .../formentry/FormEntryMenuProvider.kt | 5 +- .../screens/ProjectPreferencesActivity.kt | 11 +++ .../screens/ProjectPreferencesFragment.kt | 14 +++- .../UserInterfacePreferencesFragment.java | 84 +++++++++++-------- .../screens/ProjectPreferencesFragmentTest.kt | 8 +- 5 files changed, 84 insertions(+), 38 deletions(-) diff --git a/collect_app/src/main/java/org/odk/collect/android/formentry/FormEntryMenuProvider.kt b/collect_app/src/main/java/org/odk/collect/android/formentry/FormEntryMenuProvider.kt index 176364ba527..8f76e1971a6 100644 --- a/collect_app/src/main/java/org/odk/collect/android/formentry/FormEntryMenuProvider.kt +++ b/collect_app/src/main/java/org/odk/collect/android/formentry/FormEntryMenuProvider.kt @@ -110,8 +110,9 @@ class FormEntryMenuProvider( showIfNotShowing(RecordingWarningDialogFragment::class.java, activity.supportFragmentManager) } else { formEntryViewModel.updateAnswersForScreen(answersProvider.answers, false) - val pref = Intent(activity, ProjectPreferencesActivity::class.java) - activity.startActivityForResult(pref, ApplicationConstants.RequestCodes.CHANGE_SETTINGS) + val intent = Intent(activity, ProjectPreferencesActivity::class.java) + intent.putExtra(ProjectPreferencesActivity.EXTRA_IN_FORM_ENTRY, true) + activity.startActivityForResult(intent, ApplicationConstants.RequestCodes.CHANGE_SETTINGS) } true } diff --git a/collect_app/src/main/java/org/odk/collect/android/preferences/screens/ProjectPreferencesActivity.kt b/collect_app/src/main/java/org/odk/collect/android/preferences/screens/ProjectPreferencesActivity.kt index 1e0b6c32b2f..5229f9e3411 100644 --- a/collect_app/src/main/java/org/odk/collect/android/preferences/screens/ProjectPreferencesActivity.kt +++ b/collect_app/src/main/java/org/odk/collect/android/preferences/screens/ProjectPreferencesActivity.kt @@ -22,6 +22,7 @@ import org.odk.collect.android.fragments.dialogs.MovingBackwardsDialog.MovingBac import org.odk.collect.android.fragments.dialogs.ResetSettingsResultDialog.ResetSettingsResultDialogListener import org.odk.collect.android.injection.DaggerUtils import org.odk.collect.android.mainmenu.MainMenuActivity +import org.odk.collect.androidshared.ui.FragmentFactoryBuilder import org.odk.collect.metadata.PropertyManager import org.odk.collect.strings.localization.LocalizedActivity import javax.inject.Inject @@ -37,6 +38,12 @@ class ProjectPreferencesActivity : lateinit var propertyManager: PropertyManager override fun onCreate(savedInstanceState: Bundle?) { + supportFragmentManager.fragmentFactory = FragmentFactoryBuilder() + .forClass(ProjectPreferencesFragment::class.java) { + ProjectPreferencesFragment(intent.getBooleanExtra(EXTRA_IN_FORM_ENTRY, false)) + } + .build() + super.onCreate(savedInstanceState) setContentView(R.layout.activity_preferences_layout) DaggerUtils.getComponent(this).inject(this) @@ -67,4 +74,8 @@ class ProjectPreferencesActivity : } fun isInstanceStateSaved() = isInstanceStateSaved + + companion object { + const val EXTRA_IN_FORM_ENTRY = "in_form_entry" + } } diff --git a/collect_app/src/main/java/org/odk/collect/android/preferences/screens/ProjectPreferencesFragment.kt b/collect_app/src/main/java/org/odk/collect/android/preferences/screens/ProjectPreferencesFragment.kt index 72ec69896ff..8b1d83573fd 100644 --- a/collect_app/src/main/java/org/odk/collect/android/preferences/screens/ProjectPreferencesFragment.kt +++ b/collect_app/src/main/java/org/odk/collect/android/preferences/screens/ProjectPreferencesFragment.kt @@ -23,6 +23,7 @@ import android.view.MenuInflater import android.view.MenuItem import android.view.View import android.view.ViewGroup +import androidx.core.os.bundleOf import androidx.preference.Preference import org.odk.collect.android.R import org.odk.collect.android.injection.DaggerUtils @@ -33,7 +34,7 @@ import org.odk.collect.androidshared.data.Consumable import org.odk.collect.androidshared.ui.DialogFragmentUtils import org.odk.collect.androidshared.ui.multiclicksafe.MultiClickGuard -class ProjectPreferencesFragment : +class ProjectPreferencesFragment(private val inFormEntry: Boolean) : BaseProjectPreferencesFragment(), Preference.OnPreferenceClickListener { @@ -68,6 +69,10 @@ class ProjectPreferencesFragment : findPreference(CHANGE_ADMIN_PASSWORD_PREFERENCE_KEY)!!.onPreferenceClickListener = this findPreference(PROJECT_MANAGEMENT_PREFERENCE_KEY)!!.onPreferenceClickListener = this findPreference(ACCESS_CONTROL_PREFERENCE_KEY)!!.onPreferenceClickListener = this + + if (inFormEntry) { + findPreference(PROJECT_MANAGEMENT_PREFERENCE_KEY)!!.isEnabled = false + } } override fun onPreferenceClick(preference: Preference): Boolean { @@ -75,7 +80,12 @@ class ProjectPreferencesFragment : when (preference.key) { PROTOCOL_PREFERENCE_KEY -> displayPreferences(ServerPreferencesFragment()) PROJECT_DISPLAY_PREFERENCE_KEY -> displayPreferences(ProjectDisplayPreferencesFragment()) - USER_INTERFACE_PREFERENCE_KEY -> displayPreferences(UserInterfacePreferencesFragment()) + USER_INTERFACE_PREFERENCE_KEY -> { + val fragment = UserInterfacePreferencesFragment() + fragment.arguments = + bundleOf(UserInterfacePreferencesFragment.ARG_IN_FORM_ENTRY to inFormEntry) + displayPreferences(fragment) + } MAPS_PREFERENCE_KEY -> displayPreferences(MapsPreferencesFragment()) FORM_MANAGEMENT_PREFERENCE_KEY -> displayPreferences(FormManagementPreferencesFragment()) USER_AND_DEVICE_IDENTITY_PREFERENCE_KEY -> displayPreferences(IdentityPreferencesFragment()) diff --git a/collect_app/src/main/java/org/odk/collect/android/preferences/screens/UserInterfacePreferencesFragment.java b/collect_app/src/main/java/org/odk/collect/android/preferences/screens/UserInterfacePreferencesFragment.java index 617ab4f61a7..962a48cbbed 100644 --- a/collect_app/src/main/java/org/odk/collect/android/preferences/screens/UserInterfacePreferencesFragment.java +++ b/collect_app/src/main/java/org/odk/collect/android/preferences/screens/UserInterfacePreferencesFragment.java @@ -22,25 +22,35 @@ import android.content.Context; import android.os.Bundle; + import androidx.preference.ListPreference; + import org.odk.collect.android.R; import org.odk.collect.android.injection.DaggerUtils; import org.odk.collect.android.mainmenu.MainMenuActivity; import org.odk.collect.android.utilities.LocaleHelper; import org.odk.collect.android.version.VersionInformation; + import java.util.ArrayList; import java.util.TreeMap; + import javax.inject.Inject; public class UserInterfacePreferencesFragment extends BaseProjectPreferencesFragment { + public static final String ARG_IN_FORM_ENTRY = "in_form_entry"; + @Inject VersionInformation versionInformation; + private boolean inFormEntry; + @Override public void onAttach(Context context) { super.onAttach(context); DaggerUtils.getComponent(context).inject(this); + + inFormEntry = requireArguments().getBoolean(ARG_IN_FORM_ENTRY); } @Override @@ -58,16 +68,20 @@ private void initThemePrefs() { final ListPreference pref = findPreference(KEY_APP_THEME); if (pref != null) { - pref.setSummary(pref.getEntry()); - pref.setOnPreferenceChangeListener((preference, newValue) -> { - int index = ((ListPreference) preference).findIndexOfValue(newValue.toString()); - String entry = (String) ((ListPreference) preference).getEntries()[index]; - if (pref.getEntry() == null || !pref.getEntry().equals(entry)) { - preference.setSummary(entry); - startActivityAndCloseAllOthers(getActivity(), MainMenuActivity.class); - } - return true; - }); + if (!inFormEntry) { + pref.setSummary(pref.getEntry()); + pref.setOnPreferenceChangeListener((preference, newValue) -> { + int index = ((ListPreference) preference).findIndexOfValue(newValue.toString()); + String entry = (String) ((ListPreference) preference).getEntries()[index]; + if (pref.getEntry() == null || !pref.getEntry().equals(entry)) { + preference.setSummary(entry); + startActivityAndCloseAllOthers(getActivity(), MainMenuActivity.class); + } + return true; + }); + } else { + pref.setEnabled(false); + } } } @@ -103,31 +117,35 @@ private void initLanguagePrefs() { final ListPreference pref = findPreference(KEY_APP_LANGUAGE); if (pref != null) { - TreeMap languageList = LocaleHelper.languageList(); - ArrayList entryValues = new ArrayList<>(); - entryValues.add(0, ""); - entryValues.addAll(languageList.values()); - pref.setEntryValues(entryValues.toArray(new String[0])); - ArrayList entries = new ArrayList<>(); - entries.add(0, getActivity().getResources() - .getString(org.odk.collect.strings.R.string.use_device_language)); - entries.addAll(languageList.keySet()); - pref.setEntries(entries.toArray(new String[0])); - if (pref.getValue() == null) { - //set Default value to "Use phone locale" - pref.setValueIndex(0); - } - pref.setSummary(pref.getEntry()); - pref.setOnPreferenceChangeListener((preference, newValue) -> { - int index = ((ListPreference) preference).findIndexOfValue(newValue.toString()); - String entry = (String) ((ListPreference) preference).getEntries()[index]; - preference.setSummary(entry); + if (!inFormEntry) { + TreeMap languageList = LocaleHelper.languageList(); + ArrayList entryValues = new ArrayList<>(); + entryValues.add(0, ""); + entryValues.addAll(languageList.values()); + pref.setEntryValues(entryValues.toArray(new String[0])); + ArrayList entries = new ArrayList<>(); + entries.add(0, getActivity().getResources() + .getString(org.odk.collect.strings.R.string.use_device_language)); + entries.addAll(languageList.keySet()); + pref.setEntries(entries.toArray(new String[0])); + if (pref.getValue() == null) { + //set Default value to "Use phone locale" + pref.setValueIndex(0); + } + pref.setSummary(pref.getEntry()); + pref.setOnPreferenceChangeListener((preference, newValue) -> { + int index = ((ListPreference) preference).findIndexOfValue(newValue.toString()); + String entry = (String) ((ListPreference) preference).getEntries()[index]; + preference.setSummary(entry); - settingsProvider.getUnprotectedSettings().save(KEY_APP_LANGUAGE, newValue.toString()); + settingsProvider.getUnprotectedSettings().save(KEY_APP_LANGUAGE, newValue.toString()); - startActivityAndCloseAllOthers(getActivity(), MainMenuActivity.class); - return true; - }); + startActivityAndCloseAllOthers(getActivity(), MainMenuActivity.class); + return true; + }); + } else { + pref.setEnabled(false); + } } } } diff --git a/collect_app/src/test/java/org/odk/collect/android/preferences/screens/ProjectPreferencesFragmentTest.kt b/collect_app/src/test/java/org/odk/collect/android/preferences/screens/ProjectPreferencesFragmentTest.kt index 3012f7969ea..05a16f1cae0 100644 --- a/collect_app/src/test/java/org/odk/collect/android/preferences/screens/ProjectPreferencesFragmentTest.kt +++ b/collect_app/src/test/java/org/odk/collect/android/preferences/screens/ProjectPreferencesFragmentTest.kt @@ -16,6 +16,7 @@ import org.odk.collect.android.injection.config.AppDependencyModule import org.odk.collect.android.preferences.ProjectPreferencesViewModel import org.odk.collect.android.support.CollectHelpers import org.odk.collect.android.utilities.AdminPasswordProvider +import org.odk.collect.androidshared.ui.FragmentFactoryBuilder import org.odk.collect.fragmentstest.FragmentScenarioLauncherRule import org.odk.collect.settings.keys.ProtectedProjectKeys import org.odk.collect.shared.settings.Settings @@ -30,8 +31,13 @@ class ProjectPreferencesFragmentTest { } private val projectPreferencesViewModel = ProjectPreferencesViewModel(adminPasswordProvider) + private val fragmentFactory = FragmentFactoryBuilder() + .forClass(ProjectPreferencesFragment::class) { + ProjectPreferencesFragment(false) + }.build() + @get:Rule - val launcherRule = FragmentScenarioLauncherRule() + val launcherRule = FragmentScenarioLauncherRule(defaultFactory = fragmentFactory) @Before fun setup() { From c3a411362e076ea1800571c62d9b9528a0a680e0 Mon Sep 17 00:00:00 2001 From: Callum Stott Date: Wed, 30 Oct 2024 16:18:05 +0000 Subject: [PATCH 2/6] Add summary to mark settings that are disabled --- .../preferences/screens/ProjectPreferencesFragment.kt | 5 ++++- .../screens/UserInterfacePreferencesFragment.java | 2 ++ strings/src/main/res/values/strings.xml | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/collect_app/src/main/java/org/odk/collect/android/preferences/screens/ProjectPreferencesFragment.kt b/collect_app/src/main/java/org/odk/collect/android/preferences/screens/ProjectPreferencesFragment.kt index 8b1d83573fd..32b30c56c4b 100644 --- a/collect_app/src/main/java/org/odk/collect/android/preferences/screens/ProjectPreferencesFragment.kt +++ b/collect_app/src/main/java/org/odk/collect/android/preferences/screens/ProjectPreferencesFragment.kt @@ -71,7 +71,10 @@ class ProjectPreferencesFragment(private val inFormEntry: Boolean) : findPreference(ACCESS_CONTROL_PREFERENCE_KEY)!!.onPreferenceClickListener = this if (inFormEntry) { - findPreference(PROJECT_MANAGEMENT_PREFERENCE_KEY)!!.isEnabled = false + findPreference(PROJECT_MANAGEMENT_PREFERENCE_KEY)!!.also { + it.isEnabled = false + it.setSummary(org.odk.collect.strings.R.string.setting_not_available_during_form_entry) + } } } diff --git a/collect_app/src/main/java/org/odk/collect/android/preferences/screens/UserInterfacePreferencesFragment.java b/collect_app/src/main/java/org/odk/collect/android/preferences/screens/UserInterfacePreferencesFragment.java index 962a48cbbed..b8876bc501f 100644 --- a/collect_app/src/main/java/org/odk/collect/android/preferences/screens/UserInterfacePreferencesFragment.java +++ b/collect_app/src/main/java/org/odk/collect/android/preferences/screens/UserInterfacePreferencesFragment.java @@ -81,6 +81,7 @@ private void initThemePrefs() { }); } else { pref.setEnabled(false); + pref.setSummary(org.odk.collect.strings.R.string.setting_not_available_during_form_entry); } } } @@ -145,6 +146,7 @@ private void initLanguagePrefs() { }); } else { pref.setEnabled(false); + pref.setSummary(org.odk.collect.strings.R.string.setting_not_available_during_form_entry); } } } diff --git a/strings/src/main/res/values/strings.xml b/strings/src/main/res/values/strings.xml index 25754345d5e..8651e0bd545 100644 --- a/strings/src/main/res/values/strings.xml +++ b/strings/src/main/res/values/strings.xml @@ -949,6 +949,9 @@ Settings unlocked + + Setting not available during form entry +