HomeFragment.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. package de.tudarmstadt.informatik.hostage.ui2.fragment;
  2. import android.app.AlertDialog;
  3. import android.app.Fragment;
  4. import android.app.FragmentManager;
  5. import android.content.BroadcastReceiver;
  6. import android.content.Context;
  7. import android.content.DialogInterface;
  8. import android.content.Intent;
  9. import android.content.IntentFilter;
  10. import android.content.SharedPreferences;
  11. import android.os.Bundle;
  12. import android.support.v4.content.LocalBroadcastManager;
  13. import android.util.Log;
  14. import android.view.LayoutInflater;
  15. import android.view.View;
  16. import android.view.ViewGroup;
  17. import android.widget.CompoundButton;
  18. import android.widget.ImageView;
  19. import android.widget.Switch;
  20. import android.widget.TextView;
  21. import de.tudarmstadt.informatik.hostage.R;
  22. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  23. import de.tudarmstadt.informatik.hostage.dao.ProfileManager;
  24. import de.tudarmstadt.informatik.hostage.logging.UglyDbHelper;
  25. import de.tudarmstadt.informatik.hostage.model.Profile;
  26. import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
  27. import de.tudarmstadt.informatik.hostage.ui2.fragment.opengl.ThreatIndicatorGLRenderer;
  28. /**
  29. * @author Alexander Brakowski
  30. * @created 13.01.14 19:06
  31. */
  32. public class HomeFragment extends Fragment {
  33. private Switch mHomeSwitchConnection;
  34. private TextView mHomeTextName;
  35. private TextView mHomeTextSecurity;
  36. private TextView mHomeTextAttacks;
  37. private TextView mHomeTextProfile;
  38. private TextView mHomeTextProfileHeader;
  39. private ImageView mHomeProfileImage;
  40. private ImageView mHomeConnectionInfoButton;
  41. private View rootView;
  42. private BroadcastReceiver mReceiver;
  43. private CompoundButton.OnCheckedChangeListener switchChangeListener = null;
  44. private int defaultTextColor;
  45. private ProfileManager mProfileManager;
  46. private SharedPreferences mConnectionInfo;
  47. private UglyDbHelper dbh;
  48. private static ThreatIndicatorGLRenderer.ThreatLevel THREAT_LEVEL = ThreatIndicatorGLRenderer.ThreatLevel.NO_THREAT;
  49. private void assignViews() {
  50. mHomeSwitchConnection = (Switch) rootView.findViewById(R.id.home_switch_connection);
  51. mHomeTextName = (TextView) rootView.findViewById(R.id.home_text_name);
  52. mHomeTextSecurity = (TextView) rootView.findViewById(R.id.home_text_security);
  53. mHomeTextAttacks = (TextView) rootView.findViewById(R.id.home_text_attacks);
  54. mHomeTextProfile = (TextView) rootView.findViewById(R.id.home_text_profile);
  55. mHomeTextProfileHeader = (TextView) rootView.findViewById(R.id.home_text_profile_header);
  56. mHomeProfileImage = (ImageView) rootView.findViewById(R.id.home_image_profile);
  57. mHomeConnectionInfoButton = (ImageView) rootView.findViewById(R.id.home_button_connection_info);
  58. }
  59. private void registerBroadcastReceiver(){
  60. mReceiver = new BroadcastReceiver() {
  61. @Override
  62. public void onReceive(Context context, Intent intent) {
  63. updateUI();
  64. }
  65. };
  66. LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, new IntentFilter(getString(R.string.broadcast)));
  67. }
  68. public HomeFragment(){}
  69. public void setStateNotActive(boolean initial){
  70. mHomeTextName.setTextColor(getResources().getColor(R.color.light_grey));
  71. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.light_grey));
  72. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.light_grey));
  73. mHomeTextProfile.setTextColor(getResources().getColor(R.color.light_grey));
  74. mHomeTextProfileHeader.setTextColor(getResources().getColor(R.color.light_grey));
  75. if(!initial){
  76. ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.ThreatLevel.NOT_MONITORING);
  77. }
  78. mHomeSwitchConnection.setChecked(false);
  79. }
  80. public void setStateNotActive(){
  81. setStateNotActive(false);
  82. }
  83. public void setStateActive(){
  84. setStateActive(false);
  85. }
  86. public void setStateActive(boolean initial){
  87. mHomeTextAttacks.setVisibility(View.VISIBLE);
  88. mHomeTextSecurity.setVisibility(View.VISIBLE);
  89. mHomeTextName.setTextColor(defaultTextColor);
  90. mHomeTextProfile.setTextColor(defaultTextColor);
  91. mHomeTextProfileHeader.setTextColor(defaultTextColor);
  92. if(!initial){
  93. ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.ThreatLevel.NO_THREAT);
  94. }
  95. mHomeSwitchConnection.setChecked(true);
  96. }
  97. public void setStateNotConnected(){
  98. mHomeTextSecurity.setVisibility(View.INVISIBLE);
  99. mHomeTextAttacks.setVisibility(View.INVISIBLE);
  100. mHomeTextProfile.setVisibility(View.INVISIBLE);
  101. mHomeTextProfileHeader.setVisibility(View.INVISIBLE);
  102. mHomeProfileImage.setVisibility(View.INVISIBLE);
  103. mHomeConnectionInfoButton.setVisibility(View.INVISIBLE);
  104. mHomeTextName.setText("Not connected");
  105. }
  106. public void setStateConnected(){
  107. mHomeTextProfile.setVisibility(View.VISIBLE);
  108. mHomeTextProfileHeader.setVisibility(View.VISIBLE);
  109. mHomeProfileImage.setVisibility(View.VISIBLE);
  110. mHomeConnectionInfoButton.setVisibility(View.VISIBLE);
  111. }
  112. public void updateUI(){
  113. Profile profile = mProfileManager.getCurrentActivatedProfile();
  114. if(profile != null){
  115. mHomeTextProfile.setText(profile.mLabel);
  116. mHomeProfileImage.setImageBitmap(profile.getIconBitmap());
  117. }
  118. if(HelperUtils.isWifiConnected(getActivity())){
  119. setStateConnected();
  120. mHomeTextName.setText(mConnectionInfo.getString(getString(R.string.connection_info_ssid), ""));
  121. }
  122. boolean hasActiveListeners = false;
  123. int totalAttacks = 0;
  124. int totalLogged = 0;
  125. if(MainActivity.getInstance().isServiceBound()){
  126. for(String protocol: getResources().getStringArray(R.array.protocols)){
  127. if(MainActivity.getInstance().getHoneyService().isRunning(protocol)){
  128. hasActiveListeners = true;
  129. int attacks = MainActivity.getInstance().getHoneyService().getNumberOfActiveConnections(protocol);
  130. totalAttacks += attacks;
  131. if(attacks == 0){
  132. int log_attacks = dbh.numBssidSeen(protocol, mConnectionInfo.getString(getString(R.string.connection_info_bssid), null));
  133. totalLogged += log_attacks;
  134. if(log_attacks > 0){
  135. if(THREAT_LEVEL != ThreatIndicatorGLRenderer.ThreatLevel.LIVE_THREAT){
  136. THREAT_LEVEL = ThreatIndicatorGLRenderer.ThreatLevel.PAST_THREAT;
  137. }
  138. } else {
  139. if(THREAT_LEVEL != ThreatIndicatorGLRenderer.ThreatLevel.LIVE_THREAT && THREAT_LEVEL != ThreatIndicatorGLRenderer.ThreatLevel.PAST_THREAT){
  140. THREAT_LEVEL = ThreatIndicatorGLRenderer.ThreatLevel.NO_THREAT;
  141. }
  142. }
  143. } else {
  144. THREAT_LEVEL = ThreatIndicatorGLRenderer.ThreatLevel.LIVE_THREAT;
  145. }
  146. }
  147. }
  148. }
  149. if(hasActiveListeners){
  150. setStateActive(true);
  151. switch(THREAT_LEVEL){
  152. case NO_THREAT:
  153. mHomeTextAttacks.setText("0 attacks recorded");
  154. mHomeTextSecurity.setText("Secure");
  155. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_dark_green));
  156. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_dark_green));
  157. break;
  158. case PAST_THREAT:
  159. mHomeTextAttacks.setText(totalLogged + (totalLogged == 1 ? " attack" : " attacks") + " logged");
  160. mHomeTextSecurity.setText("Insecure");
  161. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_yellow));
  162. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_yellow));
  163. break;
  164. case LIVE_THREAT:
  165. mHomeTextAttacks.setText(totalAttacks + (totalAttacks == 1 ? " attack" : " attacks") + " recorded");
  166. mHomeTextSecurity.setText("Insecure");
  167. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_red));
  168. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_red));
  169. break;
  170. }
  171. ThreatIndicatorGLRenderer.setThreatLevel(THREAT_LEVEL);
  172. } else {
  173. mHomeSwitchConnection.setOnCheckedChangeListener(null);
  174. setStateNotActive();
  175. THREAT_LEVEL = ThreatIndicatorGLRenderer.ThreatLevel.NO_THREAT;
  176. if(!HelperUtils.isWifiConnected(getActivity())){
  177. setStateNotConnected();
  178. }
  179. mHomeSwitchConnection.setOnCheckedChangeListener(switchChangeListener);
  180. }
  181. }
  182. @Override
  183. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  184. Bundle savedInstanceState) {
  185. super.onCreateView(inflater, container, savedInstanceState);
  186. getActivity().setTitle(getResources().getString(R.string.drawer_overview));
  187. dbh = new UglyDbHelper(getActivity());
  188. mProfileManager = ProfileManager.getInstance();
  189. mConnectionInfo = getActivity().getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);
  190. rootView = inflater.inflate(R.layout.fragment_home, container, false);
  191. assignViews();
  192. // hook up the connection info button
  193. mHomeConnectionInfoButton.setOnClickListener(new View.OnClickListener() {
  194. @Override
  195. public void onClick(View v) {
  196. ConnectionInfoDialogFragment connectionInfoDialogFragment = new ConnectionInfoDialogFragment();
  197. connectionInfoDialogFragment.show(getFragmentManager().beginTransaction(), connectionInfoDialogFragment.getTag());
  198. }
  199. });
  200. defaultTextColor = mHomeTextName.getCurrentTextColor();
  201. setStateNotActive(true);
  202. setStateNotConnected();
  203. registerBroadcastReceiver();
  204. updateUI();
  205. final String[] protocols = getResources().getStringArray(R.array.protocols);
  206. mHomeSwitchConnection = (Switch) rootView.findViewById(R.id.home_switch_connection);
  207. if(switchChangeListener == null){
  208. switchChangeListener = new CompoundButton.OnCheckedChangeListener() {
  209. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  210. if(!HelperUtils.isWifiConnected(getActivity())){
  211. new AlertDialog.Builder(getActivity())
  212. .setTitle("Information")
  213. .setMessage("You are not connected to a WiFi network. \n\nPlease connect to one, before trying to activate HosTaGe.")
  214. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  215. public void onClick(DialogInterface dialog, int which) {
  216. }
  217. })
  218. .setIcon(android.R.drawable.ic_dialog_info)
  219. .show();
  220. setStateNotActive();
  221. setStateNotConnected();
  222. } else {
  223. if(isChecked){
  224. for(String protocol: protocols){
  225. MainActivity.getInstance().getHoneyService().startListener(protocol);
  226. }
  227. setStateActive();
  228. } else {
  229. MainActivity.getInstance().getHoneyService().stopListeners();
  230. MainActivity.getInstance().stopAndUnbind();
  231. setStateNotActive();
  232. }
  233. }
  234. }
  235. };
  236. }
  237. mHomeSwitchConnection.setOnCheckedChangeListener(switchChangeListener);
  238. rootView.findViewById(R.id.home_profile_details).setOnClickListener(
  239. new View.OnClickListener() {
  240. @Override
  241. public void onClick(View v) {
  242. Fragment fragment = new ProfileManagerFragment();
  243. MainActivity.getInstance().injectFragment(fragment, false);
  244. }
  245. }
  246. );
  247. return rootView;
  248. }
  249. @Override
  250. public void onDestroy(){
  251. super.onDestroy();
  252. LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
  253. }
  254. }