diff -r 4feced261c68 -r de822cd3df3a 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 Tue Jan 21 22:38:13 2014 +0100 +++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TeamCreatorActivity.java Tue Jan 21 22:43:06 2014 +0100 @@ -61,341 +61,341 @@ * as parameter oldTeamName. */ public class TeamCreatorActivity extends Activity implements Runnable { - public static final String PARAMETER_EXISTING_TEAMNAME = "existingTeamName"; - private static final String TAG = TeamCreatorActivity.class.getSimpleName(); - - 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 voiceButton; - private ScrollView scroller; - private MediaPlayer mp = null; - private boolean initComplete = false; - - private String existingTeamName = null; + public static final String PARAMETER_EXISTING_TEAMNAME = "existingTeamName"; + private static final String TAG = TeamCreatorActivity.class.getSimpleName(); + + 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 voiceButton; + private ScrollView scroller; + private MediaPlayer mp = null; + private boolean initComplete = false; + + private String existingTeamName = null; - private final List> flagsData = new ArrayList>(); - private final List> typesData = new ArrayList>(); - private final List> gravesData = new ArrayList>(); - private final List> hatsData = new ArrayList>(); - private final List voicesData = new ArrayList(); - private final List fortsData = new ArrayList(); + private final List> flagsData = new ArrayList>(); + private final List> typesData = new ArrayList>(); + private final List> gravesData = new ArrayList>(); + private final List> hatsData = new ArrayList>(); + private final List voicesData = new ArrayList(); + private final List fortsData = new ArrayList(); + + 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); + } - 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); + // Set up view + setContentView(R.layout.team_creation); - name = (TextView) findViewById(R.id.txtName); - difficulty = (Spinner) findViewById(R.id.spinType); - grave = (Spinner) findViewById(R.id.spinGrave); - flag = (Spinner) findViewById(R.id.spinFlag); - voice = (Spinner) findViewById(R.id.spinVoice); - fort = (Spinner) findViewById(R.id.spinFort); + name = (TextView) findViewById(R.id.txtName); + difficulty = (Spinner) findViewById(R.id.spinType); + grave = (Spinner) findViewById(R.id.spinGrave); + flag = (Spinner) findViewById(R.id.spinFlag); + voice = (Spinner) findViewById(R.id.spinVoice); + fort = (Spinner) findViewById(R.id.spinFort); - imgFort = (ImageView) findViewById(R.id.imgFort); + imgFort = (ImageView) findViewById(R.id.imgFort); + + voiceButton = (ImageButton) findViewById(R.id.btnPlay); + + scroller = (ScrollView) findViewById(R.id.scroller); - voiceButton = (ImageButton) findViewById(R.id.btnPlay); - - scroller = (ScrollView) findViewById(R.id.scroller); - - // 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); + // 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); - hogHat.add((Spinner) team_creation_entry - .findViewById(R.id.spinTeam1)); - hogDice.add((ImageButton) team_creation_entry - .findViewById(R.id.btnTeam1)); - hogName.add((EditText) team_creation_entry - .findViewById(R.id.txtTeam1)); - } + hogHat.add((Spinner) team_creation_entry + .findViewById(R.id.spinTeam1)); + hogDice.add((ImageButton) team_creation_entry + .findViewById(R.id.btnTeam1)); + hogName.add((EditText) team_creation_entry + .findViewById(R.id.txtTeam1)); + } - grave.setAdapter(createMapSpinnerAdapter(gravesData)); - flag.setAdapter(createMapSpinnerAdapter(flagsData)); - difficulty.setAdapter(createMapSpinnerAdapter(typesData)); - SpinnerAdapter hatAdapter = createMapSpinnerAdapter(hatsData); - for (Spinner spin : hogHat) { - spin.setAdapter(hatAdapter); - } + grave.setAdapter(createMapSpinnerAdapter(gravesData)); + flag.setAdapter(createMapSpinnerAdapter(flagsData)); + difficulty.setAdapter(createMapSpinnerAdapter(typesData)); + SpinnerAdapter hatAdapter = createMapSpinnerAdapter(hatsData); + for (Spinner spin : hogHat) { + spin.setAdapter(hatAdapter); + } - voice.setAdapter(createListSpinnerAdapter(voicesData)); - voiceButton.setOnClickListener(voiceClicker); + voice.setAdapter(createListSpinnerAdapter(voicesData)); + voiceButton.setOnClickListener(voiceClicker); + + fort.setAdapter(createListSpinnerAdapter(fortsData)); + fort.setOnItemSelectedListener(fortSelector); + + new Thread(this).start(); + } + + 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; + } - fort.setAdapter(createListSpinnerAdapter(fortsData)); - fort.setOnItemSelectedListener(fortSelector); + public void run(){ + try { + final List> gravesDataNew = FrontendDataUtils.getGraves(this); + runOnUiThread(new Runnable(){ + public void run() { + gravesData.addAll(gravesDataNew); + ((SimpleAdapter)grave.getAdapter()).notifyDataSetChanged(); + } + }); - new Thread(this).start(); - } + final List> flagsDataNew = FrontendDataUtils.getFlags(this); + runOnUiThread(new Runnable(){ + public void run() { + flagsData.addAll(flagsDataNew); + ((SimpleAdapter)flag.getAdapter()).notifyDataSetChanged(); + } + }); + + final List> typesDataNew = FrontendDataUtils.getTypes(this); + runOnUiThread(new Runnable(){ + public void run() { + typesData.addAll(typesDataNew); + ((SimpleAdapter)difficulty.getAdapter()).notifyDataSetChanged(); + } + }); + + final List> hatsDataNew = FrontendDataUtils.getHats(this); + runOnUiThread(new Runnable(){ + public void run() { + hatsData.addAll(hatsDataNew); + ((SimpleAdapter)hogHat.get(0).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; - } - - public void run(){ - try { - final List> gravesDataNew = FrontendDataUtils.getGraves(this); - runOnUiThread(new Runnable(){ - public void run() { - gravesData.addAll(gravesDataNew); - ((SimpleAdapter)grave.getAdapter()).notifyDataSetChanged(); - } - }); - - final List> flagsDataNew = FrontendDataUtils.getFlags(this); - runOnUiThread(new Runnable(){ - public void run() { - flagsData.addAll(flagsDataNew); - ((SimpleAdapter)flag.getAdapter()).notifyDataSetChanged(); - } - }); - - final List> typesDataNew = FrontendDataUtils.getTypes(this); - runOnUiThread(new Runnable(){ - public void run() { - typesData.addAll(typesDataNew); - ((SimpleAdapter)difficulty.getAdapter()).notifyDataSetChanged(); - } - }); - - final List> hatsDataNew = FrontendDataUtils.getHats(this); - runOnUiThread(new Runnable(){ - public void run() { - hatsData.addAll(hatsDataNew); - ((SimpleAdapter)hogHat.get(0).getAdapter()).notifyDataSetChanged(); - } - }); - - final List voicesDataNew = FrontendDataUtils.getVoices(this); - runOnUiThread(new Runnable(){ - public void run() { - voicesData.addAll(voicesDataNew); - ((ArrayAdapter)voice.getAdapter()).notifyDataSetChanged(); - } - }); - - final List 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) { - mp.release(); - mp = null; - } - } + final List voicesDataNew = FrontendDataUtils.getVoices(this); + runOnUiThread(new Runnable(){ + public void run() { + voicesData.addAll(voicesDataNew); + ((ArrayAdapter)voice.getAdapter()).notifyDataSetChanged(); + } + }); + + final List fortsDataNew = FrontendDataUtils.getForts(this); + runOnUiThread(new Runnable(){ + public void run() { + fortsData.addAll(fortsDataNew); + ((ArrayAdapter)fort.getAdapter()).notifyDataSetChanged(); + } + }); - @Override - protected void onSaveInstanceState(Bundle outState) { - super.onSaveInstanceState(outState); - outState.putString(PARAMETER_EXISTING_TEAMNAME, existingTeamName); - } + 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 onBackPressed() { - if(initComplete) { - saveTeam(); - } - setResult(RESULT_OK); - super.onBackPressed(); - } + public void onDestroy() { + super.onDestroy(); + if (mp != null) { + mp.release(); + mp = null; + } + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + outState.putString(PARAMETER_EXISTING_TEAMNAME, existingTeamName); + } + + public void onBackPressed() { + if(initComplete) { + saveTeam(); + } + setResult(RESULT_OK); + super.onBackPressed(); + } + + 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(); + int levelInt = (Integer)((Map) difficulty.getSelectedItem()).get("level"); - 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(); - int levelInt = (Integer)((Map) difficulty.getSelectedItem()).get("level"); - - 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); - // 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(); - } - }; + 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); + // 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 OnItemSelectedListener fortSelector = new OnItemSelectedListener() { - public void onItemSelected(AdapterView arg0, View arg1, - int position, long arg3) { - String fortName = (String) arg0.getAdapter().getItem(position); - try { - File fortImage = FileUtils.getDataPathFile(TeamCreatorActivity.this, "Forts", fortName, "L.png"); - Drawable fortIconDrawable = Drawable.createFromPath(fortImage.getAbsolutePath()); - imgFort.setImageDrawable(fortIconDrawable); - } catch(IOException e) { - Log.e(TAG, "Unable to show fort image", e); - } - scroller.fullScroll(ScrollView.FOCUS_DOWN);// Scroll the scrollview - // to the bottom, work - // around for scrollview - // invalidation (scrolls - // back to top) - } + private OnItemSelectedListener fortSelector = new OnItemSelectedListener() { + public void onItemSelected(AdapterView arg0, View arg1, + int position, long arg3) { + String fortName = (String) arg0.getAdapter().getItem(position); + try { + File fortImage = FileUtils.getDataPathFile(TeamCreatorActivity.this, "Forts", fortName, "L.png"); + Drawable fortIconDrawable = Drawable.createFromPath(fortImage.getAbsolutePath()); + imgFort.setImageDrawable(fortIconDrawable); + } catch(IOException e) { + Log.e(TAG, "Unable to show fort image", e); + } + scroller.fullScroll(ScrollView.FOCUS_DOWN);// Scroll the scrollview + // to the bottom, work + // around for scrollview + // invalidation (scrolls + // back to top) + } - public void onNothingSelected(AdapterView arg0) { - } + public void onNothingSelected(AdapterView arg0) { + } - }; + }; - private OnClickListener voiceClicker = new OnClickListener() { - public void onClick(View v) { - try { - File dir = FileUtils.getDataPathFile(TeamCreatorActivity.this, "Sounds", "voices", (String)voice.getSelectedItem()); - String file = ""; - File[] dirs = dir.listFiles(); - File f = dirs[(int) Math.round(Math.random() * dirs.length)]; - if (f.getName().endsWith(".ogg")) - file = f.getAbsolutePath(); + private OnClickListener voiceClicker = new OnClickListener() { + public void onClick(View v) { + try { + File dir = FileUtils.getDataPathFile(TeamCreatorActivity.this, "Sounds", "voices", (String)voice.getSelectedItem()); + String file = ""; + File[] dirs = dir.listFiles(); + File f = dirs[(int) Math.round(Math.random() * dirs.length)]; + if (f.getName().endsWith(".ogg")) + file = f.getAbsolutePath(); - if (mp == null) - mp = new MediaPlayer(); - else - mp.reset(); - mp.setDataSource(file); - mp.prepare(); - mp.start(); - } catch (IllegalArgumentException e) { - Log.e(TAG, "Unable to play voice sample", e); - } catch (IllegalStateException e) { - Log.e(TAG, "Unable to play voice sample", e); - } catch (IOException e) { - Log.e(TAG, "Unable to play voice sample", e); - } - } - }; + if (mp == null) + mp = new MediaPlayer(); + else + mp.reset(); + mp.setDataSource(file); + mp.prepare(); + mp.start(); + } catch (IllegalArgumentException e) { + Log.e(TAG, "Unable to play voice sample", e); + } catch (IllegalStateException e) { + Log.e(TAG, "Unable to play voice sample", e); + } catch (IOException e) { + Log.e(TAG, "Unable to play voice sample", e); + } + } + }; + + @SuppressWarnings("unchecked") + private void setTeamValues(Team t){ + if (t == null) { + return; + } + + try { + name.setText(t.name); + voice.setSelection(findPosition((ArrayAdapter) voice.getAdapter(), t.voice)); + fort.setSelection(findPosition((ArrayAdapter) fort.getAdapter(), t.fort)); + difficulty.setSelection(findPosition(typesData, "level", Integer.valueOf(t.hogs.get(0).level))); + grave.setSelection(findPosition(gravesData, "txt", t.grave)); + flag.setSelection(findPosition(flagsData, "txt", t.flag)); - @SuppressWarnings("unchecked") - private void setTeamValues(Team t){ - if (t == null) { - return; - } - - try { - name.setText(t.name); - voice.setSelection(findPosition((ArrayAdapter) voice.getAdapter(), t.voice)); - fort.setSelection(findPosition((ArrayAdapter) fort.getAdapter(), t.fort)); - difficulty.setSelection(findPosition(typesData, "level", Integer.valueOf(t.hogs.get(0).level))); - grave.setSelection(findPosition(gravesData, "txt", t.grave)); - flag.setSelection(findPosition(flagsData, "txt", t.flag)); - - for (int i = 0; i < Team.HEDGEHOGS_PER_TEAM; i++) { - hogHat.get(i).setSelection(findPosition(hatsData, "txt", t.hogs.get(i).hat)); - hogName.get(i).setText(t.hogs.get(i).name); - } - } catch(NoSuchElementException e) { - Toast.makeText(getApplicationContext(), R.string.error_team_attribute_not_found, Toast.LENGTH_LONG).show(); - finish(); - } - } + for (int i = 0; i < Team.HEDGEHOGS_PER_TEAM; i++) { + hogHat.get(i).setSelection(findPosition(hatsData, "txt", t.hogs.get(i).hat)); + hogName.get(i).setText(t.hogs.get(i).name); + } + } catch(NoSuchElementException e) { + Toast.makeText(getApplicationContext(), R.string.error_team_attribute_not_found, Toast.LENGTH_LONG).show(); + finish(); + } + } + + int findPosition(ArrayAdapter adapter, String value) throws NoSuchElementException { + int position = adapter.getPosition(value); + if(position<0) { + throw new NoSuchElementException(); + } + return position; + } - int findPosition(ArrayAdapter adapter, String value) throws NoSuchElementException { - int position = adapter.getPosition(value); - if(position<0) { - throw new NoSuchElementException(); - } - return position; - } - - int findPosition(List> data, String key, Object value) throws NoSuchElementException { - int position = 0; - for (Map map : data) { - if (map.get(key).equals(value)) { - return position; - } - position++; - } - throw new NoSuchElementException(); - } + int findPosition(List> data, String key, Object value) throws NoSuchElementException { + int position = 0; + for (Map map : data) { + if (map.get(key).equals(value)) { + return position; + } + position++; + } + throw new NoSuchElementException(); + } + + private SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() { - private SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() { - - public boolean setViewValue(View view, Object data, - String textRepresentation) { - if (view instanceof ImageView && data instanceof Bitmap) { - ImageView v = (ImageView) view; - v.setImageBitmap((Bitmap) data); - return true; - } else { - return false; - } - } - }; + public boolean setViewValue(View view, Object data, + String textRepresentation) { + if (view instanceof ImageView && data instanceof Bitmap) { + ImageView v = (ImageView) view; + v.setImageBitmap((Bitmap) data); + return true; + } else { + return false; + } + } + }; }