From ffe09908e498951de6ff7045a416285b4b5fe363 Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Wed, 9 Aug 2017 03:23:25 -0500 Subject: ProfileActivity: Extract base class for fixing fragments --- app/src/main/AndroidManifest.xml | 2 +- .../com/wireguard/android/ProfileActivity.java | 62 +++----------------- .../com/wireguard/android/ProfileListActivity.java | 68 ++++++++++++++++++++++ .../com/wireguard/android/ProfileListFragment.java | 2 +- app/src/main/res/layout/profile_activity.xml | 21 ------- app/src/main/res/layout/profile_list_activity.xml | 21 +++++++ 6 files changed, 99 insertions(+), 77 deletions(-) create mode 100644 app/src/main/java/com/wireguard/android/ProfileListActivity.java delete mode 100644 app/src/main/res/layout/profile_activity.xml create mode 100644 app/src/main/res/layout/profile_list_activity.xml (limited to 'app/src/main') diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e585fe14..2c02aa95 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,7 +12,7 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@android:style/Theme.Material.Light.DarkActionBar"> - + diff --git a/app/src/main/java/com/wireguard/android/ProfileActivity.java b/app/src/main/java/com/wireguard/android/ProfileActivity.java index 722f1dca..1d6ff994 100644 --- a/app/src/main/java/com/wireguard/android/ProfileActivity.java +++ b/app/src/main/java/com/wireguard/android/ProfileActivity.java @@ -1,38 +1,26 @@ package com.wireguard.android; -import android.app.Fragment; -import android.app.FragmentManager; -import android.app.FragmentTransaction; -import android.content.res.Configuration; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; /** - * Activity that allows creating/viewing/editing/deleting WireGuard profiles. + * Base class for activities that use ProfileListFragment and ProfileDetailFragment. */ -public class ProfileActivity extends ServiceClientActivity { +class ProfileActivity extends ServiceClientActivity { public static final String KEY_PROFILE_NAME = "profile_name"; - - // FIXME: These must match the constants in profile_list_activity.xml - private static final String TAG_DETAIL = "detail"; - private static final String TAG_LIST = "list"; + protected static final String TAG_DETAIL = "detail"; + protected static final String TAG_LIST = "list"; private String currentProfile; - private boolean isSplitLayout; public ProfileActivity() { super(ProfileService.class); } - @Override - public void onBackPressed() { - if (!isSplitLayout && currentProfile != null) { - onProfileSelected(null); - } else { - super.onBackPressed(); - } + protected String getCurrentProfile() { + return currentProfile; } @Override @@ -43,11 +31,6 @@ public class ProfileActivity extends ServiceClientActivity 0) { + fm.popBackStack(); + } else { + super.onBackPressed(); + } + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + // Set up the base layout and fill it with fragments. + setContentView(R.layout.profile_list_activity); + final int orientation = getResources().getConfiguration().orientation; + isSplitLayout = orientation == Configuration.ORIENTATION_LANDSCAPE; + updateLayout(getCurrentProfile()); + } + + public void onProfileSelected(String profile) { + updateLayout(profile); + setCurrentProfile(profile); + } + + private void updateLayout(String profile) { + final FragmentManager fm = getFragmentManager(); + final Fragment detailFragment = fm.findFragmentByTag(TAG_DETAIL); + final Fragment listFragment = fm.findFragmentByTag(TAG_LIST); + final FragmentTransaction transaction = fm.beginTransaction(); + if (profile != null) { + if (isSplitLayout) { + if (listFragment.isHidden()) + transaction.show(listFragment); + } else { + transaction.hide(listFragment); + } + if (detailFragment.isHidden()) + transaction.show(detailFragment); + } else { + if (isSplitLayout) { + if (detailFragment.isHidden()) + transaction.show(detailFragment); + } else { + transaction.hide(detailFragment); + } + if (listFragment.isHidden()) + transaction.show(listFragment); + } + transaction.commit(); + ((ProfileDetailFragment) detailFragment).setProfile(profile); + } +} diff --git a/app/src/main/java/com/wireguard/android/ProfileListFragment.java b/app/src/main/java/com/wireguard/android/ProfileListFragment.java index ed72b63b..7c9ecd4d 100644 --- a/app/src/main/java/com/wireguard/android/ProfileListFragment.java +++ b/app/src/main/java/com/wireguard/android/ProfileListFragment.java @@ -25,7 +25,7 @@ public class ProfileListFragment extends ServiceClientFragment parent, View view, int position, long id) { final Profile profile = (Profile) parent.getItemAtPosition(position); - ((ProfileActivity) getActivity()).onProfileSelected(profile.getName()); + ((ProfileListActivity) getActivity()).onProfileSelected(profile.getName()); } }); listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { diff --git a/app/src/main/res/layout/profile_activity.xml b/app/src/main/res/layout/profile_activity.xml deleted file mode 100644 index 2645552b..00000000 --- a/app/src/main/res/layout/profile_activity.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - diff --git a/app/src/main/res/layout/profile_list_activity.xml b/app/src/main/res/layout/profile_list_activity.xml new file mode 100644 index 00000000..2645552b --- /dev/null +++ b/app/src/main/res/layout/profile_list_activity.xml @@ -0,0 +1,21 @@ + + + + + + + -- cgit v1.2.3