Provide a menu with options to save VPN profiles
[strongswan.git] / src / frontends / android / src / org / strongswan / android / ui / VpnProfileDetailActivity.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 org.strongswan.android.R;
21 import org.strongswan.android.data.VpnProfile;
22 import org.strongswan.android.data.VpnProfileDataSource;
23
24 import android.app.Activity;
25 import android.content.Intent;
26 import android.os.Bundle;
27 import android.util.Log;
28 import android.view.Menu;
29 import android.view.MenuInflater;
30 import android.view.MenuItem;
31 import android.view.Window;
32 import android.widget.EditText;
33
34 public class VpnProfileDetailActivity extends Activity
35 {
36 private VpnProfileDataSource mDataSource;
37 private Long mId;
38 private VpnProfile mProfile;
39 private EditText mName;
40 private EditText mGateway;
41 private EditText mUsername;
42 private EditText mPassword;
43
44 @Override
45 public void onCreate(Bundle savedInstanceState)
46 {
47 super.onCreate(savedInstanceState);
48
49 /* the title is set when we load the profile, if any */
50 getActionBar().setDisplayHomeAsUpEnabled(true);
51
52 mDataSource = new VpnProfileDataSource(this);
53 mDataSource.open();
54
55 setContentView(R.layout.profile_detail_view);
56
57 mName = (EditText)findViewById(R.id.name);
58 mPassword = (EditText)findViewById(R.id.password);
59 mGateway = (EditText)findViewById(R.id.gateway);
60 mUsername = (EditText)findViewById(R.id.username);
61
62 mId = savedInstanceState == null ? null : savedInstanceState.getLong(VpnProfileDataSource.KEY_ID);
63 if (mId == null)
64 {
65 Bundle extras = getIntent().getExtras();
66 mId = extras == null ? null : extras.getLong(VpnProfileDataSource.KEY_ID);
67 }
68
69 loadProfileData();
70 }
71
72 @Override
73 protected void onDestroy()
74 {
75 super.onDestroy();
76 mDataSource.close();
77 }
78
79 @Override
80 protected void onSaveInstanceState(Bundle outState)
81 {
82 super.onSaveInstanceState(outState);
83 outState.putLong(VpnProfileDataSource.KEY_ID, mId);
84 }
85
86 @Override
87 public boolean onCreateOptionsMenu(Menu menu)
88 {
89 MenuInflater inflater = getMenuInflater();
90 inflater.inflate(R.menu.profile_edit, menu);
91 return true;
92 }
93
94 @Override
95 public boolean onOptionsItemSelected(MenuItem item)
96 {
97 switch (item.getItemId())
98 {
99 case android.R.id.home:
100 case R.id.menu_cancel:
101 finish();
102 return true;
103 case R.id.menu_accept:
104 saveProfile();
105 return true;
106 default:
107 return super.onOptionsItemSelected(item);
108 }
109 }
110
111 /**
112 * Save or update the profile depending on whether we actually have a
113 * profile object or not (this was created in updateProfileData)
114 */
115 private void saveProfile()
116 {
117 if (verifyInput())
118 {
119 if (mProfile != null)
120 {
121 updateProfileData();
122 mDataSource.updateVpnProfile(mProfile);
123 }
124 else
125 {
126 mProfile = new VpnProfile();
127 updateProfileData();
128 mDataSource.insertProfile(mProfile);
129 }
130 setResult(RESULT_OK, new Intent().putExtra(VpnProfileDataSource.KEY_ID, mProfile.getId()));
131 finish();
132 }
133 }
134
135 /**
136 * Verify the user input and display error messages.
137 * @return true if the input is valid
138 */
139 private boolean verifyInput()
140 {
141 boolean valid = true;
142 if (mGateway.getText().toString().trim().isEmpty())
143 {
144 mGateway.setError(getString(R.string.alert_text_no_input_gateway));
145 valid = false;
146 }
147 if (mUsername.getText().toString().trim().isEmpty())
148 {
149 mUsername.setError(getString(R.string.alert_text_no_input_username));
150 valid = false;
151 }
152 return valid;
153 }
154
155 /**
156 * Update the profile object with the data entered by the user
157 */
158 private void updateProfileData()
159 {
160 /* the name is optional, we default to the gateway if none is given */
161 String name = mName.getText().toString().trim();
162 String gateway = mGateway.getText().toString().trim();
163 mProfile.setName(name.isEmpty() ? gateway : name);
164 mProfile.setGateway(gateway);
165 mProfile.setUsername(mUsername.getText().toString().trim());
166 String password = mPassword.getText().toString().trim();
167 password = password.isEmpty() ? null : password;
168 mProfile.setPassword(password);
169 }
170
171 /**
172 * Load an existing profile if we got an ID
173 */
174 private void loadProfileData()
175 {
176 getActionBar().setTitle(R.string.add_profile);
177 if (mId != null)
178 {
179 mProfile = mDataSource.getVpnProfile(mId);
180 if (mProfile != null)
181 {
182 mName.setText(mProfile.getName());
183 mGateway.setText(mProfile.getGateway());
184 mUsername.setText(mProfile.getUsername());
185 mPassword.setText(mProfile.getPassword());
186 getActionBar().setTitle(mProfile.getName());
187 }
188 else
189 {
190 Log.e(VpnProfileDetailActivity.class.getSimpleName(),
191 "VPN profile with id " + mId + " not found");
192 finish();
193 }
194 }
195 }
196 }