0
0
mirror of https://github.com/schwabe/ics-openvpn.git synced 2024-09-19 19:42:29 +02:00

Move to viewpager2

This commit is contained in:
Arne Schwabe 2024-07-26 15:26:17 +02:00
parent aae9561392
commit 6b0a2e59b2
8 changed files with 294 additions and 317 deletions

View File

@ -13,6 +13,7 @@ androidx-security-crypto = "1.1.0-alpha06"
androidx-constraintlayout = "2.1.4"
androidx-cardview = "1.0.0"
androidx-recyclerview = "1.3.2"
androidx-viewpager2 = "1.1.0"
bouncycastle = "1.69"
mpandroidchart = "v3.1.0"
kotlin = "1.9.20"
@ -29,6 +30,7 @@ android-view-material = { group = "com.google.android.material", name = "materia
androidx-annotation = { group = "androidx.annotation", name = "annotation", version.ref = "androidx-annotation" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "androidx-appcompat" }
androidx-cardview = { group = "androidx.cardview", name = "cardview", version.ref = "androidx-cardview" }
androidx-viewpager2 = { group = "androidx.viewpager2", name = "viewpager2", version.ref= "androidx-viewpager2" }
androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "androidx-constraintlayout" }
androidx-core-ktx = { group = "androidx.core", name = "core", version.ref = "androidx-core-ktx" }
androidx-fragment-ktx = { group = "androidx.fragment", name = "fragment-ktx", version.ref = "androidx-fragment-ktx" }

View File

@ -237,6 +237,7 @@ dependencies {
uiImplementation(libs.android.view.material)
uiImplementation(libs.androidx.appcompat)
uiImplementation(libs.androidx.cardview)
uiImplementation(libs.androidx.viewpager2)
uiImplementation(libs.androidx.constraintlayout)
uiImplementation(libs.androidx.core.ktx)
uiImplementation(libs.androidx.fragment.ktx)

View File

@ -11,23 +11,26 @@ import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.viewpager.widget.ViewPager
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import de.blinkt.openvpn.R
import de.blinkt.openvpn.fragments.*
import de.blinkt.openvpn.fragments.ImportRemoteConfig.Companion.newInstance
import de.blinkt.openvpn.views.ScreenSlidePagerAdapter
class MainActivity : BaseActivity() {
private lateinit var mPager: ViewPager
private lateinit var mPager: ViewPager2
private lateinit var mPagerAdapter: ScreenSlidePagerAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
// Instantiate a ViewPager and a PagerAdapter.
mPager = findViewById(R.id.pager)
mPagerAdapter = ScreenSlidePagerAdapter(supportFragmentManager, this)
val tablayout: TabLayout = findViewById(R.id.tab_layout)
mPagerAdapter = ScreenSlidePagerAdapter(supportFragmentManager, lifecycle, this)
/* Toolbar and slider should have the same elevation */disableToolbarElevation()
mPagerAdapter.addTab(R.string.vpn_list_title, VPNProfileList::class.java)
@ -41,6 +44,11 @@ class MainActivity : BaseActivity() {
mPagerAdapter.addTab(R.string.openvpn_log, LogFragment::class.java)
mPagerAdapter.addTab(R.string.about, AboutFragment::class.java)
mPager.setAdapter(mPagerAdapter)
TabLayoutMediator(tablayout, mPager) { tab, position ->
tab.text = mPagerAdapter.getPageTitle(position)
}.attach()
}
private fun disableToolbarElevation() {

View File

@ -1,220 +0,0 @@
/*
* Copyright (c) 2012-2016 Arne Schwabe
* Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt
*/
package de.blinkt.openvpn.activities;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import androidx.appcompat.app.ActionBar;
import androidx.viewpager.widget.ViewPager;
import de.blinkt.openvpn.R;
import de.blinkt.openvpn.VpnProfile;
import de.blinkt.openvpn.core.ProfileManager;
import de.blinkt.openvpn.fragments.Settings_Allowed_Apps;
import de.blinkt.openvpn.fragments.Settings_Authentication;
import de.blinkt.openvpn.fragments.Settings_Basic;
import de.blinkt.openvpn.fragments.Settings_Connections;
import de.blinkt.openvpn.fragments.Settings_IP;
import de.blinkt.openvpn.fragments.Settings_Obscure;
import de.blinkt.openvpn.fragments.Settings_Routing;
import de.blinkt.openvpn.fragments.Settings_UserEditable;
import de.blinkt.openvpn.fragments.ShowConfigFragment;
import de.blinkt.openvpn.fragments.VPNProfileList;
import de.blinkt.openvpn.views.ScreenSlidePagerAdapter;
public class VPNPreferences extends BaseActivity {
static final Class[] validFragments = new Class[]{
Settings_Authentication.class, Settings_Basic.class, Settings_IP.class,
Settings_Obscure.class, Settings_Routing.class, ShowConfigFragment.class,
Settings_Connections.class, Settings_Allowed_Apps.class,
};
private String mProfileUUID;
private VpnProfile mProfile;
private ViewPager mPager;
private ScreenSlidePagerAdapter mPagerAdapter;
public VPNPreferences() {
super();
}
@TargetApi(Build.VERSION_CODES.KITKAT)
protected boolean isValidFragment(String fragmentName) {
for (Class c: validFragments)
if (c.getName().equals(fragmentName))
return true;
return false;
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString(getIntent().getStringExtra(getPackageName() + ".profileUUID"),mProfileUUID);
super.onSaveInstanceState(outState);
}
@Override
protected void onResume() {
super.onResume();
getProfile();
// When a profile is deleted from a category fragment in hadset mod we need to finish
// this activity as well when returning
if (mProfile==null || mProfile.profileDeleted) {
setResult(VPNProfileList.RESULT_VPN_DELETED);
finish();
}
if (mProfile.mTemporaryProfile)
{
Toast.makeText(this, "Temporary profiles cannot be edited", Toast.LENGTH_LONG).show();
finish();
}
}
private void getProfile() {
Intent intent = getIntent();
if(intent!=null) {
String profileUUID = intent.getStringExtra(getPackageName() + ".profileUUID");
if(profileUUID==null) {
Bundle initialArguments = getIntent().getBundleExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS);
profileUUID = initialArguments.getString(getPackageName() + ".profileUUID");
}
if(profileUUID!=null){
mProfileUUID = profileUUID;
mProfile = ProfileManager.get(this, mProfileUUID);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
mProfileUUID = getIntent().getStringExtra(getPackageName() + ".profileUUID");
if(savedInstanceState!=null){
String savedUUID = savedInstanceState.getString(getPackageName() + ".profileUUID");
if(savedUUID!=null)
mProfileUUID=savedUUID;
}
super.onCreate(savedInstanceState);
mProfile = ProfileManager.get(this,mProfileUUID);
if(mProfile==null) {
Toast.makeText(this, "Profile to edit cannot be found.", Toast.LENGTH_LONG).show();
finish();
return;
}
setTitle(getString(R.string.edit_profile_title, mProfile.getName()));
setContentView(R.layout.main_activity);
disableToolbarElevation();
// Instantiate a ViewPager and a PagerAdapter.
mPager = findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager(), this);
Bundle fragmentArguments = new Bundle();
fragmentArguments.putString(getPackageName() + ".profileUUID",mProfileUUID);
mPagerAdapter.setFragmentArgs(fragmentArguments);
if (mProfile.mUserEditable) {
mPagerAdapter.addTab(R.string.basic, Settings_Basic.class);
mPagerAdapter.addTab(R.string.server_list, Settings_Connections.class);
mPagerAdapter.addTab(R.string.ipdns, Settings_IP.class);
mPagerAdapter.addTab(R.string.routing, Settings_Routing.class);
mPagerAdapter.addTab(R.string.settings_auth, Settings_Authentication.class);
mPagerAdapter.addTab(R.string.advanced, Settings_Obscure.class);
} else {
mPagerAdapter.addTab(R.string.basic, Settings_UserEditable.class);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mPagerAdapter.addTab(R.string.vpn_allowed_apps, Settings_Allowed_Apps.class);
}
mPagerAdapter.addTab(R.string.generated_config, ShowConfigFragment.class);
mPager.setAdapter(mPagerAdapter);
//TabBarView tabs = (TabBarView) findViewById(R.id.sliding_tabs);
//tabs.setViewPager(mPager);
}
@Override
public void onBackPressed() {
setResult(RESULT_OK, getIntent());
super.onBackPressed();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.remove_vpn)
askProfileRemoval();
if (item.getItemId() == R.id.duplicate_vpn) {
Intent data = new Intent();
data.putExtra(VpnProfile.EXTRA_PROFILEUUID, mProfileUUID);
setResult(VPNProfileList.RESULT_VPN_DUPLICATE, data);
finish();
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.vpnpreferences_menu, menu);
return super.onCreateOptionsMenu(menu);
}
private void askProfileRemoval() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Confirm deletion");
dialog.setMessage(getString(R.string.remove_vpn_query, mProfile.mName));
dialog.setPositiveButton(android.R.string.yes,
(dialog1, which) -> removeProfile(mProfile));
dialog.setNegativeButton(android.R.string.no,null);
dialog.create().show();
}
protected void removeProfile(VpnProfile profile) {
ProfileManager.getInstance(this).removeProfile(this,profile);
setResult(VPNProfileList.RESULT_VPN_DELETED);
finish();
}
private void disableToolbarElevation() {
ActionBar toolbar = getSupportActionBar();
toolbar.setElevation(0);
}
}

View File

@ -0,0 +1,208 @@
/*
* Copyright (c) 2012-2016 Arne Schwabe
* Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt
*/
package de.blinkt.openvpn.activities
import android.annotation.TargetApi
import android.app.AlertDialog
import android.content.DialogInterface
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.preference.PreferenceActivity
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import de.blinkt.openvpn.R
import de.blinkt.openvpn.VpnProfile
import de.blinkt.openvpn.core.ProfileManager
import de.blinkt.openvpn.fragments.Settings_Allowed_Apps
import de.blinkt.openvpn.fragments.Settings_Authentication
import de.blinkt.openvpn.fragments.Settings_Basic
import de.blinkt.openvpn.fragments.Settings_Connections
import de.blinkt.openvpn.fragments.Settings_IP
import de.blinkt.openvpn.fragments.Settings_Obscure
import de.blinkt.openvpn.fragments.Settings_Routing
import de.blinkt.openvpn.fragments.Settings_UserEditable
import de.blinkt.openvpn.fragments.ShowConfigFragment
import de.blinkt.openvpn.fragments.VPNProfileList
import de.blinkt.openvpn.views.ScreenSlidePagerAdapter
class VPNPreferences : BaseActivity() {
private var mProfileUUID: String? = null
private var mProfile: VpnProfile? = null
private lateinit var mPager: ViewPager2
private lateinit var mPagerAdapter: ScreenSlidePagerAdapter
@TargetApi(Build.VERSION_CODES.KITKAT)
protected fun isValidFragment(fragmentName: String): Boolean {
for (c in validFragments) if (c.name == fragmentName) return true
return false
}
override fun onStop() {
super.onStop()
}
override fun onSaveInstanceState(outState: Bundle) {
outState.putString(intent.getStringExtra("$packageName.profileUUID"), mProfileUUID)
super.onSaveInstanceState(outState)
}
override fun onResume() {
super.onResume()
profile
// When a profile is deleted from a category fragment in hadset mod we need to finish
// this activity as well when returning
if (mProfile == null || mProfile!!.profileDeleted) {
setResult(VPNProfileList.RESULT_VPN_DELETED)
finish()
}
if (mProfile!!.mTemporaryProfile) {
Toast.makeText(this, "Temporary profiles cannot be edited", Toast.LENGTH_LONG).show()
finish()
}
}
private val profile: Unit
get() {
val intent = intent
if (intent != null) {
var profileUUID = intent.getStringExtra("$packageName.profileUUID")
if (profileUUID == null) {
val initialArguments =
getIntent().getBundleExtra(PreferenceActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS)
profileUUID = initialArguments!!.getString("$packageName.profileUUID")
}
if (profileUUID != null) {
mProfileUUID = profileUUID
mProfile = ProfileManager.get(this, mProfileUUID)
}
}
}
override fun onCreate(savedInstanceState: Bundle?) {
mProfileUUID = intent.getStringExtra("$packageName.profileUUID")
if (savedInstanceState != null) {
val savedUUID = savedInstanceState.getString("$packageName.profileUUID")
if (savedUUID != null) mProfileUUID = savedUUID
}
super.onCreate(savedInstanceState)
mProfile = ProfileManager.get(this, mProfileUUID)
if (mProfile == null) {
Toast.makeText(this, "Profile to edit cannot be found.", Toast.LENGTH_LONG).show()
finish()
return
}
title = getString(R.string.edit_profile_title, mProfile!!.name)
setContentView(R.layout.main_activity)
disableToolbarElevation()
// Instantiate a ViewPager and a PagerAdapter.
mPager = findViewById(R.id.pager)
val tablayout: TabLayout = findViewById(R.id.tab_layout)
mPagerAdapter = ScreenSlidePagerAdapter(supportFragmentManager, lifecycle, this)
val fragmentArguments = Bundle()
fragmentArguments.putString("$packageName.profileUUID", mProfileUUID)
mPagerAdapter.setFragmentArgs(fragmentArguments)
if (mProfile!!.mUserEditable) {
mPagerAdapter.addTab(R.string.basic, Settings_Basic::class.java)
mPagerAdapter.addTab(R.string.server_list, Settings_Connections::class.java)
mPagerAdapter.addTab(R.string.ipdns, Settings_IP::class.java)
mPagerAdapter.addTab(R.string.routing, Settings_Routing::class.java)
mPagerAdapter.addTab(R.string.settings_auth, Settings_Authentication::class.java)
mPagerAdapter.addTab(R.string.advanced, Settings_Obscure::class.java)
} else {
mPagerAdapter.addTab(R.string.basic, Settings_UserEditable::class.java)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mPagerAdapter.addTab(R.string.vpn_allowed_apps, Settings_Allowed_Apps::class.java)
}
mPagerAdapter.addTab(R.string.generated_config, ShowConfigFragment::class.java)
mPager.setAdapter(mPagerAdapter)
//TabBarView tabs = (TabBarView) findViewById(R.id.sliding_tabs);
//tabs.setViewPager(mPager);
TabLayoutMediator(tablayout, mPager) { tab, position ->
tab.text = mPagerAdapter.getPageTitle(position)
}.attach()
}
override fun onBackPressed() {
setResult(RESULT_OK, intent)
super.onBackPressed()
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (item.itemId == R.id.remove_vpn) askProfileRemoval()
if (item.itemId == R.id.duplicate_vpn) {
val data = Intent()
data.putExtra(VpnProfile.EXTRA_PROFILEUUID, mProfileUUID)
setResult(VPNProfileList.RESULT_VPN_DUPLICATE, data)
finish()
}
return super.onOptionsItemSelected(item)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.vpnpreferences_menu, menu)
return super.onCreateOptionsMenu(menu)
}
private fun askProfileRemoval() {
val dialog = AlertDialog.Builder(this)
dialog.setTitle("Confirm deletion")
dialog.setMessage(getString(R.string.remove_vpn_query, mProfile!!.mName))
dialog.setPositiveButton(
android.R.string.yes
) { dialog1: DialogInterface?, which: Int -> removeProfile(mProfile) }
dialog.setNegativeButton(android.R.string.no, null)
dialog.create().show()
}
protected fun removeProfile(profile: VpnProfile?) {
ProfileManager.getInstance(this).removeProfile(this, profile)
setResult(VPNProfileList.RESULT_VPN_DELETED)
finish()
}
private fun disableToolbarElevation() {
val toolbar = supportActionBar
toolbar!!.elevation = 0f
}
companion object {
val validFragments: Array<Class<*>> = arrayOf(
Settings_Authentication::class.java,
Settings_Basic::class.java,
Settings_IP::class.java,
Settings_Obscure::class.java,
Settings_Routing::class.java,
ShowConfigFragment::class.java,
Settings_Connections::class.java,
Settings_Allowed_Apps::class.java,
)
}
}

View File

@ -1,80 +0,0 @@
/*
* Copyright (c) 2012-2016 Arne Schwabe
* Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt
*/
package de.blinkt.openvpn.views;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import java.util.Vector;
/**
* Created by arne on 18.11.14.
*/
public class ScreenSlidePagerAdapter extends FragmentPagerAdapter {
private final Resources res;
private Bundle mFragmentArguments;
public void setFragmentArgs(Bundle fragmentArguments) {
mFragmentArguments = fragmentArguments;
}
static class Tab {
public Class<? extends Fragment> fragmentClass;
String mName;
public Tab(Class<? extends Fragment> fClass, String name){
mName = name;
fragmentClass = fClass;
}
}
private Vector<Tab> mTabs = new Vector<Tab>();
public ScreenSlidePagerAdapter(FragmentManager fm, Context c) {
super(fm);
res = c.getResources();
}
@NonNull
@Override
public Fragment getItem(int position) {
try {
Fragment fragment = mTabs.get(position).fragmentClass.newInstance();
if (mFragmentArguments!=null)
fragment.setArguments(mFragmentArguments);
return fragment;
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
@Override
public CharSequence getPageTitle(int position) {
return mTabs.get(position).mName;
}
@Override
public int getCount() {
return mTabs.size();
}
public void addTab(@StringRes int name, Class<? extends Fragment> fragmentClass) {
mTabs.add(new Tab(fragmentClass, res.getString(name)));
}
}

View File

@ -0,0 +1,59 @@
/*
* Copyright (c) 2012-2016 Arne Schwabe
* Distributed under the GNU GPL v2 with additional terms. For full terms see the file doc/LICENSE.txt
*/
package de.blinkt.openvpn.views
import android.content.Context
import android.content.res.Resources
import android.os.Bundle
import androidx.annotation.StringRes
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.viewpager2.adapter.FragmentStateAdapter
import java.util.Vector
/**
* Created by arne on 18.11.14.
*/
class ScreenSlidePagerAdapter(fm: FragmentManager, lc: Lifecycle, c: Context) :
FragmentStateAdapter(
fm, lc
) {
private val res: Resources = c.resources
private var mFragmentArguments: Bundle? = null
fun setFragmentArgs(fragmentArguments: Bundle?) {
mFragmentArguments = fragmentArguments
}
internal class Tab(var fragmentClass: Class<out Fragment>, var mName: String)
private val mTabs = Vector<Tab>()
override fun createFragment(position: Int): Fragment {
try {
val fragment = mTabs[position].fragmentClass.newInstance()
if (mFragmentArguments != null) fragment.arguments = mFragmentArguments
return fragment
} catch (e: InstantiationException) {
e.printStackTrace()
} catch (e: IllegalAccessException) {
e.printStackTrace()
}
throw IndexOutOfBoundsException("index wrong")
}
fun getPageTitle(position: Int): CharSequence {
return mTabs[position].mName
}
override fun getItemCount(): Int {
return mTabs.size
}
fun addTab(@StringRes name: Int, fragmentClass: Class<out Fragment>) {
mTabs.add(Tab(fragmentClass, res.getString(name)))
}
}

View File

@ -9,23 +9,22 @@
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
style="@style/blinkt.tabLayout"
app:tabMaxWidth="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
app:tabMode="scrollable"
<androidx.viewpager.widget.ViewPager
/>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="0dp"
android:layout_weight="3"/>
<com.google.android.material.tabs.TabLayout
android:id="@+id/tab_layout"
style="@style/blinkt.tabLayout"
app:tabMaxWidth="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
app:tabMode="scrollable"
/>
</androidx.viewpager.widget.ViewPager>
</LinearLayout>