]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/frontends/android/app/src/main/java/org/strongswan/android/ui/MainActivity.java
Update copyright headers after acquisition by secunet
[thirdparty/strongswan.git] / src / frontends / android / app / src / main / java / org / strongswan / android / ui / MainActivity.java
1 /*
2 * Copyright (C) 2012-2018 Tobias Brunner
3 * Copyright (C) 2012 Giuliano Grassi
4 * Copyright (C) 2012 Ralf Sager
5 *
6 * Copyright (C) secunet Security Networks AG
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 package org.strongswan.android.ui;
20
21 import android.app.Dialog;
22 import android.content.DialogInterface;
23 import android.content.Intent;
24 import android.os.Build;
25 import android.os.Bundle;
26 import android.text.format.Formatter;
27 import android.view.Menu;
28 import android.view.MenuItem;
29 import android.widget.Toast;
30
31 import org.strongswan.android.R;
32 import org.strongswan.android.data.VpnProfile;
33 import org.strongswan.android.logic.StrongSwanApplication;
34 import org.strongswan.android.logic.TrustedCertificateManager;
35 import org.strongswan.android.ui.VpnProfileListFragment.OnVpnProfileSelectedListener;
36
37 import java.io.File;
38 import java.util.ArrayList;
39 import java.util.List;
40
41 import androidx.appcompat.app.ActionBar;
42 import androidx.appcompat.app.AlertDialog;
43 import androidx.appcompat.app.AppCompatActivity;
44 import androidx.appcompat.app.AppCompatDialogFragment;
45 import androidx.fragment.app.Fragment;
46 import androidx.fragment.app.FragmentManager;
47 import androidx.fragment.app.FragmentTransaction;
48
49 public class MainActivity extends AppCompatActivity implements OnVpnProfileSelectedListener
50 {
51 public static final String CONTACT_EMAIL = "android@strongswan.org";
52 public static final String EXTRA_CRL_LIST = "org.strongswan.android.CRL_LIST";
53
54 /**
55 * Use "bring your own device" (BYOD) features
56 */
57 public static final boolean USE_BYOD = true;
58
59 private static final String DIALOG_TAG = "Dialog";
60
61 @Override
62 public void onCreate(Bundle savedInstanceState)
63 {
64 super.onCreate(savedInstanceState);
65 setContentView(R.layout.main);
66
67 ActionBar bar = getSupportActionBar();
68 bar.setDisplayShowHomeEnabled(true);
69 bar.setDisplayShowTitleEnabled(false);
70 bar.setIcon(R.mipmap.ic_app);
71
72 /* load CA certificates in a background thread */
73 ((StrongSwanApplication)getApplication()).getExecutor().execute(() -> {
74 TrustedCertificateManager.getInstance().load();
75 });
76 }
77
78 @Override
79 public boolean onCreateOptionsMenu(Menu menu)
80 {
81 getMenuInflater().inflate(R.menu.main, menu);
82 return true;
83 }
84
85 @Override
86 public boolean onPrepareOptionsMenu(Menu menu)
87 {
88 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
89 {
90 menu.removeItem(R.id.menu_import_profile);
91 }
92 return true;
93 }
94
95 @Override
96 public boolean onOptionsItemSelected(MenuItem item)
97 {
98 switch (item.getItemId())
99 {
100 case R.id.menu_import_profile:
101 Intent intent = new Intent(this, VpnProfileImportActivity.class);
102 startActivity(intent);
103 return true;
104 case R.id.menu_manage_certs:
105 Intent certIntent = new Intent(this, TrustedCertificatesActivity.class);
106 startActivity(certIntent);
107 return true;
108 case R.id.menu_crl_cache:
109 clearCRLs();
110 return true;
111 case R.id.menu_show_log:
112 Intent logIntent = new Intent(this, LogActivity.class);
113 startActivity(logIntent);
114 return true;
115 case R.id.menu_settings:
116 Intent settingsIntent = new Intent(this, SettingsActivity.class);
117 startActivity(settingsIntent);
118 return true;
119 default:
120 return super.onOptionsItemSelected(item);
121 }
122 }
123
124 @Override
125 public void onVpnProfileSelected(VpnProfile profile)
126 {
127 Intent intent = new Intent(this, VpnProfileControlActivity.class);
128 intent.setAction(VpnProfileControlActivity.START_PROFILE);
129 intent.putExtra(VpnProfileControlActivity.EXTRA_VPN_PROFILE_ID, profile.getUUID().toString());
130 startActivity(intent);
131 }
132
133 /**
134 * Ask the user whether to clear the CRL cache.
135 */
136 private void clearCRLs()
137 {
138 final String FILE_PREFIX = "crl-";
139 ArrayList<String> list = new ArrayList<>();
140
141 for (String file : fileList())
142 {
143 if (file.startsWith(FILE_PREFIX))
144 {
145 list.add(file);
146 }
147 }
148 if (list.size() == 0)
149 {
150 Toast.makeText(this, R.string.clear_crl_cache_msg_none, Toast.LENGTH_SHORT).show();
151 return;
152 }
153 removeFragmentByTag(DIALOG_TAG);
154
155 Bundle args = new Bundle();
156 args.putStringArrayList(EXTRA_CRL_LIST, list);
157
158 CRLCacheDialog dialog = new CRLCacheDialog();
159 dialog.setArguments(args);
160 dialog.show(this.getSupportFragmentManager(), DIALOG_TAG);
161 }
162
163 /**
164 * Dismiss dialog if shown
165 */
166 public void removeFragmentByTag(String tag)
167 {
168 FragmentManager fm = getSupportFragmentManager();
169 Fragment login = fm.findFragmentByTag(tag);
170 if (login != null)
171 {
172 FragmentTransaction ft = fm.beginTransaction();
173 ft.remove(login);
174 ft.commit();
175 }
176 }
177
178 /**
179 * Confirmation dialog to clear CRL cache
180 */
181 public static class CRLCacheDialog extends AppCompatDialogFragment
182 {
183 @Override
184 public Dialog onCreateDialog(Bundle savedInstanceState)
185 {
186 final List<String> list = getArguments().getStringArrayList(EXTRA_CRL_LIST);
187 String size;
188 long s = 0;
189
190 for (String file : list)
191 {
192 File crl = getActivity().getFileStreamPath(file);
193 s += crl.length();
194 }
195 size = Formatter.formatFileSize(getActivity(), s);
196
197 AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
198 .setTitle(R.string.clear_crl_cache_title)
199 .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener()
200 {
201 @Override
202 public void onClick(DialogInterface dialog, int which)
203 {
204 dismiss();
205 }
206 })
207 .setPositiveButton(R.string.clear, new DialogInterface.OnClickListener()
208 {
209 @Override
210 public void onClick(DialogInterface dialog, int whichButton)
211 {
212 for (String file : list)
213 {
214 getActivity().deleteFile(file);
215 }
216 }
217 });
218 builder.setMessage(getActivity().getResources().getQuantityString(R.plurals.clear_crl_cache_msg, list.size(), list.size(), size));
219 return builder.create();
220 }
221 }
222 }