Provide a menu with options to save VPN profiles
[strongswan.git] / src / frontends / android / src / org / strongswan / android / ui / VpnProfileListFragment.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;
19
20 import java.util.List;
21
22 import org.strongswan.android.R;
23 import org.strongswan.android.data.VpnProfile;
24 import org.strongswan.android.data.VpnProfileDataSource;
25 import org.strongswan.android.ui.adapter.VpnProfileAdapter;
26
27 import android.app.Activity;
28 import android.app.Fragment;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.os.Bundle;
32 import android.view.LayoutInflater;
33 import android.view.Menu;
34 import android.view.MenuInflater;
35 import android.view.MenuItem;
36 import android.view.View;
37 import android.view.ViewGroup;
38 import android.widget.ListView;
39
40 public class VpnProfileListFragment extends Fragment
41 {
42 private static final int ADD_REQUEST = 1;
43
44 private List<VpnProfile> mVpnProfiles;
45 private VpnProfileDataSource mDataSource;
46 private VpnProfileAdapter mListAdapter;
47 private ListView mListView;
48
49 @Override
50 public View onCreateView(LayoutInflater inflater, ViewGroup container,
51 Bundle savedInstanceState)
52 {
53 View view = inflater.inflate(R.layout.profile_list_fragment, null);
54
55 mListView = (ListView)view.findViewById(R.id.profile_list);
56 mListView.setEmptyView(view.findViewById(R.id.profile_list_empty));
57 mListView.setAdapter(mListAdapter);
58
59 return view;
60 }
61
62 @Override
63 public void onCreate(Bundle savedInstanceState)
64 {
65 super.onCreate(savedInstanceState);
66 setHasOptionsMenu(true);
67
68 Context context = getActivity().getApplicationContext();
69
70 mDataSource = new VpnProfileDataSource(this.getActivity());
71 mDataSource.open();
72
73 /* cached list of profiles used as backend for the ListView */
74 mVpnProfiles = mDataSource.getAllVpnProfiles();
75
76 mListAdapter = new VpnProfileAdapter(context, R.layout.profile_list_item, mVpnProfiles);
77 }
78
79 @Override
80 public void onDestroy()
81 {
82 super.onDestroy();
83 mDataSource.close();
84 }
85
86 @Override
87 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
88 {
89 inflater.inflate(R.menu.profile_list, menu);
90 }
91
92 @Override
93 public boolean onOptionsItemSelected(MenuItem item)
94 {
95 switch (item.getItemId())
96 {
97 case R.id.add_profile:
98 Intent connectionIntent = new Intent(getActivity(),
99 VpnProfileDetailActivity.class);
100 startActivityForResult(connectionIntent, ADD_REQUEST);
101 return true;
102 default:
103 return super.onOptionsItemSelected(item);
104 }
105 }
106
107 @Override
108 public void onActivityResult(int requestCode, int resultCode, Intent data)
109 {
110 switch (requestCode)
111 {
112 case ADD_REQUEST:
113 if (resultCode != Activity.RESULT_OK)
114 {
115 return;
116 }
117 long id = data.getLongExtra(VpnProfileDataSource.KEY_ID, 0);
118 VpnProfile profile = mDataSource.getVpnProfile(id);
119 if (profile != null)
120 {
121 mVpnProfiles.add(profile);
122 mListAdapter.notifyDataSetChanged();
123 }
124 return;
125 }
126 super.onActivityResult(requestCode, resultCode, data);
127 }
128 }