|
1 package org.hedgewars.hedgeroid.netplay; |
|
2 |
|
3 import java.util.ArrayList; |
|
4 import java.util.Collection; |
|
5 import java.util.Collections; |
|
6 import java.util.List; |
|
7 import java.util.Map; |
|
8 |
|
9 import org.hedgewars.hedgeroid.R; |
|
10 import org.hedgewars.hedgeroid.netplay.RoomList.Observer; |
|
11 |
|
12 import android.content.Context; |
|
13 import android.content.res.Resources; |
|
14 import android.text.Layout.Alignment; |
|
15 import android.text.SpannableStringBuilder; |
|
16 import android.text.Spanned; |
|
17 import android.text.style.AlignmentSpan; |
|
18 import android.view.LayoutInflater; |
|
19 import android.view.View; |
|
20 import android.view.ViewGroup; |
|
21 import android.widget.BaseAdapter; |
|
22 import android.widget.TextView; |
|
23 |
|
24 public class RoomListAdapter extends BaseAdapter implements Observer { |
|
25 private List<Room> rooms = new ArrayList<Room>(); |
|
26 private Context context; |
|
27 |
|
28 public RoomListAdapter(Context context) { |
|
29 this.context = context; |
|
30 } |
|
31 |
|
32 public int getCount() { |
|
33 return rooms.size(); |
|
34 } |
|
35 |
|
36 public Object getItem(int position) { |
|
37 return rooms.get(position); |
|
38 } |
|
39 |
|
40 public long getItemId(int position) { |
|
41 return rooms.get(position).id; |
|
42 } |
|
43 |
|
44 public boolean hasStableIds() { |
|
45 return true; |
|
46 } |
|
47 |
|
48 public void setList(Collection<Room> rooms) { |
|
49 this.rooms = new ArrayList<Room>(rooms); |
|
50 Collections.reverse(this.rooms); // We want to show the newest rooms first |
|
51 notifyDataSetChanged(); |
|
52 } |
|
53 |
|
54 private static Spanned formatExtra(Resources res, Room room) { |
|
55 String ownermsg = res.getString(R.string.roomlist_owner, room.owner); |
|
56 String mapmsg = res.getString(R.string.roomlist_map, Room.formatMapName(res, room.map)); |
|
57 String schememsg = res.getString(R.string.roomlist_scheme, room.scheme); |
|
58 String weaponsmsg = res.getString(R.string.roomlist_weapons, room.weapons); |
|
59 SpannableStringBuilder ssb = new SpannableStringBuilder(); |
|
60 ssb.append(ownermsg).append(" ").append(mapmsg).append("\n").append(schememsg).append(" ").append(weaponsmsg); |
|
61 |
|
62 int weaponOffset = ownermsg.length()+1+mapmsg.length()+1+schememsg.length()+1; |
|
63 ssb.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_OPPOSITE), ownermsg.length(), ownermsg.length()+mapmsg.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
|
64 ssb.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_OPPOSITE), weaponOffset, weaponOffset+weaponsmsg.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
|
65 return ssb; |
|
66 } |
|
67 |
|
68 public View getView(int position, View convertView, ViewGroup parent) { |
|
69 View v = convertView; |
|
70 TextView tv1; |
|
71 if (v == null) { |
|
72 LayoutInflater vi = LayoutInflater.from(context); |
|
73 v = vi.inflate(android.R.layout.simple_list_item_2, null); |
|
74 tv1 = (TextView)v.findViewById(android.R.id.text1); |
|
75 tv1.setCompoundDrawablePadding(5); |
|
76 } else { |
|
77 tv1 = (TextView)v.findViewById(android.R.id.text1); |
|
78 } |
|
79 |
|
80 Room room = rooms.get(position); |
|
81 int iconRes = room.inProgress ? R.drawable.roomlist_ingame : R.drawable.roomlist_preparing; |
|
82 TextView tv2 = (TextView)v.findViewById(android.R.id.text2); |
|
83 tv1.setCompoundDrawablesWithIntrinsicBounds(iconRes, 0, 0, 0); |
|
84 tv1.setText(room.name); |
|
85 tv2.setText(formatExtra(context.getResources(), room)); |
|
86 return v; |
|
87 } |
|
88 |
|
89 public void itemAdded(Map<String, Room> map, String key, Room value) { |
|
90 setList(map.values()); |
|
91 } |
|
92 |
|
93 public void itemRemoved(Map<String, Room> map, String key, Room oldValue) { |
|
94 setList(map.values()); |
|
95 } |
|
96 |
|
97 public void itemReplaced(Map<String, Room> map, String key, Room oldValue, |
|
98 Room newValue) { |
|
99 setList(map.values()); |
|
100 } |
|
101 } |