1 package org.hedgewars.hedgeroid.netplay; |
1 package org.hedgewars.hedgeroid.netplay; |
2 |
2 |
3 import org.hedgewars.hedgeroid.R; |
3 import org.hedgewars.hedgeroid.R; |
4 import org.hedgewars.hedgeroid.netplay.NetplayService.NetplayBinder; |
|
5 |
4 |
6 import android.content.ComponentName; |
|
7 import android.content.Intent; |
|
8 import android.content.ServiceConnection; |
|
9 import android.os.Bundle; |
5 import android.os.Bundle; |
10 import android.os.IBinder; |
|
11 import android.support.v4.app.ListFragment; |
6 import android.support.v4.app.ListFragment; |
12 import android.view.ContextMenu; |
7 import android.view.ContextMenu; |
|
8 import android.view.ContextMenu.ContextMenuInfo; |
13 import android.view.LayoutInflater; |
9 import android.view.LayoutInflater; |
14 import android.view.MenuInflater; |
10 import android.view.MenuInflater; |
15 import android.view.MenuItem; |
11 import android.view.MenuItem; |
16 import android.view.View; |
12 import android.view.View; |
17 import android.view.ViewGroup; |
13 import android.view.ViewGroup; |
18 import android.view.ContextMenu.ContextMenuInfo; |
14 import android.widget.AdapterView.AdapterContextMenuInfo; |
19 import android.widget.Toast; |
15 import android.widget.Toast; |
20 import android.widget.AdapterView.AdapterContextMenuInfo; |
|
21 |
16 |
22 public class PlayerlistFragment extends ListFragment { |
17 public class PlayerlistFragment extends ListFragment { |
23 private NetplayService netplayService; |
18 private Netplay netconn; |
24 private PlayerListAdapter playerListAdapter; |
19 private PlayerListAdapter playerListAdapter; |
25 |
20 |
26 @Override |
21 @Override |
27 public void onCreate(Bundle savedInstanceState) { |
22 public void onCreate(Bundle savedInstanceState) { |
28 super.onCreate(savedInstanceState); |
23 super.onCreate(savedInstanceState); |
29 getActivity().bindService(new Intent(getActivity(), NetplayService.class), serviceConnection, 0); |
24 netconn = Netplay.getAppInstance(getActivity().getApplicationContext()); |
30 playerListAdapter = new PlayerListAdapter(getActivity()); |
25 playerListAdapter = new PlayerListAdapter(getActivity()); |
|
26 playerListAdapter.setList(Netplay.getAppInstance(getActivity().getApplicationContext()).playerList); |
31 setListAdapter(playerListAdapter); |
27 setListAdapter(playerListAdapter); |
32 } |
28 } |
33 |
29 |
|
30 @Override |
|
31 public void onDestroy() { |
|
32 super.onDestroy(); |
|
33 playerListAdapter.invalidate(); |
|
34 } |
|
35 |
34 @Override |
36 @Override |
35 public void onActivityCreated(Bundle savedInstanceState) { |
37 public void onActivityCreated(Bundle savedInstanceState) { |
36 super.onActivityCreated(savedInstanceState); |
38 super.onActivityCreated(savedInstanceState); |
37 registerForContextMenu(getListView()); |
39 registerForContextMenu(getListView()); |
38 } |
40 } |
39 |
41 |
40 @Override |
42 @Override |
41 public void onCreateContextMenu(ContextMenu menu, View v, |
43 public void onCreateContextMenu(ContextMenu menu, View v, |
42 ContextMenuInfo menuInfo) { |
44 ContextMenuInfo menuInfo) { |
43 super.onCreateContextMenu(menu, v, menuInfo); |
45 super.onCreateContextMenu(menu, v, menuInfo); |
|
46 AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo; |
44 MenuInflater inflater = getActivity().getMenuInflater(); |
47 MenuInflater inflater = getActivity().getMenuInflater(); |
45 inflater.inflate(R.menu.lobby_playerlist_context, menu); |
48 inflater.inflate(R.menu.lobby_playerlist_context, menu); |
|
49 menu.setHeaderIcon(R.drawable.human); |
|
50 menu.setHeaderTitle(playerListAdapter.getItem(info.position).name); |
46 } |
51 } |
47 |
52 |
48 @Override |
53 @Override |
49 public boolean onContextItemSelected(MenuItem item) { |
54 public boolean onContextItemSelected(MenuItem item) { |
50 AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo(); |
55 AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo(); |
51 switch(item.getItemId()) { |
56 switch(item.getItemId()) { |
52 case R.id.player_info: |
57 case R.id.player_info: |
53 Player p = playerListAdapter.getItem(info.position); |
58 Player p = playerListAdapter.getItem(info.position); |
54 if(netplayService != null) { |
59 netconn.sendPlayerInfoQuery(p.name); |
55 netplayService.sendPlayerInfoQuery(p.name); |
|
56 } |
|
57 return true; |
60 return true; |
58 case R.id.player_follow: |
61 case R.id.player_follow: |
59 Toast.makeText(getActivity(), R.string.not_implemented_yet, Toast.LENGTH_SHORT).show(); |
62 Toast.makeText(getActivity(), R.string.not_implemented_yet, Toast.LENGTH_SHORT).show(); |
60 return true; |
63 return true; |
61 default: |
64 default: |
62 return super.onContextItemSelected(item); |
65 return super.onContextItemSelected(item); |
63 } |
66 } |
64 } |
67 } |
65 |
68 |
66 @Override |
69 @Override |
67 public void onDestroy() { |
|
68 super.onDestroy(); |
|
69 getActivity().unbindService(serviceConnection); |
|
70 } |
|
71 |
|
72 @Override |
|
73 public View onCreateView(LayoutInflater inflater, ViewGroup container, |
70 public View onCreateView(LayoutInflater inflater, ViewGroup container, |
74 Bundle savedInstanceState) { |
71 Bundle savedInstanceState) { |
75 return inflater.inflate(R.layout.lobby_players_fragment, container, false); |
72 return inflater.inflate(R.layout.lobby_players_fragment, container, false); |
76 } |
73 } |
77 |
|
78 private ServiceConnection serviceConnection = new ServiceConnection() { |
|
79 public void onServiceConnected(ComponentName className, IBinder binder) { |
|
80 netplayService = ((NetplayBinder) binder).getService(); |
|
81 playerListAdapter.setList(netplayService.playerList); |
|
82 } |
|
83 |
|
84 public void onServiceDisconnected(ComponentName className) { |
|
85 // TODO navigate away |
|
86 playerListAdapter.invalidate(); |
|
87 netplayService = null; |
|
88 } |
|
89 }; |
|
90 } |
74 } |