summaryrefslogtreecommitdiff
path: root/src/de/blinkt/openvpn/OpenVPN.java
blob: db9802007cc14d10e05177efe6b48164e7961de9 (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
package de.blinkt.openvpn;

import java.util.LinkedList;
import java.util.Vector;

import android.content.Context;
import android.os.Build;

public class OpenVPN {
	

	public static LinkedList<LogItem> logbuffer;
	
	private static Vector<LogListener> logListener;
	private static Vector<StateListener> stateListener;
	private static String[] mBconfig;
	
	static {
		logbuffer  = new LinkedList<LogItem>();
		logListener = new Vector<OpenVPN.LogListener>();
		stateListener = new Vector<OpenVPN.StateListener>();
		logInformation();
	}
	
	static class LogItem {
		public static final int ERROR = 1;
		public static final int INFO = 2;
		public static final int VERBOSE = 3;

		private Object [] mArgs = null;
		private String mMessage = null;
		private int mRessourceId;
		// Default log priority
		int mLevel = INFO;
		
		public LogItem(int ressourceId, Object[] args) {
		 mRessourceId = ressourceId;
		 mArgs = args;
		}

		
		public LogItem(int loglevel,int ressourceId, Object[] args) {
			 mRessourceId = ressourceId;
			 mArgs = args;
			 mLevel = loglevel;
			}

		
		public LogItem(String message) {
			mMessage = message;
		}

		public LogItem(int loglevel, String msg) {
			mLevel = loglevel;
			mMessage = msg;
		}


		public LogItem(int loglevel, int ressourceId) {
			mRessourceId =ressourceId;
			mLevel = loglevel;
		}


		String getString(Context c) {
			if(mMessage !=null) {
				return mMessage;
			} else {
				if(mArgs == null)
					return c.getString(mRessourceId);
				else
					return c.getString(mRessourceId,mArgs);
			}
		}
	}
	
	private static final int MAXLOGENTRIES = 200;


	public static final String MANAGMENT_PREFIX = "M:";


	



	public interface LogListener {
		void newLog(LogItem logItem);
	}
	
	public interface StateListener {
		void updateState(String state, String logmessage);
	}

	synchronized static void logMessage(int level,String prefix, String message)
	{
		newlogItem(new LogItem(prefix +  message));

	}

	synchronized static void clearLog() {
		logbuffer.clear();
		logInformation();
	}

	private static void logInformation() {
		
		logInfo(R.string.mobile_info,Build.MODEL, Build.BOARD,Build.BRAND,Build.VERSION.SDK_INT);
	}

	synchronized static void addLogListener(LogListener ll){
		logListener.add(ll);
	}

	synchronized static void removeLogListener(LogListener ll) {
		logListener.remove(ll);
	}

	
	synchronized static void addSpeedListener(StateListener sl){
		stateListener.add(sl);
	}

	synchronized static void removeSpeedListener(StateListener sl) {
		stateListener.remove(sl);
	}


	synchronized public static LogItem[] getlogbuffer() {

		// The stoned way of java to return an array from a vector
		// brought to you by eclipse auto complete
		return (LogItem[]) logbuffer.toArray(new LogItem[logbuffer.size()]);

	}
	public static void logBuilderConfig(String[] bconfig) {
		mBconfig = bconfig;
	}
	public static void triggerLogBuilderConfig() {
		if(mBconfig==null) {
			logMessage(0, "", "No active interface");
		} else {
			for (String item : mBconfig) {
				logMessage(0, "", item);
			}	
		}

	}

	public synchronized static void updateStateString(String state, String msg) {
		for (StateListener sl : stateListener) {
			sl.updateState(state,msg);
		}
	}

	public static void logInfo(String message) {
		
	}

	public static void logInfo(int ressourceId, Object... args) {
		newlogItem(new LogItem(LogItem.INFO, ressourceId, args));
	}

	private static void newlogItem(LogItem logItem) {
		logbuffer.addLast(logItem);
		if(logbuffer.size()>MAXLOGENTRIES)
			logbuffer.removeFirst();
		
		for (LogListener ll : logListener) {
			ll.newLog(logItem);
		}
	}

	public static void logError(String msg) {
		newlogItem(new LogItem(LogItem.ERROR, msg));
		
	}

	public static void logError(int ressourceId) {
		newlogItem(new LogItem(LogItem.ERROR, ressourceId));
	}
	public static void logError(int ressourceId, Object... args) {
		newlogItem(new LogItem(LogItem.ERROR, ressourceId,args));
	}
	
	
}