Commit f1843d5b authored by Drahomír Karchňák's avatar Drahomír Karchňák

Added city and country to serve info. Populating list view with server model.

parent f07e9012
<?xml version="1.0" encoding="utf-8"?>
<TextView
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="6dip"
android:id="@+id/item_text"
android:textColor="@color/black"
android:textSize="24sp" />
\ No newline at end of file
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/serverName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@android:string/unknownName"
android:textColor="@color/black"
android:textSize="20sp" />
<TextView
android:id="@+id/serverLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@android:string/unknownName"
android:textColor="@color/gray" />
</LinearLayout>
</FrameLayout>
\ No newline at end of file
......@@ -8,6 +8,7 @@
<!-- ff2d313a -->
<color name="black">#ff000000</color>
<color name="gray">#ff333333</color>
<color name="titlepage_tint">#ff353535</color>
<!-- <color name="titlepage_tint">#ff3a3a3a</color> -->
......
......@@ -8,11 +8,13 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import at.alladin.rmbt.android.util.ConfigHelper;
import at.alladin.rmbt.android.util.Server;
import cz.nic.netmetr.R;
public class RMBTServerSelectFragment extends Fragment {
......@@ -23,52 +25,41 @@ public class RMBTServerSelectFragment extends Fragment {
ListView listView = (ListView) view.findViewById(R.id.server_select_list);
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("Vybrat server automaticky");
arrayList.add("Server 1");
arrayList.add("Server 2");
arrayList.add("Server 3");
arrayList.add("Server 4");
arrayList.add("Server 5");
arrayList.add("Server 6");
arrayList.add("Server 7");
arrayList.add("Server 8");
arrayList.add("Server 9");
arrayList.add("Server 10");
arrayList.add("Server 11");
arrayList.add("Server 12");
arrayList.add("Server 13");
arrayList.add("Server 14");
arrayList.add("Server 15");
final StableArrayAdapter arrayAdapter = new StableArrayAdapter(this.getActivity(), R.layout.server_select_item, arrayList);
listView.setAdapter(arrayAdapter);
Set<String> serversSet = ConfigHelper.getServers(this.getActivity());
ArrayList<Server> servers = new ArrayList<Server>();
for(String serverString : serversSet) {
servers.add(Server.decodeFromString(serverString));
}
ServerSelectItemAdapter serverSelectItemAdapter = new ServerSelectItemAdapter(this.getActivity(), servers);
listView.setAdapter(serverSelectItemAdapter);
return view;
}
private class StableArrayAdapter extends ArrayAdapter<String> {
private class ServerSelectItemAdapter extends ArrayAdapter<Server> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
public ServerSelectItemAdapter(Context context, ArrayList<Server> servers) {
super(context, 0, servers);
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
public View getView(int position, View convertView, ViewGroup parent) {
Server server = getItem(position);
@Override
public boolean hasStableIds() {
return true;
}
if(convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.server_select_item, parent, false);
}
TextView serverName = (TextView) convertView.findViewById(R.id.serverName);
TextView serverLocation = (TextView) convertView.findViewById(R.id.serverLocation);
serverName.setText(server.getName());
serverLocation.setText(server.getFormattedLocation());
return convertView;
}
}
}
\ No newline at end of file
......@@ -238,12 +238,15 @@ public class CheckSettingsTask extends AsyncTask<Void, Void, JSONArray>
for (int i = 0; i < servers.length(); i++)
{
final JSONObject serverObj = servers.getJSONObject(i);
final String serverName = serverObj.getString("name");
final String serverUuid = serverObj.getString("uuid");
final Server server = new Server(serverName, serverUuid);
final String serverUuid = serverObj.getString("uuid");
final String serverName = serverObj.getString("name");
final String serverCity = serverObj.getString("city");
final String serverCountry = serverObj.getString("country");
serverSet.add(server.encode());
final Server server = new Server(serverName, serverUuid, serverCity, serverCountry);
serverSet.add(server.encodeToString());
Log.i(getClass().getSimpleName(), server.toString());
}
......
......@@ -608,14 +608,16 @@ public final class ConfigHelper
return getSharedPreferences(context).getStringSet("servers", null);
}
public static void setServers(final Context context, final Set<String> servers)
{
public static void setServers(final Context context, final Set<String> servers) {
final Set<String> oldServers = getServers(context);
//Is there any difference between old and new servers
if (oldServers == null && servers == null)
return;
if (oldServers != null && servers != null
&& servers.equals(oldServers))
return;
getSharedPreferences(context).edit().putStringSet("servers", servers).apply();
}
}
package at.alladin.rmbt.android.util;
import android.util.Base64;
public class Server
{
protected final String name;
public class Server {
private static final String ENCODING_FORMAT_SPLIT = ";";
private static final String ENCODING_FORMAT = "%s" + ENCODING_FORMAT_SPLIT +
"%s" + ENCODING_FORMAT_SPLIT +
"%s" + ENCODING_FORMAT_SPLIT +
"%s" + ENCODING_FORMAT_SPLIT;
private static final int ENCODING_DATA_LENGTH = 4;
private static final String LOCATION_FORMAT = "%s, %s";
protected final String uuid;
protected final String name;
protected final String city;
protected final String country;
public Server(String name, String uuid)
{
super();
this.name = name;
public Server(String uuid, String name, String city, String country) {
this.uuid = uuid;
this.name = name;
this.city = city;
this.country = country;
}
public String encode()
{
return Base64.encodeToString(name.getBytes(), Base64.DEFAULT) + ";" + uuid;
public String encodeToString() {
return String.format(ENCODING_FORMAT, uuid, name, city, country);
}
public static Server decode(String data)
{
final String[] split = data.split(";");
final String name = new String(Base64.decode(split[0], Base64.DEFAULT));
final String uuid = split[1];
return new Server(name, uuid);
public static Server decodeFromString(String data) {
final String[] splitResult = data.split(ENCODING_FORMAT_SPLIT);
if(splitResult.length < ENCODING_DATA_LENGTH) return null;
return new Server(splitResult[0], splitResult[1], splitResult[2], splitResult[3]);
}
public String getName()
{
public String getName() {
return name;
}
public String getUuid()
{
public String getUuid() {
return uuid;
}
public String getCity() {
return city;
}
public String getCountry() {
return country;
}
public String getFormattedLocation() {
return String.format(LOCATION_FORMAT, getCity(), getCountry());
}
@Override
public int hashCode()
{
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
result = prime * result + ((city == null) ? 0 : city.hashCode());
result = prime * result + ((country == null) ? 0 : country.hashCode());
return result;
}
......@@ -56,21 +75,22 @@ public class Server
return false;
if (getClass() != obj.getClass())
return false;
Server other = (Server) obj;
if (name == null)
{
if (other.name != null)
return false;
if (name == null) {
if (other.name != null) return false;
}
else if (!name.equals(other.name))
else if (!name.equals(other.name)) {
return false;
if (uuid == null)
{
if (other.uuid != null)
return false;
}
else if (!uuid.equals(other.uuid))
if (uuid == null) {
if (other.uuid != null) return false;
}
else if (!uuid.equals(other.uuid)) {
return false;
}
return true;
}
......
......@@ -370,8 +370,10 @@ public class SettingsResource extends ServerResource
while (rs.next()) {
final JSONObject obj = new JSONObject();
obj.put("name", rs.getString("name"));
obj.put("uuid", rs.getString("uuid"));
obj.put("name", rs.getString("name"));
obj.put("city", rs.getString("city"));
obj.put("country", rs.getString("country"));
result.put(obj);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment