Provide a menu with options to save VPN profiles
[strongswan.git] / src / frontends / android / src / org / strongswan / android / ui / adapter / VpnProfileAdapter.java
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2012 Giuliano Grassi
4 * Copyright (C) 2012 Ralf Sager
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 package org.strongswan.android.ui.adapter;
19
20 import java.util.Collections;
21 import java.util.Comparator;
22 import java.util.List;
23
24 import org.strongswan.android.R;
25 import org.strongswan.android.data.VpnProfile;
26
27 import android.content.Context;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.widget.ArrayAdapter;
32 import android.widget.TextView;
33
34 public class VpnProfileAdapter extends ArrayAdapter<VpnProfile>
35 {
36 private final int resource;
37 private final List<VpnProfile> items;
38
39 public VpnProfileAdapter(Context context, int resource,
40 List<VpnProfile> items)
41 {
42 super(context, resource, items);
43 this.resource = resource;
44 this.items = items;
45 sortItems();
46 }
47
48 @Override
49 public View getView(int position, View convertView, ViewGroup parent)
50 {
51 View vpnProfileView;
52 if (convertView != null)
53 {
54 vpnProfileView = convertView;
55 }
56 else
57 {
58 LayoutInflater inflater = LayoutInflater.from(getContext());
59 vpnProfileView = inflater.inflate(resource, null);
60 }
61 VpnProfile profile = getItem(position);
62 TextView tv = (TextView)vpnProfileView.findViewById(R.id.profile_item_name);
63 tv.setText(profile.getName());
64 tv = (TextView)vpnProfileView.findViewById(R.id.profile_item_gateway);
65 tv.setText(getContext().getString(R.string.profile_gateway_label) + " " + profile.getGateway());
66 tv = (TextView)vpnProfileView.findViewById(R.id.profile_item_username);
67 tv.setText(getContext().getString(R.string.profile_username_label) + " " + profile.getUsername());
68 return vpnProfileView;
69 }
70
71 @Override
72 public void notifyDataSetChanged()
73 {
74 sortItems();
75 super.notifyDataSetChanged();
76 }
77
78 private void sortItems()
79 {
80 Collections.sort(this.items, new Comparator<VpnProfile>() {
81 @Override
82 public int compare(VpnProfile lhs, VpnProfile rhs)
83 {
84 return lhs.getName().compareToIgnoreCase(rhs.getName());
85 }
86 });
87 }
88 }