android: Avoid deprecated tabs in the ActionBar in TrustedCertificatesActivity
[strongswan.git] / src / frontends / android / app / src / main / java / org / strongswan / android / ui / TrustedCertificatesActivity.java
1 /*
2 * Copyright (C) 2012-2015 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.Context;
20 import android.content.Intent;
21 import android.os.Build;
22 import android.os.Bundle;
23 import android.support.design.widget.TabLayout;
24 import android.support.v4.app.Fragment;
25 import android.support.v4.app.FragmentManager;
26 import android.support.v4.app.FragmentPagerAdapter;
27 import android.support.v4.view.ViewPager;
28 import android.support.v7.app.ActionBar;
29 import android.support.v7.app.AppCompatActivity;
30 import android.view.Menu;
31 import android.view.MenuItem;
32
33 import org.strongswan.android.R;
34 import org.strongswan.android.data.VpnProfileDataSource;
35 import org.strongswan.android.logic.TrustedCertificateManager;
36 import org.strongswan.android.logic.TrustedCertificateManager.TrustedCertificateSource;
37 import org.strongswan.android.security.TrustedCertificateEntry;
38 import org.strongswan.android.ui.CertificateDeleteConfirmationDialog.OnCertificateDeleteListener;
39
40 import java.security.KeyStore;
41
42 public class TrustedCertificatesActivity extends AppCompatActivity implements TrustedCertificateListFragment.OnTrustedCertificateSelectedListener, OnCertificateDeleteListener
43 {
44 public static final String SELECT_CERTIFICATE = "org.strongswan.android.action.SELECT_CERTIFICATE";
45 private static final String DIALOG_TAG = "Dialog";
46 private static final int IMPORT_CERTIFICATE = 0;
47 private TrustedCertificatesPagerAdapter mAdapter;
48 private ViewPager mPager;
49 private boolean mSelect;
50
51 @Override
52 public void onCreate(Bundle savedInstanceState)
53 {
54 super.onCreate(savedInstanceState);
55 setContentView(R.layout.trusted_certificates_activity);
56
57 ActionBar actionBar = getSupportActionBar();
58 actionBar.setDisplayHomeAsUpEnabled(true);
59
60 mAdapter = new TrustedCertificatesPagerAdapter(getSupportFragmentManager(), this);
61
62 mPager = (ViewPager)findViewById(R.id.viewpager);
63 mPager.setAdapter(mAdapter);
64
65 TabLayout tabs = (TabLayout)findViewById(R.id.tabs);
66 tabs.setupWithViewPager(mPager);
67
68 mSelect = SELECT_CERTIFICATE.equals(getIntent().getAction());
69 }
70
71 @Override
72 public boolean onCreateOptionsMenu(Menu menu)
73 {
74 getMenuInflater().inflate(R.menu.certificates, menu);
75 return true;
76 }
77
78 @Override
79 public boolean onPrepareOptionsMenu(Menu menu)
80 {
81 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
82 {
83 menu.removeItem(R.id.menu_import_certificate);
84 }
85 return true;
86 }
87
88 @Override
89 public boolean onOptionsItemSelected(MenuItem item)
90 {
91 switch (item.getItemId())
92 {
93 case android.R.id.home:
94 finish();
95 return true;
96 case R.id.menu_reload_certs:
97 reloadCertificates();
98 return true;
99 case R.id.menu_import_certificate:
100 Intent intent = new Intent(this, TrustedCertificateImportActivity.class);
101 startActivityForResult(intent, IMPORT_CERTIFICATE);
102 return true;
103 }
104 return super.onOptionsItemSelected(item);
105 }
106
107 @Override
108 protected void onActivityResult(int requestCode, int resultCode, Intent data)
109 {
110 switch (requestCode)
111 {
112 case IMPORT_CERTIFICATE:
113 if (resultCode == Activity.RESULT_OK)
114 {
115 reloadCertificates();
116 }
117 return;
118 }
119 super.onActivityResult(requestCode, resultCode, data);
120 }
121
122 @Override
123 public void onTrustedCertificateSelected(TrustedCertificateEntry selected)
124 {
125 if (mSelect)
126 {
127 /* the user selected a certificate, return to calling activity */
128 Intent intent = new Intent();
129 intent.putExtra(VpnProfileDataSource.KEY_CERTIFICATE, selected.getAlias());
130 setResult(Activity.RESULT_OK, intent);
131 finish();
132 }
133 else if (mAdapter.getSource(mPager.getCurrentItem()) == TrustedCertificateSource.LOCAL)
134 {
135 Bundle args = new Bundle();
136 args.putString(CertificateDeleteConfirmationDialog.ALIAS, selected.getAlias());
137 CertificateDeleteConfirmationDialog dialog = new CertificateDeleteConfirmationDialog();
138 dialog.setArguments(args);
139 dialog.show(getSupportFragmentManager(), DIALOG_TAG);
140 }
141 }
142
143 @Override
144 public void onDelete(String alias)
145 {
146 try
147 {
148 KeyStore store = KeyStore.getInstance("LocalCertificateStore");
149 store.load(null, null);
150 store.deleteEntry(alias);
151 reloadCertificates();
152 }
153 catch (Exception e)
154 {
155 e.printStackTrace();
156 }
157 }
158
159 private void reloadCertificates()
160 {
161 TrustedCertificateManager.getInstance().reset();
162 }
163
164 public static class TrustedCertificatesPagerAdapter extends FragmentPagerAdapter
165 {
166 private TrustedCertificatesTab mTabs[];
167
168 public TrustedCertificatesPagerAdapter(FragmentManager fm, Context context)
169 {
170 super(fm);
171 mTabs = new TrustedCertificatesTab[]{
172 new TrustedCertificatesTab(context.getString(R.string.system_tab), TrustedCertificateSource.SYSTEM),
173 new TrustedCertificatesTab(context.getString(R.string.user_tab), TrustedCertificateSource.USER),
174 new TrustedCertificatesTab(context.getString(R.string.local_tab), TrustedCertificateSource.LOCAL),
175 };
176 }
177
178 @Override
179 public int getCount()
180 {
181 return mTabs.length;
182 }
183
184 @Override
185 public CharSequence getPageTitle(int position)
186 {
187 return mTabs[position].getTitle();
188 }
189
190 public TrustedCertificateSource getSource(int position)
191 {
192 return mTabs[position].getSource();
193 }
194
195 @Override
196 public Fragment getItem(int position)
197 {
198 TrustedCertificateListFragment fragment = new TrustedCertificateListFragment();
199 Bundle args = new Bundle();
200 args.putSerializable(TrustedCertificateListFragment.EXTRA_CERTIFICATE_SOURCE, mTabs[position].getSource());
201 fragment.setArguments(args);
202 return fragment;
203 }
204 }
205
206 public static class TrustedCertificatesTab
207 {
208 private final String mTitle;
209 private final TrustedCertificateSource mSource;
210
211 public TrustedCertificatesTab(String title, TrustedCertificateSource source)
212 {
213 mTitle = title;
214 mSource = source;
215 }
216
217 public String getTitle()
218 {
219 return mTitle;
220 }
221
222 public TrustedCertificateSource getSource()
223 {
224 return mSource;
225 }
226 }
227 }