ProfileManagerListAdapter.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package de.tudarmstadt.informatik.hostage.ui2.adapter;
  2. import android.app.AlertDialog;
  3. import android.content.Context;
  4. import android.content.DialogInterface;
  5. import android.content.Intent;
  6. import android.graphics.BitmapFactory;
  7. import android.view.ContextThemeWrapper;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11. import android.widget.ArrayAdapter;
  12. import android.widget.ImageButton;
  13. import android.widget.ImageView;
  14. import android.widget.RelativeLayout;
  15. import android.widget.TextView;
  16. import java.util.LinkedList;
  17. import java.util.List;
  18. import de.tudarmstadt.informatik.hostage.Hostage;
  19. import de.tudarmstadt.informatik.hostage.R;
  20. import de.tudarmstadt.informatik.hostage.persistence.ProfileManager;
  21. import de.tudarmstadt.informatik.hostage.model.Profile;
  22. import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
  23. import de.tudarmstadt.informatik.hostage.ui2.activity.ProfileEditActivity;
  24. import de.tudarmstadt.informatik.hostage.ui2.layouts.FlowLayout;
  25. import de.tudarmstadt.informatik.hostage.ui2.swipelist.SwipeListView;
  26. /**
  27. * This adapter creates the item views for the profile manager by making use the viewholder pattern
  28. *
  29. * @author Alexander Brakowski
  30. * @created 14.01.14 18:00
  31. */
  32. public class ProfileManagerListAdapter extends ArrayAdapter<Profile> {
  33. /**
  34. * Holds our views, to reduce the number of view lookups immensly
  35. */
  36. private class ViewHolder {
  37. public TextView mLabelView;
  38. public TextView mTextView;
  39. public ImageView mImageSelected;
  40. public ImageView mItemIcon;
  41. public ImageButton mButtonEdit;
  42. public ImageButton mButtonDelete;
  43. public View mSeperator;
  44. public FlowLayout mBadgesContainer;
  45. }
  46. /**
  47. * The context nedded for resource lookups
  48. */
  49. private final Context mContext;
  50. /**
  51. * The profiles to display in the list
  52. */
  53. private final List<Profile> mValues;
  54. /**
  55. * A reference to the list view itself
  56. */
  57. private SwipeListView mList;
  58. /**
  59. * A simple constructor to propagate this object with neccessary references to needed objects
  60. *
  61. * @param context needed for resource lookups
  62. * @param objects the profiles to display
  63. * @param list a reference to the list view
  64. */
  65. public ProfileManagerListAdapter(Context context, List<Profile> objects, SwipeListView list) {
  66. super(context, R.layout.profile_manager_list_item, objects);
  67. this.mContext = context;
  68. this.mValues = objects;
  69. this.mList = list;
  70. }
  71. /**
  72. * {@inheritDoc}
  73. */
  74. @Override
  75. public View getView(final int position, View convertView, ViewGroup parent) {
  76. LayoutInflater inflater = (LayoutInflater) mContext
  77. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  78. View rowView = convertView;
  79. ViewHolder holder = null;
  80. final Profile item = mValues.get(position);
  81. // if the current item has the show tooltip flag set, render this item as an tooltip view
  82. if(item.mShowTooltip){
  83. rowView = inflater.inflate(R.layout.profile_manager_list_item_help, parent, false);
  84. rowView.findViewById(R.id.profile_manager_help_dismiss).setOnClickListener(new View.OnClickListener() {
  85. @Override
  86. public void onClick(View v) {
  87. ProfileManagerListAdapter.this.remove(item);
  88. ProfileManagerListAdapter.this.notifyDataSetChanged();
  89. mList.dismiss(position);
  90. // just show the tooltip as long as it was not dismissed
  91. MainActivity.getContext().getSharedPreferences(
  92. MainActivity.getContext().getString(R.string.shared_preference_path), Hostage.MODE_PRIVATE
  93. ).edit().putBoolean("dismissedProfileSwipeHelp", true).commit();
  94. }
  95. });
  96. } else {
  97. // put our views into an view holder, if it is new
  98. if (rowView == null || rowView.getTag() == null) {
  99. rowView = inflater.inflate(R.layout.profile_manager_list_item, parent, false);
  100. holder = new ViewHolder();
  101. holder.mLabelView = (TextView) rowView.findViewById(R.id.profile_manager_item_label);
  102. holder.mTextView = (TextView) rowView.findViewById(R.id.profile_manager_item_text);
  103. holder.mImageSelected = (ImageView) rowView.findViewById(R.id.profile_manager_item_activated);
  104. holder.mItemIcon = (ImageView) rowView.findViewById(R.id.profile_manager_item_image);
  105. holder.mButtonEdit = (ImageButton) rowView.findViewById(R.id.profile_manager_item_button_edit);
  106. holder.mButtonDelete = (ImageButton) rowView.findViewById(R.id.profile_manager_item_button_delete);
  107. holder.mSeperator = rowView.findViewById(R.id.profile_manager_item_seperator);
  108. holder.mBadgesContainer = (FlowLayout) rowView.findViewById(R.id.badges_container);
  109. rowView.setTag(holder);
  110. } else {
  111. // save the viewholder to the tag of the view, so we can reuse it later
  112. holder = (ViewHolder) rowView.getTag();
  113. }
  114. // swipe listview needs some cleanup
  115. ((SwipeListView) parent).recycle(rowView, position);
  116. // fill the item view with the correct data
  117. holder.mTextView.setText(item.mText);
  118. holder.mLabelView.setText(item.mLabel);
  119. if (item.getIconBitmap() != null) {
  120. //Bitmap bitmap = Bitmap.createScaledBitmap(item.getIconBitmap(), 32, 32, true);
  121. holder.mItemIcon.setImageBitmap(item.getIconBitmap());
  122. } else {
  123. holder.mItemIcon.setImageBitmap(BitmapFactory.decodeResource(MainActivity.context.getResources(), R.drawable.ic_launcher));
  124. }
  125. // open the profile edit activity, if the edit button was pressed
  126. holder.mButtonEdit.setOnClickListener(new View.OnClickListener() {
  127. @Override
  128. public void onClick(View v) {
  129. Intent intent = new Intent(mContext, ProfileEditActivity.class);
  130. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  131. intent.putExtra("profile_id", item.mId);
  132. mContext.startActivity(intent);
  133. }
  134. });
  135. // delete the profile, if the delete button was pressed. But shows an confirm dialog first.
  136. holder.mButtonDelete.setOnClickListener(new View.OnClickListener() {
  137. @Override
  138. public void onClick(View v) {
  139. new AlertDialog.Builder(mContext)
  140. .setTitle(R.string.delete_profile)
  141. .setMessage(R.string.really_want_delete_profiel)
  142. .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
  143. @Override
  144. public void onClick(DialogInterface dialog, int which) {
  145. }
  146. })
  147. .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
  148. public void onClick(DialogInterface dialog, int which) {
  149. ProfileManager profileManager = ProfileManager.getInstance();
  150. profileManager.deleteProfile(item);
  151. profileManager.getProfileListAdapter().notifyDataSetChanged();
  152. mList.closeOpenedItems();
  153. }
  154. })
  155. .setIcon(android.R.drawable.ic_dialog_alert)
  156. .show();
  157. }
  158. });
  159. // show all the active protocols of an profile in form of badges at the bottom of an profile list item
  160. holder.mBadgesContainer.removeAllViews();
  161. boolean hasProtocols = false;
  162. List<String> profiles = new LinkedList<String>(item.getActiveProtocols());
  163. if (item.mGhostActive) {
  164. profiles.add("GHOST");
  165. }
  166. for (String protocol : profiles) {
  167. hasProtocols = true;
  168. TextView textView = new TextView(new ContextThemeWrapper(mContext, R.style.ProfileManagerListBadge));
  169. textView.setText(protocol);
  170. holder.mBadgesContainer.addView(textView);
  171. }
  172. if (!hasProtocols) {
  173. holder.mBadgesContainer.setVisibility(View.INVISIBLE);
  174. } else {
  175. holder.mBadgesContainer.setVisibility(View.VISIBLE);
  176. }
  177. // do some styling when an profile is flagged as active
  178. RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) holder.mTextView.getLayoutParams();
  179. if (!item.mActivated) {
  180. lp.setMargins(0, 0, 0, 0);
  181. holder.mTextView.setLayoutParams(lp);
  182. holder.mImageSelected.setVisibility(View.GONE);
  183. } else {
  184. holder.mImageSelected.setVisibility(View.VISIBLE);
  185. }
  186. if (!item.isEditable()) {
  187. holder.mButtonDelete.setVisibility(View.GONE);
  188. holder.mSeperator.setVisibility(View.GONE);
  189. } else {
  190. holder.mButtonDelete.setVisibility(View.VISIBLE);
  191. holder.mSeperator.setVisibility(View.VISIBLE);
  192. }
  193. }
  194. return rowView;
  195. }
  196. }