package org.pihole.dnsproxy; import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.os.Bundle; import android.preference.PreferenceActivity; import android.preference.PreferenceFragment; import android.util.Log; public class SettingsActivity extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit(); } public static class SettingsFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.settings); } @Override public void onResume() { super.onResume(); getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); DNSServerAddress.toggleEnabled(this); DNSServerAddress.displayValue(this); WifiListener.SSID.displayValue(this); } @Override public void onPause() { super.onPause(); getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this); } @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { if (key.equals("use_automatic_dns_server_discovery")) { DNSServerAddress.toggleEnabled(this); } else if (key.equals("dns_server_address")) { DNSServerAddress.displayValue(this); } else if (key.equals("use_wifi_listener")) { WifiListener.toggle(this); } else if (key.equals("use_wifi_listener_for_activation")) { if (sharedPreferences.getBoolean("use_wifi_listener_for_activation", false)) { WifiListener.Activation.enable(this); } } else if (key.equals("wifi_listener_ssid")) { WifiListener.SSID.displayValue(this); } } static class DNSServerAddress { public static void toggleEnabled(PreferenceFragment context) { context.findPreference("dns_server_address").setEnabled( !context.getPreferenceScreen().getSharedPreferences().getBoolean("use_automatic_dns_server_discovery", true) ); } public static void displayValue(PreferenceFragment context) { context.findPreference("dns_server_address").setSummary( context.getPreferenceScreen().getSharedPreferences().getString("dns_server_address", "") ); } } static class WifiListener { public static void toggle(PreferenceFragment context) { SharedPreferences sharedPreferences = context.getPreferenceScreen().getSharedPreferences(); if (sharedPreferences.getBoolean("use_wifi_listener", false)) { WifiListenerService.start(context.getActivity()); } else { disable(context); } } public static void disable(PreferenceFragment context) { SharedPreferences sharedPreferences = context.getPreferenceScreen().getSharedPreferences(); WifiListenerService.disable(context.getActivity()); } static class Activation { public static void enable(PreferenceFragment context) { SharedPreferences sharedPreferences = context.getPreferenceScreen().getSharedPreferences(); WifiListenerService.OnActivationListener.askLocationIfNeeded(context.getActivity()); if (sharedPreferences.getString("wifi_listener_ssid", "").equals("")) { String ssid = WifiListenerService.getWifiSSID(context.getActivity()); if (!ssid.equals("unknown ssid")) { SharedPreferences.Editor sharedPreferencesEditor = sharedPreferences.edit(); sharedPreferencesEditor.putString("wifi_listener_ssid", ssid); sharedPreferencesEditor.commit(); } } } } static class SSID { public static void displayValue(PreferenceFragment context) { context.findPreference("wifi_listener_ssid").setSummary( context.getPreferenceScreen().getSharedPreferences().getString("wifi_listener_ssid", "") ); } } } } }