Simplified asynchronous loading of CA certificates in MainActivity
[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.AsyncTask;
32 import android.os.Bundle;
33 import android.view.Menu;
34 import android.view.MenuInflater;
35 import android.view.MenuItem;
36 import android.view.Window;
37
38 public class MainActivity extends Activity implements OnVpnProfileSelectedListener
39 {
40 private static final int PREPARE_VPN_SERVICE = 0;
41 private VpnProfile activeProfile;
42
43 @Override
44 public void onCreate(Bundle savedInstanceState)
45 {
46 super.onCreate(savedInstanceState);
47 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
48 setContentView(R.layout.main);
49
50 ActionBar bar = getActionBar();
51 bar.setDisplayShowTitleEnabled(false);
52
53 /* load CA certificates in a background task */
54 new CertificateLoadTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, false);
55 }
56
57 @Override
58 public boolean onCreateOptionsMenu(Menu menu)
59 {
60 MenuInflater inflater = getMenuInflater();
61 inflater.inflate(R.menu.main, menu);
62 return true;
63 }
64
65 @Override
66 public boolean onOptionsItemSelected(MenuItem item)
67 {
68 switch (item.getItemId())
69 {
70 case R.id.menu_reload_certs:
71 new CertificateLoadTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, true);
72 return true;
73 default:
74 return super.onOptionsItemSelected(item);
75 }
76 }
77
78 /**
79 * Prepare the VpnService. If this succeeds the current VPN profile is
80 * started.
81 */
82 protected void prepareVpnService()
83 {
84 Intent intent = VpnService.prepare(this);
85 if (intent != null)
86 {
87 startActivityForResult(intent, PREPARE_VPN_SERVICE);
88 }
89 else
90 {
91 onActivityResult(PREPARE_VPN_SERVICE, RESULT_OK, null);
92 }
93 }
94
95 @Override
96 protected void onActivityResult(int requestCode, int resultCode, Intent data)
97 {
98 switch (requestCode)
99 {
100 case PREPARE_VPN_SERVICE:
101 if (resultCode == RESULT_OK && activeProfile != null)
102 {
103 Intent intent = new Intent(this, CharonVpnService.class);
104 intent.putExtra(VpnProfileDataSource.KEY_ID, activeProfile.getId());
105 this.startService(intent);
106 }
107 break;
108 default:
109 super.onActivityResult(requestCode, resultCode, data);
110 }
111 }
112
113 @Override
114 public void onVpnProfileSelected(VpnProfile profile)
115 {
116 activeProfile = profile;
117 prepareVpnService();
118 }
119
120 /**
121 * Class that loads or reloads the cached CA certificates.
122 */
123 private class CertificateLoadTask extends AsyncTask<Boolean, Void, TrustedCertificateManager>
124 {
125 @Override
126 protected void onPreExecute()
127 {
128 setProgressBarIndeterminateVisibility(true);
129 }
130 @Override
131 protected TrustedCertificateManager doInBackground(Boolean... params)
132 {
133 if (params.length > 0 && params[0])
134 { /* force a reload of the certificates */
135 return TrustedCertificateManager.getInstance().reload();
136 }
137 return TrustedCertificateManager.getInstance().load();
138 }
139 @Override
140 protected void onPostExecute(TrustedCertificateManager result)
141 {
142 setProgressBarIndeterminateVisibility(false);
143 }
144 }
145 }