HomeFragment.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 boolean mReceiverRegistered;
  50. private void assignViews() {
  51. mHomeSwitchConnection = (Switch) rootView.findViewById(R.id.home_switch_connection);
  52. mHomeTextName = (TextView) rootView.findViewById(R.id.home_text_name);
  53. mHomeTextSecurity = (TextView) rootView.findViewById(R.id.home_text_security);
  54. mHomeTextAttacks = (TextView) rootView.findViewById(R.id.home_text_attacks);
  55. mHomeTextProfile = (TextView) rootView.findViewById(R.id.home_text_profile);
  56. mHomeTextProfileHeader = (TextView) rootView.findViewById(R.id.home_text_profile_header);
  57. mHomeProfileImage = (ImageView) rootView.findViewById(R.id.home_image_profile);
  58. mHomeConnectionInfoButton = (ImageView) rootView.findViewById(R.id.home_button_connection_info);
  59. }
  60. private void registerBroadcastReceiver(){
  61. if(!mReceiverRegistered) {
  62. LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mReceiver, new IntentFilter(getString(R.string.broadcast)));
  63. this.mReceiverRegistered = true;
  64. }
  65. }
  66. private void unregisterBroadcastReceiver(){
  67. if(mReceiverRegistered){
  68. LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(mReceiver);
  69. this.mReceiverRegistered = false;
  70. }
  71. }
  72. public HomeFragment(){}
  73. public void setStateNotActive(boolean initial){
  74. mHomeTextName.setTextColor(getResources().getColor(R.color.light_grey));
  75. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.light_grey));
  76. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.light_grey));
  77. mHomeTextProfile.setTextColor(getResources().getColor(R.color.light_grey));
  78. mHomeTextProfileHeader.setTextColor(getResources().getColor(R.color.light_grey));
  79. if(!initial){
  80. ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.ThreatLevel.NOT_MONITORING);
  81. }
  82. mHomeSwitchConnection.setChecked(false);
  83. }
  84. public void setStateNotActive(){
  85. setStateNotActive(false);
  86. }
  87. public void setStateActive(){
  88. setStateActive(false);
  89. }
  90. public void setStateActive(boolean initial){
  91. mHomeTextAttacks.setVisibility(View.VISIBLE);
  92. mHomeTextSecurity.setVisibility(View.VISIBLE);
  93. mHomeTextName.setTextColor(defaultTextColor);
  94. mHomeTextProfile.setTextColor(defaultTextColor);
  95. mHomeTextProfileHeader.setTextColor(defaultTextColor);
  96. if(!initial){
  97. ThreatIndicatorGLRenderer.setThreatLevel(ThreatIndicatorGLRenderer.ThreatLevel.NO_THREAT);
  98. }
  99. mHomeSwitchConnection.setChecked(true);
  100. }
  101. public void setStateNotConnected(){
  102. mHomeTextSecurity.setVisibility(View.INVISIBLE);
  103. mHomeTextAttacks.setVisibility(View.INVISIBLE);
  104. mHomeTextProfile.setVisibility(View.INVISIBLE);
  105. mHomeTextProfileHeader.setVisibility(View.INVISIBLE);
  106. mHomeProfileImage.setVisibility(View.INVISIBLE);
  107. mHomeConnectionInfoButton.setVisibility(View.INVISIBLE);
  108. mHomeTextName.setText("Not connected");
  109. }
  110. public void setStateConnected(){
  111. mHomeTextProfile.setVisibility(View.VISIBLE);
  112. mHomeTextProfileHeader.setVisibility(View.VISIBLE);
  113. mHomeProfileImage.setVisibility(View.VISIBLE);
  114. mHomeConnectionInfoButton.setVisibility(View.VISIBLE);
  115. }
  116. public void updateUI(){
  117. Profile profile = mProfileManager.getCurrentActivatedProfile();
  118. if(profile != null){
  119. mHomeTextProfile.setText(profile.mLabel);
  120. mHomeProfileImage.setImageBitmap(profile.getIconBitmap());
  121. }
  122. if(HelperUtils.isWifiConnected(getActivity())){
  123. setStateConnected();
  124. mHomeTextName.setText(mConnectionInfo.getString(getString(R.string.connection_info_ssid), ""));
  125. }
  126. boolean hasActiveListeners = false;
  127. int totalAttacks = 0;
  128. int totalLogged = 0;
  129. if(MainActivity.getInstance().isServiceBound()){
  130. for(String protocol: getResources().getStringArray(R.array.protocols)){
  131. if(MainActivity.getInstance().getHoneyService().isRunning(protocol)){
  132. hasActiveListeners = true;
  133. int attacks = MainActivity.getInstance().getHoneyService().getNumberOfActiveConnections(protocol);
  134. totalAttacks += attacks;
  135. if(attacks == 0){
  136. int log_attacks = dbh.numBssidSeen(protocol, mConnectionInfo.getString(getString(R.string.connection_info_bssid), null));
  137. totalLogged += log_attacks;
  138. if(log_attacks > 0){
  139. if(THREAT_LEVEL != ThreatIndicatorGLRenderer.ThreatLevel.LIVE_THREAT){
  140. THREAT_LEVEL = ThreatIndicatorGLRenderer.ThreatLevel.PAST_THREAT;
  141. }
  142. } else {
  143. if(THREAT_LEVEL != ThreatIndicatorGLRenderer.ThreatLevel.LIVE_THREAT && THREAT_LEVEL != ThreatIndicatorGLRenderer.ThreatLevel.PAST_THREAT){
  144. THREAT_LEVEL = ThreatIndicatorGLRenderer.ThreatLevel.NO_THREAT;
  145. }
  146. }
  147. } else {
  148. THREAT_LEVEL = ThreatIndicatorGLRenderer.ThreatLevel.LIVE_THREAT;
  149. }
  150. }
  151. }
  152. }
  153. if(hasActiveListeners){
  154. setStateActive(true);
  155. switch(THREAT_LEVEL){
  156. case NO_THREAT:
  157. mHomeTextAttacks.setText("0 attacks recorded");
  158. mHomeTextSecurity.setText("Secure");
  159. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_dark_green));
  160. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_dark_green));
  161. break;
  162. case PAST_THREAT:
  163. mHomeTextAttacks.setText(totalLogged + (totalLogged == 1 ? " attack" : " attacks") + " logged");
  164. mHomeTextSecurity.setText("Insecure");
  165. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_yellow));
  166. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_yellow));
  167. break;
  168. case LIVE_THREAT:
  169. mHomeTextAttacks.setText(totalAttacks + (totalAttacks == 1 ? " attack" : " attacks") + " recorded");
  170. mHomeTextSecurity.setText("Insecure");
  171. mHomeTextAttacks.setTextColor(getResources().getColor(R.color.holo_red));
  172. mHomeTextSecurity.setTextColor(getResources().getColor(R.color.holo_red));
  173. break;
  174. }
  175. ThreatIndicatorGLRenderer.setThreatLevel(THREAT_LEVEL);
  176. } else {
  177. mHomeSwitchConnection.setOnCheckedChangeListener(null);
  178. setStateNotActive();
  179. THREAT_LEVEL = ThreatIndicatorGLRenderer.ThreatLevel.NO_THREAT;
  180. if(!HelperUtils.isWifiConnected(getActivity())){
  181. setStateNotConnected();
  182. }
  183. mHomeSwitchConnection.setOnCheckedChangeListener(switchChangeListener);
  184. }
  185. }
  186. @Override
  187. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  188. Bundle savedInstanceState) {
  189. super.onCreateView(inflater, container, savedInstanceState);
  190. getActivity().setTitle(getResources().getString(R.string.drawer_overview));
  191. dbh = new UglyDbHelper(getActivity());
  192. mProfileManager = ProfileManager.getInstance();
  193. mConnectionInfo = getActivity().getSharedPreferences(getString(R.string.connection_info), Context.MODE_PRIVATE);
  194. rootView = inflater.inflate(R.layout.fragment_home, container, false);
  195. assignViews();
  196. // hook up the connection info button
  197. mHomeConnectionInfoButton.setOnClickListener(new View.OnClickListener() {
  198. @Override
  199. public void onClick(View v) {
  200. ConnectionInfoDialogFragment connectionInfoDialogFragment = new ConnectionInfoDialogFragment();
  201. connectionInfoDialogFragment.show(getFragmentManager().beginTransaction(), connectionInfoDialogFragment.getTag());
  202. }
  203. });
  204. defaultTextColor = mHomeTextName.getCurrentTextColor();
  205. setStateNotActive(true);
  206. setStateNotConnected();
  207. mReceiver = new BroadcastReceiver() {
  208. @Override
  209. public void onReceive(Context context, Intent intent) {
  210. updateUI();
  211. }
  212. };
  213. updateUI();
  214. final String[] protocols = getResources().getStringArray(R.array.protocols);
  215. mHomeSwitchConnection = (Switch) rootView.findViewById(R.id.home_switch_connection);
  216. if(switchChangeListener == null){
  217. switchChangeListener = new CompoundButton.OnCheckedChangeListener() {
  218. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  219. if(!HelperUtils.isWifiConnected(getActivity())){
  220. new AlertDialog.Builder(getActivity())
  221. .setTitle("Information")
  222. .setMessage("You are not connected to a WiFi network. \n\nPlease connect to one, before trying to activate HosTaGe.")
  223. .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  224. public void onClick(DialogInterface dialog, int which) {
  225. }
  226. })
  227. .setIcon(android.R.drawable.ic_dialog_info)
  228. .show();
  229. setStateNotActive();
  230. setStateNotConnected();
  231. } else {
  232. if(isChecked){
  233. for(String protocol: protocols){
  234. if(!MainActivity.getInstance().getHoneyService().isRunning(protocol)) MainActivity.getInstance().getHoneyService().startListener(protocol);
  235. }
  236. setStateActive();
  237. } else {
  238. MainActivity.getInstance().getHoneyService().stopListeners();
  239. MainActivity.getInstance().stopAndUnbind();
  240. setStateNotActive();
  241. }
  242. }
  243. }
  244. };
  245. }
  246. mHomeSwitchConnection.setOnCheckedChangeListener(switchChangeListener);
  247. rootView.findViewById(R.id.home_profile_details).setOnClickListener(
  248. new View.OnClickListener() {
  249. @Override
  250. public void onClick(View v) {
  251. Fragment fragment = new ProfileManagerFragment();
  252. MainActivity.getInstance().injectFragment(fragment, false);
  253. }
  254. }
  255. );
  256. return rootView;
  257. }
  258. @Override
  259. public void onStart(){
  260. super.onStart();
  261. registerBroadcastReceiver();
  262. }
  263. @Override
  264. public void onStop(){
  265. super.onStop();
  266. unregisterBroadcastReceiver();
  267. }
  268. @Override
  269. public void onDestroy(){
  270. unregisterBroadcastReceiver();
  271. super.onDestroy();
  272. }
  273. }