project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TeamCreatorActivity.java
branchhedgeroid
changeset 7857 2bc61f8841a1
parent 7586 33924ff4af50
child 10017 de822cd3df3a
equal deleted inserted replaced
7855:ddcdedd3330b 7857:2bc61f8841a1
     1 /*
     1 /*
     2  * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
     2  * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
     3  * Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com>
     3  * Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com>
       
     4  * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
     4  *
     5  *
     5  * This program is free software; you can redistribute it and/or modify
     6  * This program is free software; you can redistribute it and/or
     6  * it under the terms of the GNU General Public License as published by
     7  * modify it under the terms of the GNU General Public License
     7  * the Free Software Foundation; version 2 of the License
     8  * as published by the Free Software Foundation; either version 2
       
     9  * of the License, or (at your option) any later version.
     8  *
    10  *
     9  * This program is distributed in the hope that it will be useful,
    11  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    14  * GNU General Public License for more details.
    13  *
    15  *
    14  * You should have received a copy of the GNU General Public License
    16  * You should have received a copy of the GNU General Public License
    15  * along with this program; if not, write to the Free Software
    17  * along with this program; if not, write to the Free Software
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
    18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    17  */
    19  */
    18 
       
    19 
    20 
    20 package org.hedgewars.hedgeroid;
    21 package org.hedgewars.hedgeroid;
    21 
    22 
    22 import java.io.File;
    23 import java.io.File;
    23 import java.io.FileNotFoundException;
    24 import java.io.FileNotFoundException;
    24 import java.io.FileOutputStream;
       
    25 import java.io.IOException;
    25 import java.io.IOException;
    26 import java.util.ArrayList;
    26 import java.util.ArrayList;
    27 import java.util.HashMap;
       
    28 import java.util.List;
    27 import java.util.List;
       
    28 import java.util.Map;
       
    29 import java.util.NoSuchElementException;
    29 
    30 
    30 import org.hedgewars.hedgeroid.Datastructures.FrontendDataUtils;
    31 import org.hedgewars.hedgeroid.Datastructures.FrontendDataUtils;
       
    32 import org.hedgewars.hedgeroid.Datastructures.Hog;
    31 import org.hedgewars.hedgeroid.Datastructures.Team;
    33 import org.hedgewars.hedgeroid.Datastructures.Team;
       
    34 import org.hedgewars.hedgeroid.util.FileUtils;
    32 
    35 
    33 import android.app.Activity;
    36 import android.app.Activity;
    34 import android.graphics.Bitmap;
    37 import android.graphics.Bitmap;
    35 import android.graphics.drawable.Drawable;
    38 import android.graphics.drawable.Drawable;
    36 import android.media.MediaPlayer;
    39 import android.media.MediaPlayer;
    37 import android.os.Bundle;
    40 import android.os.Bundle;
       
    41 import android.util.Log;
    38 import android.view.View;
    42 import android.view.View;
    39 import android.view.View.OnClickListener;
    43 import android.view.View.OnClickListener;
    40 import android.view.View.OnFocusChangeListener;
       
    41 import android.widget.AdapterView;
    44 import android.widget.AdapterView;
    42 import android.widget.AdapterView.OnItemSelectedListener;
    45 import android.widget.AdapterView.OnItemSelectedListener;
    43 import android.widget.ArrayAdapter;
    46 import android.widget.ArrayAdapter;
    44 import android.widget.EditText;
    47 import android.widget.EditText;
    45 import android.widget.ImageButton;
    48 import android.widget.ImageButton;
    47 import android.widget.LinearLayout;
    50 import android.widget.LinearLayout;
    48 import android.widget.RelativeLayout;
    51 import android.widget.RelativeLayout;
    49 import android.widget.ScrollView;
    52 import android.widget.ScrollView;
    50 import android.widget.SimpleAdapter;
    53 import android.widget.SimpleAdapter;
    51 import android.widget.Spinner;
    54 import android.widget.Spinner;
       
    55 import android.widget.SpinnerAdapter;
    52 import android.widget.TextView;
    56 import android.widget.TextView;
    53 import android.widget.Toast;
    57 import android.widget.Toast;
    54 
    58 
    55 public class TeamCreatorActivity extends Activity implements Runnable{
    59 /**
    56 
    60  * Edit or create a team. If a team should be edited, it is supplied in the extras
       
    61  * as parameter oldTeamName.
       
    62  */
       
    63 public class TeamCreatorActivity extends Activity implements Runnable {
       
    64 	public static final String PARAMETER_EXISTING_TEAMNAME = "existingTeamName";
       
    65 	private static final String TAG = TeamCreatorActivity.class.getSimpleName();
       
    66 	
    57 	private TextView name;
    67 	private TextView name;
    58 	private Spinner difficulty, grave, flag, voice, fort;
    68 	private Spinner difficulty, grave, flag, voice, fort;
    59 	private ImageView imgFort;
    69 	private ImageView imgFort;
    60 	private ArrayList<ImageButton> hogDice = new ArrayList<ImageButton>();
    70 	private ArrayList<ImageButton> hogDice = new ArrayList<ImageButton>();
    61 	private ArrayList<Spinner> hogHat = new ArrayList<Spinner>();
    71 	private ArrayList<Spinner> hogHat = new ArrayList<Spinner>();
    62 	private ArrayList<EditText> hogName = new ArrayList<EditText>();
    72 	private ArrayList<EditText> hogName = new ArrayList<EditText>();
    63 	private ImageButton back, save, voiceButton;
    73 	private ImageButton voiceButton;
    64 	private ScrollView scroller;
    74 	private ScrollView scroller;
    65 	private MediaPlayer mp = null;
    75 	private MediaPlayer mp = null;
    66 	private boolean settingsChanged = false;
    76 	private boolean initComplete = false;
    67 	private boolean saved = false;
    77 	
    68 	private String fileName = null;
    78 	private String existingTeamName = null;
    69 
    79 
    70 	private final List<HashMap<String, ?>> flagsData = new ArrayList<HashMap<String, ?>>();
    80 	private final List<Map<String, ?>> flagsData = new ArrayList<Map<String, ?>>();
    71 	private final List<HashMap<String, ?>> typesData = new ArrayList<HashMap<String, ?>>();
    81 	private final List<Map<String, ?>> typesData = new ArrayList<Map<String, ?>>();
    72 	private final List<HashMap<String, ?>> gravesData = new ArrayList<HashMap<String, ?>>();
    82 	private final List<Map<String, ?>> gravesData = new ArrayList<Map<String, ?>>();
    73 	private final List<HashMap<String, ?>> hatsData = new ArrayList<HashMap<String, ?>>();
    83 	private final List<Map<String, ?>> hatsData = new ArrayList<Map<String, ?>>();
    74 	private final List<String> voicesData = new ArrayList<String>();
    84 	private final List<String> voicesData = new ArrayList<String>();
    75 	private final List<String> fortsData = new ArrayList<String>();
    85 	private final List<String> fortsData = new ArrayList<String>();
    76 
    86 
    77 	public void onCreate(Bundle savedInstanceState) {
    87 	public void onCreate(Bundle savedInstanceState) {
    78 		super.onCreate(savedInstanceState);
    88 		super.onCreate(savedInstanceState);
       
    89 		initComplete = false;
       
    90 		
       
    91 		// Restore state and read parameters 
       
    92 		if(savedInstanceState != null) {
       
    93 			existingTeamName = savedInstanceState.getString(PARAMETER_EXISTING_TEAMNAME);
       
    94 		} else {
       
    95 			existingTeamName = getIntent().getStringExtra(PARAMETER_EXISTING_TEAMNAME);
       
    96 		}
       
    97 		
       
    98 		// Set up view
    79 		setContentView(R.layout.team_creation);
    99 		setContentView(R.layout.team_creation);
    80 
   100 
    81 		name = (TextView) findViewById(R.id.txtName);
   101 		name = (TextView) findViewById(R.id.txtName);
    82 		difficulty = (Spinner) findViewById(R.id.spinType);
   102 		difficulty = (Spinner) findViewById(R.id.spinType);
    83 		grave = (Spinner) findViewById(R.id.spinGrave);
   103 		grave = (Spinner) findViewById(R.id.spinGrave);
    85 		voice = (Spinner) findViewById(R.id.spinVoice);
   105 		voice = (Spinner) findViewById(R.id.spinVoice);
    86 		fort = (Spinner) findViewById(R.id.spinFort);
   106 		fort = (Spinner) findViewById(R.id.spinFort);
    87 
   107 
    88 		imgFort = (ImageView) findViewById(R.id.imgFort);
   108 		imgFort = (ImageView) findViewById(R.id.imgFort);
    89 
   109 
    90 		back = (ImageButton) findViewById(R.id.btnBack);
       
    91 		save = (ImageButton) findViewById(R.id.btnSave);
       
    92 		voiceButton = (ImageButton) findViewById(R.id.btnPlay);
   110 		voiceButton = (ImageButton) findViewById(R.id.btnPlay);
    93 
   111 
    94 		scroller = (ScrollView) findViewById(R.id.scroller);
   112 		scroller = (ScrollView) findViewById(R.id.scroller);
    95 
   113 
    96 		save.setOnClickListener(saveClicker);
   114 		// Wire view elements
    97 		back.setOnClickListener(backClicker);
       
    98 
       
    99 		LinearLayout ll = (LinearLayout) findViewById(R.id.HogsContainer);
   115 		LinearLayout ll = (LinearLayout) findViewById(R.id.HogsContainer);
   100 		for (int i = 0; i < ll.getChildCount(); i++) {
   116 		for (int i = 0; i < ll.getChildCount(); i++) {
   101 			RelativeLayout team_creation_entry = (RelativeLayout) ll.getChildAt(i);
   117 			RelativeLayout team_creation_entry = (RelativeLayout) ll.getChildAt(i);
   102 
   118 
   103 			hogHat.add((Spinner) team_creation_entry
   119 			hogHat.add((Spinner) team_creation_entry
   106 					.findViewById(R.id.btnTeam1));
   122 					.findViewById(R.id.btnTeam1));
   107 			hogName.add((EditText) team_creation_entry
   123 			hogName.add((EditText) team_creation_entry
   108 					.findViewById(R.id.txtTeam1));
   124 					.findViewById(R.id.txtTeam1));
   109 		}
   125 		}
   110 
   126 
   111 		SimpleAdapter sa = new SimpleAdapter(this, gravesData,
   127 		grave.setAdapter(createMapSpinnerAdapter(gravesData));
       
   128 		flag.setAdapter(createMapSpinnerAdapter(flagsData));
       
   129 		difficulty.setAdapter(createMapSpinnerAdapter(typesData));
       
   130 		SpinnerAdapter hatAdapter = createMapSpinnerAdapter(hatsData);
       
   131 		for (Spinner spin : hogHat) {
       
   132 			spin.setAdapter(hatAdapter);
       
   133 		}
       
   134 
       
   135 
       
   136 		voice.setAdapter(createListSpinnerAdapter(voicesData));
       
   137 		voiceButton.setOnClickListener(voiceClicker);
       
   138 
       
   139 		fort.setAdapter(createListSpinnerAdapter(fortsData));
       
   140 		fort.setOnItemSelectedListener(fortSelector);
       
   141 
       
   142 		new Thread(this).start();
       
   143 	}
       
   144 
       
   145 	private SpinnerAdapter createMapSpinnerAdapter(List<? extends Map<String, ?>> data) {
       
   146 		SimpleAdapter sa = new SimpleAdapter(this, data,
   112 				R.layout.spinner_textimg_entry, new String[] { "txt", "img" },
   147 				R.layout.spinner_textimg_entry, new String[] { "txt", "img" },
   113 				new int[] { R.id.spinner_txt, R.id.spinner_img });
   148 				new int[] { R.id.spinner_txt, R.id.spinner_img });
   114 		sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry);
   149 		sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry);
   115 		sa.setViewBinder(viewBinder);
   150 		sa.setViewBinder(viewBinder);
   116 		grave.setAdapter(sa);
   151 		return sa;
   117 		grave.setOnFocusChangeListener(focusser);
   152 	}
   118 
   153 	
   119 		sa = new SimpleAdapter(this, flagsData, R.layout.spinner_textimg_entry,
   154 	private SpinnerAdapter createListSpinnerAdapter(List<String> data) {
   120 				new String[] { "txt", "img" }, new int[] { R.id.spinner_txt,
   155 		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listview_item, data);
   121 				R.id.spinner_img });
       
   122 		sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry);
       
   123 		sa.setViewBinder(viewBinder);
       
   124 		flag.setAdapter(sa);
       
   125 		flag.setOnFocusChangeListener(focusser);
       
   126 
       
   127 		sa = new SimpleAdapter(this, typesData, R.layout.spinner_textimg_entry,
       
   128 				new String[] { "txt", "img" }, new int[] { R.id.spinner_txt,
       
   129 				R.id.spinner_img });
       
   130 		sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry);
       
   131 		difficulty.setAdapter(sa);
       
   132 		difficulty.setOnFocusChangeListener(focusser);
       
   133 
       
   134 		sa = new SimpleAdapter(this, hatsData, R.layout.spinner_textimg_entry,
       
   135 				new String[] { "txt", "img" }, new int[] { R.id.spinner_txt,
       
   136 				R.id.spinner_img });
       
   137 		sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry);
       
   138 		sa.setViewBinder(viewBinder);
       
   139 		for (Spinner spin : hogHat) {
       
   140 			spin.setAdapter(sa);
       
   141 		}
       
   142 
       
   143 		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listview_item, voicesData);
       
   144 		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
   156 		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
   145 		voice.setAdapter(adapter);
   157 		return adapter;
   146 		voice.setOnFocusChangeListener(focusser);
   158 	}
   147 		voiceButton.setOnClickListener(voiceClicker);
   159 	
   148 
       
   149 		adapter = new ArrayAdapter<String>(this, R.layout.listview_item, fortsData);
       
   150 		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       
   151 		fort.setAdapter(adapter);
       
   152 		fort.setOnItemSelectedListener(fortSelector);
       
   153 		fort.setOnFocusChangeListener(focusser);
       
   154 
       
   155 		new Thread(this).start();
       
   156 	}
       
   157 
       
   158 	public void run(){
   160 	public void run(){
   159 		final ArrayList<HashMap<String, ?>> gravesDataNew = FrontendDataUtils.getGraves(this);
   161 		try {
   160 		this.runOnUiThread(new Runnable(){
   162 			final List<Map<String, ?>> gravesDataNew = FrontendDataUtils.getGraves(this);
   161 			public void run() {
   163 			runOnUiThread(new Runnable(){
   162 				copy(gravesData, gravesDataNew);
   164 				public void run() {
   163 				((SimpleAdapter)grave.getAdapter()).notifyDataSetChanged();
   165 					gravesData.addAll(gravesDataNew);
   164 			}
   166 					((SimpleAdapter)grave.getAdapter()).notifyDataSetChanged();
   165 		});
   167 				}
   166 		
   168 			});
   167 		final ArrayList<HashMap<String, ?>> flagsDataNew = FrontendDataUtils.getFlags(this);
   169 			
   168 		this.runOnUiThread(new Runnable(){
   170 			final List<Map<String, ?>> flagsDataNew = FrontendDataUtils.getFlags(this);
   169 			public void run() {
   171 			runOnUiThread(new Runnable(){
   170 				copy(flagsData, flagsDataNew);
   172 				public void run() {
   171 				((SimpleAdapter)flag.getAdapter()).notifyDataSetChanged();
   173 					flagsData.addAll(flagsDataNew);
   172 			}
   174 					((SimpleAdapter)flag.getAdapter()).notifyDataSetChanged();
   173 		});
   175 				}
   174 		
   176 			});
   175 		final ArrayList<HashMap<String, ?>> typesDataNew = FrontendDataUtils.getTypes(this);
   177 			
   176 		this.runOnUiThread(new Runnable(){
   178 			final List<Map<String, ?>> typesDataNew = FrontendDataUtils.getTypes(this);
   177 			public void run() {
   179 			runOnUiThread(new Runnable(){
   178 				copy(typesData, typesDataNew);
   180 				public void run() {
   179 				((SimpleAdapter)difficulty.getAdapter()).notifyDataSetChanged();
   181 					typesData.addAll(typesDataNew);
   180 			}
   182 					((SimpleAdapter)difficulty.getAdapter()).notifyDataSetChanged();
   181 		});
   183 				}
   182 		
   184 			});
   183 		final ArrayList<HashMap<String, ?>> hatsDataNew = FrontendDataUtils.getHats(this);
   185 			
   184 		this.runOnUiThread(new Runnable(){
   186 			final List<Map<String, ?>> hatsDataNew = FrontendDataUtils.getHats(this);
   185 			public void run() {
   187 			runOnUiThread(new Runnable(){
   186 				copy(hatsData, hatsDataNew);
   188 				public void run() {
   187 				((SimpleAdapter)hogHat.get(0).getAdapter()).notifyDataSetChanged();
   189 					hatsData.addAll(hatsDataNew);
   188 			}
   190 					((SimpleAdapter)hogHat.get(0).getAdapter()).notifyDataSetChanged();
   189 		});
   191 				}
   190 		
   192 			});
   191 		final ArrayList<String> voicesDataNew = FrontendDataUtils.getVoices(this);
   193 			
   192 		this.runOnUiThread(new Runnable(){
   194 			final List<String> voicesDataNew = FrontendDataUtils.getVoices(this);
   193 			public void run() {
   195 			runOnUiThread(new Runnable(){
   194 				copy(voicesData, voicesDataNew);
   196 				public void run() {
   195 				((ArrayAdapter<String>)voice.getAdapter()).notifyDataSetChanged();
   197 					voicesData.addAll(voicesDataNew);
   196 			}
   198 					((ArrayAdapter<?>)voice.getAdapter()).notifyDataSetChanged();
   197 		});
   199 				}
   198 		
   200 			});
   199 		final ArrayList<String> fortsDataNew = FrontendDataUtils.getForts(this);
   201 			
   200 		this.runOnUiThread(new Runnable(){
   202 			final List<String> fortsDataNew = FrontendDataUtils.getForts(this);
   201 			public void run() {
   203 			runOnUiThread(new Runnable(){
   202 				copy(fortsData, fortsDataNew);
   204 				public void run() {
   203 				((ArrayAdapter<String>)fort.getAdapter()).notifyDataSetChanged();
   205 					fortsData.addAll(fortsDataNew);
   204 			}
   206 					((ArrayAdapter<?>)fort.getAdapter()).notifyDataSetChanged();
   205 		});
   207 				}
   206 	}
   208 			});
   207 	
   209 			
   208 	private static <T> void copy(List<T> dest, List<T> src){
   210 			if(existingTeamName!=null) {
   209 		for(T t: src) dest.add(t);
   211 				final Team loadedTeam = Team.load(Team.getTeamfileByName(getApplicationContext(), existingTeamName));
   210 	}
   212 				if(loadedTeam==null) {
   211 
   213 					existingTeamName = null;
       
   214 				} else {
       
   215 					runOnUiThread(new Runnable(){
       
   216 						public void run() {
       
   217 							setTeamValues(loadedTeam);
       
   218 						}
       
   219 					});
       
   220 				}
       
   221 			}
       
   222 			runOnUiThread(new Runnable(){
       
   223 				public void run() {
       
   224 					initComplete = true;
       
   225 				}
       
   226 			});
       
   227 		} catch(FileNotFoundException e) {
       
   228 			this.runOnUiThread(new Runnable(){
       
   229 				public void run() {
       
   230 					Toast.makeText(getApplicationContext(), R.string.error_missing_sdcard_or_files, Toast.LENGTH_LONG).show();
       
   231 					finish();
       
   232 				}
       
   233 			});
       
   234 		}
       
   235 	}
       
   236 	
   212 	public void onDestroy() {
   237 	public void onDestroy() {
   213 		super.onDestroy();
   238 		super.onDestroy();
   214 		if (mp != null) {
   239 		if (mp != null) {
   215 			mp.release();
   240 			mp.release();
   216 			mp = null;
   241 			mp = null;
   217 		}
   242 		}
   218 	}
   243 	}
   219 
   244 
   220 	private OnFocusChangeListener focusser = new OnFocusChangeListener() {
   245 	@Override
   221 		public void onFocusChange(View v, boolean hasFocus) {
   246 	protected void onSaveInstanceState(Bundle outState) {
   222 			settingsChanged = true;
   247 		super.onSaveInstanceState(outState);
   223 		}
   248 		outState.putString(PARAMETER_EXISTING_TEAMNAME, existingTeamName);
   224 
   249 	}
   225 	};
       
   226 
   250 
   227 	public void onBackPressed() {
   251 	public void onBackPressed() {
   228 		onFinishing();
   252 		if(initComplete) {
       
   253 			saveTeam();
       
   254 		}
       
   255 		setResult(RESULT_OK);
   229 		super.onBackPressed();
   256 		super.onBackPressed();
   230 
   257 	}
   231 	}
   258 
   232 
   259 	private void saveTeam() {
   233 	private OnClickListener backClicker = new OnClickListener() {
   260 		String teamName = name.getText().toString();
   234 		public void onClick(View v) {
   261 		String teamFlag = (String)((Map<String, Object>) flag.getSelectedItem()).get("txt");
   235 			onFinishing();
   262 		String teamFort = fort.getSelectedItem().toString();
   236 			finish();
   263 		String teamGrave = (String)((Map<String, Object>) grave.getSelectedItem()).get("txt");
   237 		}
   264 		String teamVoice = voice.getSelectedItem().toString();
   238 	};
   265 		int levelInt = (Integer)((Map<String, Object>) difficulty.getSelectedItem()).get("level");
   239 
   266 		
   240 	private void onFinishing() {
   267 		List<Hog> hogs = new ArrayList<Hog>();
   241 		if (settingsChanged) {
   268 		for (int i = 0; i < hogName.size(); i++) {
   242 			setResult(RESULT_OK);
   269 			String name = hogName.get(i).getText().toString();
   243 		} else {
   270 			String hat = ((Map<String, Object>) hogHat.get(i).getSelectedItem()).get("txt").toString();
   244 			setResult(RESULT_CANCELED);
   271 			hogs.add(new Hog(name, hat, levelInt));
   245 		}
   272 		}
   246 	}
   273 		
   247 
   274 		Team team = new Team(teamName, teamGrave, teamFlag, teamVoice, teamFort, hogs);
   248 	private OnClickListener saveClicker = new OnClickListener() {
   275 		File teamsDir = new File(getFilesDir(), Team.DIRECTORY_TEAMS);
   249 		public void onClick(View v) {
   276 		if (!teamsDir.exists()) teamsDir.mkdir();
   250 			Toast.makeText(TeamCreatorActivity.this, R.string.saved, Toast.LENGTH_SHORT).show();
   277 		
   251 			saved = true;
   278 		File newFile = Team.getTeamfileByName(this, teamName);
   252 			Team team = new Team();
   279 		File oldFile = null;
   253 			team.name = name.getText().toString();
   280 		if(existingTeamName != null) {
   254 			HashMap<String, Object> hashmap = (HashMap<String, Object>) flag.getSelectedItem();
   281 			oldFile = Team.getTeamfileByName(this, existingTeamName);
   255 
   282 		}
   256 			team.flag = (String) hashmap.get("txt");
   283 		try {
   257 			team.fort = fort.getSelectedItem().toString();
   284 			team.save(newFile);
   258 			hashmap = (HashMap<String, Object>) grave.getSelectedItem();
   285 			// If the team was renamed, delete the old file.
   259 			team.grave = hashmap.get("txt").toString();
   286 			if(oldFile != null && oldFile.isFile() && !oldFile.equals(newFile)) {
   260 			team.hash = "0";
   287 				oldFile.delete();
   261 			team.voice = voice.getSelectedItem().toString();
   288 			}
   262 			team.file = fileName;
   289 			existingTeamName = teamName;
   263 
   290 		} catch(IOException e) {
   264 			hashmap = ((HashMap<String, Object>) difficulty.getSelectedItem());
   291 			Toast.makeText(getApplicationContext(), R.string.error_save_failed, Toast.LENGTH_SHORT).show();
   265 			String levelString = hashmap.get("txt").toString();
   292 		}
   266 			int levelInt;
       
   267 			if (levelString.equals(getString(R.string.human))) {
       
   268 				levelInt = 0;
       
   269 			} else if (levelString.equals(getString(R.string.bot5))) {
       
   270 				levelInt = 1;
       
   271 			} else if (levelString.equals(getString(R.string.bot4))) {
       
   272 				levelInt = 2;
       
   273 			} else if (levelString.equals(getString(R.string.bot3))) {
       
   274 				levelInt = 3;
       
   275 			} else if (levelString.equals(getString(R.string.bot2))) {
       
   276 				levelInt = 4;
       
   277 			} else {
       
   278 				levelInt = 5;
       
   279 			}
       
   280 
       
   281 			for (int i = 0; i < hogName.size(); i++) {
       
   282 				team.hogNames[i] = hogName.get(i).getText().toString();
       
   283 				hashmap = (HashMap<String, Object>) hogHat.get(i).getSelectedItem();
       
   284 				team.hats[i] = hashmap.get("txt").toString();
       
   285 				team.levels[i] = levelInt;
       
   286 			}
       
   287 			try {
       
   288 				File teamsDir = new File(getFilesDir().getAbsolutePath() + '/' + Team.DIRECTORY_TEAMS);
       
   289 				if (!teamsDir.exists()) teamsDir.mkdir();
       
   290 				if(team.file == null){
       
   291 					team.setFileName(TeamCreatorActivity.this);
       
   292 				}
       
   293 				FileOutputStream fos = new FileOutputStream(String.format("%s/%s", teamsDir.getAbsolutePath(), team.file));
       
   294 				team.writeToXml(fos);
       
   295 			} catch (FileNotFoundException e) {
       
   296 				e.printStackTrace();
       
   297 			}
       
   298 		}
       
   299 
       
   300 	};
   293 	};
   301 
   294 
   302 	private OnItemSelectedListener fortSelector = new OnItemSelectedListener() {
   295 	private OnItemSelectedListener fortSelector = new OnItemSelectedListener() {
   303 		public void onItemSelected(AdapterView<?> arg0, View arg1,
   296 		public void onItemSelected(AdapterView<?> arg0, View arg1,
   304 				int position, long arg3) {
   297 				int position, long arg3) {
   305 			settingsChanged = true;
       
   306 			String fortName = (String) arg0.getAdapter().getItem(position);
   298 			String fortName = (String) arg0.getAdapter().getItem(position);
   307 			Drawable fortIconDrawable = Drawable.createFromPath(Utils
   299 			try {
   308 					.getDataPath(TeamCreatorActivity.this)
   300 				File fortImage = FileUtils.getDataPathFile(TeamCreatorActivity.this, "Forts", fortName, "L.png");
   309 					+ "Forts/"
   301 				Drawable fortIconDrawable = Drawable.createFromPath(fortImage.getAbsolutePath());
   310 					+ fortName + "L.png");
   302 				imgFort.setImageDrawable(fortIconDrawable);
   311 			imgFort.setImageDrawable(fortIconDrawable);
   303 			} catch(IOException e) {
       
   304 				Log.e(TAG, "Unable to show fort image", e);
       
   305 			}
   312 			scroller.fullScroll(ScrollView.FOCUS_DOWN);// Scroll the scrollview
   306 			scroller.fullScroll(ScrollView.FOCUS_DOWN);// Scroll the scrollview
   313 			// to the bottom, work
   307 			// to the bottom, work
   314 			// around for scollview
   308 			// around for scrollview
   315 			// invalidation (scrolls
   309 			// invalidation (scrolls
   316 			// back to top)
   310 			// back to top)
   317 		}
   311 		}
   318 
   312 
   319 		public void onNothingSelected(AdapterView<?> arg0) {
   313 		public void onNothingSelected(AdapterView<?> arg0) {
   322 	};
   316 	};
   323 
   317 
   324 	private OnClickListener voiceClicker = new OnClickListener() {
   318 	private OnClickListener voiceClicker = new OnClickListener() {
   325 		public void onClick(View v) {
   319 		public void onClick(View v) {
   326 			try {
   320 			try {
   327 				File dir = new File(String.format("%sSounds/voices/%s",
   321 				File dir = FileUtils.getDataPathFile(TeamCreatorActivity.this, "Sounds", "voices", (String)voice.getSelectedItem());
   328 						Utils.getDataPath(TeamCreatorActivity.this),
       
   329 						voice.getSelectedItem()));
       
   330 				String file = "";
   322 				String file = "";
   331 				File[] dirs = dir.listFiles();
   323 				File[] dirs = dir.listFiles();
   332 				File f = dirs[(int) Math.round(Math.random() * dirs.length)];
   324 				File f = dirs[(int) Math.round(Math.random() * dirs.length)];
   333 				if (f.getName().endsWith(".ogg"))
   325 				if (f.getName().endsWith(".ogg"))
   334 					file = f.getAbsolutePath();
   326 					file = f.getAbsolutePath();
   339 					mp.reset();
   331 					mp.reset();
   340 				mp.setDataSource(file);
   332 				mp.setDataSource(file);
   341 				mp.prepare();
   333 				mp.prepare();
   342 				mp.start();
   334 				mp.start();
   343 			} catch (IllegalArgumentException e) {
   335 			} catch (IllegalArgumentException e) {
   344 				e.printStackTrace();
   336 				Log.e(TAG, "Unable to play voice sample", e);
   345 			} catch (IllegalStateException e) {
   337 			} catch (IllegalStateException e) {
   346 				e.printStackTrace();
   338 				Log.e(TAG, "Unable to play voice sample", e);
   347 			} catch (IOException e) {
   339 			} catch (IOException e) {
   348 				e.printStackTrace();
   340 				Log.e(TAG, "Unable to play voice sample", e);
   349 			}
   341 			}
   350 		}
   342 		}
   351 	};
   343 	};
   352 
   344 
       
   345 	@SuppressWarnings("unchecked")
   353 	private void setTeamValues(Team t){
   346 	private void setTeamValues(Team t){
   354 
   347 		if (t == null) {
   355 		if (t != null) {
   348 			return;
       
   349 		}
       
   350 		
       
   351 		try {
   356 			name.setText(t.name);
   352 			name.setText(t.name);
   357 			int position = ((ArrayAdapter<String>) voice.getAdapter()).getPosition(t.voice);
   353 			voice.setSelection(findPosition((ArrayAdapter<String>) voice.getAdapter(), t.voice));
   358 			voice.setSelection(position);
   354 			fort.setSelection(findPosition((ArrayAdapter<String>) fort.getAdapter(), t.fort));
   359 
   355 			difficulty.setSelection(findPosition(typesData, "level", Integer.valueOf(t.hogs.get(0).level)));
   360 			position = ((ArrayAdapter<String>) fort.getAdapter()).getPosition(t.fort);
   356 			grave.setSelection(findPosition(gravesData, "txt", t.grave));
   361 			fort.setSelection(position);
   357 			flag.setSelection(findPosition(flagsData, "txt", t.flag));
   362 
   358 	
   363 			position = 0;
   359 			for (int i = 0; i < Team.HEDGEHOGS_PER_TEAM; i++) {
   364 			for (HashMap<String, ?> hashmap : typesData) {
   360 				hogHat.get(i).setSelection(findPosition(hatsData, "txt", t.hogs.get(i).hat));
   365 				if (hashmap.get("txt").equals(t.levels[0])) {
   361 				hogName.get(i).setText(t.hogs.get(i).name);
   366 					difficulty.setSelection(position);
   362 			}
   367 					break;
   363 		} catch(NoSuchElementException e) {
   368 				}
   364 			Toast.makeText(getApplicationContext(), R.string.error_team_attribute_not_found, Toast.LENGTH_LONG).show();
   369 			}
   365 			finish();
   370 
   366 		}
   371 			position = 0;
   367 	}
   372 			for (HashMap<String, ?> hashmap : gravesData) {
   368 
   373 				if (hashmap.get("txt").equals(t.grave)) {
   369 	int findPosition(ArrayAdapter<String> adapter, String value) throws NoSuchElementException {
   374 					grave.setSelection(position);
   370 		int position = adapter.getPosition(value);
   375 					break;
   371 		if(position<0) {
   376 				}
   372 			throw new NoSuchElementException();
   377 			}
   373 		}
   378 
   374 		return position;
   379 			position = 0;
   375 	}
   380 			for (HashMap<String, ?> hashmap : typesData) {
   376 	
   381 				if (hashmap.get("txt").equals(t.flag)) {
   377 	int findPosition(List<? extends Map<String, ?>> data, String key, Object value) throws NoSuchElementException {
   382 					flag.setSelection(position);
   378 		int position = 0;
   383 					break;
   379 		for (Map<String, ?> map : data) {
   384 				}
   380 			if (map.get(key).equals(value)) {
   385 			}
   381 				return position;
   386 
   382 			}
   387 			for (int i = 0; i < Team.maxNumberOfHogs; i++) {
   383 			position++;
   388 				position = 0;
   384 		}
   389 				for (HashMap<String, ?> hashmap : hatsData) {
   385 		throw new NoSuchElementException();
   390 					if (hashmap.get("txt").equals(t.hats[i])) {
   386 	}
   391 						hogHat.get(i).setSelection(position);
       
   392 					}
       
   393 				}
       
   394 
       
   395 				hogName.get(i).setText(t.hogNames[i]);
       
   396 			}
       
   397 			this.fileName = t.file;
       
   398 		}
       
   399 	}
       
   400 
       
   401 
   387 
   402 	private SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() {
   388 	private SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() {
   403 
   389 
   404 		public boolean setViewValue(View view, Object data,
   390 		public boolean setViewValue(View view, Object data,
   405 				String textRepresentation) {
   391 				String textRepresentation) {
   410 			} else {
   396 			} else {
   411 				return false;
   397 				return false;
   412 			}
   398 			}
   413 		}
   399 		}
   414 	};
   400 	};
   415 
       
   416 }
   401 }