Преглед изворни кода

rewritten switch logic in service fragment

Fabio Arnold пре 9 година
родитељ
комит
28e4507a45

+ 2 - 1
src/de/tudarmstadt/informatik/hostage/ui/fragment/HomeFragment.java

@@ -292,7 +292,8 @@ public class HomeFragment extends Fragment {
 				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
 					if (isChecked) { // switch activated
 						// we need a network connection
-						if (!HelperUtils.isNetworkAvailable(getActivity())) {
+						// 2.5.2015 Fabio: for now we only check for wifi connections
+						if (!HelperUtils.isWifiConnected(getActivity())) {
 							new AlertDialog.Builder(getActivity()).setTitle(R.string.information).setMessage(R.string.network_not_connected_msg)
 									.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
 										public void onClick(DialogInterface dialog, int which) {

+ 49 - 54
src/de/tudarmstadt/informatik/hostage/ui/fragment/ServicesFragment.java

@@ -186,60 +186,55 @@ public class ServicesFragment extends TrackerFragment {
                     mProfile = ProfileManager.getInstance().getCurrentActivatedProfile();
                     //SK: Temp bugfix
                         //if (!HelperUtils.isNetworkAvailable(getActivity())) {
-                    if (!HelperUtils.isWifiConnected(getActivity())) {
-                            if(!MainActivity.getInstance().getHostageService().hasRunningListeners()) {
-                                new AlertDialog.Builder(getActivity())
-                                        .setTitle(R.string.information)
-                                        .setMessage(R.string.wifi_not_connected_msg)
-                                        .setPositiveButton(android.R.string.ok,
-                                                new DialogInterface.OnClickListener() {
-                                                    public void onClick(DialogInterface dialog, int which) {
-
-                                                    }
-                                                }
-                                        )
-                                        .setIcon(android.R.drawable.ic_dialog_info)
-                                        .show();
-                                if(mServicesSwitchService.isChecked()) {
-                                    setStateNotActive();
-                                }
-                                setStateNotConnected();
-                            }
-                            else{
-                                setStateActive();
-                            }
-                    } else {
-                        if (MainActivity.getInstance().isServiceBound()) {
-                            if (isChecked) {
-                                for (String protocol : protocols) {
-                                    if (!protocol.equals("GHOST")) {
-                                        if (!MainActivity.getInstance().getHostageService().isRunning(protocol)) {
-                                            MainActivity.getInstance().getHostageService().startListener(protocol);
-                                        }
-                                    } else {
-                                        if (mProfile.mGhostActive) {
-
-                                            mGhostPorts = mProfile.getGhostPorts();
-                                            if (mGhostPorts.length != 0) {
-                                                for (Integer port : mGhostPorts) {
-                                                    if (!MainActivity.getInstance().getHostageService().isRunning("GHOST", port)) {
-                                                        MainActivity.getInstance().getHostageService().startListener("GHOST", port);
-                                                    }
-                                                }
-                                            }
-
-
-                                        }
-                                    }
-                                }
-                                setStateActive();
-                            } else{
-                                MainActivity.getInstance().getHostageService().stopListeners();
-                                MainActivity.getInstance().stopAndUnbind();
-                                setStateNotActive();
-                            }
-                        }
-                    }
+					//replaced with if (!HelperUtils.isWifiConnected(getActivity())) {
+
+					if (isChecked) { // switch activated
+						// we need a network connection
+						if (!HelperUtils.isWifiConnected(getActivity())) {
+							new AlertDialog.Builder(getActivity())
+									.setTitle(R.string.information)
+									.setMessage(R.string.wifi_not_connected_msg)
+									.setPositiveButton(android.R.string.ok,
+											new DialogInterface.OnClickListener() {
+												public void onClick(DialogInterface dialog, int which) {
+
+												}
+											}
+									)
+									.setIcon(android.R.drawable.ic_dialog_info)
+									.show();
+							setStateNotActive();
+							setStateNotConnected();
+						} else { // we have a connection
+							// activate all protocols
+							for (String protocol : protocols) {
+								if (protocol.equals("GHOST") && mProfile.mGhostActive) {
+									mGhostPorts = mProfile.getGhostPorts();
+									if (mGhostPorts.length != 0) {
+										for (Integer port : mGhostPorts) {
+											if (MainActivity.getInstance().getHostageService() != null
+													&& !MainActivity.getInstance().getHostageService().isRunning("GHOST", port)) {
+												MainActivity.getInstance().getHostageService().startListener("GHOST", port);
+											}
+										}
+									}
+								} else {
+									if (MainActivity.getInstance().getHostageService() != null
+											&& !MainActivity.getInstance().getHostageService().isRunning(protocol)) {
+										MainActivity.getInstance().getHostageService().startListener(protocol);
+									}
+								}
+							}
+							setStateActive();
+						}
+					} else { // switch deactivated
+						if (MainActivity.getInstance().getHostageService() != null) {
+							// why should the hostage service not be running??
+							MainActivity.getInstance().getHostageService().stopListeners();
+							MainActivity.getInstance().stopAndUnbind();
+						}
+						setStateNotActive();
+					}
                 }
 
             };