diff options
author | Samuel Holland <samuel@sholland.org> | 2017-08-09 05:12:00 -0500 |
---|---|---|
committer | Samuel Holland <samuel@sholland.org> | 2017-08-09 05:12:00 -0500 |
commit | 52cdf3e7e5fb4292d1dfa99ba123701ca7959092 (patch) | |
tree | 9ce3ef9fc6d6865429789423deb1079f739461f2 | |
parent | 2e3daa89139652e0af1ec011ac1da6105a1d871e (diff) |
ProfileActivity: Replace fragments instead of hiding
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
11 files changed, 151 insertions, 108 deletions
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2c02aa95..2544e546 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -12,6 +12,10 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@android:style/Theme.Material.Light.DarkActionBar"> + <activity + android:name=".ProfileDetailActivity" + android:label="" + android:parentActivityName=".ProfileListActivity" /> <activity android:name=".ProfileListActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> diff --git a/app/src/main/java/com/wireguard/android/PlaceholderFragment.java b/app/src/main/java/com/wireguard/android/PlaceholderFragment.java new file mode 100644 index 00000000..e17aac03 --- /dev/null +++ b/app/src/main/java/com/wireguard/android/PlaceholderFragment.java @@ -0,0 +1,18 @@ +package com.wireguard.android; + +import android.app.Fragment; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +/** + * Fragment that shows a placeholder message. + */ + +public class PlaceholderFragment extends Fragment { + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { + return inflater.inflate(R.layout.placeholder_fragment, parent, false); + } +} diff --git a/app/src/main/java/com/wireguard/android/ProfileActivity.java b/app/src/main/java/com/wireguard/android/ProfileActivity.java index 1d6ff994..87cc6aa0 100644 --- a/app/src/main/java/com/wireguard/android/ProfileActivity.java +++ b/app/src/main/java/com/wireguard/android/ProfileActivity.java @@ -8,10 +8,11 @@ import android.view.MenuItem; * Base class for activities that use ProfileListFragment and ProfileDetailFragment. */ -class ProfileActivity extends ServiceClientActivity<ProfileServiceInterface> { +abstract class ProfileActivity extends ServiceClientActivity<ProfileServiceInterface> { public static final String KEY_PROFILE_NAME = "profile_name"; protected static final String TAG_DETAIL = "detail"; protected static final String TAG_LIST = "list"; + protected static final String TAG_PLACEHOLDER = "placeholder"; private String currentProfile; diff --git a/app/src/main/java/com/wireguard/android/ProfileDetailActivity.java b/app/src/main/java/com/wireguard/android/ProfileDetailActivity.java new file mode 100644 index 00000000..1e178c71 --- /dev/null +++ b/app/src/main/java/com/wireguard/android/ProfileDetailActivity.java @@ -0,0 +1,19 @@ +package com.wireguard.android; + +import android.app.Fragment; +import android.os.Bundle; + +/** + * Activity that allows viewing information about a single WireGuard profile. + */ + +public class ProfileDetailActivity extends ProfileActivity { + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.profile_detail_activity); + setTitle(getCurrentProfile()); + Fragment detailFragment = getFragmentManager().findFragmentByTag(TAG_DETAIL); + ((ProfileDetailFragment) detailFragment).setProfile(getCurrentProfile()); + } +} diff --git a/app/src/main/java/com/wireguard/android/ProfileListActivity.java b/app/src/main/java/com/wireguard/android/ProfileListActivity.java index f7199645..60c35462 100644 --- a/app/src/main/java/com/wireguard/android/ProfileListActivity.java +++ b/app/src/main/java/com/wireguard/android/ProfileListActivity.java @@ -1,9 +1,8 @@ package com.wireguard.android; import android.app.Fragment; -import android.app.FragmentManager; import android.app.FragmentTransaction; -import android.content.res.Configuration; +import android.content.Intent; import android.os.Bundle; /** @@ -14,55 +13,54 @@ 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()); + isSplitLayout = findViewById(R.id.fragment_container) != null; + if (!isSplitLayout) { + // Avoid ProfileDetailFragment adding its menu when it is not in the view hierarchy. + final Fragment fragment = getFragmentManager().findFragmentByTag(TAG_DETAIL); + if (fragment != null) { + final FragmentTransaction transaction = getFragmentManager().beginTransaction(); + transaction.remove(fragment); + transaction.commit(); + } + } + onProfileSelected(getCurrentProfile()); } public void onProfileSelected(String profile) { - updateLayout(profile); - setCurrentProfile(profile); + if (isSplitLayout) { + updateLayout(profile); + setCurrentProfile(profile); + } else if (profile != null) { + final Intent intent = new Intent(this, ProfileDetailActivity.class); + intent.putExtra(KEY_PROFILE_NAME, profile); + startActivity(intent); + setCurrentProfile(null); + } } - 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(); + public void updateLayout(String profile) { + final Fragment fragment = getFragmentManager().findFragmentById(R.id.fragment_container); if (profile != null) { - if (isSplitLayout) { - if (listFragment.isHidden()) - transaction.show(listFragment); + if (fragment instanceof ProfileDetailFragment) { + final ProfileDetailFragment detailFragment = (ProfileDetailFragment) fragment; + detailFragment.setProfile(profile); } else { - transaction.hide(listFragment); + final ProfileDetailFragment detailFragment = new ProfileDetailFragment(); + detailFragment.setProfile(profile); + final FragmentTransaction transaction = getFragmentManager().beginTransaction(); + transaction.replace(R.id.fragment_container, detailFragment, TAG_DETAIL); + transaction.commit(); } - if (detailFragment.isHidden()) - transaction.show(detailFragment); } else { - if (isSplitLayout) { - if (detailFragment.isHidden()) - transaction.show(detailFragment); - } else { - transaction.hide(detailFragment); + if (!(fragment instanceof PlaceholderFragment)) { + final PlaceholderFragment placeholderFragment = new PlaceholderFragment(); + final FragmentTransaction transaction = getFragmentManager().beginTransaction(); + transaction.replace(R.id.fragment_container, placeholderFragment, TAG_PLACEHOLDER); + transaction.commit(); } - if (listFragment.isHidden()) - transaction.show(listFragment); } - transaction.commit(); - ((ProfileDetailFragment) detailFragment).setProfile(profile); } } diff --git a/app/src/main/res/layout-land/profile_list_activity.xml b/app/src/main/res/layout-land/profile_list_activity.xml new file mode 100644 index 00000000..dc7b5c3b --- /dev/null +++ b/app/src/main/res/layout-land/profile_list_activity.xml @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:baselineAligned="false" + android:orientation="horizontal"> + + <fragment + android:name="com.wireguard.android.ProfileListFragment" + android:layout_width="0dp" + android:layout_height="match_parent" + android:layout_weight="1" + android:tag="list" /> + + <FrameLayout + android:id="@+id/fragment_container" + android:layout_width="0dp" + android:layout_height="match_parent" + android:layout_weight="2" /> +</LinearLayout> diff --git a/app/src/main/res/layout/placeholder_fragment.xml b/app/src/main/res/layout/placeholder_fragment.xml new file mode 100644 index 00000000..bb89ef1a --- /dev/null +++ b/app/src/main/res/layout/placeholder_fragment.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="utf-8"?> +<TextView xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:gravity="center" + android:text="@string/placeholder_text" /> diff --git a/app/src/main/res/layout/profile_detail_activity.xml b/app/src/main/res/layout/profile_detail_activity.xml new file mode 100644 index 00000000..0be8d4bd --- /dev/null +++ b/app/src/main/res/layout/profile_detail_activity.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="utf-8"?> +<fragment xmlns:android="http://schemas.android.com/apk/res/android" + android:name="com.wireguard.android.ProfileDetailFragment" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:tag="detail" /> diff --git a/app/src/main/res/layout/profile_detail_fragment.xml b/app/src/main/res/layout/profile_detail_fragment.xml index 248f11a1..c08236ce 100644 --- a/app/src/main/res/layout/profile_detail_fragment.xml +++ b/app/src/main/res/layout/profile_detail_fragment.xml @@ -4,68 +4,53 @@ <data> - <import type="android.view.View" /> - <variable name="profile" type="com.wireguard.config.Profile" /> </data> - <FrameLayout + <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp"> - <TextView - android:layout_width="match_parent" - android:layout_height="match_parent" - android:gravity="center" - android:text="@string/placeholder_text" - android:visibility="@{profile == null ? View.VISIBLE : View.GONE}" /> - - <ScrollView + <RelativeLayout android:layout_width="match_parent" - android:layout_height="match_parent" - android:visibility="@{profile == null ? View.GONE : View.VISIBLE}"> + android:layout_height="wrap_content"> - <RelativeLayout + <TextView + android:id="@+id/profile_name_label" android:layout_width="match_parent" - android:layout_height="wrap_content"> - - <TextView - android:id="@+id/profile_name_label" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:layout_alignParentTop="true" - android:labelFor="@+id/profile_name_text" - android:text="@string/profile_name" /> - - <TextView - android:id="@+id/profile_name_text" - style="?android:attr/textAppearanceMedium" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:layout_below="@+id/profile_name_label" - android:text="@{profile.name}" /> - - <TextView - android:id="@+id/public_key_label" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:layout_below="@+id/profile_name_text" - android:labelFor="@+id/public_key_text" - android:text="@string/public_key" /> + android:layout_height="wrap_content" + android:layout_alignParentTop="true" + android:labelFor="@+id/profile_name_text" + android:text="@string/profile_name" /> + + <TextView + android:id="@+id/profile_name_text" + style="?android:attr/textAppearanceMedium" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_below="@+id/profile_name_label" + android:text="@{profile.name}" /> - <TextView - android:id="@+id/public_key_text" - style="?android:attr/textAppearanceMedium" - android:layout_width="match_parent" - android:layout_height="wrap_content" - android:layout_below="@+id/public_key_label" - android:ellipsize="end" - android:maxLines="1" - android:text="@{profile.interface.publicKey}" /> - </RelativeLayout> - </ScrollView> - </FrameLayout> + <TextView + android:id="@+id/public_key_label" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_below="@+id/profile_name_text" + android:labelFor="@+id/public_key_text" + android:text="@string/public_key" /> + + <TextView + android:id="@+id/public_key_text" + style="?android:attr/textAppearanceMedium" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:layout_below="@+id/public_key_label" + android:ellipsize="end" + android:maxLines="1" + android:text="@{profile.interface.publicKey}" /> + </RelativeLayout> + </ScrollView> </layout> diff --git a/app/src/main/res/layout/profile_list_activity.xml b/app/src/main/res/layout/profile_list_activity.xml index 2645552b..f66a0cb3 100644 --- a/app/src/main/res/layout/profile_list_activity.xml +++ b/app/src/main/res/layout/profile_list_activity.xml @@ -1,21 +1,6 @@ <?xml version="1.0" encoding="utf-8"?> -<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" +<fragment xmlns:android="http://schemas.android.com/apk/res/android" + android:name="com.wireguard.android.ProfileListFragment" android:layout_width="match_parent" android:layout_height="match_parent" - android:baselineAligned="false" - android:orientation="horizontal"> - - <fragment - android:name="com.wireguard.android.ProfileListFragment" - android:layout_width="0dp" - android:layout_height="match_parent" - android:layout_weight="1" - android:tag="list" /> - - <fragment - android:name="com.wireguard.android.ProfileDetailFragment" - android:layout_width="0dp" - android:layout_height="match_parent" - android:layout_weight="2" - android:tag="detail" /> -</LinearLayout> + android:tag="list" /> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4010e776..872f9761 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -3,6 +3,7 @@ <string name="app_name">WireGuard</string> <string name="connected">Connected</string> <string name="disconnected">Disconnected</string> + <string name="edit">Edit</string> <string name="placeholder_text">No profile selected</string> <string name="profile_name">Profile name</string> <string name="public_key">Public key</string> |