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