ServicesListAdapter.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package de.tudarmstadt.informatik.hostage.ui2.adapter;
  2. import java.util.List;
  3. import android.app.AlertDialog;
  4. import android.content.Context;
  5. import android.content.DialogInterface;
  6. import android.os.Build;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.ArrayAdapter;
  11. import android.widget.CompoundButton;
  12. import android.widget.Switch;
  13. import android.widget.TextView;
  14. import de.tudarmstadt.informatik.hostage.R;
  15. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  16. import de.tudarmstadt.informatik.hostage.model.Profile;
  17. import de.tudarmstadt.informatik.hostage.persistence.ProfileManager;
  18. import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
  19. import de.tudarmstadt.informatik.hostage.ui2.model.ServicesListItem;
  20. /**
  21. * @author Daniel Lazar
  22. * @created 06.02.14.
  23. * a list adapter for loading switches and service information asynchronously
  24. */
  25. public class ServicesListAdapter extends ArrayAdapter<ServicesListItem> {
  26. private final Context context;
  27. private final List<ServicesListItem> values;
  28. int sdk = Build.VERSION.SDK_INT;
  29. private Context mActivity;
  30. private Switch mServicesSwitch;
  31. private CompoundButton.OnCheckedChangeListener mListener;
  32. private Profile mProfile;
  33. private Integer[] mGhostPorts;
  34. /**
  35. * constructor
  36. *
  37. * @param context Context of the current activity
  38. * @param objects List of ServicesListItem which contains all the protocols
  39. */
  40. public ServicesListAdapter(Context context, List<ServicesListItem> objects) {
  41. super(context, R.layout.services_list_item, objects);
  42. this.context = context;
  43. this.values = objects;
  44. }
  45. /**
  46. * method to save important information from parent fragment
  47. *
  48. * @param activity activicty from parent fragment
  49. * @param servicesSwitch the switch from parent fragment
  50. * @param mainListener Listener from parent fragment
  51. */
  52. public void setActivity(Context activity, Switch servicesSwitch, CompoundButton.OnCheckedChangeListener mainListener) {
  53. mActivity = activity;
  54. mServicesSwitch = servicesSwitch;
  55. mListener = mainListener;
  56. }
  57. /**
  58. * main method of ServicesListAdapter which initializes the holder if null
  59. * Also activates protocols and switches
  60. *
  61. * @param position current position in list
  62. * @param convertView
  63. * @param parent
  64. * @return
  65. */
  66. @Override
  67. public View getView(final int position, View convertView, ViewGroup parent) {
  68. LayoutInflater inflater = (LayoutInflater) context
  69. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  70. View rowView = convertView;
  71. ViewHolder holder;
  72. final ServicesListItem item = values.get(position);
  73. if (rowView == null) {
  74. rowView = inflater.inflate(R.layout.services_list_item, parent, false);
  75. holder = new ViewHolder();
  76. assert rowView != null;
  77. holder.protocolName = (TextView) rowView.findViewById(R.id.services_item_name);
  78. holder.recordedAttacks = (TextView) rowView.findViewById(R.id.services_item_rec_attacks);
  79. holder.activated = (Switch) rowView.findViewById(R.id.services_item_switch);
  80. holder.circle = rowView.findViewById(R.id.services_circle);
  81. rowView.setTag(holder);
  82. } else {
  83. holder = (ViewHolder) rowView.getTag();
  84. }
  85. holder.protocolName.setText(item.protocol);
  86. holder.activated.setTag(item);
  87. this.updateStatus(item, holder);
  88. holder.activated.setOnCheckedChangeListener(
  89. new CompoundButton.OnCheckedChangeListener() {
  90. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  91. ServicesListItem item = (ServicesListItem) buttonView.getTag();
  92. mProfile = ProfileManager.getInstance().getCurrentActivatedProfile();
  93. if (!HelperUtils.isNetworkAvailable(mActivity)) {
  94. new AlertDialog.Builder(mActivity)
  95. .setTitle(R.string.information)
  96. .setMessage(R.string.wifi_not_connected_msg)
  97. .setPositiveButton(android.R.string.ok,
  98. new DialogInterface.OnClickListener() {
  99. public void onClick(DialogInterface dialog,
  100. int which) {
  101. }
  102. }
  103. )
  104. .setIcon(android.R.drawable.ic_dialog_info).show();
  105. buttonView.setChecked(false);
  106. } else {
  107. //check if switch is set to ON and start the concrete listener for the protocol
  108. if (isChecked) {
  109. if(item.protocol.equals("GHOST")){
  110. if(mProfile.mGhostActive){
  111. mGhostPorts = mProfile.getGhostPorts();
  112. if(mGhostPorts.length != 0) {
  113. for(Integer port: mGhostPorts){
  114. if(!MainActivity.getInstance().getHostageService().isRunning(item.protocol, port)) {
  115. MainActivity.getInstance().getHostageService().startListener(item.protocol, port);
  116. }
  117. }
  118. //set the main switch to null, so that he won't react and starts all protocols
  119. mServicesSwitch.setOnCheckedChangeListener(null);
  120. mServicesSwitch.setChecked(true);
  121. mServicesSwitch.setOnCheckedChangeListener(mListener);
  122. buttonView.setChecked(true);
  123. }
  124. }
  125. else {
  126. buttonView.setChecked(false);
  127. }
  128. }
  129. else if (!MainActivity.getInstance().getHostageService().isRunning(item.protocol)) {
  130. MainActivity.getInstance().getHostageService().startListener(item.protocol);
  131. //set the main switch to null, so that he won't react and starts all protocols
  132. mServicesSwitch.setOnCheckedChangeListener(null);
  133. mServicesSwitch.setChecked(true);
  134. mServicesSwitch.setOnCheckedChangeListener(mListener);
  135. buttonView.setChecked(true);
  136. } else {
  137. buttonView.setChecked(true);
  138. }
  139. } else {
  140. if(item.protocol.equals("GHOST")) {
  141. mGhostPorts = mProfile.getGhostPorts();
  142. for(Integer port: mGhostPorts){
  143. if(port != null) {
  144. if(MainActivity.getInstance().getHostageService().isRunning("GHOST",port)){
  145. MainActivity.getInstance().getHostageService().stopListener("GHOST", port);
  146. }
  147. }
  148. }
  149. buttonView.setChecked(false);
  150. }
  151. else if (MainActivity.getInstance().getHostageService().isRunning(item.protocol)) {
  152. MainActivity.getInstance().getHostageService().stopListener(item.protocol);
  153. }
  154. buttonView.setChecked(false);
  155. }
  156. }
  157. }
  158. }
  159. );
  160. return rowView;
  161. }
  162. /**
  163. * method to update the current status, which includes changing the attack indication circle and the number of attacks
  164. *
  165. * @param item ServiceListItem which has information about current item, e.g. protocol, activated, attacks
  166. * @param holder ViewHolder which represents the item in the View
  167. */
  168. private void updateStatus(ServicesListItem item, ViewHolder holder) {
  169. if(item.protocol.equals("GHOST")){
  170. mProfile = ProfileManager.getInstance().getCurrentActivatedProfile();
  171. mGhostPorts = mProfile.getGhostPorts();
  172. boolean ghostActive = false;
  173. if(mGhostPorts.length != 0) {
  174. for (Integer port : mGhostPorts) {
  175. if(port != null){
  176. if (MainActivity.getInstance().getHostageService().isRunning("GHOST", port)) {
  177. ghostActive = true;
  178. }
  179. }
  180. }
  181. }
  182. if(ghostActive){
  183. holder.activated.setChecked(true);
  184. if (!MainActivity.getInstance().getHostageService().hasProtocolActiveAttacks(item.protocol)) {
  185. if (item.attacks > 0) {
  186. setBackground(holder, R.drawable.services_circle_yellow);
  187. } else {
  188. setBackground(holder, R.drawable.services_circle_green);
  189. }
  190. } else {
  191. if (MainActivity.getInstance().getHostageService().hasProtocolActiveAttacks(item.protocol)) {
  192. setBackground(holder, R.drawable.services_circle_red);
  193. }
  194. }
  195. } else if (item.attacks > 0) {
  196. holder.activated.setChecked(false);
  197. setBackground(holder, R.drawable.services_circle_yellow);
  198. } else {
  199. holder.activated.setChecked(false);
  200. setBackground(holder, R.drawable.services_circle);
  201. }
  202. }
  203. else if (MainActivity.getInstance().getHostageService().isRunning(item.protocol)) {
  204. holder.activated.setChecked(true);
  205. if (!MainActivity.getInstance().getHostageService().hasProtocolActiveAttacks(item.protocol)) {
  206. if (item.attacks > 0) {
  207. setBackground(holder, R.drawable.services_circle_yellow);
  208. } else {
  209. setBackground(holder, R.drawable.services_circle_green);
  210. }
  211. } else {
  212. if (MainActivity.getInstance().getHostageService().hasProtocolActiveAttacks(item.protocol)) {
  213. setBackground(holder, R.drawable.services_circle_red);
  214. }
  215. }
  216. } else if (item.attacks > 0) {
  217. holder.activated.setChecked(false);
  218. setBackground(holder, R.drawable.services_circle_yellow);
  219. } else {
  220. holder.activated.setChecked(false);
  221. setBackground(holder, R.drawable.services_circle);
  222. }
  223. holder.recordedAttacks
  224. .setText(String.format(MainActivity.getContext().getResources().getString(R.string.recorded_attacks) + " %d", Integer.valueOf(item.attacks)));
  225. }
  226. /**
  227. * changes the indicator circle of a service
  228. *
  229. * @param holder ViewHolder which represents the item in the View
  230. * @param drawable int which represents the ID of the drawable we want to display, e.g. on a present attack it should be R.drawable.services_circle_red
  231. */
  232. private void setBackground(ViewHolder holder, int drawable) {
  233. if (sdk < Build.VERSION_CODES.JELLY_BEAN) {
  234. holder.circle.setBackgroundDrawable(MainActivity.getInstance().getResources().getDrawable(drawable));
  235. } else {
  236. holder.circle.setBackground(MainActivity.getInstance().getResources().getDrawable(drawable));
  237. }
  238. }
  239. /**
  240. * ViewHolder stands for a row in the view
  241. */
  242. private class ViewHolder {
  243. public TextView protocolName;
  244. public TextView recordedAttacks;
  245. public Switch activated;
  246. public View circle;
  247. }
  248. }