[tor-commits] [snowflake-mobile/master] Added functionality to Settings

cohosh at torproject.org cohosh at torproject.org
Fri Jul 10 16:27:27 UTC 2020


commit 83bcdb97b462bdae7e55744d062d2a469ed73702
Author: Hashik Donthineni <HashikDonthineni at gmail.com>
Date:   Sun Jun 28 17:43:17 2020 +0530

    Added functionality to Settings
---
 .../torproject/snowflake/AppSettingsFragment.java  | 51 +++++++++++++++++++++-
 app/src/main/res/xml/app_settings.xml              | 11 +++--
 2 files changed, 57 insertions(+), 5 deletions(-)

diff --git a/app/src/main/java/org/torproject/snowflake/AppSettingsFragment.java b/app/src/main/java/org/torproject/snowflake/AppSettingsFragment.java
index 33bc456..bfc0631 100644
--- a/app/src/main/java/org/torproject/snowflake/AppSettingsFragment.java
+++ b/app/src/main/java/org/torproject/snowflake/AppSettingsFragment.java
@@ -1,10 +1,15 @@
 package org.torproject.snowflake;
 
+import android.content.SharedPreferences;
 import android.os.Bundle;
+import android.util.Log;
 
+import androidx.preference.Preference;
 import androidx.preference.PreferenceFragmentCompat;
 
-public class AppSettingsFragment extends PreferenceFragmentCompat {
+public class AppSettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener {
+
+    private static final String TAG = "AppSettingsFragment";
 
     public static AppSettingsFragment newInstance() {
         AppSettingsFragment fragment = new AppSettingsFragment();
@@ -14,5 +19,49 @@ public class AppSettingsFragment extends PreferenceFragmentCompat {
     @Override
     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
         setPreferencesFromResource(R.xml.app_settings, rootKey);
+        getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
+    }
+
+    @Override
+    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
+        Log.d(TAG, "onSharedPreferenceChanged: Key:" + key);
+        if (key.contains("switch")) {
+            //It's a switch
+            boolean val = sharedPreferences.getBoolean(key, false);
+            String edit_text = key.replace("switch", "edit_text");
+            Preference editTextPreference = findPreference(edit_text);
+            editTextPreference.setEnabled(val);
+
+            if (val) {
+                findPreference(key).setSummary(""); //Summary of switch is null when turned on.
+
+                String previousValue = sharedPreferences.getString(edit_text, "");
+                if (!previousValue.equals(""))
+                    editTextPreference.setSummary(previousValue); //When Switch is turned on set the summary to previously set Value.
+                else
+                    editTextPreference.setSummary("Using Default"); //If there is no previous value, then using null.
+            } else {
+                findPreference(key).setSummary("Using Default"); //Default is shown when switch is off.
+            }
+        } else {
+            //It's an Edit Text
+            String editValue = sharedPreferences.getString(key, "");
+            if (!editValue.equals(""))
+                findPreference(key).setSummary(editValue); //Setting Edit text to edited value
+            else
+                findPreference(key).setSummary("Using Default"); //Setting Edit text to Default because user left it empty.
+        }
+    }
+
+    @Override
+    public void onResume() {
+        super.onResume();
+        getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
+    }
+
+    @Override
+    public void onPause() {
+        super.onPause();
+        getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
     }
 }
diff --git a/app/src/main/res/xml/app_settings.xml b/app/src/main/res/xml/app_settings.xml
index ae85095..a8d08d8 100644
--- a/app/src/main/res/xml/app_settings.xml
+++ b/app/src/main/res/xml/app_settings.xml
@@ -7,6 +7,7 @@
         app:iconSpaceReserved="false">
         <SwitchPreference
             android:key="stun_switch"
+            android:summary="Using Default"
             android:title="Set STUN server"
             app:iconSpaceReserved="false" />
         <EditTextPreference
@@ -15,29 +16,31 @@
             android:selectAllOnFocus="true"
             android:singleLine="true"
             android:summary="Using Default"
-            android:title="Set STUN server"
+            android:title="Custom STUN server"
             app:iconSpaceReserved="false" />
 
         <SwitchPreference
             android:key="broker_switch"
+            android:summary="Using Default"
             android:title="Set broker URL"
             app:iconSpaceReserved="false" />
         <EditTextPreference
             android:enabled="false"
-            android:key="broker_switch_edit_text"
+            android:key="broker_edit_text"
             android:selectAllOnFocus="true"
             android:singleLine="true"
             android:summary="Using Default"
-            android:title="Set broker URL"
+            android:title="Custom broker URL"
             app:iconSpaceReserved="false" />
 
         <SwitchPreference
             android:key="relay_switch"
+            android:summary="Using Default"
             android:title="Set relay URL"
             app:iconSpaceReserved="false" />
         <EditTextPreference
             android:enabled="false"
-            android:key="relay_switch_edit_text"
+            android:key="relay_edit_text"
             android:selectAllOnFocus="true"
             android:singleLine="true"
             android:summary="Using Default"





More information about the tor-commits mailing list