project_files/Android-build/SDL-android-project/src/org/hedgewars/hedgeroid/TeamCreatorActivity.java
changeset 7586 33924ff4af50
parent 7584 7831c84cc644
child 10017 de822cd3df3a
equal deleted inserted replaced
7584:7831c84cc644 7586:33924ff4af50
    22 
    22 
    23 import java.io.File;
    23 import java.io.File;
    24 import java.io.FileNotFoundException;
    24 import java.io.FileNotFoundException;
    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;
    29 import java.util.Map;
    28 import java.util.Map;
    30 import java.util.NoSuchElementException;
    29 import java.util.NoSuchElementException;
    31 
    30 
    32 import org.hedgewars.hedgeroid.Datastructures.FrontendDataUtils;
    31 import org.hedgewars.hedgeroid.Datastructures.FrontendDataUtils;
    37 import android.app.Activity;
    36 import android.app.Activity;
    38 import android.graphics.Bitmap;
    37 import android.graphics.Bitmap;
    39 import android.graphics.drawable.Drawable;
    38 import android.graphics.drawable.Drawable;
    40 import android.media.MediaPlayer;
    39 import android.media.MediaPlayer;
    41 import android.os.Bundle;
    40 import android.os.Bundle;
       
    41 import android.util.Log;
    42 import android.view.View;
    42 import android.view.View;
    43 import android.view.View.OnClickListener;
    43 import android.view.View.OnClickListener;
    44 import android.widget.AdapterView;
    44 import android.widget.AdapterView;
    45 import android.widget.AdapterView.OnItemSelectedListener;
    45 import android.widget.AdapterView.OnItemSelectedListener;
    46 import android.widget.ArrayAdapter;
    46 import android.widget.ArrayAdapter;
    60  * Edit or create a team. If a team should be edited, it is supplied in the extras
    60  * Edit or create a team. If a team should be edited, it is supplied in the extras
    61  * as parameter oldTeamName.
    61  * as parameter oldTeamName.
    62  */
    62  */
    63 public class TeamCreatorActivity extends Activity implements Runnable {
    63 public class TeamCreatorActivity extends Activity implements Runnable {
    64 	public static final String PARAMETER_EXISTING_TEAMNAME = "existingTeamName";
    64 	public static final String PARAMETER_EXISTING_TEAMNAME = "existingTeamName";
       
    65 	private static final String TAG = TeamCreatorActivity.class.getSimpleName();
    65 	
    66 	
    66 	private TextView name;
    67 	private TextView name;
    67 	private Spinner difficulty, grave, flag, voice, fort;
    68 	private Spinner difficulty, grave, flag, voice, fort;
    68 	private ImageView imgFort;
    69 	private ImageView imgFort;
    69 	private ArrayList<ImageButton> hogDice = new ArrayList<ImageButton>();
    70 	private ArrayList<ImageButton> hogDice = new ArrayList<ImageButton>();
    74 	private MediaPlayer mp = null;
    75 	private MediaPlayer mp = null;
    75 	private boolean initComplete = false;
    76 	private boolean initComplete = false;
    76 	
    77 	
    77 	private String existingTeamName = null;
    78 	private String existingTeamName = null;
    78 
    79 
    79 	private final List<HashMap<String, ?>> flagsData = new ArrayList<HashMap<String, ?>>();
    80 	private final List<Map<String, ?>> flagsData = new ArrayList<Map<String, ?>>();
    80 	private final List<HashMap<String, ?>> typesData = new ArrayList<HashMap<String, ?>>();
    81 	private final List<Map<String, ?>> typesData = new ArrayList<Map<String, ?>>();
    81 	private final List<HashMap<String, ?>> gravesData = new ArrayList<HashMap<String, ?>>();
    82 	private final List<Map<String, ?>> gravesData = new ArrayList<Map<String, ?>>();
    82 	private final List<HashMap<String, ?>> hatsData = new ArrayList<HashMap<String, ?>>();
    83 	private final List<Map<String, ?>> hatsData = new ArrayList<Map<String, ?>>();
    83 	private final List<String> voicesData = new ArrayList<String>();
    84 	private final List<String> voicesData = new ArrayList<String>();
    84 	private final List<String> fortsData = new ArrayList<String>();
    85 	private final List<String> fortsData = new ArrayList<String>();
    85 
    86 
    86 	public void onCreate(Bundle savedInstanceState) {
    87 	public void onCreate(Bundle savedInstanceState) {
    87 		super.onCreate(savedInstanceState);
    88 		super.onCreate(savedInstanceState);
   156 		return adapter;
   157 		return adapter;
   157 	}
   158 	}
   158 	
   159 	
   159 	public void run(){
   160 	public void run(){
   160 		try {
   161 		try {
   161 			final ArrayList<HashMap<String, ?>> gravesDataNew = FrontendDataUtils.getGraves(this);
   162 			final List<Map<String, ?>> gravesDataNew = FrontendDataUtils.getGraves(this);
   162 			runOnUiThread(new Runnable(){
   163 			runOnUiThread(new Runnable(){
   163 				public void run() {
   164 				public void run() {
   164 					gravesData.addAll(gravesDataNew);
   165 					gravesData.addAll(gravesDataNew);
   165 					((SimpleAdapter)grave.getAdapter()).notifyDataSetChanged();
   166 					((SimpleAdapter)grave.getAdapter()).notifyDataSetChanged();
   166 				}
   167 				}
   167 			});
   168 			});
   168 			
   169 			
   169 			final ArrayList<HashMap<String, ?>> flagsDataNew = FrontendDataUtils.getFlags(this);
   170 			final List<Map<String, ?>> flagsDataNew = FrontendDataUtils.getFlags(this);
   170 			runOnUiThread(new Runnable(){
   171 			runOnUiThread(new Runnable(){
   171 				public void run() {
   172 				public void run() {
   172 					flagsData.addAll(flagsDataNew);
   173 					flagsData.addAll(flagsDataNew);
   173 					((SimpleAdapter)flag.getAdapter()).notifyDataSetChanged();
   174 					((SimpleAdapter)flag.getAdapter()).notifyDataSetChanged();
   174 				}
   175 				}
   175 			});
   176 			});
   176 			
   177 			
   177 			final ArrayList<HashMap<String, ?>> typesDataNew = FrontendDataUtils.getTypes(this);
   178 			final List<Map<String, ?>> typesDataNew = FrontendDataUtils.getTypes(this);
   178 			runOnUiThread(new Runnable(){
   179 			runOnUiThread(new Runnable(){
   179 				public void run() {
   180 				public void run() {
   180 					typesData.addAll(typesDataNew);
   181 					typesData.addAll(typesDataNew);
   181 					((SimpleAdapter)difficulty.getAdapter()).notifyDataSetChanged();
   182 					((SimpleAdapter)difficulty.getAdapter()).notifyDataSetChanged();
   182 				}
   183 				}
   183 			});
   184 			});
   184 			
   185 			
   185 			final ArrayList<HashMap<String, ?>> hatsDataNew = FrontendDataUtils.getHats(this);
   186 			final List<Map<String, ?>> hatsDataNew = FrontendDataUtils.getHats(this);
   186 			runOnUiThread(new Runnable(){
   187 			runOnUiThread(new Runnable(){
   187 				public void run() {
   188 				public void run() {
   188 					hatsData.addAll(hatsDataNew);
   189 					hatsData.addAll(hatsDataNew);
   189 					((SimpleAdapter)hogHat.get(0).getAdapter()).notifyDataSetChanged();
   190 					((SimpleAdapter)hogHat.get(0).getAdapter()).notifyDataSetChanged();
   190 				}
   191 				}
   191 			});
   192 			});
   192 			
   193 			
   193 			final ArrayList<String> voicesDataNew = FrontendDataUtils.getVoices(this);
   194 			final List<String> voicesDataNew = FrontendDataUtils.getVoices(this);
   194 			runOnUiThread(new Runnable(){
   195 			runOnUiThread(new Runnable(){
   195 				public void run() {
   196 				public void run() {
   196 					voicesData.addAll(voicesDataNew);
   197 					voicesData.addAll(voicesDataNew);
   197 					((ArrayAdapter<?>)voice.getAdapter()).notifyDataSetChanged();
   198 					((ArrayAdapter<?>)voice.getAdapter()).notifyDataSetChanged();
   198 				}
   199 				}
   199 			});
   200 			});
   200 			
   201 			
   201 			final ArrayList<String> fortsDataNew = FrontendDataUtils.getForts(this);
   202 			final List<String> fortsDataNew = FrontendDataUtils.getForts(this);
   202 			runOnUiThread(new Runnable(){
   203 			runOnUiThread(new Runnable(){
   203 				public void run() {
   204 				public void run() {
   204 					fortsData.addAll(fortsDataNew);
   205 					fortsData.addAll(fortsDataNew);
   205 					((ArrayAdapter<?>)fort.getAdapter()).notifyDataSetChanged();
   206 					((ArrayAdapter<?>)fort.getAdapter()).notifyDataSetChanged();
   206 				}
   207 				}
   293 
   294 
   294 	private OnItemSelectedListener fortSelector = new OnItemSelectedListener() {
   295 	private OnItemSelectedListener fortSelector = new OnItemSelectedListener() {
   295 		public void onItemSelected(AdapterView<?> arg0, View arg1,
   296 		public void onItemSelected(AdapterView<?> arg0, View arg1,
   296 				int position, long arg3) {
   297 				int position, long arg3) {
   297 			String fortName = (String) arg0.getAdapter().getItem(position);
   298 			String fortName = (String) arg0.getAdapter().getItem(position);
   298 			Drawable fortIconDrawable = Drawable.createFromPath(FileUtils
   299 			try {
   299 					.getDataPath(TeamCreatorActivity.this)
   300 				File fortImage = FileUtils.getDataPathFile(TeamCreatorActivity.this, "Forts", fortName, "L.png");
   300 					+ "Forts/"
   301 				Drawable fortIconDrawable = Drawable.createFromPath(fortImage.getAbsolutePath());
   301 					+ fortName + "L.png");
   302 				imgFort.setImageDrawable(fortIconDrawable);
   302 			imgFort.setImageDrawable(fortIconDrawable);
   303 			} catch(IOException e) {
       
   304 				Log.e(TAG, "Unable to show fort image", e);
       
   305 			}
   303 			scroller.fullScroll(ScrollView.FOCUS_DOWN);// Scroll the scrollview
   306 			scroller.fullScroll(ScrollView.FOCUS_DOWN);// Scroll the scrollview
   304 			// to the bottom, work
   307 			// to the bottom, work
   305 			// around for scrollview
   308 			// around for scrollview
   306 			// invalidation (scrolls
   309 			// invalidation (scrolls
   307 			// back to top)
   310 			// back to top)
   313 	};
   316 	};
   314 
   317 
   315 	private OnClickListener voiceClicker = new OnClickListener() {
   318 	private OnClickListener voiceClicker = new OnClickListener() {
   316 		public void onClick(View v) {
   319 		public void onClick(View v) {
   317 			try {
   320 			try {
   318 				File dir = new File(String.format("%sSounds/voices/%s",
   321 				File dir = FileUtils.getDataPathFile(TeamCreatorActivity.this, "Sounds", "voices", (String)voice.getSelectedItem());
   319 						FileUtils.getDataPath(TeamCreatorActivity.this),
       
   320 						voice.getSelectedItem()));
       
   321 				String file = "";
   322 				String file = "";
   322 				File[] dirs = dir.listFiles();
   323 				File[] dirs = dir.listFiles();
   323 				File f = dirs[(int) Math.round(Math.random() * dirs.length)];
   324 				File f = dirs[(int) Math.round(Math.random() * dirs.length)];
   324 				if (f.getName().endsWith(".ogg"))
   325 				if (f.getName().endsWith(".ogg"))
   325 					file = f.getAbsolutePath();
   326 					file = f.getAbsolutePath();
   330 					mp.reset();
   331 					mp.reset();
   331 				mp.setDataSource(file);
   332 				mp.setDataSource(file);
   332 				mp.prepare();
   333 				mp.prepare();
   333 				mp.start();
   334 				mp.start();
   334 			} catch (IllegalArgumentException e) {
   335 			} catch (IllegalArgumentException e) {
   335 				e.printStackTrace();
   336 				Log.e(TAG, "Unable to play voice sample", e);
   336 			} catch (IllegalStateException e) {
   337 			} catch (IllegalStateException e) {
   337 				e.printStackTrace();
   338 				Log.e(TAG, "Unable to play voice sample", e);
   338 			} catch (IOException e) {
   339 			} catch (IOException e) {
   339 				e.printStackTrace();
   340 				Log.e(TAG, "Unable to play voice sample", e);
   340 			}
   341 			}
   341 		}
   342 		}
   342 	};
   343 	};
   343 
   344 
   344 	@SuppressWarnings("unchecked")
   345 	@SuppressWarnings("unchecked")