Added simple adapter for trusted certificates (to be used with a Spinner widget)
[strongswan.git] / src / frontends / android / src / org / strongswan / android / ui / MainActivity.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 import org.strongswan.android.logic.CharonVpnService;
24 import org.strongswan.android.logic.TrustedCertificateManager;
25 import org.strongswan.android.ui.VpnProfileListFragment.OnVpnProfileSelectedListener;
26
27 import android.app.ActionBar;
28 import android.app.Activity;
29 import android.content.Intent;
30 import android.net.VpnService;
31 import android.os.Bundle;
32 import android.view.Menu;
33 import android.view.MenuInflater;
34 import android.view.MenuItem;
35 import android.view.Window;
36
37 public class MainActivity extends Activity implements OnVpnProfileSelectedListener
38 {
39 private static final int PREPARE_VPN_SERVICE = 0;
40 private VpnProfile activeProfile;
41
42 @Override
43 public void onCreate(Bundle savedInstanceState)
44 {
45 super.onCreate(savedInstanceState);
46 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
47 setContentView(R.layout.main);
48
49 ActionBar bar = getActionBar();
50 bar.setDisplayShowTitleEnabled(false);
51
52 /* load CA certificates in a background thread */
53 setProgressBarIndeterminateVisibility(true);
54 new Thread(new Runnable() {
55 @Override
56 public void run()
57 {
58 TrustedCertificateManager.getInstance().load();
59 runOnUiThread(new Runnable() {
60 @Override
61 public void run()
62 {
63 setProgressBarIndeterminateVisibility(false);
64 }
65 });
66 }
67 }).start();
68 }
69
70 @Override
71 public boolean onCreateOptionsMenu(Menu menu)
72 {
73 MenuInflater inflater = getMenuInflater();
74 inflater.inflate(R.menu.main, menu);
75 return true;
76 }
77
78 @Override
79 public boolean onOptionsItemSelected(MenuItem item)
80 {
81 switch (item.getItemId())
82 {
83 case R.id.menu_reload_certs:
84 setProgressBarIndeterminateVisibility(true);
85 new Thread(new Runnable() {
86 @Override
87 public void run()
88 {
89 TrustedCertificateManager.getInstance().reload();
90 runOnUiThread(new Runnable() {
91 @Override
92 public void run()
93 {
94 setProgressBarIndeterminateVisibility(false);
95 }
96 });
97 }
98 }).start();
99 return true;
100 default:
101 return super.onOptionsItemSelected(item);
102 }
103 }
104
105 /**
106 * Prepare the VpnService. If this succeeds the current VPN profile is
107 * started.
108 */
109 protected void prepareVpnService()
110 {
111 Intent intent = VpnService.prepare(this);
112 if (intent != null)
113 {
114 startActivityForResult(intent, PREPARE_VPN_SERVICE);
115 }
116 else
117 {
118 onActivityResult(PREPARE_VPN_SERVICE, RESULT_OK, null);
119 }
120 }
121
122 @Override
123 protected void onActivityResult(int requestCode, int resultCode, Intent data)
124 {
125 switch (requestCode)
126 {
127 case PREPARE_VPN_SERVICE:
128 if (resultCode == RESULT_OK && activeProfile != null)
129 {
130 Intent intent = new Intent(this, CharonVpnService.class);
131 intent.putExtra(VpnProfileDataSource.KEY_ID, activeProfile.getId());
132 this.startService(intent);
133 }
134 break;
135 default:
136 super.onActivityResult(requestCode, resultCode, data);
137 }
138 }
139
140 @Override
141 public void onVpnProfileSelected(VpnProfile profile)
142 {
143 activeProfile = profile;
144 prepareVpnService();
145 }
146 }