Browse Source

commented HomeFragment

Alexander Brakowski 10 years ago
parent
commit
732868712f
1 changed files with 98 additions and 10 deletions
  1. 98 10
      src/de/tudarmstadt/informatik/hostage/ui2/fragment/HomeFragment.java

+ 98 - 10
src/de/tudarmstadt/informatik/hostage/ui2/fragment/HomeFragment.java

@@ -35,49 +35,73 @@ import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
 import de.tudarmstadt.informatik.hostage.ui2.fragment.opengl.ThreatIndicatorGLRenderer;
 
 /**
+ * This fragments displays the current hostage state and attacks on the device in form of an animation and simple view components
+ *
  * @author Alexander Brakowski
  * @created 13.01.14 19:06
  */
 
 public class HomeFragment extends Fragment {
 
+	/**
+	 * View objects from the layout
+	 */
 	private Switch mHomeSwitchConnection;
-
 	private TextView mHomeTextName;
-
 	private TextView mHomeTextSecurity;
-
 	private TextView mHomeTextAttacks;
-
 	private TextView mHomeTextProfile;
-
 	private TextView mHomeTextProfileHeader;
-
 	private ImageView mHomeProfileImage;
-
 	private ImageView mHomeConnectionInfoButton;
-
 	private View mRootView;
 
+	/**
+	 * This handles all the broadcasts from the Hostage service
+	 */
 	private BroadcastReceiver mReceiver;
 
+	/**
+	 * A change listener for the monitor switch
+	 */
 	private CompoundButton.OnCheckedChangeListener mSwitchChangeListener = null;
 
+
 	private int mDefaultTextColor;
 
+	/**
+	 * A reference to the profile manager
+	 */
 	private ProfileManager mProfileManager;
 
+	/**
+	 * A shared preference that holds all the connection info of the current network connection
+	 */
 	private SharedPreferences mConnectionInfo;
 
+	/**
+	 * An Helper to access the sqllite database with all the records
+	 */
 	private HostageDBOpenHelper mDbHelper;
 
+	/**
+	 * Holds a state if the broadcast receiver is registered to the hostage service
+ 	 */
 	private boolean mReceiverRegistered;
 
-	private boolean mRestoredFromSaved = false;
-
+	/**
+	 * Holds a state if the hostage service is active
+	 */
 	private boolean isActive = false;
+
+	/**
+	 * Holds a state if the device is currently connected to a network
+	 */
 	private boolean isConnected = false;
 
+	/**
+	 * Looks up all the neccessary views in the layout
+	 */
 	private void assignViews() {
 		mHomeSwitchConnection = (Switch) mRootView.findViewById(R.id.home_switch_connection);
 		mHomeTextName = (TextView) mRootView.findViewById(R.id.home_text_name);
@@ -89,6 +113,9 @@ public class HomeFragment extends Fragment {
 		mHomeConnectionInfoButton = (ImageView) mRootView.findViewById(R.id.home_button_connection_info);
 	}
 
+	/**
+	 * Registers the broadcast receiver with the hostage service
+	 */
 	private void registerBroadcastReceiver() {
 		if (!mReceiverRegistered) {
 			LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, new IntentFilter(getString(R.string.broadcast)));
@@ -96,6 +123,9 @@ public class HomeFragment extends Fragment {
 		}
 	}
 
+	/**
+	 * Unregisters the broadcast receiver
+	 */
 	private void unregisterBroadcastReceiver() {
 		if (mReceiverRegistered) {
 			LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
@@ -106,6 +136,12 @@ public class HomeFragment extends Fragment {
 	public HomeFragment() {
 	}
 
+	/**
+	 * Sets the view state to not active.
+	 * This means hiding and graying out all the informations in the view, that are not important, if the service is not active.
+	 *
+	 * @param initial indicates that the method was called on creation of view
+	 */
 	public void setStateNotActive(boolean initial) {
 		mHomeTextName.setTextColor(getResources().getColor(R.color.light_grey));
 		mHomeTextSecurity.setTextColor(getResources().getColor(R.color.light_grey));
@@ -121,14 +157,26 @@ public class HomeFragment extends Fragment {
 		isActive = false;
 	}
 
+	/**
+	 * Alias for calling setStateNotActive with the initial value being false
+	 */
 	public void setStateNotActive() {
 		setStateNotActive(false);
 	}
 
+	/**
+	 * Alias for calling setStateActive with the initial value being false
+	 */
 	public void setStateActive() {
 		setStateActive(false);
 	}
 
+	/**
+	 * Sets the state of the view to active.
+	 * That means that all the information showing the active state of the hostage service is being displayed.
+	 *
+	 * @param initial indicates that the method was called on creation of view
+	 */
 	public void setStateActive(boolean initial) {
 		mHomeTextAttacks.setVisibility(View.VISIBLE);
 		mHomeTextSecurity.setVisibility(View.VISIBLE);
@@ -145,6 +193,9 @@ public class HomeFragment extends Fragment {
 		isActive = true;
 	}
 
+	/**
+	 * Sets the state of the view to not connected by hiding information about the attacks.
+	 */
 	public void setStateNotConnected() {
 		mHomeTextSecurity.setVisibility(View.INVISIBLE);
 		mHomeTextAttacks.setVisibility(View.INVISIBLE);
@@ -157,6 +208,9 @@ public class HomeFragment extends Fragment {
 		isConnected = false;
 	}
 
+	/**
+	 * Sets the state of the view to connected by showing informations about attacks
+	 */
 	public void setStateConnected() {
 		mHomeTextProfile.setVisibility(View.VISIBLE);
 		mHomeTextProfileHeader.setVisibility(View.VISIBLE);
@@ -165,6 +219,14 @@ public class HomeFragment extends Fragment {
 		isConnected = true;
 	}
 
+	/**
+	 * Updates the view.
+	 *
+	 * That means: updating the number of attacks on the view,
+	 *             updating the threat level,
+	 *             updating the connection state,
+	 *             updating the monitoring state
+	 */
 	public void updateUI() {
 		Profile profile = mProfileManager.getCurrentActivatedProfile();
 		if (profile != null) {
@@ -172,6 +234,7 @@ public class HomeFragment extends Fragment {
 			mHomeProfileImage.setImageBitmap(profile.getIconBitmap());
 		}
 
+		// if the device is connected to an network display the network name
 		if (HelperUtils.isNetworkAvailable(getActivity())) {
 			setStateConnected();
 			String ssid = mConnectionInfo.getString(getString(R.string.connection_info_ssid), "\"\"");
@@ -184,6 +247,7 @@ public class HomeFragment extends Fragment {
 		int totalAttacks = mDbHelper.numBssidSeen(mConnectionInfo.getString(getString(R.string.connection_info_bssid), null));
 		ThreatIndicatorGLRenderer.ThreatLevel threatLevel = ThreatIndicatorGLRenderer.ThreatLevel.NOT_MONITORING;
 
+		// decides which threat level to display
 		if (MainActivity.getInstance().getHostageService() != null) {
 			if (MainActivity.getInstance().getHostageService().hasRunningListeners()) {
 				hasActiveListeners = true;
@@ -198,6 +262,7 @@ public class HomeFragment extends Fragment {
 			}
 		}
 
+		// if the monitoring is running show the information
 		if (hasActiveListeners) {
 			setStateActive(true);
 
@@ -238,6 +303,9 @@ public class HomeFragment extends Fragment {
 		}
 	}
 
+	/**
+	 * {@inheritDoc}
+	 */
 	@Override
 	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 		super.onCreateView(inflater, container, savedInstanceState);
@@ -269,9 +337,11 @@ public class HomeFragment extends Fragment {
 
 		mDefaultTextColor = mHomeTextName.getCurrentTextColor();
 
+		// sets state and connection initially to off
 		setStateNotActive(true);
 		setStateNotConnected();
 
+		// register the broadcast receiver
 		mReceiver = new BroadcastReceiver() {
 			@SuppressLint("NewApi")
 			@Override
@@ -284,12 +354,14 @@ public class HomeFragment extends Fragment {
 
 		updateUI();
 
+		// connects the switch listener to the switch button
 		mHomeSwitchConnection = (Switch) mRootView.findViewById(R.id.home_switch_connection);
 		mHomeSwitchConnection.setSaveEnabled(false);
 
 		if (mSwitchChangeListener == null) {
 			mSwitchChangeListener = new CompoundButton.OnCheckedChangeListener() {
 				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
+					// displays a alert dialog if no network is available
 					if (!HelperUtils.isNetworkAvailable(getActivity())) {
 						new AlertDialog.Builder(getActivity()).setTitle(R.string.information).setMessage(R.string.network_not_connected_msg)
 								.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@@ -302,10 +374,13 @@ public class HomeFragment extends Fragment {
 						setStateNotConnected();
 					} else {
 						if (isChecked) {
+
 							boolean protocolActivated = false;
 							if (ProfileManager.getInstance().getCurrentActivatedProfile() == null) {
+								// starts all services
 								MainActivity.getInstance().startMonitorServices(Arrays.asList(getResources().getStringArray(R.array.protocols)));
 							} else {
+								// starts the services that are actived in the current profile
 								ProfileManager profileManager = ProfileManager.getInstance();
 
 								if (profileManager.isRandomActive()) {
@@ -325,6 +400,7 @@ public class HomeFragment extends Fragment {
 							if (protocolActivated) {
 								setStateActive();
 							} else {
+								// no protocol was started, so show alert dialog
 								new AlertDialog.Builder(getActivity()).setTitle(R.string.information).setMessage(R.string.profile_no_services_msg)
 										.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
 											public void onClick(DialogInterface dialog, int which) {
@@ -335,6 +411,7 @@ public class HomeFragment extends Fragment {
 								setStateNotActive();
 							}
 						} else {
+							// stop hostage service and all listeners
 							if (MainActivity.getInstance().getHostageService() != null) {
 								MainActivity.getInstance().getHostageService().stopListeners();
 								MainActivity.getInstance().stopAndUnbind();
@@ -347,6 +424,7 @@ public class HomeFragment extends Fragment {
 		}
 		mHomeSwitchConnection.setOnCheckedChangeListener(mSwitchChangeListener);
 
+		// connects the profile text an click listener
 		mRootView.findViewById(R.id.home_profile_details).setOnClickListener(new View.OnClickListener() {
 			@Override
 			public void onClick(View v) {
@@ -355,6 +433,7 @@ public class HomeFragment extends Fragment {
 			}
 		});
 
+		// connect the attacks text to an click listener
 		View.OnClickListener attackClickListener = new View.OnClickListener() {
 			@Override
 			public void onClick(View v) {
@@ -381,12 +460,18 @@ public class HomeFragment extends Fragment {
 		return mRootView;
 	}
 
+	/**
+	 * {@inheritDoc}
+	 */
 	@Override
 	public void onStop() {
 		super.onStop();
 		unregisterBroadcastReceiver();
 	}
 
+	/**
+	 * {@inheritDoc}
+	 */
 	@Override
 	public void onStart() {
 		super.onStart();
@@ -394,6 +479,9 @@ public class HomeFragment extends Fragment {
 		updateUI();
 	}
 
+	/**
+	 * {@inheritDoc}
+	 */
 	@Override
 	public void onDestroy() {
 		super.onDestroy();