The list fragment uses a menu to provide an option to add new VPN profiles
authorTobias Brunner <tobias@strongswan.org>
Tue, 17 Jul 2012 17:40:03 +0000 (19:40 +0200)
committerTobias Brunner <tobias@strongswan.org>
Sat, 11 Aug 2012 13:10:34 +0000 (15:10 +0200)
src/frontends/android/res/menu/profile_list.xml [new file with mode: 0644]
src/frontends/android/src/org/strongswan/android/ui/MainActivity.java
src/frontends/android/src/org/strongswan/android/ui/VpnProfileListFragment.java

diff --git a/src/frontends/android/res/menu/profile_list.xml b/src/frontends/android/res/menu/profile_list.xml
new file mode 100644 (file)
index 0000000..57c9a86
--- /dev/null
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+    Copyright (C) 2012 Tobias Brunner
+    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.
+-->
+<menu xmlns:android="http://schemas.android.com/apk/res/android" >
+
+    <item android:id="@+id/add_profile"
+        android:title="@string/add_profile"
+        android:showAsAction="always|withText" />
+
+</menu>
index ae3a5e3..1db6827 100644 (file)
@@ -1,5 +1,23 @@
+/*
+ * 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;
 
+import android.app.ActionBar;
 import android.app.Activity;
 import android.content.Intent;
 import android.net.VpnService;
@@ -13,6 +31,9 @@ public class MainActivity extends Activity
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                startVpnService();
+
+               ActionBar bar = getActionBar();
+               bar.setDisplayShowTitleEnabled(false);
        }
 
        private void startVpnService()
index 6b1f419..a7211c5 100644 (file)
@@ -24,16 +24,23 @@ import org.strongswan.android.data.VpnProfile;
 import org.strongswan.android.data.VpnProfileDataSource;
 import org.strongswan.android.ui.adapter.VpnProfileAdapter;
 
+import android.app.Activity;
 import android.app.Fragment;
 import android.content.Context;
+import android.content.Intent;
 import android.os.Bundle;
 import android.view.LayoutInflater;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ListView;
 
 public class VpnProfileListFragment extends Fragment
 {
+       private static final int ADD_REQUEST = 1;
+
        private List<VpnProfile> mVpnProfiles;
        private VpnProfileDataSource mDataSource;
        private VpnProfileAdapter mListAdapter;
@@ -56,6 +63,7 @@ public class VpnProfileListFragment extends Fragment
        public void onCreate(Bundle savedInstanceState)
        {
                super.onCreate(savedInstanceState);
+               setHasOptionsMenu(true);
 
                Context context = getActivity().getApplicationContext();
 
@@ -74,4 +82,47 @@ public class VpnProfileListFragment extends Fragment
                super.onDestroy();
                mDataSource.close();
        }
+
+       @Override
+       public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
+       {
+               inflater.inflate(R.menu.profile_list, menu);
+       }
+
+       @Override
+       public boolean onOptionsItemSelected(MenuItem item)
+       {
+               switch (item.getItemId())
+               {
+                       case R.id.add_profile:
+                               Intent connectionIntent = new Intent(getActivity(),
+                                                                                                        VpnProfileDetailActivity.class);
+                               startActivityForResult(connectionIntent, ADD_REQUEST);
+                               return true;
+                       default:
+                               return super.onOptionsItemSelected(item);
+               }
+       }
+
+       @Override
+       public void onActivityResult(int requestCode, int resultCode, Intent data)
+       {
+               switch (requestCode)
+               {
+                       case ADD_REQUEST:
+                               if (resultCode != Activity.RESULT_OK)
+                               {
+                                       return;
+                               }
+                               long id = data.getLongExtra(VpnProfileDataSource.KEY_ID, 0);
+                               VpnProfile profile = mDataSource.getVpnProfile(id);
+                               if (profile != null)
+                               {
+                                       mVpnProfiles.add(profile);
+                                       mListAdapter.notifyDataSetChanged();
+                               }
+                               return;
+               }
+               super.onActivityResult(requestCode, resultCode, data);
+       }
 }