10 import android.os.Bundle; |
10 import android.os.Bundle; |
11 import android.os.CountDownTimer; |
11 import android.os.CountDownTimer; |
12 import android.os.IBinder; |
12 import android.os.IBinder; |
13 import android.support.v4.app.ListFragment; |
13 import android.support.v4.app.ListFragment; |
14 import android.view.LayoutInflater; |
14 import android.view.LayoutInflater; |
|
15 import android.view.Menu; |
|
16 import android.view.MenuInflater; |
|
17 import android.view.MenuItem; |
15 import android.view.View; |
18 import android.view.View; |
16 import android.view.ViewGroup; |
19 import android.view.ViewGroup; |
17 import android.widget.ListView; |
20 import android.widget.AdapterView.OnItemClickListener; |
|
21 import android.widget.AdapterView; |
|
22 import android.widget.Toast; |
18 |
23 |
19 public class RoomlistFragment extends ListFragment { |
24 public class RoomlistFragment extends ListFragment implements OnItemClickListener { |
20 private static final int AUTO_REFRESH_INTERVAL_MS = 10000; |
25 private static final int AUTO_REFRESH_INTERVAL_MS = 15000; |
21 |
26 |
22 private Netconn netconn; |
27 private Netconn netconn; |
23 private RoomListAdapter adapter; |
28 private RoomListAdapter adapter; |
24 private CountDownTimer autoRefreshTimer = new CountDownTimer(Long.MAX_VALUE, AUTO_REFRESH_INTERVAL_MS) { |
29 private CountDownTimer autoRefreshTimer = new CountDownTimer(Long.MAX_VALUE, AUTO_REFRESH_INTERVAL_MS) { |
25 @Override |
30 @Override |
38 super.onCreate(savedInstanceState); |
43 super.onCreate(savedInstanceState); |
39 getActivity().bindService(new Intent(getActivity(), NetplayService.class), serviceConnection, |
44 getActivity().bindService(new Intent(getActivity(), NetplayService.class), serviceConnection, |
40 Context.BIND_AUTO_CREATE); |
45 Context.BIND_AUTO_CREATE); |
41 adapter = new RoomListAdapter(getActivity()); |
46 adapter = new RoomListAdapter(getActivity()); |
42 setListAdapter(adapter); |
47 setListAdapter(adapter); |
|
48 setHasOptionsMenu(true); |
43 } |
49 } |
44 |
50 |
45 @Override |
51 @Override |
46 public View onCreateView(LayoutInflater inflater, ViewGroup container, |
52 public View onCreateView(LayoutInflater inflater, ViewGroup container, |
47 Bundle savedInstanceState) { |
53 Bundle savedInstanceState) { |
48 View v = inflater.inflate(R.layout.lobby_rooms_fragment, container, false); |
54 View v = inflater.inflate(R.layout.lobby_rooms_fragment, container, false); |
49 return v; |
55 return v; |
|
56 } |
|
57 |
|
58 @Override |
|
59 public void onActivityCreated(Bundle savedInstanceState) { |
|
60 super.onActivityCreated(savedInstanceState); |
|
61 getListView().setOnItemClickListener(this); |
50 } |
62 } |
51 |
63 |
52 @Override |
64 @Override |
53 public void onResume() { |
65 public void onResume() { |
54 super.onResume(); |
66 super.onResume(); |
68 public void onDestroy() { |
80 public void onDestroy() { |
69 super.onDestroy(); |
81 super.onDestroy(); |
70 getActivity().unbindService(serviceConnection); |
82 getActivity().unbindService(serviceConnection); |
71 } |
83 } |
72 |
84 |
|
85 @Override |
|
86 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { |
|
87 super.onCreateOptionsMenu(menu, inflater); |
|
88 inflater.inflate(R.menu.lobby_roomlist_options, menu); |
|
89 } |
|
90 |
|
91 @Override |
|
92 public boolean onOptionsItemSelected(MenuItem item) { |
|
93 switch(item.getItemId()) { |
|
94 case R.id.roomlist_refresh: |
|
95 if(netconn != null) { |
|
96 netconn.sendRoomlistRequest(); |
|
97 } |
|
98 return true; |
|
99 default: |
|
100 return super.onOptionsItemSelected(item); |
|
101 } |
|
102 } |
|
103 |
|
104 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
|
105 Toast.makeText(getActivity(), R.string.not_implemented_yet, Toast.LENGTH_SHORT).show(); |
|
106 } |
|
107 |
73 private ServiceConnection serviceConnection = new ServiceConnection() { |
108 private ServiceConnection serviceConnection = new ServiceConnection() { |
74 public void onServiceConnected(ComponentName className, IBinder binder) { |
109 public void onServiceConnected(ComponentName className, IBinder binder) { |
75 netconn = ((NetplayBinder) binder).getNetconn(); |
110 netconn = ((NetplayBinder) binder).getNetconn(); |
76 adapter.setList(netconn.roomList.getValues()); |
111 adapter.setList(netconn.roomList); |
77 netconn.roomList.observe(adapter); |
112 autoRefreshTimer.start(); |
78 } |
113 } |
79 |
114 |
80 public void onServiceDisconnected(ComponentName className) { |
115 public void onServiceDisconnected(ComponentName className) { |
81 // TODO navigate away |
116 // TODO navigate away |
82 netconn.roomList.unobserve(adapter); |
117 adapter.invalidate(); |
83 netconn = null; |
118 netconn = null; |
84 } |
119 } |
85 }; |
120 }; |
86 } |
121 } |