android: Avoid deprecated tabs in the ActionBar in TrustedCertificatesActivity

Instead we use TabLayout and ViewPager from the support libraries.
This commit is contained in:
Tobias Brunner
2016-04-27 14:24:26 +02:00
parent b9f79bc431
commit de2fbd0e3f
2 changed files with 91 additions and 90 deletions
@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2012-2014 Tobias Brunner * Copyright (C) 2012-2015 Tobias Brunner
* Hochschule fuer Technik Rapperswil * Hochschule fuer Technik Rapperswil
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
@@ -16,11 +16,15 @@
package org.strongswan.android.ui; package org.strongswan.android.ui;
import android.app.Activity; import android.app.Activity;
import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction; import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.view.Menu; import android.view.Menu;
@@ -40,6 +44,8 @@ public class TrustedCertificatesActivity extends AppCompatActivity implements Tr
public static final String SELECT_CERTIFICATE = "org.strongswan.android.action.SELECT_CERTIFICATE"; public static final String SELECT_CERTIFICATE = "org.strongswan.android.action.SELECT_CERTIFICATE";
private static final String DIALOG_TAG = "Dialog"; private static final String DIALOG_TAG = "Dialog";
private static final int IMPORT_CERTIFICATE = 0; private static final int IMPORT_CERTIFICATE = 0;
private TrustedCertificatesPagerAdapter mAdapter;
private ViewPager mPager;
private boolean mSelect; private boolean mSelect;
@Override @Override
@@ -50,42 +56,18 @@ public class TrustedCertificatesActivity extends AppCompatActivity implements Tr
ActionBar actionBar = getSupportActionBar(); ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true); actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
TrustedCertificatesTabListener listener; mAdapter = new TrustedCertificatesPagerAdapter(getSupportFragmentManager(), this);
listener = new TrustedCertificatesTabListener(this, "system", TrustedCertificateSource.SYSTEM);
actionBar.addTab(actionBar mPager = (ViewPager)findViewById(R.id.viewpager);
.newTab() mPager.setAdapter(mAdapter);
.setText(R.string.system_tab)
.setTag(listener) TabLayout tabs = (TabLayout)findViewById(R.id.tabs);
.setTabListener(listener)); tabs.setupWithViewPager(mPager);
listener = new TrustedCertificatesTabListener(this, "user", TrustedCertificateSource.USER);
actionBar.addTab(actionBar
.newTab()
.setText(R.string.user_tab)
.setTag(listener)
.setTabListener(listener));
listener = new TrustedCertificatesTabListener(this, "local", TrustedCertificateSource.LOCAL);
actionBar.addTab(actionBar
.newTab()
.setText(R.string.local_tab)
.setTag(listener)
.setTabListener(listener));
if (savedInstanceState != null)
{
actionBar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
}
mSelect = SELECT_CERTIFICATE.equals(getIntent().getAction()); mSelect = SELECT_CERTIFICATE.equals(getIntent().getAction());
} }
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putInt("tab", getSupportActionBar().getSelectedNavigationIndex());
}
@Override @Override
public boolean onCreateOptionsMenu(Menu menu) public boolean onCreateOptionsMenu(Menu menu)
{ {
@@ -148,18 +130,13 @@ public class TrustedCertificatesActivity extends AppCompatActivity implements Tr
setResult(Activity.RESULT_OK, intent); setResult(Activity.RESULT_OK, intent);
finish(); finish();
} }
else else if (mAdapter.getSource(mPager.getCurrentItem()) == TrustedCertificateSource.LOCAL)
{ {
TrustedCertificatesTabListener listener; Bundle args = new Bundle();
listener = (TrustedCertificatesTabListener)getSupportActionBar().getSelectedTab().getTag(); args.putString(CertificateDeleteConfirmationDialog.ALIAS, selected.getAlias());
if (listener.mTag == "local") CertificateDeleteConfirmationDialog dialog = new CertificateDeleteConfirmationDialog();
{ dialog.setArguments(args);
Bundle args = new Bundle(); dialog.show(getSupportFragmentManager(), DIALOG_TAG);
args.putString(CertificateDeleteConfirmationDialog.ALIAS, selected.getAlias());
CertificateDeleteConfirmationDialog dialog = new CertificateDeleteConfirmationDialog();
dialog.setArguments(args);
dialog.show(getSupportFragmentManager(), DIALOG_TAG);
}
} }
} }
@@ -184,58 +161,67 @@ public class TrustedCertificatesActivity extends AppCompatActivity implements Tr
TrustedCertificateManager.getInstance().reset(); TrustedCertificateManager.getInstance().reset();
} }
public static class TrustedCertificatesTabListener implements ActionBar.TabListener public static class TrustedCertificatesPagerAdapter extends FragmentPagerAdapter
{ {
private final String mTag; private TrustedCertificatesTab mTabs[];
public TrustedCertificatesPagerAdapter(FragmentManager fm, Context context)
{
super(fm);
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),
};
}
@Override
public int getCount()
{
return mTabs.length;
}
@Override
public CharSequence getPageTitle(int position)
{
return mTabs[position].getTitle();
}
public TrustedCertificateSource getSource(int position)
{
return mTabs[position].getSource();
}
@Override
public Fragment getItem(int position)
{
TrustedCertificateListFragment fragment = new TrustedCertificateListFragment();
Bundle args = new Bundle();
args.putSerializable(TrustedCertificateListFragment.EXTRA_CERTIFICATE_SOURCE, mTabs[position].getSource());
fragment.setArguments(args);
return fragment;
}
}
public static class TrustedCertificatesTab
{
private final String mTitle;
private final TrustedCertificateSource mSource; private final TrustedCertificateSource mSource;
private Fragment mFragment;
public TrustedCertificatesTabListener(AppCompatActivity activity, String tag, TrustedCertificateSource source) public TrustedCertificatesTab(String title, TrustedCertificateSource source)
{ {
mTag = tag; mTitle = title;
mSource = source; mSource = source;
/* 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.getSupportFragmentManager().findFragmentByTag(mTag);
if (mFragment != null && !mFragment.isDetached())
{
FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
ft.detach(mFragment);
ft.commit();
}
} }
@Override public String getTitle()
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft)
{ {
if (mFragment == null) return mTitle;
{
mFragment = new TrustedCertificateListFragment();
Bundle args = new Bundle();
args.putSerializable(TrustedCertificateListFragment.EXTRA_CERTIFICATE_SOURCE, mSource);
mFragment.setArguments(args);
ft.add(android.R.id.content, mFragment, mTag);
}
else
{
ft.attach(mFragment);
}
} }
@Override public TrustedCertificateSource getSource()
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft)
{ {
if (mFragment != null) return mSource;
{
ft.detach(mFragment);
}
}
@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft)
{
/* nothing to be done */
} }
} }
} }
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!-- <!--
Copyright (C) 2012 Tobias Brunner Copyright (C) 2012-2015 Tobias Brunner
Hochschule fuer Technik Rapperswil Hochschule fuer Technik Rapperswil
This program is free software; you can redistribute it and/or modify it This program is free software; you can redistribute it and/or modify it
@@ -13,8 +13,23 @@
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details. for more details.
--> -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent" > android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</FrameLayout> <android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>