|
@@ -1,5 +1,15 @@
|
|
|
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;
|
|
@@ -7,8 +17,8 @@ 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.model.ProfilesHolder;
|
|
|
import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
|
|
|
import de.tudarmstadt.informatik.hostage.ui2.adapter.ProfileManagerListAdapter;
|
|
|
|
|
@@ -19,44 +29,87 @@ import de.tudarmstadt.informatik.hostage.ui2.adapter.ProfileManagerListAdapter;
|
|
|
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 Profile mRandomProfile = null;
|
|
|
+
|
|
|
+ private static final String PERSIST_FILENAME = "hostage_profiles.dat";
|
|
|
|
|
|
- private UglyDbHelper dbh;
|
|
|
+ private ProfilesHolder holder;
|
|
|
|
|
|
public static ProfileManager getInstance(){
|
|
|
if(INSTANCE == null){
|
|
|
INSTANCE = new ProfileManager();
|
|
|
}
|
|
|
|
|
|
+ if(INSTANCE.getNumberOfProfiles() == 0){
|
|
|
+ INSTANCE.loadData();
|
|
|
+ }
|
|
|
+
|
|
|
return INSTANCE;
|
|
|
}
|
|
|
|
|
|
private ProfileManager(){
|
|
|
- this.mProfiles = new HashMap<Integer, Profile>();
|
|
|
- this.dbh = new UglyDbHelper(MainActivity.getContext());
|
|
|
+ holder = new ProfilesHolder();
|
|
|
+ holder.mProfiles = new HashMap<Integer, Profile>();
|
|
|
}
|
|
|
|
|
|
public void loadData(){
|
|
|
- Collection<Profile> profiles = this.dbh.getAllProfiles();
|
|
|
+ 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<Integer, Profile> 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(profiles.size() == 0){
|
|
|
- this.fillWithSampleData();
|
|
|
- profiles = this.dbh.getAllProfiles();
|
|
|
+ if(this.mRandomProfile != null){
|
|
|
+ randomizeProtocols(mRandomProfile);
|
|
|
+ }
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- this.mProfiles.clear();
|
|
|
-
|
|
|
- for(Profile p: profiles){
|
|
|
- this.mProfiles.put(p.mId, p);
|
|
|
-
|
|
|
- if(p.mActivated){
|
|
|
- this.mCurrentActivatedProfile = p;
|
|
|
- }
|
|
|
+ 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<Profile> getProfilesList(){
|
|
@@ -64,45 +117,75 @@ public class ProfileManager {
|
|
|
}
|
|
|
|
|
|
public Collection<Profile> getProfilesCollection(){
|
|
|
- if(mProfiles.size() == 0 || mProfiles == null){
|
|
|
+ if(holder.mProfiles.size() == 0 || holder.mProfiles == null){
|
|
|
this.loadData();
|
|
|
}
|
|
|
|
|
|
- return mProfiles.values();
|
|
|
+ return holder.mProfiles.values();
|
|
|
}
|
|
|
|
|
|
public Map<Integer, Profile> getMapProfiles(){
|
|
|
- return mProfiles;
|
|
|
+ return holder.mProfiles;
|
|
|
}
|
|
|
|
|
|
- public long persistProfile(Profile profile){
|
|
|
- int id = (int) this.dbh.persistProfile(profile);
|
|
|
+ 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();
|
|
|
+ }
|
|
|
|
|
|
- if(profile.mId != id){
|
|
|
- profile.mId = id;
|
|
|
+ public long persistProfile(Profile profile){
|
|
|
+ if(profile.mId == -1){
|
|
|
+ profile.mId = ++holder.mIncrementValue;
|
|
|
}
|
|
|
|
|
|
- this.mProfiles.put(id, profile);
|
|
|
+ holder.mProfiles.put(profile.mId, profile);
|
|
|
+
|
|
|
+ this.persistData();
|
|
|
|
|
|
- return id;
|
|
|
+ if(this.mProfileListAdapter != null){
|
|
|
+ this.mProfileListAdapter.notifyDataSetChanged();
|
|
|
+ }
|
|
|
+
|
|
|
+ return profile.mId;
|
|
|
}
|
|
|
|
|
|
public Profile getProfile(int id){
|
|
|
- if(this.mProfiles.containsKey(id)){
|
|
|
- return this.mProfiles.get(id);
|
|
|
+ if(holder.mProfiles.size() == 0){
|
|
|
+ loadData();
|
|
|
}
|
|
|
|
|
|
- Profile profile = this.dbh.getProfile(id);
|
|
|
- if(profile != null) return profile;
|
|
|
+ if(this.holder.mProfiles.containsKey(id)){
|
|
|
+ return this.holder.mProfiles.get(id);
|
|
|
+ }
|
|
|
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
public void addProfile(Profile profile){
|
|
|
- int id = (int) this.dbh.persistProfile(profile);
|
|
|
- profile.mId = id;
|
|
|
+ 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();
|
|
|
+ }
|
|
|
|
|
|
- this.mProfiles.put(id, profile);
|
|
|
if(this.mProfileListAdapter != null){
|
|
|
this.mProfileListAdapter.add(profile);
|
|
|
this.mProfileListAdapter.notifyDataSetChanged();
|
|
@@ -110,9 +193,10 @@ public class ProfileManager {
|
|
|
}
|
|
|
|
|
|
public void deleteProfile(Profile profile){
|
|
|
- if(this.mProfiles.containsKey(profile.mId)){
|
|
|
- this.mProfiles.remove(profile.mId);
|
|
|
- this.dbh.deleteProfile(profile.mId);
|
|
|
+ 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);
|
|
@@ -121,6 +205,11 @@ public class ProfileManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public void clearProfiles(){
|
|
|
+ holder.mProfiles.clear();
|
|
|
+ persistData();
|
|
|
+ }
|
|
|
+
|
|
|
public void activateProfile(Profile profile){
|
|
|
if(profile.equals(this.mCurrentActivatedProfile)) return;
|
|
|
|
|
@@ -131,7 +220,8 @@ public class ProfileManager {
|
|
|
|
|
|
profile.mActivated = true;
|
|
|
this.mCurrentActivatedProfile = profile;
|
|
|
- this.dbh.persistProfile(profile);
|
|
|
+ this.holder.mProfiles.put(profile.mId, profile);
|
|
|
+ persistData();
|
|
|
}
|
|
|
|
|
|
public Profile getCurrentActivatedProfile(){
|
|
@@ -147,6 +237,10 @@ public class ProfileManager {
|
|
|
return this.mProfileListAdapter;
|
|
|
}
|
|
|
|
|
|
+ public int getNumberOfProfiles(){
|
|
|
+ return holder.mProfiles.size();
|
|
|
+ }
|
|
|
+
|
|
|
public void fillWithSampleData(){
|
|
|
this.addProfile(new Profile(
|
|
|
0,
|
|
@@ -154,7 +248,7 @@ public class ProfileManager {
|
|
|
"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,
|
|
@@ -162,7 +256,7 @@ public class ProfileManager {
|
|
|
"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,
|
|
@@ -170,15 +264,19 @@ public class ProfileManager {
|
|
|
"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);
|
|
|
|
|
|
- this.addProfile(new Profile(
|
|
|
+ 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,
|
|
@@ -186,6 +284,9 @@ public class ProfileManager {
|
|
|
"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();
|
|
|
}
|
|
|
}
|