Added an activity to edit basic VPN profile details
[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.os.Bundle;
26 import android.util.Log;
27 import android.widget.EditText;
28
29 public class VpnProfileDetailActivity extends Activity
30 {
31 private VpnProfileDataSource mDataSource;
32 private Long mId;
33 private VpnProfile mProfile;
34 private EditText mName;
35 private EditText mGateway;
36 private EditText mUsername;
37 private EditText mPassword;
38
39 @Override
40 public void onCreate(Bundle savedInstanceState)
41 {
42 super.onCreate(savedInstanceState);
43
44 /* the title is set when we load the profile, if any */
45 getActionBar().setDisplayHomeAsUpEnabled(true);
46
47 mDataSource = new VpnProfileDataSource(this);
48 mDataSource.open();
49
50 setContentView(R.layout.profile_detail_view);
51
52 mName = (EditText)findViewById(R.id.name);
53 mPassword = (EditText)findViewById(R.id.password);
54 mGateway = (EditText)findViewById(R.id.gateway);
55 mUsername = (EditText)findViewById(R.id.username);
56
57 mId = savedInstanceState == null ? null : savedInstanceState.getLong(VpnProfileDataSource.KEY_ID);
58 if (mId == null)
59 {
60 Bundle extras = getIntent().getExtras();
61 mId = extras == null ? null : extras.getLong(VpnProfileDataSource.KEY_ID);
62 }
63
64 loadProfileData();
65 }
66
67 @Override
68 protected void onDestroy()
69 {
70 super.onDestroy();
71 mDataSource.close();
72 }
73
74 @Override
75 protected void onSaveInstanceState(Bundle outState)
76 {
77 super.onSaveInstanceState(outState);
78 outState.putLong(VpnProfileDataSource.KEY_ID, mId);
79 }
80
81 /**
82 * Load an existing profile if we got an ID
83 */
84 private void loadProfileData()
85 {
86 getActionBar().setTitle(R.string.add_profile);
87 if (mId != null)
88 {
89 mProfile = mDataSource.getVpnProfile(mId);
90 if (mProfile != null)
91 {
92 mName.setText(mProfile.getName());
93 mGateway.setText(mProfile.getGateway());
94 mUsername.setText(mProfile.getUsername());
95 mPassword.setText(mProfile.getPassword());
96 getActionBar().setTitle(mProfile.getName());
97 }
98 else
99 {
100 Log.e(VpnProfileDetailActivity.class.getSimpleName(),
101 "VPN profile with id " + mId + " not found");
102 finish();
103 }
104 }
105 }
106 }