summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java
blob: ad68f96e4edef927d41e4f7eef15f4b428c1f669 (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
/**
 * Copyright (c) 2013 LEAP Encryption Access Project and contributers
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package se.leap.bitmaskclient.eip;

import android.content.*;

import java.util.*;

import de.blinkt.openvpn.core.*;

public class EipStatus extends Observable implements VpnStatus.StateListener {
    public static String TAG = EipStatus.class.getSimpleName();
    private static EipStatus current_status;

    private static VpnStatus.ConnectionStatus level = VpnStatus.ConnectionStatus.LEVEL_NOTCONNECTED;
    private static boolean wants_to_disconnect = false;

    private String state, log_message;
    private int localized_res_id;

    public static EipStatus getInstance() {
        if (current_status == null) {
            current_status = new EipStatus();
            VpnStatus.addStateListener(current_status);
        }
        return current_status;
    }

    private EipStatus() {
    }

    @Override
    public void updateState(final String state, final String logmessage, final int localizedResId, final VpnStatus.ConnectionStatus level) {
        updateStatus(state, logmessage, localizedResId, level);
        if (isConnected() || isDisconnected()) {
            setConnectedOrDisconnected();
        } else if (isConnecting())
            setConnecting();
    }

    private void updateStatus(final String state, final String logmessage, final int localizedResId, final VpnStatus.ConnectionStatus level) {
        current_status = getInstance();
        current_status.setState(state);
        current_status.setLogMessage(logmessage);
        current_status.setLocalizedResId(localizedResId);
        current_status.setLevel(level);
        current_status.setChanged();
    }

    public boolean wantsToDisconnect() {
        return wants_to_disconnect;
    }

    public boolean isConnecting() {
        return
                !isConnected() &&
                        !isDisconnected() &&
                        !isPaused();
    }

    public boolean isConnected() {
        return level == VpnStatus.ConnectionStatus.LEVEL_CONNECTED;
    }

    public boolean isDisconnected() {
        return level == VpnStatus.ConnectionStatus.LEVEL_NOTCONNECTED;
    }

    public boolean isPaused() {
        return level == VpnStatus.ConnectionStatus.LEVEL_VPNPAUSED;
    }

    public void setConnecting() {
        wants_to_disconnect = false;
        current_status.setChanged();
        current_status.notifyObservers();
    }

    public void setConnectedOrDisconnected() {
        wants_to_disconnect = false;
        current_status.setChanged();
        current_status.notifyObservers();
    }

    public void setDisconnecting() {
        wants_to_disconnect = false;
    }

    public String getState() {
        return state;
    }

    public String getLogMessage() {
        return log_message;
    }

    public int getLocalizedResId() {
        return localized_res_id;
    }

    public VpnStatus.ConnectionStatus getLevel() {
        return level;
    }

    private void setState(String state) {
        this.state = state;
    }

    private void setLogMessage(String log_message) {
        this.log_message = log_message;
    }

    private void setLocalizedResId(int localized_res_id) {
        this.localized_res_id = localized_res_id;
    }

    private void setLevel(VpnStatus.ConnectionStatus level) {
        EipStatus.level = level;
    }

    public boolean errorInLast(int lines, Context context) {
        boolean result = false;
        String[] error_keywords = {"error", "ERROR", "fatal", "FATAL"};

        VpnStatus.LogItem[] log = VpnStatus.getlogbuffer();
        String message = "";
        for (int i = 1; i <= lines && log.length > i; i++) {
            message = log[log.length - i].getString(context);
            for (int j = 0; j < error_keywords.length; j++)
                if (message.contains(error_keywords[j]))
                    result = true;
        }
        return result;
    }

    @Override
    public String toString() {
        return "State: " + state + " Level: " + level.toString();
    }

}