summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/motd/MotdClient.java
blob: d80a899262148dcbaa5b36de856c2888f7f69719 (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
package se.leap.bitmaskclient.motd;

import androidx.annotation.WorkerThread;

import org.json.JSONException;
import org.json.JSONObject;

import de.blinkt.openvpn.core.VpnStatus;
import motd.IMessages;
import motd.IMotd;
import motd.Motd;
import se.leap.bitmaskclient.base.models.Provider;

public class MotdClient {
    IMotd motd;

    public MotdClient(Provider provider) {
        motd = Motd.newMotd(provider.getMotdUrl().toString(), provider.getName(), "android");
    }

    @WorkerThread
    public IMessages fetch() {
        if (!VpnStatus.isVPNActive()) {
            VpnStatus.logError("Tried to fetch Message of the Day while VPN was off.");
            return null;
        }

        return Motd.newMessages(motd.fetchLatestAsJson());
    }

    @WorkerThread
    public JSONObject fetchJson() {
        if (!VpnStatus.isVPNActive()) {
            VpnStatus.logError("Tried to fetch Message of the Day while VPN was off.");
            return null;
        }

        try {
            return new JSONObject(motd.fetchLatestAsJson());
        } catch (NullPointerException | JSONException e) {
            e.printStackTrace();
            return null;
        }

    }

}