2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2012 Giuliano Grassi
4 * Copyright (C) 2012 Ralf Sager
5 * Hochschule fuer Technik Rapperswil
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>.
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
18 package org
.strongswan
.android
.ui
;
20 import java
.util
.ArrayList
;
21 import java
.util
.HashSet
;
22 import java
.util
.List
;
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
;
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
;
47 public class VpnProfileListFragment
extends Fragment
49 private static final int ADD_REQUEST
= 1;
50 private static final int EDIT_REQUEST
= 2;
52 private List
<VpnProfile
> mVpnProfiles
;
53 private VpnProfileDataSource mDataSource
;
54 private VpnProfileAdapter mListAdapter
;
55 private ListView mListView
;
56 private OnVpnProfileSelectedListener mListener
;
59 * The activity containing this fragment should implement this interface
61 public interface OnVpnProfileSelectedListener
{
62 public void onVpnProfileSelected(VpnProfile profile
);
66 public View
onCreateView(LayoutInflater inflater
, ViewGroup container
,
67 Bundle savedInstanceState
)
69 View view
= inflater
.inflate(R
.layout
.profile_list_fragment
, null
);
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
);
82 public void onCreate(Bundle savedInstanceState
)
84 super.onCreate(savedInstanceState
);
85 setHasOptionsMenu(true
);
87 Context context
= getActivity().getApplicationContext();
89 mDataSource
= new VpnProfileDataSource(this.getActivity());
92 /* cached list of profiles used as backend for the ListView */
93 mVpnProfiles
= mDataSource
.getAllVpnProfiles();
95 mListAdapter
= new VpnProfileAdapter(context
, R
.layout
.profile_list_item
, mVpnProfiles
);
99 public void onDestroy()
106 public void onAttach(Activity activity
)
108 super.onAttach(activity
);
110 if (activity
instanceof OnVpnProfileSelectedListener
)
112 mListener
= (OnVpnProfileSelectedListener
)activity
;
117 public void onCreateOptionsMenu(Menu menu
, MenuInflater inflater
)
119 inflater
.inflate(R
.menu
.profile_list
, menu
);
123 public boolean onOptionsItemSelected(MenuItem item
)
125 switch (item
.getItemId())
127 case R
.id
.add_profile
:
128 Intent connectionIntent
= new Intent(getActivity(),
129 VpnProfileDetailActivity
.class);
130 startActivityForResult(connectionIntent
, ADD_REQUEST
);
133 return super.onOptionsItemSelected(item
);
138 public void onActivityResult(int requestCode
, int resultCode
, Intent data
)
144 if (resultCode
!= Activity
.RESULT_OK
)
148 long id
= data
.getLongExtra(VpnProfileDataSource
.KEY_ID
, 0);
149 VpnProfile profile
= mDataSource
.getVpnProfile(id
);
151 { /* in case this was an edit, we remove it first */
152 mVpnProfiles
.remove(profile
);
153 mVpnProfiles
.add(profile
);
154 mListAdapter
.notifyDataSetChanged();
158 super.onActivityResult(requestCode
, resultCode
, data
);
161 private final OnItemClickListener mVpnProfileClicked
= new OnItemClickListener() {
163 public void onItemClick(AdapterView
<?
> a
, View v
, int position
, long id
)
165 if (mListener
!= null
)
167 mListener
.onVpnProfileSelected((VpnProfile
)a
.getItemAtPosition(position
));
172 private final MultiChoiceModeListener mVpnProfileSelected
= new MultiChoiceModeListener() {
173 private HashSet
<Integer
> mSelected
;
174 private MenuItem mEditProfile
;
177 public boolean onPrepareActionMode(ActionMode mode
, Menu menu
)
183 public void onDestroyActionMode(ActionMode mode
)
188 public boolean onCreateActionMode(ActionMode mode
, Menu menu
)
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("Select Profiles");
199 public boolean onActionItemClicked(ActionMode mode
, MenuItem item
)
201 switch (item
.getItemId())
203 case R
.id
.edit_profile
:
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
);
212 case R
.id
.delete_profile
:
214 ArrayList
<VpnProfile
> profiles
= new ArrayList
<VpnProfile
>();
215 for (int position
: mSelected
)
217 profiles
.add((VpnProfile
)mListView
.getItemAtPosition(position
));
219 for (VpnProfile profile
: profiles
)
221 mDataSource
.deleteVpnProfile(profile
);
222 mVpnProfiles
.remove(profile
);
224 mListAdapter
.notifyDataSetChanged();
225 Toast
.makeText(VpnProfileListFragment
.this.getActivity(),
226 R
.string
.profiles_deleted
, Toast
.LENGTH_SHORT
).show();
237 public void onItemCheckedStateChanged(ActionMode mode
, int position
,
238 long id
, boolean checked
)
242 mSelected
.add(position
);
246 mSelected
.remove(position
);
248 final int checkedCount
= mSelected
.size();
249 mEditProfile
.setEnabled(checkedCount
== 1);
250 switch (checkedCount
)
253 mode
.setSubtitle(R
.string
.no_profile_selected
);
256 mode
.setSubtitle(R
.string
.one_profile_selected
);
259 mode
.setSubtitle(String
.format(getString(R
.string
.x_profiles_selected
), checkedCount
));