summaryrefslogtreecommitdiff
path: root/app/java/src/WifiListenerService.java
blob: e25c3253c8043d73146a3ea8ca8457735d5f4d2f (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
package org.pihole.dnsproxy;

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.net.ConnectivityManager;
import android.net.wifi.WifiManager;

import android.os.IBinder;

import android.util.Log;

public class WifiListenerService extends Service
{

    public static String NOTIFICATION_CHANNEL_ID = "org.pihole.dnsproxy.service.wifiListener";

    private BroadcastReceiver receiver;

    @Override
    public void onCreate () {
        super.onCreate();

        this.receiver = new WifiListenerReceiver();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        this.start();

        return START_STICKY;
    }

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

        super.onDestroy();
    }

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

    public void start() {
        // registerReceiver(this.receiver, new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION));
        registerReceiver(this.receiver, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION));

        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("Pihole DNS Proxy - WiFi Listener")
            .setContentText("Listening for WiFi connection change")
            .build();

        startForeground(2, notification);
    }

    public void stop() {
        unregisterReceiver(this.receiver);
        stopForeground(true);
    }
}