ProfileManager.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. package de.tudarmstadt.informatik.hostage.dao;
  2. import android.content.Context;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.ObjectInputStream;
  8. import java.io.ObjectOutputStream;
  9. import java.io.Serializable;
  10. import java.io.StreamCorruptedException;
  11. import java.util.ArrayList;
  12. import java.util.Collection;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16. import de.tudarmstadt.informatik.hostage.R;
  17. import de.tudarmstadt.informatik.hostage.model.Profile;
  18. import de.tudarmstadt.informatik.hostage.model.ProfilesHolder;
  19. import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
  20. import de.tudarmstadt.informatik.hostage.ui2.adapter.ProfileManagerListAdapter;
  21. /**
  22. * @author Alexander Brakowski
  23. * @created 10.02.14 20:24
  24. */
  25. public class ProfileManager {
  26. private static ProfileManager INSTANCE = null;
  27. private ProfileManagerListAdapter mProfileListAdapter = null;
  28. private Profile mCurrentActivatedProfile = null;
  29. private Profile mRandomProfile = null;
  30. private static final String PERSIST_FILENAME = "hostage_profiles.dat";
  31. private ProfilesHolder holder;
  32. public static ProfileManager getInstance(){
  33. if(INSTANCE == null){
  34. INSTANCE = new ProfileManager();
  35. }
  36. if(INSTANCE.getNumberOfProfiles() == 0){
  37. INSTANCE.loadData();
  38. }
  39. return INSTANCE;
  40. }
  41. private ProfileManager(){
  42. holder = new ProfilesHolder();
  43. holder.mProfiles = new HashMap<Integer, Profile>();
  44. }
  45. public void loadData(){
  46. try {
  47. FileInputStream fin = MainActivity.getContext().openFileInput(PERSIST_FILENAME);
  48. ObjectInputStream ois = new ObjectInputStream(fin);
  49. Object obj = ois.readObject();
  50. ois.close();
  51. fin.close();
  52. if(obj != null && obj instanceof ProfilesHolder){
  53. this.holder = (ProfilesHolder) obj;
  54. for(Map.Entry<Integer, Profile> entry: holder.mProfiles.entrySet()){
  55. if(entry.getValue().mActivated){
  56. this.mCurrentActivatedProfile = entry.getValue();
  57. }
  58. if(entry.getValue().mIsRandom){
  59. this.mRandomProfile = entry.getValue();
  60. }
  61. }
  62. }
  63. } catch (FileNotFoundException e) {
  64. e.printStackTrace();
  65. } catch (StreamCorruptedException e) {
  66. e.printStackTrace();
  67. } catch (IOException e) {
  68. e.printStackTrace();
  69. } catch (ClassNotFoundException e) {
  70. e.printStackTrace();
  71. } finally {
  72. if(holder.mProfiles.size() == 0){
  73. this.fillWithSampleData();
  74. loadData();
  75. }
  76. if(this.mRandomProfile != null){
  77. randomizeProtocols(mRandomProfile);
  78. }
  79. }
  80. }
  81. public void persistData(){
  82. try {
  83. FileOutputStream fout = MainActivity.getContext().openFileOutput(PERSIST_FILENAME, Context.MODE_PRIVATE);
  84. ObjectOutputStream oos = new ObjectOutputStream(fout);
  85. oos.writeObject(holder);
  86. oos.close();
  87. fout.close();
  88. } catch (FileNotFoundException e) {
  89. e.printStackTrace();
  90. } catch (IOException e) {
  91. e.printStackTrace();
  92. }
  93. }
  94. public List<Profile> getProfilesList(){
  95. return new ArrayList<Profile>(getProfilesCollection());
  96. }
  97. public Collection<Profile> getProfilesCollection(){
  98. if(holder.mProfiles.size() == 0 || holder.mProfiles == null){
  99. this.loadData();
  100. }
  101. return holder.mProfiles.values();
  102. }
  103. public Map<Integer, Profile> getMapProfiles(){
  104. return holder.mProfiles;
  105. }
  106. public void randomizeProtocols(Profile profile){
  107. for(String protocol: MainActivity.getContext().getResources().getStringArray(R.array.protocols)){
  108. double rand = Math.random();
  109. if(rand > 0.4){
  110. profile.mActiveProtocols.put(protocol, true);
  111. } else {
  112. profile.mActiveProtocols.put(protocol, false);
  113. }
  114. }
  115. persistData();
  116. }
  117. public long persistProfile(Profile profile){
  118. if(profile.mId == -1){
  119. profile.mId = ++holder.mIncrementValue;
  120. }
  121. holder.mProfiles.put(profile.mId, profile);
  122. this.persistData();
  123. if(this.mProfileListAdapter != null){
  124. this.mProfileListAdapter.notifyDataSetChanged();
  125. }
  126. return profile.mId;
  127. }
  128. public Profile getProfile(int id){
  129. if(holder.mProfiles.size() == 0){
  130. loadData();
  131. }
  132. if(this.holder.mProfiles.containsKey(id)){
  133. return this.holder.mProfiles.get(id);
  134. }
  135. return null;
  136. }
  137. public void addProfile(Profile profile){
  138. this.addProfile(profile, true);
  139. }
  140. public void addProfile(Profile profile, boolean persist){
  141. if(profile.mId == -1){
  142. profile.mId = ++holder.mIncrementValue;
  143. }
  144. holder.mProfiles.put(profile.mId, profile);
  145. if(persist){
  146. persistData();
  147. }
  148. if(this.mProfileListAdapter != null){
  149. this.mProfileListAdapter.add(profile);
  150. this.mProfileListAdapter.notifyDataSetChanged();
  151. }
  152. }
  153. public void deleteProfile(Profile profile){
  154. if(this.holder.mProfiles.containsKey(profile.mId)){
  155. this.holder.mProfiles.remove(profile.mId);
  156. this.persistData();
  157. //this.dbh.deleteProfile(profile.mId);
  158. if(this.mProfileListAdapter != null){
  159. this.mProfileListAdapter.remove(profile);
  160. this.mProfileListAdapter.notifyDataSetChanged();
  161. }
  162. }
  163. }
  164. public void clearProfiles(){
  165. holder.mProfiles.clear();
  166. persistData();
  167. }
  168. public void activateProfile(Profile profile){
  169. if(profile.equals(this.mCurrentActivatedProfile)) return;
  170. if(this.mCurrentActivatedProfile != null){
  171. this.mCurrentActivatedProfile.mActivated = false;
  172. this.persistProfile(this.mCurrentActivatedProfile);
  173. }
  174. profile.mActivated = true;
  175. this.mCurrentActivatedProfile = profile;
  176. this.holder.mProfiles.put(profile.mId, profile);
  177. persistData();
  178. }
  179. public Profile getCurrentActivatedProfile(){
  180. return mCurrentActivatedProfile;
  181. }
  182. public void setProfileListAdapter(ProfileManagerListAdapter profileListAdapter){
  183. this.mProfileListAdapter = profileListAdapter;
  184. }
  185. public ProfileManagerListAdapter getProfileListAdapter(){
  186. return this.mProfileListAdapter;
  187. }
  188. public int getNumberOfProfiles(){
  189. return holder.mProfiles.size();
  190. }
  191. public void fillWithSampleData(){
  192. this.addProfile(new Profile(
  193. 0,
  194. "Windows Vista",
  195. "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
  196. R.drawable.ic_profile_vista,
  197. false
  198. ), false);
  199. this.addProfile(new Profile(
  200. 1,
  201. "Windows 7",
  202. "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
  203. R.drawable.ic_profile_w7,
  204. false
  205. ), false);
  206. this.addProfile(new Profile(
  207. 2,
  208. "Unix Distro",
  209. "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
  210. R.drawable.ic_profile_unix,
  211. false
  212. ), false);
  213. Profile randomProfile = new Profile(
  214. 3,
  215. "Random",
  216. "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
  217. R.drawable.ic_service_green,
  218. false
  219. );
  220. randomProfile.mIsRandom = true;
  221. this.addProfile(randomProfile, false);
  222. this.addProfile(new Profile(
  223. 4,
  224. "Mix",
  225. "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.",
  226. R.drawable.ic_service_green,
  227. false
  228. ), false);
  229. holder.mIncrementValue = 4;
  230. persistData();
  231. }
  232. }