Added an activity that shows lists of CA certificates in two tabs
[strongswan.git] / src / frontends / android / src / org / strongswan / android / ui / TrustedCertificatesActivity.java
1 /*
2 * Copyright (C) 2012 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 org.strongswan.android.R;
19
20 import android.app.ActionBar;
21 import android.app.ActionBar.Tab;
22 import android.app.Activity;
23 import android.app.Fragment;
24 import android.app.FragmentTransaction;
25 import android.os.Bundle;
26 import android.view.MenuItem;
27
28 public class TrustedCertificatesActivity extends Activity
29 {
30 @Override
31 public void onCreate(Bundle savedInstanceState)
32 {
33 super.onCreate(savedInstanceState);
34 setContentView(R.layout.trusted_certificates_activity);
35
36 ActionBar actionBar = getActionBar();
37 actionBar.setDisplayHomeAsUpEnabled(true);
38 actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
39
40 actionBar.addTab(actionBar
41 .newTab()
42 .setText(R.string.system_tab)
43 .setTabListener(new TrustedCertificatesTabListener(this, "system", false)));
44 actionBar.addTab(actionBar
45 .newTab()
46 .setText(R.string.user_tab)
47 .setTabListener(new TrustedCertificatesTabListener(this, "user", true)));
48
49 if (savedInstanceState != null)
50 {
51 actionBar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
52 }
53 }
54
55 @Override
56 protected void onSaveInstanceState(Bundle outState)
57 {
58 super.onSaveInstanceState(outState);
59 outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
60 }
61
62 @Override
63 public boolean onOptionsItemSelected(MenuItem item)
64 {
65 switch (item.getItemId())
66 {
67 case android.R.id.home:
68 finish();
69 return true;
70 }
71 return super.onOptionsItemSelected(item);
72 }
73
74 public static class TrustedCertificatesTabListener implements ActionBar.TabListener
75 {
76 private final String mTag;
77 private final boolean mUser;
78 private Fragment mFragment;
79
80 public TrustedCertificatesTabListener(Activity activity, String tag, boolean user)
81 {
82 mTag = tag;
83 mUser = user;
84 /* check to see if we already have a fragment for this tab, probably
85 * from a previously saved state. if so, deactivate it, because the
86 * initial state is that no tab is shown */
87 mFragment = activity.getFragmentManager().findFragmentByTag(mTag);
88 if (mFragment != null && !mFragment.isDetached())
89 {
90 FragmentTransaction ft = activity.getFragmentManager().beginTransaction();
91 ft.detach(mFragment);
92 ft.commit();
93 }
94 }
95
96 @Override
97 public void onTabSelected(Tab tab, FragmentTransaction ft)
98 {
99 if (mFragment == null)
100 {
101 mFragment = new TrustedCertificateListFragment();
102 if (mUser)
103 { /* use non empty arguments to indicate this */
104 mFragment.setArguments(new Bundle());
105 }
106 ft.add(android.R.id.content, mFragment, mTag);
107 }
108 else
109 {
110 ft.attach(mFragment);
111 }
112 }
113
114 @Override
115 public void onTabUnselected(Tab tab, FragmentTransaction ft)
116 {
117 if (mFragment != null)
118 {
119 ft.detach(mFragment);
120 }
121 }
122
123 @Override
124 public void onTabReselected(Tab tab, FragmentTransaction ft)
125 {
126 /* nothing to be done */
127 }
128 }
129 }