summaryrefslogtreecommitdiff
path: root/app/java/src/SettingsActivity.java
blob: 0b3c7f2032c7cd6372fb4eaf127334972bd0f6f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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", "")
                    );
                }
            }
        }
    }
}