Provide a menu with options to save VPN profiles
authorTobias Brunner <tobias@strongswan.org>
Tue, 17 Jul 2012 17:45:23 +0000 (19:45 +0200)
committerTobias Brunner <tobias@strongswan.org>
Sat, 11 Aug 2012 13:10:35 +0000 (15:10 +0200)
The ID of the updated/inserted profile is sent back to the activity that
started the detail view.

src/frontends/android/res/menu/profile_edit.xml [new file with mode: 0644]
src/frontends/android/res/values/strings.xml
src/frontends/android/src/org/strongswan/android/ui/VpnProfileDetailActivity.java

diff --git a/src/frontends/android/res/menu/profile_edit.xml b/src/frontends/android/res/menu/profile_edit.xml
new file mode 100644 (file)
index 0000000..e69020e
--- /dev/null
@@ -0,0 +1,28 @@
+<?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/menu_accept"
+        android:title="@string/profile_edit_save"
+        android:showAsAction="always|withText" />
+
+    <item
+        android:id="@+id/menu_cancel"
+        android:title="@string/profile_edit_cancel"
+        android:showAsAction="ifRoom" />
+
+</menu>
index b81af92..9d52c1e 100644 (file)
     <string name="add_profile">Add VPN profile</string>
 
     <!-- VPN profile details -->
+    <string name="profile_edit_save">Save</string>
+    <string name="profile_edit_cancel">Cancel</string>
     <string name="profile_name_label">Profile Name:</string>
     <string name="profile_name_hint">(use gateway address)</string>
     <string name="profile_gateway_label">Gateway:</string>
     <string name="profile_username_label">Username:</string>
     <string name="profile_password_label">Password:</string>
     <string name="profile_password_hint">(prompt when needed)</string>
+    <!-- Warnings/Notifications in the details view -->
+    <string name="alert_text_no_input_gateway">Please enter the gateway address here</string>
+    <string name="alert_text_no_input_username">Please enter your username here</string>
 
 </resources>
index 0a8dc16..56eef15 100644 (file)
@@ -22,8 +22,13 @@ import org.strongswan.android.data.VpnProfile;
 import org.strongswan.android.data.VpnProfileDataSource;
 
 import android.app.Activity;
+import android.content.Intent;
 import android.os.Bundle;
 import android.util.Log;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.MenuItem;
+import android.view.Window;
 import android.widget.EditText;
 
 public class VpnProfileDetailActivity extends Activity
@@ -78,6 +83,91 @@ public class VpnProfileDetailActivity extends Activity
                outState.putLong(VpnProfileDataSource.KEY_ID, mId);
        }
 
+       @Override
+       public boolean onCreateOptionsMenu(Menu menu)
+       {
+               MenuInflater inflater = getMenuInflater();
+               inflater.inflate(R.menu.profile_edit, menu);
+               return true;
+       }
+
+       @Override
+       public boolean onOptionsItemSelected(MenuItem item)
+       {
+               switch (item.getItemId())
+               {
+                       case android.R.id.home:
+                       case R.id.menu_cancel:
+                               finish();
+                               return true;
+                       case R.id.menu_accept:
+                               saveProfile();
+                               return true;
+                       default:
+                               return super.onOptionsItemSelected(item);
+               }
+       }
+
+       /**
+        * Save or update the profile depending on whether we actually have a
+        * profile object or not (this was created in updateProfileData)
+        */
+       private void saveProfile()
+       {
+               if (verifyInput())
+               {
+                       if (mProfile != null)
+                       {
+                               updateProfileData();
+                               mDataSource.updateVpnProfile(mProfile);
+                       }
+                       else
+                       {
+                               mProfile = new VpnProfile();
+                               updateProfileData();
+                               mDataSource.insertProfile(mProfile);
+                       }
+                       setResult(RESULT_OK, new Intent().putExtra(VpnProfileDataSource.KEY_ID, mProfile.getId()));
+                       finish();
+               }
+       }
+
+       /**
+        * Verify the user input and display error messages.
+        * @return true if the input is valid
+        */
+       private boolean verifyInput()
+       {
+               boolean valid = true;
+               if (mGateway.getText().toString().trim().isEmpty())
+               {
+                       mGateway.setError(getString(R.string.alert_text_no_input_gateway));
+                       valid = false;
+               }
+               if (mUsername.getText().toString().trim().isEmpty())
+               {
+                       mUsername.setError(getString(R.string.alert_text_no_input_username));
+                       valid = false;
+               }
+               return valid;
+       }
+
+       /**
+        * Update the profile object with the data entered by the user
+        */
+       private void updateProfileData()
+       {
+               /* the name is optional, we default to the gateway if none is given */
+               String name = mName.getText().toString().trim();
+               String gateway = mGateway.getText().toString().trim();
+               mProfile.setName(name.isEmpty() ? gateway : name);
+               mProfile.setGateway(gateway);
+               mProfile.setUsername(mUsername.getText().toString().trim());
+               String password = mPassword.getText().toString().trim();
+               password = password.isEmpty() ? null : password;
+               mProfile.setPassword(password);
+       }
+
        /**
         * Load an existing profile if we got an ID
         */