|
1 package org.hedgewars.hedgeroid.netplay; |
|
2 |
|
3 import org.hedgewars.hedgeroid.R; |
|
4 import org.hedgewars.hedgeroid.netplay.RoomPlayerlist.PlayerInRoom; |
|
5 |
|
6 import android.os.Bundle; |
|
7 import android.support.v4.app.ListFragment; |
|
8 import android.view.ContextMenu; |
|
9 import android.view.ContextMenu.ContextMenuInfo; |
|
10 import android.view.LayoutInflater; |
|
11 import android.view.MenuInflater; |
|
12 import android.view.MenuItem; |
|
13 import android.view.View; |
|
14 import android.view.ViewGroup; |
|
15 import android.widget.AdapterView.AdapterContextMenuInfo; |
|
16 |
|
17 public class RoomPlayerlistFragment extends ListFragment { |
|
18 private Netplay netplay; |
|
19 private RoomPlayerlistAdapter adapter; |
|
20 |
|
21 @Override |
|
22 public void onCreate(Bundle savedInstanceState) { |
|
23 super.onCreate(savedInstanceState); |
|
24 netplay = Netplay.getAppInstance(getActivity().getApplicationContext()); |
|
25 adapter = new RoomPlayerlistAdapter(getActivity()); |
|
26 adapter.setSource(netplay.roomPlayerlist); |
|
27 setListAdapter(adapter); |
|
28 } |
|
29 |
|
30 @Override |
|
31 public void onDestroy() { |
|
32 super.onDestroy(); |
|
33 adapter.invalidate(); |
|
34 } |
|
35 |
|
36 @Override |
|
37 public void onActivityCreated(Bundle savedInstanceState) { |
|
38 super.onActivityCreated(savedInstanceState); |
|
39 registerForContextMenu(getListView()); |
|
40 } |
|
41 |
|
42 @Override |
|
43 public void onCreateContextMenu(ContextMenu menu, View v, |
|
44 ContextMenuInfo menuInfo) { |
|
45 super.onCreateContextMenu(menu, v, menuInfo); |
|
46 AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo; |
|
47 String playerName = adapter.getItem(info.position).player.name; |
|
48 |
|
49 MenuInflater inflater = getActivity().getMenuInflater(); |
|
50 inflater.inflate(R.menu.room_playerlist_context, menu); |
|
51 if(netplay.isChief() && !playerName.equals(netplay.getPlayerName())) { |
|
52 inflater.inflate(R.menu.room_playerlist_chief_context, menu); |
|
53 } |
|
54 menu.setHeaderIcon(R.drawable.human); |
|
55 menu.setHeaderTitle(playerName); |
|
56 } |
|
57 |
|
58 @Override |
|
59 public boolean onContextItemSelected(MenuItem item) { |
|
60 AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo(); |
|
61 PlayerInRoom player = adapter.getItem(info.position); |
|
62 switch(item.getItemId()) { |
|
63 case R.id.player_info: |
|
64 netplay.sendPlayerInfoQuery(player.player.name); |
|
65 return true; |
|
66 case R.id.player_kick: |
|
67 netplay.sendKick(player.player.name); |
|
68 return true; |
|
69 default: |
|
70 return super.onContextItemSelected(item); |
|
71 } |
|
72 } |
|
73 |
|
74 @Override |
|
75 public View onCreateView(LayoutInflater inflater, ViewGroup container, |
|
76 Bundle savedInstanceState) { |
|
77 return inflater.inflate(R.layout.fragment_playerlist, container, false); |
|
78 } |
|
79 } |