android: Avoid deprecated tabs in the ActionBar in TrustedCertificatesActivity
[strongswan.git] / src / frontends / android / app / src / main / java / org / strongswan / android / ui / TrustedCertificatesActivity.java
index c817438..fd8a519 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012-2014 Tobias Brunner
+ * Copyright (C) 2012-2015 Tobias Brunner
  * Hochschule fuer Technik Rapperswil
  *
  * This program is free software; you can redistribute it and/or modify it
 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.support.design.widget.TabLayout;
 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.AppCompatActivity;
 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";
        private static final String DIALOG_TAG = "Dialog";
        private static final int IMPORT_CERTIFICATE = 0;
+       private TrustedCertificatesPagerAdapter mAdapter;
+       private ViewPager mPager;
        private boolean mSelect;
 
        @Override
@@ -50,40 +56,16 @@ public class TrustedCertificatesActivity extends AppCompatActivity implements Tr
 
                ActionBar actionBar = getSupportActionBar();
                actionBar.setDisplayHomeAsUpEnabled(true);
-               actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
-
-               TrustedCertificatesTabListener listener;
-               listener = new TrustedCertificatesTabListener(this, "system", TrustedCertificateSource.SYSTEM);
-               actionBar.addTab(actionBar
-                                                        .newTab()
-                                                        .setText(R.string.system_tab)
-                                                        .setTag(listener)
-                                                        .setTabListener(listener));
-               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());
-       }
 
-       @Override
-       protected void onSaveInstanceState(Bundle outState)
-       {
-               super.onSaveInstanceState(outState);
-               outState.putInt("tab", getSupportActionBar().getSelectedNavigationIndex());
+               mAdapter = new TrustedCertificatesPagerAdapter(getSupportFragmentManager(), this);
+
+               mPager = (ViewPager)findViewById(R.id.viewpager);
+               mPager.setAdapter(mAdapter);
+
+               TabLayout tabs = (TabLayout)findViewById(R.id.tabs);
+               tabs.setupWithViewPager(mPager);
+
+               mSelect = SELECT_CERTIFICATE.equals(getIntent().getAction());
        }
 
        @Override
@@ -148,18 +130,13 @@ public class TrustedCertificatesActivity extends AppCompatActivity implements Tr
                        setResult(Activity.RESULT_OK, intent);
                        finish();
                }
-               else
+               else if (mAdapter.getSource(mPager.getCurrentItem()) == TrustedCertificateSource.LOCAL)
                {
-                       TrustedCertificatesTabListener listener;
-                       listener = (TrustedCertificatesTabListener)getSupportActionBar().getSelectedTab().getTag();
-                       if (listener.mTag == "local")
-                       {
-                               Bundle args = new Bundle();
-                               args.putString(CertificateDeleteConfirmationDialog.ALIAS, selected.getAlias());
-                               CertificateDeleteConfirmationDialog dialog = new CertificateDeleteConfirmationDialog();
-                               dialog.setArguments(args);
-                               dialog.show(getSupportFragmentManager(), DIALOG_TAG);
-                       }
+                       Bundle args = new Bundle();
+                       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();
        }
 
-       public static class TrustedCertificatesTabListener implements ActionBar.TabListener
+       public static class TrustedCertificatesPagerAdapter extends FragmentPagerAdapter
        {
-               private final String mTag;
-               private final TrustedCertificateSource mSource;
-               private Fragment mFragment;
+               private TrustedCertificatesTab mTabs[];
 
-               public TrustedCertificatesTabListener(AppCompatActivity activity, String tag, TrustedCertificateSource source)
+               public TrustedCertificatesPagerAdapter(FragmentManager fm, Context context)
                {
-                       mTag = tag;
-                       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();
-                       }
+                       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 void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft)
+               public int getCount()
                {
-                       if (mFragment == null)
-                       {
-                               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);
-                       }
+                       return mTabs.length;
                }
 
                @Override
-               public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft)
+               public CharSequence getPageTitle(int position)
+               {
+                       return mTabs[position].getTitle();
+               }
+
+               public TrustedCertificateSource getSource(int position)
                {
-                       if (mFragment != null)
-                       {
-                               ft.detach(mFragment);
-                       }
+                       return mTabs[position].getSource();
                }
 
                @Override
-               public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft)
+               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;
+
+               public TrustedCertificatesTab(String title, TrustedCertificateSource source)
+               {
+                       mTitle = title;
+                       mSource = source;
+               }
+
+               public String getTitle()
+               {
+                       return mTitle;
+               }
+
+               public TrustedCertificateSource getSource()
                {
-                       /* nothing to be done */
+                       return mSource;
                }
        }
 }