ServicesListAdapter.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. package de.tudarmstadt.informatik.hostage.ui.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.ui.activity.MainActivity;
  19. import de.tudarmstadt.informatik.hostage.ui.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 convert view
  63. * @param parent the parent view group
  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. if(!MainActivity.getInstance().getHostageService().hasRunningListeners()) {
  95. new AlertDialog.Builder(mActivity)
  96. .setTitle(R.string.information)
  97. .setMessage(R.string.wifi_not_connected_msg)
  98. .setPositiveButton(android.R.string.ok,
  99. new DialogInterface.OnClickListener() {
  100. public void onClick(DialogInterface dialog,
  101. int which) {
  102. }
  103. }
  104. )
  105. .setIcon(android.R.drawable.ic_dialog_info).show();
  106. if (buttonView.isChecked()) {
  107. buttonView.setChecked(false);
  108. }
  109. }
  110. } else {
  111. //check if switch is set to ON and start the concrete listener for the protocol
  112. if (isChecked) {
  113. if(item.protocol.equals("GHOST")){
  114. if(mProfile.mGhostActive){
  115. mGhostPorts = mProfile.getGhostPorts();
  116. if(mGhostPorts.length != 0) {
  117. for(Integer port: mGhostPorts){
  118. if(!MainActivity.getInstance().getHostageService().isRunning(item.protocol, port)) {
  119. MainActivity.getInstance().getHostageService().startListener(item.protocol, port);
  120. }
  121. }
  122. //set the main switch to null, so that he won't react and starts all protocols
  123. mServicesSwitch.setOnCheckedChangeListener(null);
  124. mServicesSwitch.setChecked(true);
  125. mServicesSwitch.setOnCheckedChangeListener(mListener);
  126. if(!buttonView.isChecked()) {
  127. buttonView.setChecked(true);
  128. }
  129. }
  130. }
  131. else {
  132. if(buttonView.isChecked()) {
  133. buttonView.setChecked(false);
  134. }
  135. }
  136. }
  137. else if (!MainActivity.getInstance().getHostageService().isRunning(item.protocol)) {
  138. MainActivity.getInstance().getHostageService().startListener(item.protocol);
  139. //set the main switch to null, so that he won't react and starts all protocols
  140. mServicesSwitch.setOnCheckedChangeListener(null);
  141. mServicesSwitch.setChecked(true);
  142. mServicesSwitch.setOnCheckedChangeListener(mListener);
  143. if(!buttonView.isChecked()) {
  144. buttonView.setChecked(true);
  145. }
  146. } else {
  147. if(!buttonView.isChecked()) {
  148. buttonView.setChecked(true);
  149. }
  150. }
  151. } else {
  152. if(item.protocol.equals("GHOST")) {
  153. mGhostPorts = mProfile.getGhostPorts();
  154. for(Integer port: mGhostPorts){
  155. if(port != null) {
  156. if(MainActivity.getInstance().getHostageService().isRunning("GHOST",port)){
  157. MainActivity.getInstance().getHostageService().stopListener("GHOST", port);
  158. }
  159. }
  160. }
  161. if(buttonView.isChecked()) {
  162. buttonView.setChecked(false);
  163. }
  164. }
  165. else if (MainActivity.getInstance().getHostageService().isRunning(item.protocol)) {
  166. MainActivity.getInstance().getHostageService().stopListener(item.protocol);
  167. }
  168. if(buttonView.isChecked()) {
  169. buttonView.setChecked(false);
  170. }
  171. }
  172. }
  173. }
  174. }
  175. );
  176. return rowView;
  177. }
  178. /**
  179. * method to update the current status, which includes changing the attack indication circle and the number of attacks
  180. *
  181. * @param item ServiceListItem which has information about current item, e.g. protocol, activated, attacks
  182. * @param holder ViewHolder which represents the item in the View
  183. */
  184. private void updateStatus(ServicesListItem item, ViewHolder holder) {
  185. if(item.protocol.equals("GHOST")){
  186. mProfile = ProfileManager.getInstance().getCurrentActivatedProfile();
  187. mGhostPorts = mProfile.getGhostPorts();
  188. boolean ghostActive = false;
  189. if(mGhostPorts.length != 0) {
  190. for (Integer port : mGhostPorts) {
  191. if(port != null){
  192. if (MainActivity.getInstance().getHostageService().isRunning("GHOST", port)) {
  193. ghostActive = true;
  194. }
  195. }
  196. }
  197. }
  198. if(ghostActive){
  199. if(!holder.activated.isChecked()) {
  200. holder.activated.setChecked(true);
  201. }
  202. if (!MainActivity.getInstance().getHostageService().hasProtocolActiveAttacks(item.protocol)) {
  203. if (item.attacks > 0) {
  204. setBackground(holder, R.drawable.services_circle_yellow);
  205. } else {
  206. setBackground(holder, R.drawable.services_circle_green);
  207. }
  208. } else {
  209. if (MainActivity.getInstance().getHostageService().hasProtocolActiveAttacks(item.protocol)) {
  210. setBackground(holder, R.drawable.services_circle_red);
  211. }
  212. }
  213. } else if (item.attacks > 0) {
  214. if(holder.activated.isChecked()) {
  215. holder.activated.setChecked(false);
  216. }
  217. setBackground(holder, R.drawable.services_circle_yellow);
  218. } else {
  219. if(holder.activated.isChecked()) {
  220. holder.activated.setChecked(false);
  221. }
  222. setBackground(holder, R.drawable.services_circle);
  223. }
  224. }
  225. else if (MainActivity.getInstance().getHostageService().isRunning(item.protocol)) {
  226. if(!holder.activated.isChecked()) {
  227. holder.activated.setChecked(true);
  228. }
  229. if (!MainActivity.getInstance().getHostageService().hasProtocolActiveAttacks(item.protocol)) {
  230. if (item.attacks > 0) {
  231. setBackground(holder, R.drawable.services_circle_yellow);
  232. } else {
  233. setBackground(holder, R.drawable.services_circle_green);
  234. }
  235. } else {
  236. if (MainActivity.getInstance().getHostageService().hasProtocolActiveAttacks(item.protocol)) {
  237. setBackground(holder, R.drawable.services_circle_red);
  238. }
  239. }
  240. } else if (item.attacks > 0) {
  241. if(holder.activated.isChecked()) {
  242. holder.activated.setChecked(false);
  243. }
  244. setBackground(holder, R.drawable.services_circle_yellow);
  245. } else {
  246. if(holder.activated.isChecked()) {
  247. holder.activated.setChecked(false);
  248. }
  249. setBackground(holder, R.drawable.services_circle);
  250. }
  251. holder.recordedAttacks
  252. .setText(String.format(MainActivity.getContext().getResources().getString(R.string.recorded_attacks) + " %d", Integer.valueOf(item.attacks)));
  253. }
  254. /**
  255. * changes the indicator circle of a service
  256. *
  257. * @param holder ViewHolder which represents the item in the View
  258. * @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
  259. */
  260. private void setBackground(ViewHolder holder, int drawable) {
  261. if (sdk < Build.VERSION_CODES.JELLY_BEAN) {
  262. holder.circle.setBackgroundDrawable(MainActivity.getInstance().getResources().getDrawable(drawable));
  263. } else {
  264. holder.circle.setBackground(MainActivity.getInstance().getResources().getDrawable(drawable));
  265. }
  266. }
  267. /**
  268. * ViewHolder stands for a row in the view
  269. */
  270. private class ViewHolder {
  271. public TextView protocolName;
  272. public TextView recordedAttacks;
  273. public Switch activated;
  274. public View circle;
  275. }
  276. }