ProfileManager.java 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package de.tudarmstadt.informatik.hostage.dao;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import de.tudarmstadt.informatik.hostage.R;
  8. import de.tudarmstadt.informatik.hostage.logging.UglyDbHelper;
  9. import de.tudarmstadt.informatik.hostage.model.Profile;
  10. import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
  11. import de.tudarmstadt.informatik.hostage.ui2.adapter.ProfileManagerListAdapter;
  12. /**
  13. * @author Alexander Brakowski
  14. * @created 10.02.14 20:24
  15. */
  16. public class ProfileManager {
  17. private static ProfileManager INSTANCE = null;
  18. private Map<Integer, Profile> mProfiles;
  19. private ProfileManagerListAdapter mProfileListAdapter = null;
  20. private int mProfileId = 0;
  21. private Profile mCurrentActivatedProfile = null;
  22. private UglyDbHelper dbh;
  23. public static ProfileManager getInstance(){
  24. if(INSTANCE == null){
  25. INSTANCE = new ProfileManager();
  26. }
  27. return INSTANCE;
  28. }
  29. private ProfileManager(){
  30. this.mProfiles = new HashMap<Integer, Profile>();
  31. this.dbh = new UglyDbHelper(MainActivity.getContext());
  32. }
  33. public void loadData(){
  34. Collection<Profile> profiles = this.dbh.getAllProfiles();
  35. if(profiles.size() == 0){
  36. this.fillWithSampleData();
  37. profiles = this.dbh.getAllProfiles();
  38. }
  39. this.mProfiles.clear();
  40. for(Profile p: profiles){
  41. this.mProfiles.put(p.mId, p);
  42. if(p.mActivated){
  43. this.mCurrentActivatedProfile = p;
  44. }
  45. }
  46. }
  47. public List<Profile> getProfilesList(){
  48. return new ArrayList<Profile>(getProfilesCollection());
  49. }
  50. public Collection<Profile> getProfilesCollection(){
  51. if(mProfiles.size() == 0 || mProfiles == null){
  52. this.loadData();
  53. }
  54. return mProfiles.values();
  55. }
  56. public Map<Integer, Profile> getMapProfiles(){
  57. return mProfiles;
  58. }
  59. public long persistProfile(Profile profile){
  60. int id = (int) this.dbh.persistProfile(profile);
  61. if(profile.mId != id){
  62. profile.mId = id;
  63. }
  64. this.mProfiles.put(id, profile);
  65. return id;
  66. }
  67. public Profile getProfile(int id){
  68. if(this.mProfiles.containsKey(id)){
  69. return this.mProfiles.get(id);
  70. }
  71. Profile profile = this.dbh.getProfile(id);
  72. if(profile != null) return profile;
  73. return null;
  74. }
  75. public void addProfile(Profile profile){
  76. int id = (int) this.dbh.persistProfile(profile);
  77. profile.mId = id;
  78. this.mProfiles.put(id, profile);
  79. if(this.mProfileListAdapter != null){
  80. this.mProfileListAdapter.add(profile);
  81. this.mProfileListAdapter.notifyDataSetChanged();
  82. }
  83. }
  84. public void deleteProfile(Profile profile){
  85. if(this.mProfiles.containsKey(profile.mId)){
  86. this.mProfiles.remove(profile.mId);
  87. this.dbh.deleteProfile(profile.mId);
  88. if(this.mProfileListAdapter != null){
  89. this.mProfileListAdapter.remove(profile);
  90. this.mProfileListAdapter.notifyDataSetChanged();
  91. }
  92. }
  93. }
  94. public void activateProfile(Profile profile){
  95. if(profile.equals(this.mCurrentActivatedProfile)) return;
  96. if(this.mCurrentActivatedProfile != null){
  97. this.mCurrentActivatedProfile.mActivated = false;
  98. this.persistProfile(this.mCurrentActivatedProfile);
  99. }
  100. profile.mActivated = true;
  101. this.mCurrentActivatedProfile = profile;
  102. this.dbh.persistProfile(profile);
  103. }
  104. public Profile getCurrentActivatedProfile(){
  105. return mCurrentActivatedProfile;
  106. }
  107. public void setProfileListAdapter(ProfileManagerListAdapter profileListAdapter){
  108. this.mProfileListAdapter = profileListAdapter;
  109. }
  110. public ProfileManagerListAdapter getProfileListAdapter(){
  111. return this.mProfileListAdapter;
  112. }
  113. public void fillWithSampleData(){
  114. this.addProfile(new Profile(
  115. 0,
  116. "Windows Vista",
  117. "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
  118. R.drawable.ic_profile_vista,
  119. false
  120. ));
  121. this.addProfile(new Profile(
  122. 1,
  123. "Windows 7",
  124. "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
  125. R.drawable.ic_profile_w7,
  126. false
  127. ));
  128. this.addProfile(new Profile(
  129. 2,
  130. "Unix Distro",
  131. "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
  132. R.drawable.ic_profile_unix,
  133. false
  134. ));
  135. this.addProfile(new Profile(
  136. 3,
  137. "Random",
  138. "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
  139. R.drawable.ic_service_green,
  140. false
  141. ));
  142. this.addProfile(new Profile(
  143. 4,
  144. "Mix",
  145. "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
  146. R.drawable.ic_service_green,
  147. false
  148. ));
  149. }
  150. }