android: load graphics multithreaded for superduper loadtimes
authorXeli
Sun, 27 Nov 2011 00:55:45 +0100
changeset 6447 0bb16bc5c8b4
parent 6446 a49d01b96185
child 6448 88e49851d814
android: load graphics multithreaded for superduper loadtimes
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/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	Sun Nov 27 00:54:00 2011 +0100
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/EngineProtocol/FrontendDataUtils.java	Sun Nov 27 00:55:45 2011 +0100
@@ -21,7 +21,6 @@
 
 import java.io.File;
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
@@ -167,8 +166,8 @@
 		return data;
 	}
 
-	public static ArrayList<HashMap<String, Object>> getTeams(Context c){
-		ArrayList<HashMap<String, Object>> ret = new ArrayList<HashMap<String, Object>>();
+	public static List<HashMap<String, Object>> getTeams(Context c){
+		List<HashMap<String, Object>> ret = new ArrayList<HashMap<String, Object>>();
 
 		File teamsDir = new File(c.getFilesDir().getAbsolutePath() + '/' + Team.DIRECTORY_TEAMS);
 		File[] teamFileNames = teamsDir.listFiles();
@@ -177,14 +176,14 @@
 				Team t = Team.getTeamFromXml(s.getAbsolutePath());
 				if(t != null){
 					t.file = s.getName();
-					ret.add(teamToHashMap(t));
+					ret.add(teamToMap(t));
 				}
 			}
 		}
 		return ret;
 	}
 
-	public static HashMap<String, Object> teamToHashMap(Team t){
+	public static HashMap<String, Object> teamToMap(Team t){
 		HashMap<String, Object> hashmap = new HashMap<String, Object>();
 		hashmap.put("team", t);
 		hashmap.put("txt", t.name);
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TeamCreatorActivity.java	Sun Nov 27 00:54:00 2011 +0100
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TeamCreatorActivity.java	Sun Nov 27 00:55:45 2011 +0100
@@ -24,7 +24,9 @@
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashMap;
+import java.util.List;
 
 import org.hedgewars.hedgeroid.EngineProtocol.FrontendDataUtils;
 import org.hedgewars.hedgeroid.EngineProtocol.Team;
@@ -51,7 +53,7 @@
 import android.widget.TextView;
 import android.widget.Toast;
 
-public class TeamCreatorActivity extends Activity {
+public class TeamCreatorActivity extends Activity implements Runnable{
 
 	private TextView name;
 	private Spinner difficulty, grave, flag, voice, fort;
@@ -66,6 +68,9 @@
 	private boolean saved = false;
 	private String fileName = null;
 
+	private List<HashMap<String, ?>> flagsData, typesData, gravesData, hatsData;
+	private List<String> voicesData, fortsData;
+
 	public void onCreate(Bundle savedInstanceState) {
 		super.onCreate(savedInstanceState);
 		setContentView(R.layout.team_creation);
@@ -90,8 +95,7 @@
 
 		LinearLayout ll = (LinearLayout) findViewById(R.id.HogsContainer);
 		for (int i = 0; i < ll.getChildCount(); i++) {
-			RelativeLayout team_creation_entry = (RelativeLayout) ll
-					.getChildAt(i);
+			RelativeLayout team_creation_entry = (RelativeLayout) ll.getChildAt(i);
 
 			hogHat.add((Spinner) team_creation_entry
 					.findViewById(R.id.spinTeam1));
@@ -100,7 +104,8 @@
 			hogName.add((EditText) team_creation_entry
 					.findViewById(R.id.txtTeam1));
 		}
-		ArrayList<HashMap<String, ?>> gravesData = FrontendDataUtils.getGraves(this);
+
+		gravesData = new ArrayList<HashMap<String, ?>>();
 		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 });
@@ -109,97 +114,86 @@
 		grave.setAdapter(sa);
 		grave.setOnFocusChangeListener(focusser);
 
-		ArrayList<HashMap<String, ?>> flagsData = FrontendDataUtils
-				.getFlags(this);
+		flagsData = new ArrayList<HashMap<String, ?>>();
 		sa = new SimpleAdapter(this, flagsData, R.layout.spinner_textimg_entry,
 				new String[] { "txt", "img" }, new int[] { R.id.spinner_txt,
-						R.id.spinner_img });
+				R.id.spinner_img });
 		sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry);
 		sa.setViewBinder(viewBinder);
 		flag.setAdapter(sa);
 		flag.setOnFocusChangeListener(focusser);
 
-		ArrayList<HashMap<String, ?>> typesData = FrontendDataUtils
-				.getTypes(this);
+		typesData = new ArrayList<HashMap<String, ?>>();
 		sa = new SimpleAdapter(this, typesData, R.layout.spinner_textimg_entry,
 				new String[] { "txt", "img" }, new int[] { R.id.spinner_txt,
-						R.id.spinner_img });
+				R.id.spinner_img });
 		sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry);
 		difficulty.setAdapter(sa);
 		difficulty.setOnFocusChangeListener(focusser);
 
-		ArrayList<HashMap<String, ?>> hatsData = FrontendDataUtils
-				.getHats(this);
+		hatsData = new ArrayList<HashMap<String, ?>>();
 		sa = new SimpleAdapter(this, hatsData, R.layout.spinner_textimg_entry,
 				new String[] { "txt", "img" }, new int[] { R.id.spinner_txt,
-						R.id.spinner_img });
+				R.id.spinner_img });
 		sa.setDropDownViewResource(R.layout.spinner_textimg_dropdown_entry);
 		sa.setViewBinder(viewBinder);
 		for (Spinner spin : hogHat) {
 			spin.setAdapter(sa);
 		}
 
-		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
-				R.layout.listview_item, FrontendDataUtils.getVoices(this));
+		voicesData = new ArrayList<String>();
+		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listview_item, voicesData);
 		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 		voice.setAdapter(adapter);
 		voice.setOnFocusChangeListener(focusser);
 		voiceButton.setOnClickListener(voiceClicker);
 
-		adapter = new ArrayAdapter<String>(this, R.layout.listview_item,
-				FrontendDataUtils.getForts(this));
+		fortsData = new ArrayList<String>();
+		adapter = new ArrayAdapter<String>(this, R.layout.listview_item, fortsData);
 		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 		fort.setAdapter(adapter);
 		fort.setOnItemSelectedListener(fortSelector);
 		fort.setOnFocusChangeListener(focusser);
 
-		Team t = this.getIntent().getParcelableExtra("team");
-		if (t != null) {
-			name.setText(t.name);
-			int position = ((ArrayAdapter<String>) voice.getAdapter())
-					.getPosition(t.voice);
-			voice.setSelection(position);
-
-			position = ((ArrayAdapter<String>) fort.getAdapter())
-					.getPosition(t.fort);
-			fort.setSelection(position);
-
-			position = 0;
-			for (HashMap<String, ?> hashmap : typesData) {
-				if (hashmap.get("txt").equals(t.levels[0])) {
-					difficulty.setSelection(position);
-					break;
-				}
-			}
+		new Thread(this).start();
+	}
 
-			position = 0;
-			for (HashMap<String, ?> hashmap : gravesData) {
-				if (hashmap.get("txt").equals(t.grave)) {
-					grave.setSelection(position);
-					break;
-				}
+	public void run(){
+		try {
+			Thread.sleep(2000);
+		} catch (InterruptedException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+		ArrayList<HashMap<String, ?>> gravesData = FrontendDataUtils.getGraves(this);
+		ArrayList<HashMap<String, ?>> flagsData = FrontendDataUtils.getFlags(this);
+		ArrayList<HashMap<String, ?>> typesData = FrontendDataUtils.getTypes(this);
+		ArrayList<HashMap<String, ?>> hatsData = FrontendDataUtils.getHats(this);
+		ArrayList<String> voicesData = FrontendDataUtils.getVoices(this);
+		ArrayList<String> fortsData = FrontendDataUtils.getForts(this);
+		
+		copy(this.gravesData, gravesData);
+		copy(this.flagsData, flagsData);
+		copy(this.typesData, typesData);
+		copy(this.hatsData, hatsData);
+		copy(this.voicesData, voicesData);
+		copy(this.fortsData, fortsData);
+		
+		this.runOnUiThread(new Runnable(){
+			public void run() {
+				((SimpleAdapter)grave.getAdapter()).notifyDataSetChanged();
+				((SimpleAdapter)flag.getAdapter()).notifyDataSetChanged();
+				((SimpleAdapter)difficulty.getAdapter()).notifyDataSetChanged();
+				((SimpleAdapter)hogHat.get(0).getAdapter()).notifyDataSetChanged();				
+				((ArrayAdapter<String>)fort.getAdapter()).notifyDataSetChanged();
+				((ArrayAdapter<String>)voice.getAdapter()).notifyDataSetChanged();
 			}
+		});		
 
-			position = 0;
-			for (HashMap<String, ?> 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<String, ?> hashmap : hatsData) {
-					if (hashmap.get("txt").equals(t.hats[i])) {
-						hogHat.get(i).setSelection(position);
-					}
-				}
-
-				hogName.get(i).setText(t.hogNames[i]);
-			}
-			this.fileName = t.file;
-		}
+	}
+	
+	private static <T> void copy(List<T> dest, List<T> src){
+		for(T t: src) dest.add(t);
 	}
 
 	public void onDestroy() {
@@ -304,10 +298,10 @@
 					+ fortName + "L.png");
 			imgFort.setImageDrawable(fortIconDrawable);
 			scroller.fullScroll(ScrollView.FOCUS_DOWN);// Scroll the scrollview
-														// to the bottom, work
-														// around for scollview
-														// invalidation (scrolls
-														// back to top)
+			// to the bottom, work
+			// around for scollview
+			// invalidation (scrolls
+			// back to top)
 		}
 
 		public void onNothingSelected(AdapterView<?> arg0) {
@@ -344,6 +338,55 @@
 		}
 	};
 
+	private void setTeamValues(Team t){
+
+		if (t != null) {
+			name.setText(t.name);
+			int position = ((ArrayAdapter<String>) voice.getAdapter()).getPosition(t.voice);
+			voice.setSelection(position);
+
+			position = ((ArrayAdapter<String>) fort.getAdapter()).getPosition(t.fort);
+			fort.setSelection(position);
+
+			position = 0;
+			for (HashMap<String, ?> hashmap : typesData) {
+				if (hashmap.get("txt").equals(t.levels[0])) {
+					difficulty.setSelection(position);
+					break;
+				}
+			}
+
+			position = 0;
+			for (HashMap<String, ?> hashmap : gravesData) {
+				if (hashmap.get("txt").equals(t.grave)) {
+					grave.setSelection(position);
+					break;
+				}
+			}
+
+			position = 0;
+			for (HashMap<String, ?> 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<String, ?> hashmap : hatsData) {
+					if (hashmap.get("txt").equals(t.hats[i])) {
+						hogHat.get(i).setSelection(position);
+					}
+				}
+
+				hogName.get(i).setText(t.hogNames[i]);
+			}
+			this.fileName = t.file;
+		}
+	}
+
+
 	private SimpleAdapter.ViewBinder viewBinder = new SimpleAdapter.ViewBinder() {
 
 		public boolean setViewValue(View view, Object data,
--- a/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TeamSelectionActivity.java	Sun Nov 27 00:54:00 2011 +0100
+++ b/project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TeamSelectionActivity.java	Sun Nov 27 00:55:45 2011 +0100
@@ -22,6 +22,7 @@
 import java.io.File;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 
 import org.hedgewars.hedgeroid.EngineProtocol.FrontendDataUtils;
 import org.hedgewars.hedgeroid.EngineProtocol.Team;
@@ -45,13 +46,13 @@
 import android.widget.SimpleAdapter.ViewBinder;
 import android.widget.TextView;
 
-public class TeamSelectionActivity extends Activity{
+public class TeamSelectionActivity extends Activity implements Runnable{
 
 	private static final int ACTIVITY_TEAMCREATION = 0;
 
 	private ImageButton addTeam, back;
 	private ListView availableTeams, selectedTeams;
-	private ArrayList<HashMap<String, Object>> availableTeamsList, selectedTeamsList;
+	private List<HashMap<String, Object>> availableTeamsList, selectedTeamsList;
 	private TextView txtInfo;
 
 	public void onCreate(Bundle savedInstanceState){
@@ -62,37 +63,50 @@
 		addTeam = (ImageButton) findViewById(R.id.btnAdd);
 		back = (ImageButton) findViewById(R.id.btnBack);
 		txtInfo = (TextView) findViewById(R.id.txtInfo);
-
+		selectedTeams = (ListView) findViewById(R.id.selectedTeams);
+		availableTeams = (ListView) findViewById(R.id.availableTeams);
 		addTeam.setOnClickListener(addTeamClicker);
 		back.setOnClickListener(backClicker);
 
-		availableTeams = (ListView) findViewById(R.id.availableTeams);
-		availableTeamsList = FrontendDataUtils.getTeams(this);
+		availableTeamsList = new ArrayList<HashMap<String, Object>>();
 		SimpleAdapter adapter = new SimpleAdapter(this, availableTeamsList, R.layout.team_selection_entry_simple, new String[]{"txt", "img"}, new int[]{R.id.txtName, R.id.imgDifficulty});
 		availableTeams.setAdapter(adapter);
+		availableTeams.setOnItemClickListener(availableClicker);
 		registerForContextMenu(availableTeams);
-		availableTeams.setOnItemClickListener(availableClicker);
 
-		selectedTeams = (ListView) findViewById(R.id.selectedTeams);
 		selectedTeamsList = new ArrayList<HashMap<String, Object>>();
-		ArrayList<HashMap<String, ?>> toBeRemoved = new ArrayList<HashMap<String, ?>>();
-		ArrayList<Team> teamsStartGame = getIntent().getParcelableArrayListExtra("teams");
-		for(HashMap<String, Object> hashmap : availableTeamsList){
-			for(Team t : teamsStartGame){
-				if(((Team)hashmap.get("team")).equals(t)){
-					toBeRemoved.add(hashmap);
-					selectedTeamsList.add(FrontendDataUtils.teamToHashMap(t));//create a new hashmap to ensure all variables are entered into the map
-				}
-			}
-		}
-		for(HashMap<String, ?> hashmap : toBeRemoved) availableTeamsList.remove(hashmap);
-
 		adapter = new SimpleAdapter(this, selectedTeamsList, R.layout.team_selection_entry, new String[]{"txt", "img", "color", "count"}, new int[]{R.id.txtName, R.id.imgDifficulty, R.id.teamColor, R.id.teamCount});
 		adapter.setViewBinder(viewBinder);
 		selectedTeams.setAdapter(adapter);
 		selectedTeams.setOnItemClickListener(selectedClicker);
 
 		txtInfo.setText(String.format(getResources().getString(R.string.teams_info_template), selectedTeams.getChildCount()));
+
+		new Thread(this).start();//load the teams from xml async
+	}
+
+	public void run(){
+		List<HashMap<String, Object>> teamsList = FrontendDataUtils.getTeams(this);//teams from xml
+		ArrayList<Team> teamsStartGame = getIntent().getParcelableArrayListExtra("teams");//possible selected teams
+
+		for(HashMap<String, Object> hashmap : teamsList){
+			boolean added = false;
+			for(Team t : teamsStartGame){
+				if(((Team)hashmap.get("team")).equals(t)){//add to available or add to selected
+					selectedTeamsList.add(FrontendDataUtils.teamToMap(t));//create a new hashmap to ensure all variables are entered into the map
+					added = true;
+					break;
+				}
+			}
+			if(!added) availableTeamsList.add(hashmap);
+		}
+
+		this.runOnUiThread(new Runnable(){
+			public void run() {
+				((SimpleAdapter)selectedTeams.getAdapter()).notifyDataSetChanged();
+				((SimpleAdapter)availableTeams.getAdapter()).notifyDataSetChanged();		
+			}
+		});
 	}
 
 	private ViewBinder viewBinder = new ViewBinder(){
@@ -120,6 +134,9 @@
 		}
 	}
 
+	/*
+	 * Updates the list view when TeamCreationActivity is shutdown and the user returns to this point
+	 */
 	private void updateListViews(){
 		unregisterForContextMenu(availableTeams);
 		availableTeamsList = FrontendDataUtils.getTeams(this);