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

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.preference.PreferenceManager;

public class NetworkSateReceiver extends BroadcastReceiver {
	private int lastNetwork=-1;
	private OpenVPNMangement mManangement;

	private String lastStateMsg=null;
	
	public NetworkSateReceiver(OpenVPNMangement magnagement) {
		super();
		mManangement = magnagement;
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		NetworkInfo networkInfo = getCurrentNetworkInfo(context);
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);        
		boolean sendusr1 = prefs.getBoolean("netchangereconnect", true);

		String netstatestring;
		if(networkInfo==null)
			netstatestring = "not connected";
		else  {
			String subtype = networkInfo.getSubtypeName();
			if(subtype==null) 
				subtype = "";
			String extrainfo = networkInfo.getExtraInfo();
			if(extrainfo==null)
				extrainfo="";
			
			/*
			if(networkInfo.getType()==android.net.ConnectivityManager.TYPE_WIFI) {
				WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);			
				WifiInfo wifiinfo = wifiMgr.getConnectionInfo();
				extrainfo+=wifiinfo.getBSSID();
				
				subtype += wifiinfo.getNetworkId();
			}*/
			
			
			
			netstatestring = String.format("%2$s %4$s to %1$s %3$s",networkInfo.getTypeName(),
					networkInfo.getDetailedState(),extrainfo,subtype );
		}
		
		
		
		if(networkInfo!=null && networkInfo.getState() == State.CONNECTED) {
				int newnet = networkInfo.getType();
			
				if(sendusr1 && lastNetwork!=newnet) {
					if (lastNetwork==-1)
						mManangement.resume();
					else
						mManangement.reconnect();
				}
							
				lastNetwork = newnet;
		} else if (networkInfo==null) {
			// Not connected, stop openvpn, set last connected network to no network
			lastNetwork=-1;
			if(sendusr1)
				mManangement.pause();
		}
		
		if(!netstatestring.equals(lastStateMsg))
			OpenVPN.logInfo(R.string.netstatus, netstatestring);
		lastStateMsg=netstatestring;

	}

	private NetworkInfo getCurrentNetworkInfo(Context context) {
		ConnectivityManager conn =  (ConnectivityManager)
				context.getSystemService(Context.CONNECTIVITY_SERVICE);
		
		NetworkInfo networkInfo = conn.getActiveNetworkInfo();
		return networkInfo;
	}

}