From 2e3daa89139652e0af1ec011ac1da6105a1d871e 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 Signed-off-by: Jason A. Donenfeld --- .../com/wireguard/android/ProfileActivity.java | 62 +++----------------- .../com/wireguard/android/ProfileListActivity.java | 68 ++++++++++++++++++++++ .../com/wireguard/android/ProfileListFragment.java | 2 +- 3 files changed, 77 insertions(+), 55 deletions(-) create mode 100644 app/src/main/java/com/wireguard/android/ProfileListActivity.java (limited to 'app/src/main/java/com/wireguard/android') 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() { -- cgit v1.2.3