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 org
.strongswan
.android
.R
;
21 import org
.strongswan
.android
.data
.VpnProfile
;
22 import org
.strongswan
.android
.data
.VpnProfileDataSource
;
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
;
34 public class VpnProfileDetailActivity
extends Activity
36 private VpnProfileDataSource mDataSource
;
38 private VpnProfile mProfile
;
39 private EditText mName
;
40 private EditText mGateway
;
41 private EditText mUsername
;
42 private EditText mPassword
;
45 public void onCreate(Bundle savedInstanceState
)
47 super.onCreate(savedInstanceState
);
49 /* the title is set when we load the profile, if any */
50 getActionBar().setDisplayHomeAsUpEnabled(true
);
52 mDataSource
= new VpnProfileDataSource(this);
55 setContentView(R
.layout
.profile_detail_view
);
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
);
62 mId
= savedInstanceState
== null ? null
: savedInstanceState
.getLong(VpnProfileDataSource
.KEY_ID
);
65 Bundle extras
= getIntent().getExtras();
66 mId
= extras
== null ? null
: extras
.getLong(VpnProfileDataSource
.KEY_ID
);
73 protected void onDestroy()
80 protected void onSaveInstanceState(Bundle outState
)
82 super.onSaveInstanceState(outState
);
83 outState
.putLong(VpnProfileDataSource
.KEY_ID
, mId
);
87 public boolean onCreateOptionsMenu(Menu menu
)
89 MenuInflater inflater
= getMenuInflater();
90 inflater
.inflate(R
.menu
.profile_edit
, menu
);
95 public boolean onOptionsItemSelected(MenuItem item
)
97 switch (item
.getItemId())
99 case android
.R
.id
.home
:
100 case R
.id
.menu_cancel
:
103 case R
.id
.menu_accept
:
107 return super.onOptionsItemSelected(item
);
112 * Save or update the profile depending on whether we actually have a
113 * profile object or not (this was created in updateProfileData)
115 private void saveProfile()
119 if (mProfile
!= null
)
122 mDataSource
.updateVpnProfile(mProfile
);
126 mProfile
= new VpnProfile();
128 mDataSource
.insertProfile(mProfile
);
130 setResult(RESULT_OK
, new Intent().putExtra(VpnProfileDataSource
.KEY_ID
, mProfile
.getId()));
136 * Verify the user input and display error messages.
137 * @return true if the input is valid
139 private boolean verifyInput()
141 boolean valid
= true
;
142 if (mGateway
.getText().toString().trim().isEmpty())
144 mGateway
.setError(getString(R
.string
.alert_text_no_input_gateway
));
147 if (mUsername
.getText().toString().trim().isEmpty())
149 mUsername
.setError(getString(R
.string
.alert_text_no_input_username
));
156 * Update the profile object with the data entered by the user
158 private void updateProfileData()
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
);
172 * Load an existing profile if we got an ID
174 private void loadProfileData()
176 getActionBar().setTitle(R
.string
.add_profile
);
179 mProfile
= mDataSource
.getVpnProfile(mId
);
180 if (mProfile
!= null
)
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());
190 Log
.e(VpnProfileDetailActivity
.class.getSimpleName(),
191 "VPN profile with id " + mId
+ " not found");