|
@@ -7,7 +7,9 @@ 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;
|
|
|
import de.tudarmstadt.informatik.hostage.ui2.model.ProfileListItem;
|
|
|
|
|
@@ -21,9 +23,11 @@ public class ProfileManager {
|
|
|
private Map<Integer, Profile> mProfiles;
|
|
|
private ProfileManagerListAdapter mProfileListAdapter = null;
|
|
|
|
|
|
- private int mProfileId = 4;
|
|
|
+ private int mProfileId = 0;
|
|
|
private Profile mCurrentActivatedProfile = null;
|
|
|
|
|
|
+ private UglyDbHelper dbh;
|
|
|
+
|
|
|
public static ProfileManager getInstance(){
|
|
|
if(INSTANCE == null){
|
|
|
INSTANCE = new ProfileManager();
|
|
@@ -34,45 +38,24 @@ public class ProfileManager {
|
|
|
|
|
|
private ProfileManager(){
|
|
|
this.mProfiles = new HashMap<Integer, Profile>();
|
|
|
- this.mProfiles.put(0, 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.dbh = new UglyDbHelper(MainActivity.getContext());
|
|
|
+ }
|
|
|
|
|
|
- this.mProfiles.put(1, 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
|
|
|
- ));
|
|
|
+ public void loadData(){
|
|
|
+ Collection<Profile> profiles = this.dbh.getAllProfiles();
|
|
|
|
|
|
- this.mProfiles.put(2, 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
|
|
|
- ));
|
|
|
+ if(profiles.size() == 0){
|
|
|
+ this.fillWithSampleData();
|
|
|
+ profiles = mProfiles.values();
|
|
|
+ }
|
|
|
|
|
|
- this.mProfiles.put(3, 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
|
|
|
- ));
|
|
|
+ for(Profile p: profiles){
|
|
|
+ this.mProfiles.put(p.id, p);
|
|
|
|
|
|
- this.mProfiles.put(4, 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
|
|
|
- ));
|
|
|
+ if(p.activated){
|
|
|
+ this.mCurrentActivatedProfile = p;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public List<Profile> getProfilesList(){
|
|
@@ -80,6 +63,10 @@ public class ProfileManager {
|
|
|
}
|
|
|
|
|
|
public Collection<Profile> getProfilesCollection(){
|
|
|
+ if(mProfiles.size() == 0 || mProfiles == null){
|
|
|
+ this.loadData();
|
|
|
+ }
|
|
|
+
|
|
|
return mProfiles.values();
|
|
|
}
|
|
|
|
|
@@ -87,44 +74,64 @@ public class ProfileManager {
|
|
|
return mProfiles;
|
|
|
}
|
|
|
|
|
|
+ public long persistProfile(Profile profile){
|
|
|
+ int id = (int) this.dbh.persistProfile(profile);
|
|
|
+
|
|
|
+ if(profile.id != id){
|
|
|
+ profile.id = 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){
|
|
|
- if(profile.id == -1){
|
|
|
- profile.id = mProfileId++;
|
|
|
- }
|
|
|
+ int id = (int) this.dbh.persistProfile(profile);
|
|
|
+ profile.id = id;
|
|
|
|
|
|
- this.mProfiles.put(profile.id, profile);
|
|
|
+ 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.id)){
|
|
|
this.mProfiles.remove(profile.id);
|
|
|
+ this.dbh.deleteProfile(profile.id);
|
|
|
|
|
|
if(this.mProfileListAdapter != null){
|
|
|
this.mProfileListAdapter.remove(profile);
|
|
|
+ this.mProfileListAdapter.notifyDataSetChanged();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void activeProfile(Profile profile){
|
|
|
+ public void activateProfile(Profile profile){
|
|
|
if(profile.equals(this.mCurrentActivatedProfile)) return;
|
|
|
|
|
|
if(this.mCurrentActivatedProfile != null){
|
|
|
this.mCurrentActivatedProfile.activated = false;
|
|
|
+ this.persistProfile(this.mCurrentActivatedProfile);
|
|
|
}
|
|
|
|
|
|
profile.activated = true;
|
|
|
this.mCurrentActivatedProfile = profile;
|
|
|
+
|
|
|
+ this.dbh.persistProfile(profile);
|
|
|
}
|
|
|
|
|
|
public void setProfileListAdapter(ProfileManagerListAdapter profileListAdapter){
|
|
@@ -135,4 +142,46 @@ public class ProfileManager {
|
|
|
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
|
|
|
+ ));
|
|
|
+ }
|
|
|
}
|