/*
* Copyright (C) 2012-2015 Tobias Brunner
- * HSR Hochschule fuer Technik Rapperswil
+ *
+ * Copyright (C) secunet Security Networks AG
*
* 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
package org.strongswan.android.ui;
-import android.app.Activity;
-import android.content.Context;
import android.content.Intent;
-import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.google.android.material.tabs.TabLayout;
+import com.google.android.material.tabs.TabLayoutMediator;
import org.strongswan.android.R;
import org.strongswan.android.data.VpnProfileDataSource;
import java.security.KeyStore;
+import androidx.activity.result.ActivityResultLauncher;
+import androidx.activity.result.contract.ActivityResultContracts;
+import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
-import androidx.fragment.app.FragmentManager;
-import androidx.fragment.app.FragmentPagerAdapter;
-import androidx.viewpager.widget.ViewPager;
+import androidx.fragment.app.FragmentActivity;
+import androidx.viewpager2.adapter.FragmentStateAdapter;
+import androidx.viewpager2.widget.ViewPager2;
public class TrustedCertificatesActivity extends AppCompatActivity implements TrustedCertificateListFragment.OnTrustedCertificateSelectedListener, OnCertificateDeleteListener
{
public static final String SELECT_CERTIFICATE = "org.strongswan.android.action.SELECT_CERTIFICATE";
private static final String DIALOG_TAG = "Dialog";
- private static final int IMPORT_CERTIFICATE = 0;
private TrustedCertificatesPagerAdapter mAdapter;
- private ViewPager mPager;
+ private ViewPager2 mPager;
private boolean mSelect;
+ private final ActivityResultLauncher<Intent> mImportCertificate = registerForActivityResult(
+ new ActivityResultContracts.StartActivityForResult(),
+ result -> {
+ if (result.getResultCode() == RESULT_OK)
+ {
+ reloadCertificates();
+ }
+ }
+ );
+
@Override
public void onCreate(Bundle savedInstanceState)
{
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
- mAdapter = new TrustedCertificatesPagerAdapter(getSupportFragmentManager(), this);
+ mAdapter = new TrustedCertificatesPagerAdapter(this);
- mPager = (ViewPager)findViewById(R.id.viewpager);
+ mPager = findViewById(R.id.viewpager);
mPager.setAdapter(mAdapter);
- TabLayout tabs = (TabLayout)findViewById(R.id.tabs);
- tabs.setupWithViewPager(mPager);
+ TabLayout tabs = findViewById(R.id.tabs);
+ new TabLayoutMediator(tabs, mPager, (tab, position) -> {
+ tab.setText(mAdapter.getTitle(position));
+ }).attach();
mSelect = SELECT_CERTIFICATE.equals(getIntent().getAction());
}
return true;
}
- @Override
- public boolean onPrepareOptionsMenu(Menu menu)
- {
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT)
- {
- menu.removeItem(R.id.menu_import_certificate);
- }
- return true;
- }
-
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
return true;
case R.id.menu_import_certificate:
Intent intent = new Intent(this, TrustedCertificateImportActivity.class);
- startActivityForResult(intent, IMPORT_CERTIFICATE);
+ mImportCertificate.launch(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data)
- {
- switch (requestCode)
- {
- case IMPORT_CERTIFICATE:
- if (resultCode == Activity.RESULT_OK)
- {
- reloadCertificates();
- }
- return;
- }
- super.onActivityResult(requestCode, resultCode, data);
- }
-
@Override
public void onTrustedCertificateSelected(TrustedCertificateEntry selected)
{
/* 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);
+ setResult(RESULT_OK, intent);
finish();
}
else if (mAdapter.getSource(mPager.getCurrentItem()) == TrustedCertificateSource.LOCAL)
TrustedCertificateManager.getInstance().reset();
}
- public static class TrustedCertificatesPagerAdapter extends FragmentPagerAdapter
+ public static class TrustedCertificatesPagerAdapter extends FragmentStateAdapter
{
- private TrustedCertificatesTab mTabs[];
+ private final TrustedCertificatesTab[] mTabs;
- public TrustedCertificatesPagerAdapter(FragmentManager fm, Context context)
+ public TrustedCertificatesPagerAdapter(@NonNull FragmentActivity fragmentActivity)
{
- super(fm);
+ super(fragmentActivity);
mTabs = new TrustedCertificatesTab[]{
- new TrustedCertificatesTab(context.getString(R.string.system_tab), TrustedCertificateSource.SYSTEM),
- new TrustedCertificatesTab(context.getString(R.string.user_tab), TrustedCertificateSource.USER),
- new TrustedCertificatesTab(context.getString(R.string.local_tab), TrustedCertificateSource.LOCAL),
+ new TrustedCertificatesTab(fragmentActivity.getString(R.string.system_tab), TrustedCertificateSource.SYSTEM),
+ new TrustedCertificatesTab(fragmentActivity.getString(R.string.user_tab), TrustedCertificateSource.USER),
+ new TrustedCertificatesTab(fragmentActivity.getString(R.string.local_tab), TrustedCertificateSource.LOCAL),
};
}
- @Override
- public int getCount()
- {
- return mTabs.length;
- }
-
- @Override
- public CharSequence getPageTitle(int position)
+ public CharSequence getTitle(int position)
{
return mTabs[position].getTitle();
}
}
@Override
- public Fragment getItem(int position)
+ public int getItemCount()
+ {
+ return mTabs.length;
+ }
+
+ @NonNull
+ @Override
+ public Fragment createFragment(int position)
{
TrustedCertificateListFragment fragment = new TrustedCertificateListFragment();
Bundle args = new Bundle();