summaryrefslogtreecommitdiff
path: root/app/java/src/SettingsActivity.java
blob: d04e2baa704db00762dffae33608f4669cdb5c86 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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", "")
                    );
                }
            }
        }
    }
}