ProfileManager.java 7.8 KB

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