ServicesListAdapter.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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.ui2.activity.MainActivity;
  17. import de.tudarmstadt.informatik.hostage.ui2.model.ServicesListItem;
  18. /**
  19. * @author Daniel Lazar
  20. * @created 06.02.14.
  21. * a list adapter for loading switches and service information asynchronously
  22. */
  23. public class ServicesListAdapter extends ArrayAdapter<ServicesListItem> {
  24. private final Context context;
  25. private final List<ServicesListItem> values;
  26. int sdk = Build.VERSION.SDK_INT;
  27. private Context mActivity;
  28. private Switch mServicesSwitch;
  29. private CompoundButton.OnCheckedChangeListener mListener;
  30. /**
  31. * constructor
  32. *
  33. * @param context Context of the current activity
  34. * @param objects List of ServicesListItem which contains all the protocols
  35. */
  36. public ServicesListAdapter(Context context, List<ServicesListItem> objects) {
  37. super(context, R.layout.services_list_item, objects);
  38. this.context = context;
  39. this.values = objects;
  40. }
  41. /**
  42. * method to save important information from parent fragment
  43. *
  44. * @param activity activicty from parent fragment
  45. * @param servicesSwitch the switch from parent fragment
  46. * @param mainListener Listener from parent fragment
  47. */
  48. public void setActivity(Context activity, Switch servicesSwitch, CompoundButton.OnCheckedChangeListener mainListener) {
  49. mActivity = activity;
  50. mServicesSwitch = servicesSwitch;
  51. mListener = mainListener;
  52. }
  53. /**
  54. * main method of ServicesListAdapter which initializes the holder if null
  55. * Also activates protocols and switches
  56. *
  57. * @param position current position in list
  58. * @param convertView
  59. * @param parent
  60. * @return
  61. */
  62. @Override
  63. public View getView(final int position, View convertView, ViewGroup parent) {
  64. LayoutInflater inflater = (LayoutInflater) context
  65. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  66. View rowView = convertView;
  67. ViewHolder holder;
  68. final ServicesListItem item = values.get(position);
  69. if (rowView == null) {
  70. rowView = inflater.inflate(R.layout.services_list_item, parent, false);
  71. holder = new ViewHolder();
  72. assert rowView != null;
  73. holder.protocolName = (TextView) rowView.findViewById(R.id.services_item_name);
  74. holder.recordedAttacks = (TextView) rowView.findViewById(R.id.services_item_rec_attacks);
  75. holder.activated = (Switch) rowView.findViewById(R.id.services_item_switch);
  76. holder.circle = rowView.findViewById(R.id.services_circle);
  77. rowView.setTag(holder);
  78. } else {
  79. holder = (ViewHolder) rowView.getTag();
  80. }
  81. holder.protocolName.setText(item.protocol);
  82. holder.activated.setTag(item);
  83. this.updateStatus(item, holder);
  84. holder.activated.setOnCheckedChangeListener(
  85. new CompoundButton.OnCheckedChangeListener() {
  86. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  87. ServicesListItem item = (ServicesListItem) buttonView.getTag();
  88. if (!HelperUtils.isWifiConnected(mActivity)) {
  89. new AlertDialog.Builder(mActivity)
  90. .setTitle(R.string.information)
  91. .setMessage(R.string.wifi_not_connected_msg)
  92. .setPositiveButton(android.R.string.ok,
  93. new DialogInterface.OnClickListener() {
  94. public void onClick(DialogInterface dialog,
  95. int which) {
  96. }
  97. }
  98. )
  99. .setIcon(android.R.drawable.ic_dialog_info).show();
  100. buttonView.setChecked(false);
  101. } else {
  102. //check if switch is set to ON and start the concrete listener for the protocol
  103. if (isChecked) {
  104. if (!MainActivity.getInstance().getHostageService().isRunning(item.protocol)) {
  105. MainActivity.getInstance().getHostageService().startListener(item.protocol);
  106. //set the main switch to null, so that he won't react and starts all protocols
  107. mServicesSwitch.setOnCheckedChangeListener(null);
  108. mServicesSwitch.setChecked(true);
  109. mServicesSwitch.setOnCheckedChangeListener(mListener);
  110. buttonView.setChecked(true);
  111. } else if (MainActivity.getInstance().getHostageService().isRunning(item.protocol)) {
  112. buttonView.setChecked(true);
  113. }
  114. } else {
  115. if (MainActivity.getInstance().getHostageService().isRunning(item.protocol)) {
  116. MainActivity.getInstance().getHostageService().stopListener(item.protocol);
  117. }
  118. buttonView.setChecked(false);
  119. }
  120. }
  121. }
  122. }
  123. );
  124. return rowView;
  125. }
  126. /**
  127. * method to update the current status, which includes changing the attack indication circle and the number of attacks
  128. *
  129. * @param item ServiceListItem which has information about current item, e.g. protocol, activated, attacks
  130. * @param holder ViewHolder which represents the item in the View
  131. */
  132. private void updateStatus(ServicesListItem item, ViewHolder holder) {
  133. if (MainActivity.getInstance().getHostageService().isRunning(item.protocol)) {
  134. holder.activated.setChecked(true);
  135. if (!MainActivity.getInstance().getHostageService().hasProtocolActiveAttacks(item.protocol)) {
  136. if (item.attacks > 0) {
  137. setBackground(holder, R.drawable.services_circle_yellow);
  138. } else {
  139. setBackground(holder, R.drawable.services_circle_green);
  140. }
  141. } else {
  142. if (MainActivity.getInstance().getHostageService().hasProtocolActiveAttacks(item.protocol)) {
  143. setBackground(holder, R.drawable.services_circle_red);
  144. }
  145. }
  146. } else if (item.attacks > 0) {
  147. holder.activated.setChecked(false);
  148. setBackground(holder, R.drawable.services_circle_yellow);
  149. } else {
  150. holder.activated.setChecked(false);
  151. setBackground(holder, R.drawable.services_circle);
  152. }
  153. holder.recordedAttacks
  154. .setText(String.format(MainActivity.getContext().getResources().getString(R.string.recorded_attacks) + " %d", Integer.valueOf(item.attacks)));
  155. }
  156. /**
  157. * changes the indicator circle of a service
  158. *
  159. * @param holder ViewHolder which represents the item in the View
  160. * @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
  161. */
  162. private void setBackground(ViewHolder holder, int drawable) {
  163. if (sdk < Build.VERSION_CODES.JELLY_BEAN) {
  164. holder.circle.setBackgroundDrawable(MainActivity.getInstance().getResources().getDrawable(drawable));
  165. } else {
  166. holder.circle.setBackground(MainActivity.getInstance().getResources().getDrawable(drawable));
  167. }
  168. }
  169. /**
  170. * ViewHolder stands for a row in the view
  171. */
  172. private class ViewHolder {
  173. public TextView protocolName;
  174. public TextView recordedAttacks;
  175. public Switch activated;
  176. public View circle;
  177. }
  178. }