Added a custom adapter and layout to display VPN profiles in a ListView
authorTobias Brunner <tobias@strongswan.org>
Tue, 17 Jul 2012 17:02:50 +0000 (19:02 +0200)
committerTobias Brunner <tobias@strongswan.org>
Sat, 11 Aug 2012 13:10:34 +0000 (15:10 +0200)
src/frontends/android/res/layout/profile_list_item.xml [new file with mode: 0644]
src/frontends/android/res/values/strings.xml
src/frontends/android/src/org/strongswan/android/ui/adapter/VpnProfileAdapter.java [new file with mode: 0644]

diff --git a/src/frontends/android/res/layout/profile_list_item.xml b/src/frontends/android/res/layout/profile_list_item.xml
new file mode 100644 (file)
index 0000000..f55c835
--- /dev/null
@@ -0,0 +1,49 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+    Copyright (C) 2012 Tobias Brunner
+    Copyright (C) 2012 Giuliano Grassi
+    Copyright (C) 2012 Ralf Sager
+    Hochschule fuer Technik Rapperswil
+
+    This program is free software; you can redistribute it and/or modify it
+    under the terms of the GNU General Public License as published by the
+    Free Software Foundation; either version 2 of the License, or (at your
+    option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+
+    This program is distributed in the hope that it will be useful, but
+    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+    for more details.
+-->
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:orientation="vertical"
+    android:paddingBottom="6dip"
+    android:paddingTop="4dip"
+    android:background="?android:attr/activatedBackgroundIndicator" >
+
+    <TextView
+        android:id="@+id/profile_item_name"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginLeft="15dp"
+        android:textAppearance="?android:attr/textAppearanceMedium" />
+
+    <TextView
+        android:id="@+id/profile_item_gateway"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:textColor="?android:textColorSecondary"
+        android:textAppearance="?android:attr/textAppearanceSmall"
+        android:layout_marginLeft="15dp" />
+
+    <TextView
+        android:id="@+id/profile_item_username"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:textColor="?android:textColorSecondary"
+        android:textAppearance="?android:attr/textAppearanceSmall"
+        android:layout_marginLeft="15dp" />
+
+</LinearLayout>
index f4df761..83c09a6 100644 (file)
@@ -1,7 +1,27 @@
 <?xml version="1.0" encoding="utf-8"?>
+<!--
+    Copyright (C) 2012 Tobias Brunner
+    Copyright (C) 2012 Giuliano Grassi
+    Copyright (C) 2012 Ralf Sager
+    Hochschule fuer Technik Rapperswil
+
+    This program is free software; you can redistribute it and/or modify it
+    under the terms of the GNU General Public License as published by the
+    Free Software Foundation; either version 2 of the License, or (at your
+    option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+
+    This program is distributed in the hope that it will be useful, but
+    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+    for more details.
+-->
 <resources>
 
+    <!-- Application -->
+    <string name="app_name">strongSwan VPN Client</string>
     <string name="hello">Hello World, strongSwanActivity!</string>
-    <string name="app_name">strongSwan</string>
+
+    <string name="profile_gateway_label">Gateway:</string>
+    <string name="profile_username_label">Username:</string>
 
 </resources>
\ No newline at end of file
diff --git a/src/frontends/android/src/org/strongswan/android/ui/adapter/VpnProfileAdapter.java b/src/frontends/android/src/org/strongswan/android/ui/adapter/VpnProfileAdapter.java
new file mode 100644 (file)
index 0000000..39e3e58
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * Copyright (C) 2012 Tobias Brunner
+ * Copyright (C) 2012 Giuliano Grassi
+ * Copyright (C) 2012 Ralf Sager
+ * Hochschule fuer Technik Rapperswil
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ */
+
+package org.strongswan.android.ui.adapter;
+
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+import org.strongswan.android.R;
+import org.strongswan.android.data.VpnProfile;
+
+import android.content.Context;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.TextView;
+
+public class VpnProfileAdapter extends ArrayAdapter<VpnProfile>
+{
+       private final int resource;
+       private final List<VpnProfile> items;
+
+       public VpnProfileAdapter(Context context, int resource,
+                                                        List<VpnProfile> items)
+       {
+               super(context, resource, items);
+               this.resource = resource;
+               this.items = items;
+               sortItems();
+       }
+
+       @Override
+       public View getView(int position, View convertView, ViewGroup parent)
+       {
+               View vpnProfileView;
+               if (convertView != null)
+               {
+                       vpnProfileView = convertView;
+               }
+               else
+               {
+                       LayoutInflater inflater = LayoutInflater.from(getContext());
+                       vpnProfileView = inflater.inflate(resource, null);
+               }
+               VpnProfile profile = getItem(position);
+               TextView tv = (TextView)vpnProfileView.findViewById(R.id.profile_item_name);
+               tv.setText(profile.getName());
+               tv = (TextView)vpnProfileView.findViewById(R.id.profile_item_gateway);
+               tv.setText(getContext().getString(R.string.profile_gateway_label) + " " + profile.getGateway());
+               tv = (TextView)vpnProfileView.findViewById(R.id.profile_item_username);
+               tv.setText(getContext().getString(R.string.profile_username_label) + " " + profile.getUsername());
+               return vpnProfileView;
+       }
+
+       @Override
+       public void notifyDataSetChanged()
+       {
+               sortItems();
+               super.notifyDataSetChanged();
+       }
+
+       private void sortItems()
+       {
+               Collections.sort(this.items, new Comparator<VpnProfile>() {
+                       @Override
+                       public int compare(VpnProfile lhs, VpnProfile rhs)
+                       {
+                               return lhs.getName().compareToIgnoreCase(rhs.getName());
+                       }
+               });
+       }
+}