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

import android.os.ParcelFileDescriptor;

import android.util.Log;

import java.io.IOException;

public class DNSProxyConnection {

    public static String THREAD_NAME = "org.pihole.dnsproxy.service.thread";

    private DNSProxyService service;
    private Thread thread;
    private ParcelFileDescriptor networkInterface;

    public DNSProxyConnection(DNSProxyService service) {
        this.service = service;
    }

    public void start() {
        DNSProxyRunner runner = new DNSProxyRunner(this.service);
        runner.setOnEstablishListener(parcelFileDescriptor -> {
            this.networkInterface = parcelFileDescriptor;
        });

        this.thread = new Thread(runner, DNSProxyConnection.THREAD_NAME);
        this.thread.start();
    }

    public void stop() {
        try {
            this.thread.stop();
            this.networkInterface.close();
        } catch (IOException exception) {
            Log.e(DNSProxyService.LOG_TAG, "Connection failed", exception);
        }
    }
}