Add an Activity that shows the log fragment
[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.ArrayList;
21 import java.util.HashSet;
22 import java.util.List;
23
24 import org.strongswan.android.R;
25 import org.strongswan.android.data.VpnProfile;
26 import org.strongswan.android.data.VpnProfileDataSource;
27 import org.strongswan.android.ui.adapter.VpnProfileAdapter;
28
29 import android.app.Activity;
30 import android.app.Fragment;
31 import android.content.Context;
32 import android.content.Intent;
33 import android.os.Bundle;
34 import android.view.ActionMode;
35 import android.view.LayoutInflater;
36 import android.view.Menu;
37 import android.view.MenuInflater;
38 import android.view.MenuItem;
39 import android.view.View;
40 import android.view.ViewGroup;
41 import android.widget.AbsListView.MultiChoiceModeListener;
42 import android.widget.AdapterView;
43 import android.widget.AdapterView.OnItemClickListener;
44 import android.widget.ListView;
45 import android.widget.Toast;
46
47 public class VpnProfileListFragment extends Fragment
48 {
49 private static final int ADD_REQUEST = 1;
50 private static final int EDIT_REQUEST = 2;
51
52 private List<VpnProfile> mVpnProfiles;
53 private VpnProfileDataSource mDataSource;
54 private VpnProfileAdapter mListAdapter;
55 private ListView mListView;
56 private OnVpnProfileSelectedListener mListener;
57
58 /**
59 * The activity containing this fragment should implement this interface
60 */
61 public interface OnVpnProfileSelectedListener {
62 public void onVpnProfileSelected(VpnProfile profile);
63 }
64
65 @Override
66 public View onCreateView(LayoutInflater inflater, ViewGroup container,
67 Bundle savedInstanceState)
68 {
69 View view = inflater.inflate(R.layout.profile_list_fragment, null);
70
71 mListView = (ListView)view.findViewById(R.id.profile_list);
72 mListView.setEmptyView(view.findViewById(R.id.profile_list_empty));
73 mListView.setOnItemClickListener(mVpnProfileClicked);
74 mListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
75 mListView.setMultiChoiceModeListener(mVpnProfileSelected);
76 mListView.setAdapter(mListAdapter);
77
78 return view;
79 }
80
81 @Override
82 public void onCreate(Bundle savedInstanceState)
83 {
84 super.onCreate(savedInstanceState);
85 setHasOptionsMenu(true);
86
87 Context context = getActivity().getApplicationContext();
88
89 mDataSource = new VpnProfileDataSource(this.getActivity());
90 mDataSource.open();
91
92 /* cached list of profiles used as backend for the ListView */
93 mVpnProfiles = mDataSource.getAllVpnProfiles();
94
95 mListAdapter = new VpnProfileAdapter(context, R.layout.profile_list_item, mVpnProfiles);
96 }
97
98 @Override
99 public void onDestroy()
100 {
101 super.onDestroy();
102 mDataSource.close();
103 }
104
105 @Override
106 public void onAttach(Activity activity)
107 {
108 super.onAttach(activity);
109
110 if (activity instanceof OnVpnProfileSelectedListener)
111 {
112 mListener = (OnVpnProfileSelectedListener)activity;
113 }
114 }
115
116 @Override
117 public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
118 {
119 inflater.inflate(R.menu.profile_list, menu);
120 }
121
122 @Override
123 public boolean onOptionsItemSelected(MenuItem item)
124 {
125 switch (item.getItemId())
126 {
127 case R.id.add_profile:
128 Intent connectionIntent = new Intent(getActivity(),
129 VpnProfileDetailActivity.class);
130 startActivityForResult(connectionIntent, ADD_REQUEST);
131 return true;
132 default:
133 return super.onOptionsItemSelected(item);
134 }
135 }
136
137 @Override
138 public void onActivityResult(int requestCode, int resultCode, Intent data)
139 {
140 switch (requestCode)
141 {
142 case ADD_REQUEST:
143 case EDIT_REQUEST:
144 if (resultCode != Activity.RESULT_OK)
145 {
146 return;
147 }
148 long id = data.getLongExtra(VpnProfileDataSource.KEY_ID, 0);
149 VpnProfile profile = mDataSource.getVpnProfile(id);
150 if (profile != null)
151 { /* in case this was an edit, we remove it first */
152 mVpnProfiles.remove(profile);
153 mVpnProfiles.add(profile);
154 mListAdapter.notifyDataSetChanged();
155 }
156 return;
157 }
158 super.onActivityResult(requestCode, resultCode, data);
159 }
160
161 private final OnItemClickListener mVpnProfileClicked = new OnItemClickListener() {
162 @Override
163 public void onItemClick(AdapterView<?> a, View v, int position, long id)
164 {
165 if (mListener != null)
166 {
167 mListener.onVpnProfileSelected((VpnProfile)a.getItemAtPosition(position));
168 }
169 }
170 };
171
172 private final MultiChoiceModeListener mVpnProfileSelected = new MultiChoiceModeListener() {
173 private HashSet<Integer> mSelected;
174 private MenuItem mEditProfile;
175
176 @Override
177 public boolean onPrepareActionMode(ActionMode mode, Menu menu)
178 {
179 return false;
180 }
181
182 @Override
183 public void onDestroyActionMode(ActionMode mode)
184 {
185 }
186
187 @Override
188 public boolean onCreateActionMode(ActionMode mode, Menu menu)
189 {
190 MenuInflater inflater = mode.getMenuInflater();
191 inflater.inflate(R.menu.profile_list_context, menu);
192 mEditProfile = menu.findItem(R.id.edit_profile);
193 mSelected = new HashSet<Integer>();
194 mode.setTitle(R.string.select_profiles);
195 return true;
196 }
197
198 @Override
199 public boolean onActionItemClicked(ActionMode mode, MenuItem item)
200 {
201 switch (item.getItemId())
202 {
203 case R.id.edit_profile:
204 {
205 int position = mSelected.iterator().next();
206 VpnProfile profile = (VpnProfile)mListView.getItemAtPosition(position);
207 Intent connectionIntent = new Intent(getActivity(), VpnProfileDetailActivity.class);
208 connectionIntent.putExtra(VpnProfileDataSource.KEY_ID, profile.getId());
209 startActivityForResult(connectionIntent, EDIT_REQUEST);
210 break;
211 }
212 case R.id.delete_profile:
213 {
214 ArrayList<VpnProfile> profiles = new ArrayList<VpnProfile>();
215 for (int position : mSelected)
216 {
217 profiles.add((VpnProfile)mListView.getItemAtPosition(position));
218 }
219 for (VpnProfile profile : profiles)
220 {
221 mDataSource.deleteVpnProfile(profile);
222 mVpnProfiles.remove(profile);
223 }
224 mListAdapter.notifyDataSetChanged();
225 Toast.makeText(VpnProfileListFragment.this.getActivity(),
226 R.string.profiles_deleted, Toast.LENGTH_SHORT).show();
227 break;
228 }
229 default:
230 return false;
231 }
232 mode.finish();
233 return true;
234 }
235
236 @Override
237 public void onItemCheckedStateChanged(ActionMode mode, int position,
238 long id, boolean checked)
239 {
240 if (checked)
241 {
242 mSelected.add(position);
243 }
244 else
245 {
246 mSelected.remove(position);
247 }
248 final int checkedCount = mSelected.size();
249 mEditProfile.setEnabled(checkedCount == 1);
250 switch (checkedCount)
251 {
252 case 0:
253 mode.setSubtitle(R.string.no_profile_selected);
254 break;
255 case 1:
256 mode.setSubtitle(R.string.one_profile_selected);
257 break;
258 default:
259 mode.setSubtitle(String.format(getString(R.string.x_profiles_selected), checkedCount));
260 break;
261 }
262 }
263 };
264 }