aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/fil/libre/repwifiapp/helpers/NetworkManager.java
blob: 33acbb79367b9e8b6895713974a2fad796c62d19 (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//
// Copyright 2017 Filippo "Fil" Bergamo <fil.bergamo@riseup.net>
// 
// This file is part of RepWifiApp.
//
// RepWifiApp 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.
// 
// RepWifiApp 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 RepWifiApp.  If not, see <http://www.gnu.org/licenses/>.
// 
// ********************************************************************

package fil.libre.repwifiapp.helpers;

import java.io.File;
import java.util.ArrayList;

public class NetworkManager {

	private static final String F_SEP = "\t";
	private static final int NET_MAX_AGE = 365; //Expressed in days
	
	private String _knownNetworksFile = null;
		
	public NetworkManager(String networksFilePath){
		this._knownNetworksFile = networksFilePath;
	}
	
	private AccessPointInfo searchInFile(AccessPointInfo i){
		
		if (i == null){
			return null;
		}
		
		String bssid = i.getBSSID();
		String ssid = i.getSSID();
		
		if (bssid == null || ssid == null || bssid.trim().equals("") || ssid.trim().equals("")){
			return null;
		}		
		
		AccessPointInfo ret = null;
		AccessPointInfo[] list = getKnownNetworks();
		
		if (list == null){
			return null;
		}
		
		for(AccessPointInfo toTest : list){
		
			// try to match both bssid and ssid.
			// if bssid doesn't match, but ssid does, 
			// then the network is a candidate.
			// if no bssid equality is found,
			// then return the best match (only ssid), if any
			if (toTest.getSSID().equals(ssid)){
				
				if (toTest.getBSSID().equals(bssid)){
					i.setPassword(toTest.getPassword());
					return i;
					
				}else{
					i.setPassword(toTest.getPassword());
					ret = i;
				}
				
			}
			
		}		
		
		return ret;
		
	}
	
	private boolean saveOrRemove(AccessPointInfo info, boolean save){
		
		String iText = InfoToString(info);
		if (iText == null){
			return false;
		}

		AccessPointInfo[] existingNets = getKnownNetworks();
				
		ArrayList<AccessPointInfo> newlist = new ArrayList<AccessPointInfo>();

		if (existingNets == null || existingNets.length == 0){
			//no existing storage yet, create it
			
			if (save){
				//set timestamp
				info.setLastTimeUsed(System.currentTimeMillis());
				newlist.add(info);
				AccessPointInfo[] newContents = new AccessPointInfo[newlist.size()];
				newContents = newlist.toArray(newContents);

				return saveList(newContents);

			}else{
				//nothing to do, return
				return true;
			}
			
		}
		
		
		if (save){
			//add the updated info to the storage
			info.setLastTimeUsed(System.currentTimeMillis());
			newlist.add(info);
		}

		for(AccessPointInfo old : existingNets){

			if (old == null){
				//error while loading from file. skip.
				continue;
			}
		
			// keep network only if it's not older than the max age for a network 
			else if (old.isOlderThan(NET_MAX_AGE)){
				//skip it
				continue;
			}
			
			else if (old.getBSSID().equals(info.getBSSID()) && old.getSSID().equals(info.getSSID())){
				//found previously saved entry for the same network we are managing
				//skip it
				continue;
			}
			
			else{
				//old network info that can be kept in the storage
				newlist.add(old);
			}
			
		}
		

		AccessPointInfo[] newContents = new AccessPointInfo[newlist.size()];
		newContents = newlist.toArray(newContents);

		return saveList(newContents);

	}
	
	private AccessPointInfo getFromString(String savedString){
		
		if (savedString == null || savedString.trim().equals("")) {
			return null;
		}
		
		String[] fields = savedString.split(F_SEP);
		
		if (fields.length != 4 ){
			return null;
		}
		
		String bssid = fields[0];
		String ssid = fields[1];
		String pass = fields[2];
		String lastUsed = fields[3];
		
		long lastusedmillis = 0;
		try {
			lastusedmillis = Long.parseLong(lastUsed);			
		} catch (NumberFormatException e) {
			//invalid format
			Utils.logError("Invalid time format in network manager \""+lastUsed +"\". Network BSSID: " + bssid, e);
		}
				
		if (bssid.trim().equals("") || ssid.trim().equals("") || pass.trim().equals("")){
			return null;
		}
		
		AccessPointInfo i = new AccessPointInfo(ssid, bssid, null, null, null);
		i.setPassword(pass);
		i.setLastTimeUsed(lastusedmillis);
				
		return i;
		
	}
	
	private String InfoToString(AccessPointInfo info){
		
		if (info == null){
			return null;
		}
		
		String bssid = info.getBSSID();
		String ssid = info.getSSID();
		String pass = info.getPassword();
		String tsLastUsed = "" + info.getLastTimeUsed();
		
		if (bssid == null || bssid.trim().equals("")){
			return null;
		}
		
		if (ssid == null || ssid.trim().equals("")){
			return null;
		}
		
		if (pass == null || pass.trim().equals("")){
			return null;
		}		
			
		String iText = info.getBSSID() + F_SEP + info.getSSID() + F_SEP + info.getPassword() + F_SEP + tsLastUsed;
		return iText;
		
	}
	
	private boolean saveList(AccessPointInfo[] list){
		
		if (list == null){
			return false;
		}
		
		String[] lines = new String[list.length];
		
		for (int i = 0; i<list.length; i++){
			
			String storeString = InfoToString(list[i]);
			if (storeString == null){
				return false;
			}
			lines[i] = storeString; 
		
		}
		
		return Utils.writeFileLines(this._knownNetworksFile,lines, true);
		
	}
	
	public AccessPointInfo[] getKnownNetworks(){
		
		ArrayList<AccessPointInfo> list = new ArrayList<AccessPointInfo>();
		
		File f = new File(this._knownNetworksFile);
		if (! f.exists()){
			return null;
		}

		String[] lines = Utils.readFileLines(_knownNetworksFile);
		if (lines == null || lines.length == 0){
			return null;
		}
		
		for(String l : lines){
			
			AccessPointInfo info = getFromString(l);
			if (info != null){
				list.add(info);
			}
			
		}
		
		AccessPointInfo[] ret = new AccessPointInfo[list.size()];
		ret = list.toArray(ret);
		
		return ret;
		
	}
	
	public boolean isKnown(AccessPointInfo info){
		
		AccessPointInfo i = searchInFile(info);
		if (i == null){
			return false;
		}else {
			return true;
		}
		
	}
	
	public boolean save(AccessPointInfo info){
		return saveOrRemove(info, true);
	}
	
	public boolean remove(AccessPointInfo info){
		return saveOrRemove(info, false);
	}
	
	public AccessPointInfo getSavedNetwork(AccessPointInfo i){
		return searchInFile(i);
	}
	
	
	
}