1 package org.hedgewars.hedgeroid.netplay; |
|
2 |
|
3 import java.util.Comparator; |
|
4 |
|
5 import org.hedgewars.hedgeroid.R; |
|
6 import org.hedgewars.hedgeroid.Datastructures.RoomlistRoom; |
|
7 |
|
8 import android.content.Context; |
|
9 import android.content.res.Resources; |
|
10 import android.util.Pair; |
|
11 import android.view.LayoutInflater; |
|
12 import android.view.View; |
|
13 import android.view.ViewGroup; |
|
14 import android.widget.TextView; |
|
15 |
|
16 public class RoomlistAdapter extends ObservableTreeMapAdapter<String, Pair<RoomlistRoom, Long>> { |
|
17 private Context context; |
|
18 |
|
19 public RoomlistAdapter(Context context) { |
|
20 this.context = context; |
|
21 } |
|
22 |
|
23 @Override |
|
24 protected Comparator<Pair<RoomlistRoom, Long>> getEntryOrder() { |
|
25 return RoomAgeComparator.INSTANCE; |
|
26 } |
|
27 |
|
28 public RoomlistRoom getItem(int position) { |
|
29 return getEntries().get(position).first; |
|
30 } |
|
31 |
|
32 public long getItemId(int position) { |
|
33 return getEntries().get(position).second; |
|
34 } |
|
35 |
|
36 public boolean hasStableIds() { |
|
37 return true; |
|
38 } |
|
39 |
|
40 private static CharSequence formatExtra(Resources res, RoomlistRoom room) { |
|
41 String ownermsg = res.getString(R.string.roomlist_owner, room.owner); |
|
42 String mapmsg = res.getString(R.string.roomlist_map, room.formatMapName(res)); |
|
43 String scheme = room.scheme.equals(room.weapons) ? room.scheme : room.scheme + " / " + room.weapons; |
|
44 String schememsg = res.getString(R.string.roomlist_scheme, scheme); |
|
45 return ownermsg + ". " + mapmsg + ", " + schememsg; |
|
46 } |
|
47 |
|
48 public View getView(int position, View convertView, ViewGroup parent) { |
|
49 View v = convertView; |
|
50 if (v == null) { |
|
51 LayoutInflater vi = LayoutInflater.from(context); |
|
52 v = vi.inflate(R.layout.listview_room, null); |
|
53 } |
|
54 |
|
55 RoomlistRoom room = getItem(position); |
|
56 int iconRes = room.inProgress ? R.drawable.roomlist_ingame : R.drawable.roomlist_preparing; |
|
57 |
|
58 if(v.findViewById(android.R.id.text1) == null) { |
|
59 // Tabular room list |
|
60 TextView roomnameView = (TextView)v.findViewById(R.id.roomname); |
|
61 TextView playerCountView = (TextView)v.findViewById(R.id.playercount); |
|
62 TextView teamCountView = (TextView)v.findViewById(R.id.teamcount); |
|
63 TextView ownerView = (TextView)v.findViewById(R.id.owner); |
|
64 TextView mapView = (TextView)v.findViewById(R.id.map); |
|
65 TextView schemeView = (TextView)v.findViewById(R.id.scheme); |
|
66 TextView weaponView = (TextView)v.findViewById(R.id.weapons); |
|
67 |
|
68 roomnameView.setCompoundDrawablesWithIntrinsicBounds(iconRes, 0, 0, 0); |
|
69 roomnameView.setText(room.name); |
|
70 if(playerCountView != null) { |
|
71 playerCountView.setText(String.valueOf(room.playerCount)); |
|
72 } |
|
73 if(teamCountView != null) { |
|
74 teamCountView.setText(String.valueOf(room.teamCount)); |
|
75 } |
|
76 ownerView.setText(room.owner); |
|
77 mapView.setText(room.formatMapName(context.getResources())); |
|
78 schemeView.setText(room.scheme); |
|
79 weaponView.setText(room.weapons); |
|
80 } else { |
|
81 // Small room list |
|
82 TextView v1 = (TextView)v.findViewById(android.R.id.text1); |
|
83 TextView v2 = (TextView)v.findViewById(android.R.id.text2); |
|
84 |
|
85 v1.setCompoundDrawablesWithIntrinsicBounds(iconRes, 0, 0, 0); |
|
86 v1.setText(room.name); |
|
87 v2.setText(formatExtra(context.getResources(), room)); |
|
88 } |
|
89 |
|
90 return v; |
|
91 } |
|
92 |
|
93 private static final class RoomAgeComparator implements Comparator<Pair<RoomlistRoom, Long>> { |
|
94 public static final RoomAgeComparator INSTANCE = new RoomAgeComparator(); |
|
95 public int compare(Pair<RoomlistRoom, Long> lhs, Pair<RoomlistRoom, Long> rhs) { |
|
96 return rhs.second.compareTo(lhs.second); |
|
97 } |
|
98 } |
|
99 } |
|