diff --git a/app/src/main/java/com/luca0n/joguitos/pluck/settings/SettingsFragment.java b/app/src/main/java/com/luca0n/joguitos/pluck/settings/SettingsFragment.java index ec15150..0d50ea0 100644 --- a/app/src/main/java/com/luca0n/joguitos/pluck/settings/SettingsFragment.java +++ b/app/src/main/java/com/luca0n/joguitos/pluck/settings/SettingsFragment.java @@ -28,6 +28,8 @@ import androidx.preference.PreferenceFragmentCompat; import androidx.annotation.Nullable; import androidx.preference.ListPreference; +import androidx.preference.CheckBoxPreference; +import androidx.preference.EditTextPreference; import androidx.preference.Preference; import com.luca0n.joguitos.pluck.R; @@ -44,12 +46,56 @@ public class SettingsFragment extends PreferenceFragmentCompat { addPreferencesFromResource(R.xml.preferences); // Listen for theme preference changes ListPreference preferenceTheme = (ListPreference) findPreference(getString(R.string.pref_ui_theme)); - preferenceTheme.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener(){ + CheckBoxPreference preferenceTor = (CheckBoxPreference) findPreference(getString(R.string.pref_network_tor)), + preferenceProxy = (CheckBoxPreference) findPreference(getString(R.string.pref_network_proxy)); + EditTextPreference preferenceProxyAddress = (EditTextPreference) + findPreference(getString(R.string.pref_network_proxy_address)); + + // A few preferences override others, so let's go ahead and disable some of them based on the user preferences. + // For more info, please check the comments on the OnPreferenceChangeListener below. + if (preferenceTor.isChecked()){ + preferenceProxy.setEnabled(false); + preferenceProxyAddress.setEnabled(false); + } else if (preferenceProxy.isChecked()){ + preferenceTor.setEnabled(false); + } + + Preference.OnPreferenceChangeListener listener = new Preference.OnPreferenceChangeListener(){ @Override public boolean onPreferenceChange(Preference preference, Object newValue){ - getActivity().recreate(); + // Because we are using this listener on multiple preferences, we need to check which one we are currently + // dealing with. + if (preference == preferenceTheme){ + // The user changed the app theme. In order to apply changes to this activity, we need to recreate it. + // This will cause the screen to go black for a brief moment, but will apply the selected theme. + getActivity().recreate(); + } else if (preference == preferenceTor){ + // Get the new value as a boolean since this is a CheckBoxPreference. + boolean newBoolean = (boolean) newValue; + // The user wants the app to use Tor. + // This option is not compatible with a custom proxy, so let's go ahead and disable custom proxy preferences. + preferenceProxy.setEnabled(!newBoolean); + preferenceProxyAddress.setEnabled(!newBoolean); + // Also uncheck the proxy preference in case it was previously checked. + if (newBoolean) + preferenceProxy.setChecked(false); + } else if (preference == preferenceProxy){ + // Get the new value as a boolean since this is a CheckBoxPreference. + boolean newBoolean = (boolean) newValue; + // The user wants the app to use a custom proxy. + // This option overrides the Tor preference, so let's go ahead and disable the Tor preference. + preferenceTor.setEnabled(!newBoolean); + // Also go ahead and uncheck the Tor preference in case it was previously checked. + if (newBoolean) + preferenceTor.setChecked(false); + } return true; } - }); + }; + + // Set the same listener for these preferences. + preferenceTor.setOnPreferenceChangeListener(listener); + preferenceProxy.setOnPreferenceChangeListener(listener); + preferenceTheme.setOnPreferenceChangeListener(listener); } } diff --git a/app/src/main/res/values/pref_keys.xml b/app/src/main/res/values/pref_keys.xml index 3e6969e..36195b7 100644 --- a/app/src/main/res/values/pref_keys.xml +++ b/app/src/main/res/values/pref_keys.xml @@ -26,6 +26,8 @@ Contact us at . pref_key_category_sound pref_sound_answer pref_network_tor + pref_network_proxy + pref_network_proxy_address pref_network_server https://opentdb.com/api.php pref_ui_theme diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 91cacb0..2c29282 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -114,6 +114,10 @@ Contact us at . Network Connect Using Tor Retrieves trivia data via the Tor network. Requires Orbot (recommended) or a Tor daemon with a SOCKS5 proxy listening on port 9050. + Use Proxy + Uses a proxy of your choice when connecting to a trivia server. + Proxy Address + Defines the proxy address that Pluck should use when connecting to a trivia server with a custom proxy. This will override the Tor proxy, if enabled. Pluck supports the HTTP and SOCKS protocols. Specify the proxy protocol. Server Address Specifies the server location used by the game to fetch questions when using a server as the trivia source. Leave this setting empty to use the default server. diff --git a/app/src/main/res/xml/preferences.xml b/app/src/main/res/xml/preferences.xml index 242f038..fdd8ae5 100644 --- a/app/src/main/res/xml/preferences.xml +++ b/app/src/main/res/xml/preferences.xml @@ -47,6 +47,15 @@ Contact us at . android:key="@string/pref_network_tor" android:title="@string/pref_network_tor_title" android:summary="@string/pref_network_tor_summary" /> + +