diff options
author | Samuel Holland <samuel@sholland.org> | 2017-08-09 03:23:25 -0500 |
---|---|---|
committer | Samuel Holland <samuel@sholland.org> | 2017-08-09 03:23:25 -0500 |
commit | 2e3daa89139652e0af1ec011ac1da6105a1d871e (patch) | |
tree | fdc400159b84328742079c8a03c4d99fc934e602 /app/src/main/java/com/wireguard | |
parent | 529e320e3fa20a70f8918fa5d7e559438d5c071e (diff) |
ProfileActivity: Extract base class for fixing fragments
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main/java/com/wireguard')
3 files changed, 77 insertions, 55 deletions
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<ProfileServiceInterface> { +class ProfileActivity extends ServiceClientActivity<ProfileServiceInterface> { 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<ProfileServiceInterfa currentProfile = savedInstanceState.getString(KEY_PROFILE_NAME); else currentProfile = getIntent().getStringExtra(KEY_PROFILE_NAME); - // Set up the base layout and fill it with fragments. - setContentView(R.layout.profile_activity); - final int orientation = getResources().getConfiguration().orientation; - isSplitLayout = orientation == Configuration.ORIENTATION_LANDSCAPE; - updateLayout(currentProfile); } @Override @@ -68,42 +51,13 @@ public class ProfileActivity extends ServiceClientActivity<ProfileServiceInterfa } - public void onProfileSelected(String profile) { - updateLayout(profile); - currentProfile = profile; - } - @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString(KEY_PROFILE_NAME, currentProfile); } - 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); + protected void setCurrentProfile(String profile) { + currentProfile = profile; } } diff --git a/app/src/main/java/com/wireguard/android/ProfileListActivity.java b/app/src/main/java/com/wireguard/android/ProfileListActivity.java new file mode 100644 index 00000000..f7199645 --- /dev/null +++ b/app/src/main/java/com/wireguard/android/ProfileListActivity.java @@ -0,0 +1,68 @@ +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; + +/** + * Activity that allows creating/viewing/editing/deleting WireGuard profiles. + */ + +public class ProfileListActivity extends ProfileActivity { + private boolean isSplitLayout; + + @Override + public void onBackPressed() { + final FragmentManager fm = getFragmentManager(); + if (fm.getBackStackEntryCount() > 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<ProfileServiceInt @Override public void onItemClick(AdapterView<?> 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() { |