package org.pihole.dnsproxy; import android.app.Activity; import android.app.AlertDialog; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.net.VpnService; import android.net.wifi.WifiManager; import android.net.wifi.WifiInfo; import android.os.Bundle; import android.preference.MultiSelectListPreference; import android.preference.PreferenceActivity; import android.preference.PreferenceFragment; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.TextureView; import android.view.View; import android.widget.Button; import android.widget.Toast; import android.provider.Settings; import java.util.List; public class SettingsActivity extends PreferenceActivity { private MultiSelectListPreference knownSsids; @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("")) { WifiManager wifiManager = (WifiManager) context.getActivity().getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); String ssid = wifiInfo.getSSID(); // remove quotes around ssid ssid = ssid.substring(1, ssid.length() - 1); 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", "") ); } } } } }