diff -r d70a5b0d1190 -r 0481bd74267c project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TeamCreatorActivity.java --- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TeamCreatorActivity.java Sun Aug 12 22:37:57 2012 +0200 +++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TeamCreatorActivity.java Sun Aug 12 22:46:23 2012 +0200 @@ -20,11 +20,13 @@ package org.hedgewars.hedgeroid; import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.NoSuchElementException; import org.hedgewars.hedgeroid.Datastructures.FrontendDataUtils; import org.hedgewars.hedgeroid.Datastructures.Hog; @@ -37,7 +39,6 @@ import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; -import android.view.View.OnFocusChangeListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; @@ -49,23 +50,29 @@ import android.widget.ScrollView; import android.widget.SimpleAdapter; import android.widget.Spinner; +import android.widget.SpinnerAdapter; import android.widget.TextView; import android.widget.Toast; -public class TeamCreatorActivity extends Activity implements Runnable{ - +/** + * Edit or create a team. If a team should be edited, it is supplied in the extras + * as parameter oldTeamName. + */ +public class TeamCreatorActivity extends Activity implements Runnable { + public static final String PARAMETER_EXISTING_TEAMNAME = "existingTeamName"; + private TextView name; private Spinner difficulty, grave, flag, voice, fort; private ImageView imgFort; private ArrayList hogDice = new ArrayList(); private ArrayList hogHat = new ArrayList(); private ArrayList hogName = new ArrayList(); - private ImageButton back, save, voiceButton; + private ImageButton voiceButton; private ScrollView scroller; private MediaPlayer mp = null; - private boolean settingsChanged = false; - private boolean saved = false; - private String fileName = null; + private boolean initComplete = false; + + private String existingTeamName = null; private final List> flagsData = new ArrayList>(); private final List> typesData = new ArrayList>(); @@ -76,6 +83,16 @@ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + initComplete = false; + + // Restore state and read parameters + if(savedInstanceState != null) { + existingTeamName = savedInstanceState.getString(PARAMETER_EXISTING_TEAMNAME); + } else { + existingTeamName = getIntent().getStringExtra(PARAMETER_EXISTING_TEAMNAME); + } + + // Set up view setContentView(R.layout.team_creation); name = (TextView) findViewById(R.id.txtName); @@ -87,15 +104,11 @@ imgFort = (ImageView) findViewById(R.id.imgFort); - back = (ImageButton) findViewById(R.id.btnBack); - save = (ImageButton) findViewById(R.id.btnSave); voiceButton = (ImageButton) findViewById(R.id.btnPlay); scroller = (ScrollView) findViewById(R.id.scroller); - save.setOnClickListener(saveClicker); - back.setOnClickListener(backClicker); - + // Wire view elements LinearLayout ll = (LinearLayout) findViewById(R.id.HogsContainer); for (int i = 0; i < ll.getChildCount(); i++) { RelativeLayout team_creation_entry = (RelativeLayout) ll.getChildAt(i); @@ -108,107 +121,116 @@ .findViewById(R.id.txtTeam1)); } - SimpleAdapter sa = new SimpleAdapter(this, gravesData, - R.layout.spinner_textimg_entry, new String[] { "txt", "img" }, - new int[] { R.id.spinner_txt, R.id.spinner_img }); - sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry); - sa.setViewBinder(viewBinder); - grave.setAdapter(sa); - grave.setOnFocusChangeListener(focusser); - - sa = new SimpleAdapter(this, flagsData, R.layout.spinner_textimg_entry, - new String[] { "txt", "img" }, new int[] { R.id.spinner_txt, - R.id.spinner_img }); - sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry); - sa.setViewBinder(viewBinder); - flag.setAdapter(sa); - flag.setOnFocusChangeListener(focusser); - - sa = new SimpleAdapter(this, typesData, R.layout.spinner_textimg_entry, - new String[] { "txt", "img" }, new int[] { R.id.spinner_txt, - R.id.spinner_img }); - sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry); - difficulty.setAdapter(sa); - difficulty.setOnFocusChangeListener(focusser); - - sa = new SimpleAdapter(this, hatsData, R.layout.spinner_textimg_entry, - new String[] { "txt", "img" }, new int[] { R.id.spinner_txt, - R.id.spinner_img }); - sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry); - sa.setViewBinder(viewBinder); + grave.setAdapter(createMapSpinnerAdapter(gravesData)); + flag.setAdapter(createMapSpinnerAdapter(flagsData)); + difficulty.setAdapter(createMapSpinnerAdapter(typesData)); + SpinnerAdapter hatAdapter = createMapSpinnerAdapter(hatsData); for (Spinner spin : hogHat) { - spin.setAdapter(sa); + spin.setAdapter(hatAdapter); } - ArrayAdapter adapter = new ArrayAdapter(this, R.layout.listview_item, voicesData); - adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - voice.setAdapter(adapter); - voice.setOnFocusChangeListener(focusser); + + voice.setAdapter(createListSpinnerAdapter(voicesData)); voiceButton.setOnClickListener(voiceClicker); - adapter = new ArrayAdapter(this, R.layout.listview_item, fortsData); - adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); - fort.setAdapter(adapter); + fort.setAdapter(createListSpinnerAdapter(fortsData)); fort.setOnItemSelectedListener(fortSelector); - fort.setOnFocusChangeListener(focusser); new Thread(this).start(); } - public void run(){ - final ArrayList> gravesDataNew = FrontendDataUtils.getGraves(this); - this.runOnUiThread(new Runnable(){ - public void run() { - copy(gravesData, gravesDataNew); - ((SimpleAdapter)grave.getAdapter()).notifyDataSetChanged(); - } - }); - - final ArrayList> flagsDataNew = FrontendDataUtils.getFlags(this); - this.runOnUiThread(new Runnable(){ - public void run() { - copy(flagsData, flagsDataNew); - ((SimpleAdapter)flag.getAdapter()).notifyDataSetChanged(); - } - }); - - final ArrayList> typesDataNew = FrontendDataUtils.getTypes(this); - this.runOnUiThread(new Runnable(){ - public void run() { - copy(typesData, typesDataNew); - ((SimpleAdapter)difficulty.getAdapter()).notifyDataSetChanged(); - } - }); - - final ArrayList> hatsDataNew = FrontendDataUtils.getHats(this); - this.runOnUiThread(new Runnable(){ - public void run() { - copy(hatsData, hatsDataNew); - ((SimpleAdapter)hogHat.get(0).getAdapter()).notifyDataSetChanged(); - } - }); - - final ArrayList voicesDataNew = FrontendDataUtils.getVoices(this); - this.runOnUiThread(new Runnable(){ - public void run() { - copy(voicesData, voicesDataNew); - ((ArrayAdapter)voice.getAdapter()).notifyDataSetChanged(); - } - }); - - final ArrayList fortsDataNew = FrontendDataUtils.getForts(this); - this.runOnUiThread(new Runnable(){ - public void run() { - copy(fortsData, fortsDataNew); - ((ArrayAdapter)fort.getAdapter()).notifyDataSetChanged(); - } - }); + private SpinnerAdapter createMapSpinnerAdapter(List> data) { + SimpleAdapter sa = new SimpleAdapter(this, data, + R.layout.spinner_textimg_entry, new String[] { "txt", "img" }, + new int[] { R.id.spinner_txt, R.id.spinner_img }); + sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry); + sa.setViewBinder(viewBinder); + return sa; + } + + private SpinnerAdapter createListSpinnerAdapter(List data) { + ArrayAdapter adapter = new ArrayAdapter(this, R.layout.listview_item, data); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + return adapter; } - private static void copy(List dest, List src){ - for(T t: src) dest.add(t); + public void run(){ + try { + final ArrayList> gravesDataNew = FrontendDataUtils.getGraves(this); + runOnUiThread(new Runnable(){ + public void run() { + gravesData.addAll(gravesDataNew); + ((SimpleAdapter)grave.getAdapter()).notifyDataSetChanged(); + } + }); + + final ArrayList> flagsDataNew = FrontendDataUtils.getFlags(this); + runOnUiThread(new Runnable(){ + public void run() { + flagsData.addAll(flagsDataNew); + ((SimpleAdapter)flag.getAdapter()).notifyDataSetChanged(); + } + }); + + final ArrayList> typesDataNew = FrontendDataUtils.getTypes(this); + runOnUiThread(new Runnable(){ + public void run() { + typesData.addAll(typesDataNew); + ((SimpleAdapter)difficulty.getAdapter()).notifyDataSetChanged(); + } + }); + + final ArrayList> hatsDataNew = FrontendDataUtils.getHats(this); + runOnUiThread(new Runnable(){ + public void run() { + hatsData.addAll(hatsDataNew); + ((SimpleAdapter)hogHat.get(0).getAdapter()).notifyDataSetChanged(); + } + }); + + final ArrayList voicesDataNew = FrontendDataUtils.getVoices(this); + runOnUiThread(new Runnable(){ + public void run() { + voicesData.addAll(voicesDataNew); + ((ArrayAdapter)voice.getAdapter()).notifyDataSetChanged(); + } + }); + + final ArrayList fortsDataNew = FrontendDataUtils.getForts(this); + runOnUiThread(new Runnable(){ + public void run() { + fortsData.addAll(fortsDataNew); + ((ArrayAdapter)fort.getAdapter()).notifyDataSetChanged(); + } + }); + + if(existingTeamName!=null) { + final Team loadedTeam = Team.load(Team.getTeamfileByName(getApplicationContext(), existingTeamName)); + if(loadedTeam==null) { + existingTeamName = null; + } else { + runOnUiThread(new Runnable(){ + public void run() { + setTeamValues(loadedTeam); + } + }); + } + } + runOnUiThread(new Runnable(){ + public void run() { + initComplete = true; + } + }); + } catch(FileNotFoundException e) { + this.runOnUiThread(new Runnable(){ + public void run() { + Toast.makeText(getApplicationContext(), R.string.error_missing_sdcard_or_files, Toast.LENGTH_LONG).show(); + finish(); + } + }); + } } - + public void onDestroy() { super.onDestroy(); if (mp != null) { @@ -217,93 +239,88 @@ } } - private OnFocusChangeListener focusser = new OnFocusChangeListener() { - public void onFocusChange(View v, boolean hasFocus) { - settingsChanged = true; - } - - }; + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + outState.putString(PARAMETER_EXISTING_TEAMNAME, existingTeamName); + } public void onBackPressed() { - onFinishing(); + if(initComplete) { + saveTeam(); + } + setResult(RESULT_OK); super.onBackPressed(); - } - private OnClickListener backClicker = new OnClickListener() { - public void onClick(View v) { - onFinishing(); - finish(); + private int getDifficultyLevelFromText(String text) { + if (text.equals(getString(R.string.human))) { + return 0; + } else if (text.equals(getString(R.string.bot5))) { + return 1; + } else if (text.equals(getString(R.string.bot4))) { + return 2; + } else if (text.equals(getString(R.string.bot3))) { + return 3; + } else if (text.equals(getString(R.string.bot2))) { + return 4; + } else { + return 5; + } + } + + private String getTxtFromDifficulty(int level) { + switch(level) { + case 0: return getString(R.string.human); + case 1: return getString(R.string.bot5); + case 2: return getString(R.string.bot4); + case 3: return getString(R.string.bot3); + case 4: return getString(R.string.bot2); + default: return getString(R.string.bot1); + } + } + + private void saveTeam() { + String teamName = name.getText().toString(); + String teamFlag = (String)((Map) flag.getSelectedItem()).get("txt"); + String teamFort = fort.getSelectedItem().toString(); + String teamGrave = (String)((Map) grave.getSelectedItem()).get("txt"); + String teamVoice = voice.getSelectedItem().toString(); + String levelString = (String)((Map) difficulty.getSelectedItem()).get("txt"); + int levelInt = getDifficultyLevelFromText(levelString); + + List hogs = new ArrayList(); + for (int i = 0; i < hogName.size(); i++) { + String name = hogName.get(i).getText().toString(); + String hat = ((Map) hogHat.get(i).getSelectedItem()).get("txt").toString(); + hogs.add(new Hog(name, hat, levelInt)); + } + + Team team = new Team(teamName, teamGrave, teamFlag, teamVoice, teamFort, hogs); + File teamsDir = new File(getFilesDir(), Team.DIRECTORY_TEAMS); + if (!teamsDir.exists()) teamsDir.mkdir(); + + File newFile = Team.getTeamfileByName(this, teamName); + File oldFile = null; + if(existingTeamName != null) { + oldFile = Team.getTeamfileByName(this, existingTeamName); + } + try { + team.save(newFile); + Toast.makeText(TeamCreatorActivity.this, R.string.saved, Toast.LENGTH_SHORT).show(); + // If the team was renamed, delete the old file. + if(oldFile != null && oldFile.isFile() && !oldFile.equals(newFile)) { + oldFile.delete(); + } + existingTeamName = teamName; + } catch(IOException e) { + Toast.makeText(getApplicationContext(), R.string.error_save_failed, Toast.LENGTH_SHORT).show(); } }; - private void onFinishing() { - if (settingsChanged) { - setResult(RESULT_OK); - } else { - setResult(RESULT_CANCELED); - } - } - - private OnClickListener saveClicker = new OnClickListener() { - public void onClick(View v) { - String teamName = name.getText().toString(); - String teamFlag = (String)((Map) flag.getSelectedItem()).get("txt"); - String teamFort = fort.getSelectedItem().toString(); - String teamGrave = (String)((Map) grave.getSelectedItem()).get("txt"); - String teamVoice = voice.getSelectedItem().toString(); - String levelString = (String)((Map) difficulty.getSelectedItem()).get("txt"); - int levelInt; - if (levelString.equals(getString(R.string.human))) { - levelInt = 0; - } else if (levelString.equals(getString(R.string.bot5))) { - levelInt = 1; - } else if (levelString.equals(getString(R.string.bot4))) { - levelInt = 2; - } else if (levelString.equals(getString(R.string.bot3))) { - levelInt = 3; - } else if (levelString.equals(getString(R.string.bot2))) { - levelInt = 4; - } else { - levelInt = 5; - } - - List hogs = new ArrayList(); - for (int i = 0; i < hogName.size(); i++) { - String name = hogName.get(i).getText().toString(); - String hat = ((Map) hogHat.get(i).getSelectedItem()).get("txt").toString(); - hogs.add(new Hog(name, hat, levelInt)); - } - - Team team = new Team(teamName, teamGrave, teamFlag, teamVoice, teamFort, hogs); - File teamsDir = new File(getFilesDir(), Team.DIRECTORY_TEAMS); - if (!teamsDir.exists()) teamsDir.mkdir(); - if(fileName == null){ - fileName = createNewFilename(Utils.replaceBadChars(team.name)); - } - try { - team.save(new File(teamsDir, fileName)); - Toast.makeText(TeamCreatorActivity.this, R.string.saved, Toast.LENGTH_SHORT).show(); - saved = true; - } catch(IOException e) { - Toast.makeText(getApplicationContext(), R.string.error_save_failed, Toast.LENGTH_SHORT).show(); - } - } - }; - - private String createNewFilename(String suggestedName){ - File f = new File(getFilesDir(), suggestedName); - if(f.exists()){ - return createNewFilename(suggestedName + (int)(Math.random()*10)); - } else { - return suggestedName + (int)(Math.random()*10); - } - } - private OnItemSelectedListener fortSelector = new OnItemSelectedListener() { public void onItemSelected(AdapterView arg0, View arg1, int position, long arg3) { - settingsChanged = true; String fortName = (String) arg0.getAdapter().getItem(position); Drawable fortIconDrawable = Drawable.createFromPath(Utils .getDataPath(TeamCreatorActivity.this) @@ -351,54 +368,48 @@ } }; + @SuppressWarnings("unchecked") private void setTeamValues(Team t){ - - if (t != null) { + if (t == null) { + return; + } + + try { name.setText(t.name); - int position = ((ArrayAdapter) voice.getAdapter()).getPosition(t.voice); - voice.setSelection(position); - - position = ((ArrayAdapter) fort.getAdapter()).getPosition(t.fort); - fort.setSelection(position); - - position = 0; - for (HashMap hashmap : typesData) { - if (t.hogs.get(0) != null && hashmap.get("txt").equals(t.hogs.get(0).level)) { - difficulty.setSelection(position); - break; - } - } - - position = 0; - for (HashMap hashmap : gravesData) { - if (hashmap.get("txt").equals(t.grave)) { - grave.setSelection(position); - break; - } - } - - position = 0; - for (HashMap hashmap : typesData) { - if (hashmap.get("txt").equals(t.flag)) { - flag.setSelection(position); - break; - } - } - - for (int i = 0; i < Team.maxNumberOfHogs; i++) { - position = 0; - for (HashMap hashmap : hatsData) { - if (hashmap.get("txt").equals(t.hogs.get(i).hat)) { - hogHat.get(i).setSelection(position); - } - } - + voice.setSelection(findPosition((ArrayAdapter) voice.getAdapter(), t.voice)); + fort.setSelection(findPosition((ArrayAdapter) fort.getAdapter(), t.fort)); + difficulty.setSelection(findPosition(typesData, getTxtFromDifficulty(t.hogs.get(0).level))); // TODO store actual difficulty int in typesData + grave.setSelection(findPosition(gravesData, t.grave)); + flag.setSelection(findPosition(flagsData, t.flag)); + + for (int i = 0; i < Team.HEDGEHOGS_PER_TEAM; i++) { + hogHat.get(i).setSelection(findPosition(hatsData, t.hogs.get(i).hat)); hogName.get(i).setText(t.hogs.get(i).name); } - //this.fileName = t.file; + } catch(NoSuchElementException e) { + Toast.makeText(getApplicationContext(), R.string.error_team_attribute_not_found, Toast.LENGTH_LONG).show(); + finish(); } } + int findPosition(ArrayAdapter adapter, String key) throws NoSuchElementException { + int position = adapter.getPosition(key); + if(position<0) { + throw new NoSuchElementException(); + } + return position; + } + + int findPosition(List> data, String txtValue) throws NoSuchElementException { + int position = 0; + for (Map map : data) { + if (map.get("txt").equals(txtValue)) { + return position; + } + position++; + } + throw new NoSuchElementException(); + } private SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() { @@ -413,5 +424,4 @@ } } }; - }