summaryrefslogtreecommitdiff
path: root/app/java/src/WifiListenerService.java
blob: 0920677a170254c3dcc1ce6170df21e2ea04906f (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package org.pihole.dnsproxy;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;

import android.location.LocationManager;

import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import android.os.IBinder;

import android.preference.PreferenceManager;

import android.util.Log;

public class WifiListenerService extends Service
{

    public static String NOTIFICATION_CHANNEL_ID = "org.pihole.dnsproxy.service.wifiListener.NOTIFICATION";
    public static String ACTION_START = "org.pihole.dnsproxy.service.wifiListener.START";
    public static String ACTION_STOP = "org.pihole.dnsproxy.service.wifiListener.STOP";
    public static String ACTION_STOP_SET_PREFERENCE = "org.pihole.dnsproxy.service.wifiListener.STOP_SET_PREFERENCE";

    private BroadcastReceiver receiver = new WifiListenerReceiver();

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // start the service
        if (intent.getAction().equals(WifiListenerService.ACTION_START)) {
            this.listen();

            return Service.START_STICKY;
        }

        // stop the service
        else if (intent.getAction().equals(WifiListenerService.ACTION_STOP)) {
            try {
                this.deafen();
            } catch (Exception exception) {}

            return Service.START_NOT_STICKY;
        }

        // stop and disable
        else if (intent.getAction().equals(WifiListenerService.ACTION_STOP_SET_PREFERENCE)) {
            WifiListenerService.disable(this);

            return Service.START_NOT_STICKY;
        }

        return Service.START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        this.deafen();

        super.onDestroy();
    }

    @Override
    public IBinder onBind(Intent intent) { return null; }

    /**
     * Start the service
     */
    public static void start(Context context) {
        context.startService((new Intent(context, WifiListenerService.class)).setAction(WifiListenerService.ACTION_START));
    }

    /**
     * Stop the service
     */
    public static void stop(Context context) {
        context.startService((new Intent(context, WifiListenerService.class)).setAction(WifiListenerService.ACTION_STOP));
    }

    /**
     * Start/Stop based on setting
     */
    public static void toggle(Context context) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

        if (sharedPreferences.getBoolean("use_wifi_listener", false)) {
            WifiListenerService.start(context);
        } else {
            WifiListenerService.stop(context);
        }
    }

    /**
     * Stop Wifi Listener and disable completely
     */
    public static void disable(Context context) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor sharedPreferencesEditor = sharedPreferences.edit();

        WifiListenerService.stop(context);
        sharedPreferencesEditor.putBoolean("use_wifi_listener", false);
        sharedPreferencesEditor.commit();

        WifiListenerService.OnActivationListener.disable(context);
    }

    /**
     * Get current Wifi SSID
     */
    public static String getWifiSSID(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        String ssid = wifiInfo.getSSID();

        // remove quotes around ssid
        ssid = ssid.substring(1, ssid.length() - 1);

        return ssid;
    }

    /**
     * Setup listener
     */
    private void listen() {
        registerReceiver(this.receiver, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION));

        this.startForeground();
    }

    /**
     * Stop listening
     */
    private void deafen() {
        unregisterReceiver(this.receiver);

        stopForeground(true);
    }

    /**
     * Start the Foreground notification process
     */
    private void startForeground() {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        NotificationChannel channel = new NotificationChannel(
            WifiListenerService.NOTIFICATION_CHANNEL_ID,
            WifiListenerService.NOTIFICATION_CHANNEL_ID,
            NotificationManager.IMPORTANCE_DEFAULT
        );
        manager.createNotificationChannel(channel);

        Notification notification = new Notification.Builder(this, WifiListenerService.NOTIFICATION_CHANNEL_ID)
            .setSmallIcon(R.drawable.logo)
            .setContentTitle(getString(R.string.app_label) + " - WiFi Listener")
            .setContentText(getString(R.string.wifi_listener_service__notification__text))
            .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, SettingsActivity.class), Intent.FLAG_ACTIVITY_NEW_TASK))
            .addAction(
                R.drawable.logo,
                getString(R.string.wifi_listener_service__notification__action__stop_listener),
                PendingIntent.getService(this, 0,
                    (new Intent(this, WifiListenerService.class)).setAction(WifiListenerService.ACTION_STOP_SET_PREFERENCE),
                    PendingIntent.FLAG_IMMUTABLE
                )
            )
            .addAction(
                R.drawable.logo,
                getString(R.string.wifi_listener_service__notification__action__start_proxy),
                PendingIntent.getService(this, 0,
                    (new Intent(this, DNSProxyService.class)).setAction(DNSProxyService.ACTION_START),
                    PendingIntent.FLAG_IMMUTABLE
                )
            )
            .build();

        startForeground(2, notification);
    }

    /**
     * SubClass to enable auto-activation
     */
    public static class OnActivationListener {

        /**
         * Disable auto-activation
         */
        public static void disable(Context context) {
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
            SharedPreferences.Editor sharedPreferencesEditor = sharedPreferences.edit();
            LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

            sharedPreferencesEditor.putBoolean("use_wifi_listener_for_activation", false);
            sharedPreferencesEditor.commit();

            if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                context.startActivity((new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
            }
        }

        /**
         * Check if location services are needed
         */
        public static boolean checkLocationNeeded(Context context) {
            SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

            if (!sharedPreferences.getBoolean("use_wifi_listener_for_activation", false)) {
                return false;
            }

            LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

            return !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        }

        /**
         * Ask User to enable location services if they are needed
         */
        public static void askLocationIfNeeded(Activity context) {
            if (checkLocationNeeded(context)) {
                context.requestPermissions(new String[]{
                    android.Manifest.permission.ACCESS_FINE_LOCATION,
                }, 3765);

                context.startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
            }
        }
    }
}