|
@@ -7,33 +7,53 @@ import android.net.wifi.WifiInfo;
|
|
|
import android.net.wifi.WifiManager;
|
|
import android.net.wifi.WifiManager;
|
|
|
import android.widget.Toast;
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Created by david on 01/01/17.
|
|
* Created by david on 01/01/17.
|
|
|
*/
|
|
*/
|
|
|
-
|
|
|
|
|
-public class NetworkManager implements Event {
|
|
|
|
|
|
|
+public class NetworkEvent implements Event {
|
|
|
@Override
|
|
@Override
|
|
|
- public void trigger(Context context, HashMap<String, Object> hm) {
|
|
|
|
|
- List<String> SSIDs = (ArrayList<String>)hm.get("SSIDs");
|
|
|
|
|
- //SSIDs.add("Cueva");
|
|
|
|
|
|
|
+ public boolean trigger(Context context, HashMap data) {
|
|
|
|
|
+
|
|
|
|
|
+ assert (data.get("type") != null);
|
|
|
|
|
+
|
|
|
|
|
+ Connection c = new Connection();
|
|
|
|
|
+ c.state = (NetworkInfo.State)data.get("state");
|
|
|
|
|
+ c.type = (int) data.get("type");
|
|
|
|
|
+ c.ValidSSIDs =(List<String>)data.get("SSIDs");
|
|
|
|
|
+
|
|
|
|
|
|
|
|
ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
NetworkInfo mActive = conn.getActiveNetworkInfo();
|
|
NetworkInfo mActive = conn.getActiveNetworkInfo();
|
|
|
|
|
|
|
|
if (mActive == null) {
|
|
if (mActive == null) {
|
|
|
Toast.makeText(context, "No active connection" , Toast.LENGTH_SHORT).show();
|
|
Toast.makeText(context, "No active connection" , Toast.LENGTH_SHORT).show();
|
|
|
- return;
|
|
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (c.state != null && c.state != mActive.getState()) {
|
|
|
|
|
+ return false;
|
|
|
}
|
|
}
|
|
|
- if (!mActive.isConnected()) {
|
|
|
|
|
|
|
+ /*if (!mActive.isConnected()) {
|
|
|
Toast.makeText(context, "Active connection is disconnected" , Toast.LENGTH_SHORT).show();
|
|
Toast.makeText(context, "Active connection is disconnected" , Toast.LENGTH_SHORT).show();
|
|
|
return;
|
|
return;
|
|
|
- }
|
|
|
|
|
|
|
+ }*/
|
|
|
|
|
+ if (mActive.getType() != c.type)
|
|
|
|
|
+ return false;
|
|
|
|
|
|
|
|
|
|
+ if (c.ValidSSIDs != null && c.ValidSSIDs.size() > 0 && c.type == ConnectivityManager.TYPE_WIFI){
|
|
|
|
|
+ WifiManager wifiManager = (WifiManager) context.getSystemService (Context.WIFI_SERVICE);
|
|
|
|
|
+ WifiInfo info = wifiManager.getConnectionInfo ();
|
|
|
|
|
+ String ssid = info.getSSID(); //Returns "ssid"
|
|
|
|
|
+ ssid = ssid.substring(1, ssid.length()-1);
|
|
|
|
|
+ if (!c.ValidSSIDs.contains(ssid))
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ return true;
|
|
|
|
|
+ /*
|
|
|
switch (mActive.getType()) {
|
|
switch (mActive.getType()) {
|
|
|
case ConnectivityManager.TYPE_MOBILE:
|
|
case ConnectivityManager.TYPE_MOBILE:
|
|
|
//FIXME: triggers with reason == 'connected' on connection and
|
|
//FIXME: triggers with reason == 'connected' on connection and
|
|
@@ -46,10 +66,8 @@ public class NetworkManager implements Event {
|
|
|
Toast.makeText(context, ss, Toast.LENGTH_SHORT).show();
|
|
Toast.makeText(context, ss, Toast.LENGTH_SHORT).show();
|
|
|
break;
|
|
break;
|
|
|
case ConnectivityManager.TYPE_WIFI:
|
|
case ConnectivityManager.TYPE_WIFI:
|
|
|
- WifiManager wifiManager = (WifiManager) context.getSystemService (Context.WIFI_SERVICE);
|
|
|
|
|
- WifiInfo info = wifiManager.getConnectionInfo ();
|
|
|
|
|
- String ssid = info.getSSID(); //Returns "ssid"
|
|
|
|
|
- ssid = ssid.substring(1, ssid.length()-1);
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
|
|
|
|
|
|
|
|
if (SSIDs.contains(ssid)) {
|
|
if (SSIDs.contains(ssid)) {
|
|
@@ -61,5 +79,16 @@ public class NetworkManager implements Event {
|
|
|
default:
|
|
default:
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
|
|
+ */
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Created by david on 01/01/17.
|
|
|
|
|
+ */
|
|
|
|
|
+
|
|
|
|
|
+ static class Connection {
|
|
|
|
|
+ public int type;
|
|
|
|
|
+ public List<String> ValidSSIDs;
|
|
|
|
|
+ public NetworkInfo.State state;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|