2012-08-14 08:10:52 +00:00
|
|
|
/*
|
2014-05-30 15:40:24 +00:00
|
|
|
* Copyright (C) 2012-2014 Tobias Brunner
|
2012-08-14 08:10:52 +00:00
|
|
|
* Hochschule fuer Technik Rapperswil
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.strongswan.android.ui;
|
|
|
|
|
|
|
|
import org.strongswan.android.R;
|
2012-08-14 09:47:32 +00:00
|
|
|
import org.strongswan.android.data.VpnProfileDataSource;
|
2014-05-30 15:50:46 +00:00
|
|
|
import org.strongswan.android.logic.TrustedCertificateManager;
|
2014-05-30 14:15:25 +00:00
|
|
|
import org.strongswan.android.logic.TrustedCertificateManager.TrustedCertificateSource;
|
2014-05-30 10:40:53 +00:00
|
|
|
import org.strongswan.android.security.TrustedCertificateEntry;
|
2012-08-14 08:10:52 +00:00
|
|
|
|
|
|
|
import android.app.ActionBar;
|
|
|
|
import android.app.ActionBar.Tab;
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.app.Fragment;
|
|
|
|
import android.app.FragmentTransaction;
|
2012-08-14 09:47:32 +00:00
|
|
|
import android.content.Intent;
|
2012-08-14 08:10:52 +00:00
|
|
|
import android.os.Bundle;
|
2014-05-30 15:50:46 +00:00
|
|
|
import android.view.Menu;
|
2012-08-14 08:10:52 +00:00
|
|
|
import android.view.MenuItem;
|
|
|
|
|
2012-08-14 09:47:32 +00:00
|
|
|
public class TrustedCertificatesActivity extends Activity implements TrustedCertificateListFragment.OnTrustedCertificateSelectedListener
|
2012-08-14 08:10:52 +00:00
|
|
|
{
|
2014-05-30 15:34:49 +00:00
|
|
|
public static final String SELECT_CERTIFICATE = "org.strongswan.android.action.SELECT_CERTIFICATE";
|
2014-05-30 15:40:24 +00:00
|
|
|
private boolean mSelect;
|
2014-05-30 15:34:49 +00:00
|
|
|
|
2012-08-14 08:10:52 +00:00
|
|
|
@Override
|
|
|
|
public void onCreate(Bundle savedInstanceState)
|
|
|
|
{
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.trusted_certificates_activity);
|
|
|
|
|
|
|
|
ActionBar actionBar = getActionBar();
|
|
|
|
actionBar.setDisplayHomeAsUpEnabled(true);
|
|
|
|
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
|
|
|
|
|
2014-05-30 16:44:08 +00:00
|
|
|
TrustedCertificatesTabListener listener;
|
|
|
|
listener = new TrustedCertificatesTabListener(this, "system", TrustedCertificateSource.SYSTEM);
|
2012-08-14 08:10:52 +00:00
|
|
|
actionBar.addTab(actionBar
|
|
|
|
.newTab()
|
|
|
|
.setText(R.string.system_tab)
|
2014-05-30 16:44:08 +00:00
|
|
|
.setTag(listener)
|
|
|
|
.setTabListener(listener));
|
|
|
|
listener = new TrustedCertificatesTabListener(this, "user", TrustedCertificateSource.USER);
|
2012-08-14 08:10:52 +00:00
|
|
|
actionBar.addTab(actionBar
|
|
|
|
.newTab()
|
|
|
|
.setText(R.string.user_tab)
|
2014-05-30 16:44:08 +00:00
|
|
|
.setTag(listener)
|
|
|
|
.setTabListener(listener));
|
|
|
|
listener = new TrustedCertificatesTabListener(this, "local", TrustedCertificateSource.LOCAL);
|
2014-05-30 14:15:25 +00:00
|
|
|
actionBar.addTab(actionBar
|
|
|
|
.newTab()
|
|
|
|
.setText(R.string.local_tab)
|
2014-05-30 16:44:08 +00:00
|
|
|
.setTag(listener)
|
|
|
|
.setTabListener(listener));
|
2012-08-14 08:10:52 +00:00
|
|
|
|
|
|
|
if (savedInstanceState != null)
|
|
|
|
{
|
|
|
|
actionBar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
|
|
|
|
}
|
2014-05-30 15:40:24 +00:00
|
|
|
mSelect = SELECT_CERTIFICATE.equals(getIntent().getAction());
|
2012-08-14 08:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onSaveInstanceState(Bundle outState)
|
|
|
|
{
|
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
|
|
|
|
}
|
|
|
|
|
2014-05-30 15:50:46 +00:00
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu)
|
|
|
|
{
|
|
|
|
getMenuInflater().inflate(R.menu.certificates, menu);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-14 08:10:52 +00:00
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item)
|
|
|
|
{
|
|
|
|
switch (item.getItemId())
|
|
|
|
{
|
|
|
|
case android.R.id.home:
|
|
|
|
finish();
|
|
|
|
return true;
|
2014-05-30 15:50:46 +00:00
|
|
|
case R.id.menu_reload_certs:
|
2014-05-30 16:44:08 +00:00
|
|
|
reloadCertificates();
|
2014-05-30 15:50:46 +00:00
|
|
|
return true;
|
2012-08-14 08:10:52 +00:00
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
2012-08-14 09:47:32 +00:00
|
|
|
@Override
|
|
|
|
public void onTrustedCertificateSelected(TrustedCertificateEntry selected)
|
|
|
|
{
|
2014-05-30 15:40:24 +00:00
|
|
|
if (mSelect)
|
|
|
|
{
|
|
|
|
/* the user selected a certificate, return to calling activity */
|
|
|
|
Intent intent = new Intent();
|
|
|
|
intent.putExtra(VpnProfileDataSource.KEY_CERTIFICATE, selected.getAlias());
|
|
|
|
setResult(Activity.RESULT_OK, intent);
|
|
|
|
finish();
|
|
|
|
}
|
2012-08-14 09:47:32 +00:00
|
|
|
}
|
|
|
|
|
2014-05-30 16:44:08 +00:00
|
|
|
private void reloadCertificates()
|
|
|
|
{
|
|
|
|
TrustedCertificateManager.getInstance().reset();
|
|
|
|
for (int i = 0; i < getActionBar().getTabCount(); i++)
|
|
|
|
{
|
|
|
|
Tab tab = getActionBar().getTabAt(i);
|
|
|
|
TrustedCertificatesTabListener listener = (TrustedCertificatesTabListener)tab.getTag();
|
|
|
|
listener.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-14 08:10:52 +00:00
|
|
|
public static class TrustedCertificatesTabListener implements ActionBar.TabListener
|
|
|
|
{
|
|
|
|
private final String mTag;
|
2014-05-30 14:15:25 +00:00
|
|
|
private final TrustedCertificateSource mSource;
|
2012-08-14 08:10:52 +00:00
|
|
|
private Fragment mFragment;
|
|
|
|
|
2014-05-30 14:15:25 +00:00
|
|
|
public TrustedCertificatesTabListener(Activity activity, String tag, TrustedCertificateSource source)
|
2012-08-14 08:10:52 +00:00
|
|
|
{
|
|
|
|
mTag = tag;
|
2014-05-30 14:15:25 +00:00
|
|
|
mSource = source;
|
2012-08-14 08:10:52 +00:00
|
|
|
/* check to see if we already have a fragment for this tab, probably
|
|
|
|
* from a previously saved state. if so, deactivate it, because the
|
|
|
|
* initial state is that no tab is shown */
|
|
|
|
mFragment = activity.getFragmentManager().findFragmentByTag(mTag);
|
|
|
|
if (mFragment != null && !mFragment.isDetached())
|
|
|
|
{
|
|
|
|
FragmentTransaction ft = activity.getFragmentManager().beginTransaction();
|
|
|
|
ft.detach(mFragment);
|
|
|
|
ft.commit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTabSelected(Tab tab, FragmentTransaction ft)
|
|
|
|
{
|
|
|
|
if (mFragment == null)
|
|
|
|
{
|
|
|
|
mFragment = new TrustedCertificateListFragment();
|
2014-05-30 14:15:25 +00:00
|
|
|
Bundle args = new Bundle();
|
|
|
|
args.putSerializable(TrustedCertificateListFragment.EXTRA_CERTIFICATE_SOURCE, mSource);
|
|
|
|
mFragment.setArguments(args);
|
2012-08-14 08:10:52 +00:00
|
|
|
ft.add(android.R.id.content, mFragment, mTag);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ft.attach(mFragment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTabUnselected(Tab tab, FragmentTransaction ft)
|
|
|
|
{
|
|
|
|
if (mFragment != null)
|
|
|
|
{
|
|
|
|
ft.detach(mFragment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onTabReselected(Tab tab, FragmentTransaction ft)
|
|
|
|
{
|
|
|
|
/* nothing to be done */
|
|
|
|
}
|
2014-05-30 15:50:46 +00:00
|
|
|
|
2014-05-30 16:44:08 +00:00
|
|
|
public void reset()
|
2014-05-30 15:50:46 +00:00
|
|
|
{
|
2014-05-30 16:44:08 +00:00
|
|
|
if (mFragment != null)
|
|
|
|
{
|
|
|
|
((TrustedCertificateListFragment)mFragment).reset();
|
|
|
|
}
|
2014-05-30 15:50:46 +00:00
|
|
|
}
|
|
|
|
}
|
2012-08-14 08:10:52 +00:00
|
|
|
}
|