android: Automatically reload certificates if manager is reset
[strongswan.git] / src / frontends / android / app / src / main / java / org / strongswan / android / ui / TrustedCertificatesActivity.java
1 /*
2 * Copyright (C) 2012-2014 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 package org.strongswan.android.ui;
17
18 import android.app.Activity;
19 import android.content.Intent;
20 import android.os.Build;
21 import android.os.Bundle;
22 import android.support.v4.app.Fragment;
23 import android.support.v4.app.FragmentTransaction;
24 import android.support.v7.app.ActionBar;
25 import android.support.v7.app.AppCompatActivity;
26 import android.view.Menu;
27 import android.view.MenuItem;
28
29 import org.strongswan.android.R;
30 import org.strongswan.android.data.VpnProfileDataSource;
31 import org.strongswan.android.logic.TrustedCertificateManager;
32 import org.strongswan.android.logic.TrustedCertificateManager.TrustedCertificateSource;
33 import org.strongswan.android.security.TrustedCertificateEntry;
34 import org.strongswan.android.ui.CertificateDeleteConfirmationDialog.OnCertificateDeleteListener;
35
36 import java.security.KeyStore;
37
38 public class TrustedCertificatesActivity extends AppCompatActivity implements TrustedCertificateListFragment.OnTrustedCertificateSelectedListener, OnCertificateDeleteListener
39 {
40 public static final String SELECT_CERTIFICATE = "org.strongswan.android.action.SELECT_CERTIFICATE";
41 private static final String DIALOG_TAG = "Dialog";
42 private static final int IMPORT_CERTIFICATE = 0;
43 private boolean mSelect;
44
45 @Override
46 public void onCreate(Bundle savedInstanceState)
47 {
48 super.onCreate(savedInstanceState);
49 setContentView(R.layout.trusted_certificates_activity);
50
51 ActionBar actionBar = getSupportActionBar();
52 actionBar.setDisplayHomeAsUpEnabled(true);
53 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
54
55 TrustedCertificatesTabListener listener;
56 listener = new TrustedCertificatesTabListener(this, "system", TrustedCertificateSource.SYSTEM);
57 actionBar.addTab(actionBar
58 .newTab()
59 .setText(R.string.system_tab)
60 .setTag(listener)
61 .setTabListener(listener));
62 listener = new TrustedCertificatesTabListener(this, "user", TrustedCertificateSource.USER);
63 actionBar.addTab(actionBar
64 .newTab()
65 .setText(R.string.user_tab)
66 .setTag(listener)
67 .setTabListener(listener));
68 listener = new TrustedCertificatesTabListener(this, "local", TrustedCertificateSource.LOCAL);
69 actionBar.addTab(actionBar
70 .newTab()
71 .setText(R.string.local_tab)
72 .setTag(listener)
73 .setTabListener(listener));
74
75 if (savedInstanceState != null)
76 {
77 actionBar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
78 }
79 mSelect = SELECT_CERTIFICATE.equals(getIntent().getAction());
80 }
81
82 @Override
83 protected void onSaveInstanceState(Bundle outState)
84 {
85 super.onSaveInstanceState(outState);
86 outState.putInt("tab", getSupportActionBar().getSelectedNavigationIndex());
87 }
88
89 @Override
90 public boolean onCreateOptionsMenu(Menu menu)
91 {
92 getMenuInflater().inflate(R.menu.certificates, menu);
93 return true;
94 }
95
96 @Override
97 public boolean onPrepareOptionsMenu(Menu menu)
98 {
99 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
100 {
101 menu.removeItem(R.id.menu_import_certificate);
102 }
103 return true;
104 }
105
106 @Override
107 public boolean onOptionsItemSelected(MenuItem item)
108 {
109 switch (item.getItemId())
110 {
111 case android.R.id.home:
112 finish();
113 return true;
114 case R.id.menu_reload_certs:
115 reloadCertificates();
116 return true;
117 case R.id.menu_import_certificate:
118 Intent intent = new Intent(this, TrustedCertificateImportActivity.class);
119 startActivityForResult(intent, IMPORT_CERTIFICATE);
120 return true;
121 }
122 return super.onOptionsItemSelected(item);
123 }
124
125 @Override
126 protected void onActivityResult(int requestCode, int resultCode, Intent data)
127 {
128 switch (requestCode)
129 {
130 case IMPORT_CERTIFICATE:
131 if (resultCode == Activity.RESULT_OK)
132 {
133 reloadCertificates();
134 }
135 return;
136 }
137 super.onActivityResult(requestCode, resultCode, data);
138 }
139
140 @Override
141 public void onTrustedCertificateSelected(TrustedCertificateEntry selected)
142 {
143 if (mSelect)
144 {
145 /* the user selected a certificate, return to calling activity */
146 Intent intent = new Intent();
147 intent.putExtra(VpnProfileDataSource.KEY_CERTIFICATE, selected.getAlias());
148 setResult(Activity.RESULT_OK, intent);
149 finish();
150 }
151 else
152 {
153 TrustedCertificatesTabListener listener;
154 listener = (TrustedCertificatesTabListener)getSupportActionBar().getSelectedTab().getTag();
155 if (listener.mTag == "local")
156 {
157 Bundle args = new Bundle();
158 args.putString(CertificateDeleteConfirmationDialog.ALIAS, selected.getAlias());
159 CertificateDeleteConfirmationDialog dialog = new CertificateDeleteConfirmationDialog();
160 dialog.setArguments(args);
161 dialog.show(getSupportFragmentManager(), DIALOG_TAG);
162 }
163 }
164 }
165
166 @Override
167 public void onDelete(String alias)
168 {
169 try
170 {
171 KeyStore store = KeyStore.getInstance("LocalCertificateStore");
172 store.load(null, null);
173 store.deleteEntry(alias);
174 reloadCertificates();
175 }
176 catch (Exception e)
177 {
178 e.printStackTrace();
179 }
180 }
181
182 private void reloadCertificates()
183 {
184 TrustedCertificateManager.getInstance().reset();
185 }
186
187 public static class TrustedCertificatesTabListener implements ActionBar.TabListener
188 {
189 private final String mTag;
190 private final TrustedCertificateSource mSource;
191 private Fragment mFragment;
192
193 public TrustedCertificatesTabListener(AppCompatActivity activity, String tag, TrustedCertificateSource source)
194 {
195 mTag = tag;
196 mSource = source;
197 /* check to see if we already have a fragment for this tab, probably
198 * from a previously saved state. if so, deactivate it, because the
199 * initial state is that no tab is shown */
200 mFragment = activity.getSupportFragmentManager().findFragmentByTag(mTag);
201 if (mFragment != null && !mFragment.isDetached())
202 {
203 FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
204 ft.detach(mFragment);
205 ft.commit();
206 }
207 }
208
209 @Override
210 public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft)
211 {
212 if (mFragment == null)
213 {
214 mFragment = new TrustedCertificateListFragment();
215 Bundle args = new Bundle();
216 args.putSerializable(TrustedCertificateListFragment.EXTRA_CERTIFICATE_SOURCE, mSource);
217 mFragment.setArguments(args);
218 ft.add(android.R.id.content, mFragment, mTag);
219 }
220 else
221 {
222 ft.attach(mFragment);
223 }
224 }
225
226 @Override
227 public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft)
228 {
229 if (mFragment != null)
230 {
231 ft.detach(mFragment);
232 }
233 }
234
235 @Override
236 public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft)
237 {
238 /* nothing to be done */
239 }
240 }
241 }