1 /*
2  * Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game
3  * Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com>
4  * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19  */
20 
21 package org.hedgewars.hedgeroid;
22 
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 
29 import org.hedgewars.hedgeroid.Datastructures.FrontendDataUtils;
30 import org.hedgewars.hedgeroid.Datastructures.Team;
31 
32 import android.app.ListActivity;
33 import android.content.Intent;
34 import android.os.Bundle;
35 import android.view.ContextMenu;
36 import android.view.MenuItem;
37 import android.view.View;
38 import android.view.View.OnClickListener;
39 import android.widget.AdapterView;
40 import android.widget.AdapterView.AdapterContextMenuInfo;
41 import android.widget.AdapterView.OnItemClickListener;
42 import android.widget.ImageButton;
43 import android.widget.SimpleAdapter;
44 
45 public class TeamListActivity extends ListActivity implements OnItemClickListener {
46     private List<Team> teams;
47     private ImageButton addButton;
48 
49     @Override
onCreate(Bundle savedInstanceState)50     protected void onCreate(Bundle savedInstanceState) {
51         super.onCreate(savedInstanceState);
52         setContentView(R.layout.activity_teamlist);
53         addButton = (ImageButton)findViewById(R.id.btnAdd);
54         addButton.setOnClickListener(new OnClickListener() {
55             public void onClick(View v) {
56                 editTeam(null);
57             }
58         });
59     }
60 
61     @Override
onResume()62     public void onResume() {
63         super.onResume();
64         updateList();
65         getListView().setOnItemClickListener(this);
66         registerForContextMenu(getListView());
67     }
68 
onItemClick(AdapterView<?> adapterView, View v, int position, long arg3)69     public void onItemClick(AdapterView<?> adapterView, View v, int position, long arg3) {
70         editTeam(teams.get(position).name);
71     }
72 
73     @Override
onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuinfo)74     public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuinfo){
75         menu.add(ContextMenu.NONE, 0, ContextMenu.NONE, R.string.edit);
76         menu.add(ContextMenu.NONE, 1, ContextMenu.NONE, R.string.delete);
77     }
78 
79     @Override
onContextItemSelected(MenuItem item)80     public boolean onContextItemSelected(MenuItem item){
81         AdapterView.AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
82         int position = menuInfo.position;
83         Team team = teams.get(position);
84         switch(item.getItemId()){
85         case 0:
86             editTeam(team.name);
87             return true;
88         case 1:
89             Team.getTeamfileByName(getApplicationContext(), team.name).delete();
90             updateList();
91             return true;
92         }
93         return false;
94     }
95 
updateList()96     private void updateList() {
97         teams = FrontendDataUtils.getTeams(getApplicationContext());
98         Collections.sort(teams, Team.NAME_ORDER);
99         SimpleAdapter adapter = new SimpleAdapter(this, teamsToMaps(teams), R.layout.team_selection_entry_simple, new String[]{"txt", "img"}, new int[]{R.id.txtName, R.id.imgDifficulty});
100         setListAdapter(adapter);
101     }
102 
editTeam(String teamName)103     private void editTeam(String teamName) {
104         Intent i = new Intent(this, TeamCreatorActivity.class);
105         i.putExtra(TeamCreatorActivity.PARAMETER_EXISTING_TEAMNAME, teamName);
106         startActivity(i);
107     }
108 
109     private static final int[] botlevelDrawables = new int[] {
110         R.drawable.human, R.drawable.bot5, R.drawable.bot4, R.drawable.bot3, R.drawable.bot2, R.drawable.bot1
111     };
112 
teamsToMaps(List<Team> teams)113     private List<Map<String, ?>> teamsToMaps(List<Team> teams) {
114         List<Map<String, ?>> result = new ArrayList<Map<String,?>>();
115         for(Team t : teams) {
116             HashMap<String, Object> map = new HashMap<String, Object>();
117             map.put("team", t);
118             map.put("txt", t.name);
119             int botlevel = t.hogs.get(0).level;
120             if(botlevel<0 || botlevel>=botlevelDrawables.length) {
121                 map.put("img", R.drawable.bot1);
122             } else {
123                 map.put("img", botlevelDrawables[botlevel]);
124             }
125             result.add(map);
126         }
127         return result;
128     }
129 }
130