ServicesListAdapter.java 14 KB

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