123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- package de.tudarmstadt.informatik.hostage.dao;
- 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.logging.UglyDbHelper;
- import de.tudarmstadt.informatik.hostage.model.Profile;
- 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 Map<Integer, Profile> mProfiles;
- private ProfileManagerListAdapter mProfileListAdapter = null;
- private int mProfileId = 0;
- private Profile mCurrentActivatedProfile = null;
- private UglyDbHelper dbh;
- public static ProfileManager getInstance(){
- if(INSTANCE == null){
- INSTANCE = new ProfileManager();
- }
- return INSTANCE;
- }
- private ProfileManager(){
- this.mProfiles = new HashMap<Integer, Profile>();
- this.dbh = new UglyDbHelper(MainActivity.getContext());
- }
- public void loadData(){
- Collection<Profile> profiles = this.dbh.getAllProfiles();
- if(profiles.size() == 0){
- this.fillWithSampleData();
- profiles = this.dbh.getAllProfiles();
- }
- this.mProfiles.clear();
- for(Profile p: profiles){
- this.mProfiles.put(p.mId, p);
- if(p.mActivated){
- this.mCurrentActivatedProfile = p;
- }
- }
- }
- public List<Profile> getProfilesList(){
- return new ArrayList<Profile>(getProfilesCollection());
- }
- public Collection<Profile> getProfilesCollection(){
- if(mProfiles.size() == 0 || mProfiles == null){
- this.loadData();
- }
- return mProfiles.values();
- }
- public Map<Integer, Profile> getMapProfiles(){
- return mProfiles;
- }
- public long persistProfile(Profile profile){
- int id = (int) this.dbh.persistProfile(profile);
- if(profile.mId != id){
- profile.mId = id;
- }
- this.mProfiles.put(id, profile);
- return id;
- }
- public Profile getProfile(int id){
- if(this.mProfiles.containsKey(id)){
- return this.mProfiles.get(id);
- }
- Profile profile = this.dbh.getProfile(id);
- if(profile != null) return profile;
- return null;
- }
- public void addProfile(Profile profile){
- int id = (int) this.dbh.persistProfile(profile);
- profile.mId = id;
- this.mProfiles.put(id, profile);
- if(this.mProfileListAdapter != null){
- this.mProfileListAdapter.add(profile);
- this.mProfileListAdapter.notifyDataSetChanged();
- }
- }
- public void deleteProfile(Profile profile){
- if(this.mProfiles.containsKey(profile.mId)){
- this.mProfiles.remove(profile.mId);
- this.dbh.deleteProfile(profile.mId);
- if(this.mProfileListAdapter != null){
- this.mProfileListAdapter.remove(profile);
- this.mProfileListAdapter.notifyDataSetChanged();
- }
- }
- }
- 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.dbh.persistProfile(profile);
- }
- public Profile getCurrentActivatedProfile(){
- return mCurrentActivatedProfile;
- }
- public void setProfileListAdapter(ProfileManagerListAdapter profileListAdapter){
- this.mProfileListAdapter = profileListAdapter;
- }
- public ProfileManagerListAdapter getProfileListAdapter(){
- return this.mProfileListAdapter;
- }
- 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
- ));
- 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
- ));
- 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
- ));
- this.addProfile(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
- ));
- 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
- ));
- }
- }
|