ServicesListAdapter.java 12 KB

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