1 package org.hedgewars.hedgeroid.netplay; |
|
2 |
|
3 import org.hedgewars.hedgeroid.R; |
|
4 |
|
5 import android.os.Bundle; |
|
6 import android.os.CountDownTimer; |
|
7 import android.support.v4.app.ListFragment; |
|
8 import android.view.LayoutInflater; |
|
9 import android.view.View; |
|
10 import android.view.ViewGroup; |
|
11 import android.widget.AdapterView; |
|
12 import android.widget.AdapterView.OnItemClickListener; |
|
13 |
|
14 public class RoomlistFragment extends ListFragment implements OnItemClickListener { |
|
15 private static final int AUTO_REFRESH_INTERVAL_MS = 15000; |
|
16 |
|
17 private Netplay netplay; |
|
18 private RoomlistAdapter adapter; |
|
19 private CountDownTimer autoRefreshTimer = new CountDownTimer(Long.MAX_VALUE, AUTO_REFRESH_INTERVAL_MS) { |
|
20 @Override |
|
21 public void onTick(long millisUntilFinished) { |
|
22 netplay.sendRoomlistRequest(); |
|
23 } |
|
24 |
|
25 @Override |
|
26 public void onFinish() { } |
|
27 }; |
|
28 |
|
29 @Override |
|
30 public void onCreate(Bundle savedInstanceState) { |
|
31 super.onCreate(savedInstanceState); |
|
32 netplay = Netplay.getAppInstance(getActivity().getApplicationContext()); |
|
33 adapter = new RoomlistAdapter(getActivity()); |
|
34 adapter.setSource(netplay.roomList); |
|
35 setListAdapter(adapter); |
|
36 } |
|
37 |
|
38 @Override |
|
39 public View onCreateView(LayoutInflater inflater, ViewGroup container, |
|
40 Bundle savedInstanceState) { |
|
41 return inflater.inflate(R.layout.lobby_rooms_fragment, container, false); |
|
42 } |
|
43 |
|
44 @Override |
|
45 public void onActivityCreated(Bundle savedInstanceState) { |
|
46 super.onActivityCreated(savedInstanceState); |
|
47 getListView().setOnItemClickListener(this); |
|
48 } |
|
49 |
|
50 @Override |
|
51 public void onResume() { |
|
52 super.onResume(); |
|
53 autoRefreshTimer.start(); |
|
54 } |
|
55 |
|
56 @Override |
|
57 public void onPause() { |
|
58 super.onPause(); |
|
59 autoRefreshTimer.cancel(); |
|
60 } |
|
61 |
|
62 @Override |
|
63 public void onDestroy() { |
|
64 super.onDestroy(); |
|
65 adapter.invalidate(); |
|
66 } |
|
67 |
|
68 public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
|
69 netplay.sendJoinRoom(adapter.getItem(position).name); |
|
70 } |
|
71 } |
|