|
@@ -5,6 +5,7 @@ import org.json.JSONException;
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
import android.content.Context;
|
|
|
+import android.content.SharedPreferences;
|
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
import java.io.FileNotFoundException;
|
|
@@ -21,6 +22,8 @@ import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.Random;
|
|
|
|
|
|
+import de.tudarmstadt.informatik.hostage.Hostage;
|
|
|
+import de.tudarmstadt.informatik.hostage.Listener;
|
|
|
import de.tudarmstadt.informatik.hostage.R;
|
|
|
import de.tudarmstadt.informatik.hostage.model.Profile;
|
|
|
import de.tudarmstadt.informatik.hostage.ui2.activity.MainActivity;
|
|
@@ -70,6 +73,9 @@ public class ProfileManager {
|
|
|
*/
|
|
|
public HashMap<Integer, Profile> mProfiles;
|
|
|
|
|
|
+ private SharedPreferences mSharedPreferences;
|
|
|
+ private SharedPreferences.Editor mSharedEditor;
|
|
|
+
|
|
|
/**
|
|
|
* Since the profile manager should only have one instance in the whole app, we are using the singleton pattern.
|
|
|
* This method creates a new instance of the profile manager, if no instance already exists, and returns it.
|
|
@@ -93,6 +99,10 @@ public class ProfileManager {
|
|
|
*/
|
|
|
private ProfileManager(){
|
|
|
mProfiles = new HashMap<Integer, Profile>();
|
|
|
+
|
|
|
+ String sharedPreferencePath = MainActivity.getContext().getString(R.string.shared_preference_path);
|
|
|
+ mSharedPreferences = MainActivity.getContext().getSharedPreferences(sharedPreferencePath, Hostage.MODE_PRIVATE);
|
|
|
+ mSharedEditor = mSharedPreferences.edit();
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -145,7 +155,7 @@ public class ProfileManager {
|
|
|
}
|
|
|
|
|
|
if(p.mActivated){
|
|
|
- this.mCurrentActivatedProfile = p;
|
|
|
+ activateProfile(p, false);
|
|
|
}
|
|
|
|
|
|
if(p.mIsRandom){
|
|
@@ -355,12 +365,21 @@ public class ProfileManager {
|
|
|
persistData();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Same as {@see activateProfile(Profile profile, boolean persist)} but with persist arg being always true
|
|
|
+ * @param profile the profile to active
|
|
|
+ */
|
|
|
+ public void activateProfile(Profile profile){
|
|
|
+ this.activateProfile(profile, true);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Makes a given profile active.
|
|
|
*
|
|
|
* @param profile the profile to activate
|
|
|
+ * @param persist indicates if the profile should be persisted after activating
|
|
|
*/
|
|
|
- public void activateProfile(Profile profile){
|
|
|
+ public void activateProfile(Profile profile, boolean persist){
|
|
|
if(profile.equals(this.mCurrentActivatedProfile)) return;
|
|
|
|
|
|
if(this.mCurrentActivatedProfile != null){
|
|
@@ -371,7 +390,33 @@ public class ProfileManager {
|
|
|
profile.mActivated = true;
|
|
|
this.mCurrentActivatedProfile = profile;
|
|
|
this.mProfiles.put(profile.mId, profile);
|
|
|
- persistData();
|
|
|
+
|
|
|
+ mSharedEditor.putString("os", profile.mLabel);
|
|
|
+ mSharedEditor.commit();
|
|
|
+
|
|
|
+ if(persist) persistData();
|
|
|
+
|
|
|
+ if(this.mProfileListAdapter != null){
|
|
|
+ this.mProfileListAdapter.notifyDataSetChanged();
|
|
|
+ }
|
|
|
+
|
|
|
+ if(MainActivity.getInstance().getHostageService() != null){
|
|
|
+ if(MainActivity.getInstance().getHostageService().hasRunningListeners()){
|
|
|
+ List<String> protocolsToStart = profile.getActiveProtocols();
|
|
|
+
|
|
|
+ for(Listener listener: MainActivity.getInstance().getHostageService().getListeners()){
|
|
|
+ if(listener.isRunning()){
|
|
|
+ if(protocolsToStart.contains(listener.getProtocolName())){
|
|
|
+ protocolsToStart.remove(listener.getProtocolName());
|
|
|
+ } else {
|
|
|
+ MainActivity.getInstance().getHostageService().stopListenerAllPorts(listener.getProtocolName());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ MainActivity.getInstance().startMonitorServices(protocolsToStart);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -554,7 +599,8 @@ public class ProfileManager {
|
|
|
this.addProfile(paranoidProfile, false);
|
|
|
|
|
|
mIncrementValue = 8;
|
|
|
- this.mCurrentActivatedProfile = paranoidProfile;
|
|
|
+
|
|
|
+ this.activateProfile(paranoidProfile, false);
|
|
|
|
|
|
persistData();
|
|
|
}
|