1 /* 2 * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game 3 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com> 4 * 5 * This program is free software; you can redistribute it and/or 6 * modify it under the terms of the GNU General Public License 7 * as published by the Free Software Foundation; either version 2 8 * of the License, or (at your option) any later version. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 18 */ 19 20 package org.hedgewars.hedgeroid; 21 22 import java.util.ArrayList; 23 import java.util.Collection; 24 import java.util.Collections; 25 import java.util.List; 26 27 import org.hedgewars.hedgeroid.R; 28 import org.hedgewars.hedgeroid.Datastructures.FrontendDataUtils; 29 import org.hedgewars.hedgeroid.Datastructures.Team; 30 31 import android.app.Activity; 32 import android.app.AlertDialog; 33 import android.app.Dialog; 34 import android.content.DialogInterface; 35 import android.content.DialogInterface.OnClickListener; 36 import android.os.Bundle; 37 import android.support.v4.app.DialogFragment; 38 39 public class TeamAddDialog extends DialogFragment { 40 private static final String STATE_TEAMS_ALREADY_IN_GAME = "teamAlreadyInGame"; 41 private ArrayList<String> teamsAlreadyInGame; 42 private List<Team> availableTeams; 43 private Listener listener; 44 45 public static interface Listener { onTeamAddDialogSubmitted(Team newTeam)46 void onTeamAddDialogSubmitted(Team newTeam); 47 } 48 TeamAddDialog()49 public TeamAddDialog() { 50 // Only for reflection-based instantiation by the framework 51 } 52 TeamAddDialog(Collection<String> teamsAlreadyInGame)53 TeamAddDialog(Collection<String> teamsAlreadyInGame) { 54 this.teamsAlreadyInGame = new ArrayList<String>(teamsAlreadyInGame); 55 } 56 57 @Override onAttach(Activity activity)58 public void onAttach(Activity activity) { 59 super.onAttach(activity); 60 try { 61 listener = (Listener) activity; 62 } catch(ClassCastException e) { 63 throw new ClassCastException("Activity " + activity + " must implement TeamAddDialog.Listener to use TeamAddDialog."); 64 } 65 } 66 67 @Override onDetach()68 public void onDetach() { 69 super.onDetach(); 70 listener = null; 71 } 72 73 @Override onCreate(Bundle savedInstanceState)74 public void onCreate(Bundle savedInstanceState) { 75 super.onCreate(savedInstanceState); 76 if(savedInstanceState != null) { 77 teamsAlreadyInGame = savedInstanceState.getStringArrayList(STATE_TEAMS_ALREADY_IN_GAME); 78 } 79 availableTeams = new ArrayList<Team>(); 80 List<Team> teams = FrontendDataUtils.getTeams(getActivity()); 81 for(Team team : teams) { 82 if(!teamsAlreadyInGame.contains(team.name)) { 83 availableTeams.add(team); 84 } 85 } 86 Collections.sort(availableTeams, Team.NAME_ORDER); 87 } 88 89 // TODO use icons for the teams (corresponding to botlevel) 90 @Override onCreateDialog(Bundle savedInstanceState)91 public Dialog onCreateDialog(Bundle savedInstanceState) { 92 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 93 builder.setTitle(R.string.dialog_addteam_title); 94 builder.setIcon(R.drawable.human); 95 String[] teamNames = new String[availableTeams.size()]; 96 for(int i=0; i<availableTeams.size(); i++) { 97 teamNames[i] = availableTeams.get(i).name; 98 } 99 builder.setItems(teamNames, new OnClickListener() { 100 public void onClick(DialogInterface dialog, int which) { 101 listener.onTeamAddDialogSubmitted(availableTeams.get(which)); 102 } 103 }); 104 return builder.create(); 105 } 106 107 @Override onSaveInstanceState(Bundle outState)108 public void onSaveInstanceState(Bundle outState) { 109 super.onSaveInstanceState(outState); 110 outState.putStringArrayList(STATE_TEAMS_ALREADY_IN_GAME, teamsAlreadyInGame); 111 } 112 } 113