Use proper names when storing custom teams, also fixes the bug which created new teams when you editted one of the default ones
authorXeli
Fri, 25 Nov 2011 19:36:03 +0100
changeset 6432 ffb18b44415d
parent 6431 f0a7c5415119
child 6433 c8e4b3f29e1e
Use proper names when storing custom teams, also fixes the bug which created new teams when you editted one of the default ones
project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/EngineProtocol/FrontendDataUtils.java
project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/EngineProtocol/Team.java
project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TeamCreatorActivity.java
project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TeamSelectionActivity.java
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/EngineProtocol/FrontendDataUtils.java	Fri Nov 25 19:33:55 2011 +0100
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/EngineProtocol/FrontendDataUtils.java	Fri Nov 25 19:36:03 2011 +0100
@@ -176,6 +176,7 @@
 			for(File s : teamFileNames){
 				Team t = Team.getTeamFromXml(s.getAbsolutePath());
 				if(t != null){
+					t.file = s.getName();
 					ret.add(teamToHashMap(t));
 				}
 			}
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/EngineProtocol/Team.java	Fri Nov 25 19:33:55 2011 +0100
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/EngineProtocol/Team.java	Fri Nov 25 19:36:03 2011 +0100
@@ -19,6 +19,7 @@
 package org.hedgewars.hedgeroid.EngineProtocol;
 
 import java.io.BufferedReader;
+import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
@@ -30,6 +31,7 @@
 import org.xmlpull.v1.XmlPullParserFactory;
 import org.xmlpull.v1.XmlSerializer;
 
+import android.content.Context;
 import android.os.Parcel;
 import android.os.Parcelable;
 import android.util.Xml;
@@ -61,6 +63,7 @@
 	private static final int STATE_HOG_ROOT = 2;
 
 	public String name, grave, flag, voice, fort, hash;
+	public String file = null;
 
 	public static int maxNumberOfHogs = 0;
 	public static int maxNumberOfTeams = 0;
@@ -130,6 +133,22 @@
 		}
 	}
 
+	public void setFileName(Context c){
+		if(file == null){
+		  	file = validFileName(c, name);
+		}
+	}
+	private String validFileName(Context c, String fileName){
+		String absolutePath = String.format("%s/%s", c.getFilesDir(), fileName);
+		File f = new File(absolutePath);
+		if(f.exists()){
+			String newFileName = fileName + (int)(Math.random()*10);
+			return validFileName(c, newFileName);
+		}else{
+			return fileName;
+		}
+	}
+	
 	/*
 	 * XML METHODS
 	 */
@@ -311,6 +330,7 @@
 		dest.writeIntArray(levels);
 		dest.writeInt(color);
 		dest.writeInt(hogCount);
+		dest.writeString(file);
 	}
 
 
@@ -326,6 +346,7 @@
 		src.readIntArray(levels);
 		color = src.readInt();
 		hogCount = src.readInt();
+		file = src.readString();
 	}
 
 	public static final Parcelable.Creator<Team> CREATOR = new Parcelable.Creator<Team>() {
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TeamCreatorActivity.java	Fri Nov 25 19:33:55 2011 +0100
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TeamCreatorActivity.java	Fri Nov 25 19:36:03 2011 +0100
@@ -64,6 +64,7 @@
 	private MediaPlayer mp = null;
 	private boolean settingsChanged = false;
 	private boolean saved = false;
+	private String fileName = null;
 
 	public void onCreate(Bundle savedInstanceState) {
 		super.onCreate(savedInstanceState);
@@ -197,6 +198,7 @@
 
 				hogName.get(i).setText(t.hogNames[i]);
 			}
+			this.fileName = t.file;
 		}
 	}
 
@@ -242,8 +244,7 @@
 			saved = true;
 			Team team = new Team();
 			team.name = name.getText().toString();
-			HashMap<String, Object> hashmap = (HashMap<String, Object>) flag
-					.getSelectedItem();
+			HashMap<String, Object> hashmap = (HashMap<String, Object>) flag.getSelectedItem();
 
 			team.flag = (String) hashmap.get("txt");
 			team.fort = fort.getSelectedItem().toString();
@@ -251,6 +252,7 @@
 			team.grave = hashmap.get("txt").toString();
 			team.hash = "0";
 			team.voice = voice.getSelectedItem().toString();
+			team.file = fileName;
 
 			hashmap = ((HashMap<String, Object>) difficulty.getSelectedItem());
 			String levelString = hashmap.get("txt").toString();
@@ -271,18 +273,17 @@
 
 			for (int i = 0; i < hogName.size(); i++) {
 				team.hogNames[i] = hogName.get(i).getText().toString();
-				hashmap = (HashMap<String, Object>) hogHat.get(i)
-						.getSelectedItem();
+				hashmap = (HashMap<String, Object>) hogHat.get(i).getSelectedItem();
 				team.hats[i] = hashmap.get("txt").toString();
 				team.levels[i] = levelInt;
 			}
 			try {
-				File teamsDir = new File(getFilesDir().getAbsolutePath() + '/'
-						+ Team.DIRECTORY_TEAMS);
-				if (!teamsDir.exists())
-					teamsDir.mkdir();
-				FileOutputStream fos = new FileOutputStream(String.format(
-						"%s/%s.xml", teamsDir.getAbsolutePath(), team.name));
+				File teamsDir = new File(getFilesDir().getAbsolutePath() + '/' + Team.DIRECTORY_TEAMS);
+				if (!teamsDir.exists()) teamsDir.mkdir();
+				if(team.file == null){
+					team.setFileName(TeamCreatorActivity.this);
+				}
+				FileOutputStream fos = new FileOutputStream(String.format("%s/%s", teamsDir.getAbsolutePath(), team.file));
 				team.writeToXml(fos);
 			} catch (FileNotFoundException e) {
 				e.printStackTrace();
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TeamSelectionActivity.java	Fri Nov 25 19:33:55 2011 +0100
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TeamSelectionActivity.java	Fri Nov 25 19:36:03 2011 +0100
@@ -241,7 +241,8 @@
 			selectAvailableTeamsItem(position);
 			return true;
 		case 1://delete
-			File f = new File(String.format("%s/%s/%s.xml", TeamSelectionActivity.this.getFilesDir(), Team.DIRECTORY_TEAMS, availableTeamsList.get(position).get("txt")));
+			Team team = (Team)availableTeamsList.get(position).get("team");
+			File f = new File(String.format("%s/%s/%s", TeamSelectionActivity.this.getFilesDir(), Team.DIRECTORY_TEAMS, team.file));
 			f.delete();
 			availableTeamsList.remove(position);
 			((SimpleAdapter)availableTeams.getAdapter()).notifyDataSetChanged();