package de.tudarmstadt.informatik.hostage.dao; import android.content.Context; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.io.StreamCorruptedException; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import de.tudarmstadt.informatik.hostage.R; import de.tudarmstadt.informatik.hostage.model.Profile; import de.tudarmstadt.informatik.hostage.model.ProfilesHolder; import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity; import de.tudarmstadt.informatik.hostage.ui2.adapter.ProfileManagerListAdapter; /** * @author Alexander Brakowski * @created 10.02.14 20:24 */ public class ProfileManager { private static ProfileManager INSTANCE = null; private ProfileManagerListAdapter mProfileListAdapter = null; private Profile mCurrentActivatedProfile = null; private Profile mRandomProfile = null; private static final String PERSIST_FILENAME = "hostage_profiles.dat"; private ProfilesHolder holder; public static ProfileManager getInstance(){ if(INSTANCE == null){ INSTANCE = new ProfileManager(); } if(INSTANCE.getNumberOfProfiles() == 0){ INSTANCE.loadData(); } return INSTANCE; } private ProfileManager(){ holder = new ProfilesHolder(); holder.mProfiles = new HashMap(); } public void loadData(){ try { FileInputStream fin = MainActivity.getContext().openFileInput(PERSIST_FILENAME); ObjectInputStream ois = new ObjectInputStream(fin); Object obj = ois.readObject(); ois.close(); fin.close(); if(obj != null && obj instanceof ProfilesHolder){ this.holder = (ProfilesHolder) obj; for(Map.Entry entry: holder.mProfiles.entrySet()){ if(entry.getValue().mActivated){ this.mCurrentActivatedProfile = entry.getValue(); } if(entry.getValue().mIsRandom){ this.mRandomProfile = entry.getValue(); } } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (StreamCorruptedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if(holder.mProfiles.size() == 0){ this.fillWithSampleData(); loadData(); } if(this.mRandomProfile != null){ randomizeProtocols(mRandomProfile); } } } public void persistData(){ try { FileOutputStream fout = MainActivity.getContext().openFileOutput(PERSIST_FILENAME, Context.MODE_PRIVATE); ObjectOutputStream oos = new ObjectOutputStream(fout); oos.writeObject(holder); oos.close(); fout.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public List getProfilesList(){ return new ArrayList(getProfilesCollection()); } public Collection getProfilesCollection(){ if(holder.mProfiles.size() == 0 || holder.mProfiles == null){ this.loadData(); } return holder.mProfiles.values(); } public Map getMapProfiles(){ return holder.mProfiles; } public void randomizeProtocols(Profile profile){ for(String protocol: MainActivity.getContext().getResources().getStringArray(R.array.protocols)){ double rand = Math.random(); if(rand > 0.4){ profile.mActiveProtocols.put(protocol, true); } else { profile.mActiveProtocols.put(protocol, false); } } persistData(); } public long persistProfile(Profile profile){ if(profile.mId == -1){ profile.mId = ++holder.mIncrementValue; } holder.mProfiles.put(profile.mId, profile); this.persistData(); if(this.mProfileListAdapter != null){ this.mProfileListAdapter.notifyDataSetChanged(); } return profile.mId; } public Profile getProfile(int id){ if(holder.mProfiles.size() == 0){ loadData(); } if(this.holder.mProfiles.containsKey(id)){ return this.holder.mProfiles.get(id); } return null; } public void addProfile(Profile profile){ this.addProfile(profile, true); } public void addProfile(Profile profile, boolean persist){ if(profile.mId == -1){ profile.mId = ++holder.mIncrementValue; } holder.mProfiles.put(profile.mId, profile); if(persist){ persistData(); } if(this.mProfileListAdapter != null){ this.mProfileListAdapter.add(profile); this.mProfileListAdapter.notifyDataSetChanged(); } } public void deleteProfile(Profile profile){ if(this.holder.mProfiles.containsKey(profile.mId)){ this.holder.mProfiles.remove(profile.mId); this.persistData(); //this.dbh.deleteProfile(profile.mId); if(this.mProfileListAdapter != null){ this.mProfileListAdapter.remove(profile); this.mProfileListAdapter.notifyDataSetChanged(); } } } public void clearProfiles(){ holder.mProfiles.clear(); persistData(); } public void activateProfile(Profile profile){ if(profile.equals(this.mCurrentActivatedProfile)) return; if(this.mCurrentActivatedProfile != null){ this.mCurrentActivatedProfile.mActivated = false; this.persistProfile(this.mCurrentActivatedProfile); } profile.mActivated = true; this.mCurrentActivatedProfile = profile; this.holder.mProfiles.put(profile.mId, profile); persistData(); } public Profile getCurrentActivatedProfile(){ return mCurrentActivatedProfile; } public void setProfileListAdapter(ProfileManagerListAdapter profileListAdapter){ this.mProfileListAdapter = profileListAdapter; } public ProfileManagerListAdapter getProfileListAdapter(){ return this.mProfileListAdapter; } public int getNumberOfProfiles(){ return holder.mProfiles.size(); } public void fillWithSampleData(){ this.addProfile(new Profile( 0, "Windows Vista", "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.", R.drawable.ic_profile_vista, false ), false); this.addProfile(new Profile( 1, "Windows 7", "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.", R.drawable.ic_profile_w7, false ), false); this.addProfile(new Profile( 2, "Unix Distro", "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.", R.drawable.ic_profile_unix, false ), false); Profile randomProfile = new Profile( 3, "Random", "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.", R.drawable.ic_service_green, false ); randomProfile.mIsRandom = true; this.addProfile(randomProfile, false); this.addProfile(new Profile( 4, "Mix", "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat.", R.drawable.ic_service_green, false ), false); holder.mIncrementValue = 4; persistData(); } }