ProfileActivity: Replace fragments instead of hiding

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Samuel Holland 2017-08-09 05:12:00 -05:00
parent 2e3daa8913
commit 52cdf3e7e5
11 changed files with 149 additions and 106 deletions

View File

@ -12,6 +12,10 @@
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@android:style/Theme.Material.Light.DarkActionBar"> android:theme="@android:style/Theme.Material.Light.DarkActionBar">
<activity
android:name=".ProfileDetailActivity"
android:label=""
android:parentActivityName=".ProfileListActivity" />
<activity android:name=".ProfileListActivity"> <activity android:name=".ProfileListActivity">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />

View File

@ -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);
}
}

View File

@ -8,10 +8,11 @@ import android.view.MenuItem;
* Base class for activities that use ProfileListFragment and ProfileDetailFragment. * 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"; public static final String KEY_PROFILE_NAME = "profile_name";
protected static final String TAG_DETAIL = "detail"; protected static final String TAG_DETAIL = "detail";
protected static final String TAG_LIST = "list"; protected static final String TAG_LIST = "list";
protected static final String TAG_PLACEHOLDER = "placeholder";
private String currentProfile; private String currentProfile;

View File

@ -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());
}
}

View File

@ -1,9 +1,8 @@
package com.wireguard.android; package com.wireguard.android;
import android.app.Fragment; import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction; import android.app.FragmentTransaction;
import android.content.res.Configuration; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
/** /**
@ -13,56 +12,55 @@ import android.os.Bundle;
public class ProfileListActivity extends ProfileActivity { public class ProfileListActivity extends ProfileActivity {
private boolean isSplitLayout; private boolean isSplitLayout;
@Override
public void onBackPressed() {
final FragmentManager fm = getFragmentManager();
if (fm.getBackStackEntryCount() > 0) {
fm.popBackStack();
} else {
super.onBackPressed();
}
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// Set up the base layout and fill it with fragments.
setContentView(R.layout.profile_list_activity); setContentView(R.layout.profile_list_activity);
final int orientation = getResources().getConfiguration().orientation; isSplitLayout = findViewById(R.id.fragment_container) != null;
isSplitLayout = orientation == Configuration.ORIENTATION_LANDSCAPE; if (!isSplitLayout) {
updateLayout(getCurrentProfile()); // 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) { public void onProfileSelected(String profile) {
updateLayout(profile); if (isSplitLayout) {
setCurrentProfile(profile); 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) { public void updateLayout(String profile) {
final FragmentManager fm = getFragmentManager(); final Fragment fragment = getFragmentManager().findFragmentById(R.id.fragment_container);
final Fragment detailFragment = fm.findFragmentByTag(TAG_DETAIL);
final Fragment listFragment = fm.findFragmentByTag(TAG_LIST);
final FragmentTransaction transaction = fm.beginTransaction();
if (profile != null) { if (profile != null) {
if (isSplitLayout) { if (fragment instanceof ProfileDetailFragment) {
if (listFragment.isHidden()) final ProfileDetailFragment detailFragment = (ProfileDetailFragment) fragment;
transaction.show(listFragment); detailFragment.setProfile(profile);
} else { } 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 { } else {
if (isSplitLayout) { if (!(fragment instanceof PlaceholderFragment)) {
if (detailFragment.isHidden()) final PlaceholderFragment placeholderFragment = new PlaceholderFragment();
transaction.show(detailFragment); final FragmentTransaction transaction = getFragmentManager().beginTransaction();
} else { transaction.replace(R.id.fragment_container, placeholderFragment, TAG_PLACEHOLDER);
transaction.hide(detailFragment); transaction.commit();
} }
if (listFragment.isHidden())
transaction.show(listFragment);
} }
transaction.commit();
((ProfileDetailFragment) detailFragment).setProfile(profile);
} }
} }

View File

@ -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>

View File

@ -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" />

View File

@ -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" />

View File

@ -4,68 +4,53 @@
<data> <data>
<import type="android.view.View" />
<variable <variable
name="profile" name="profile"
type="com.wireguard.config.Profile" /> type="com.wireguard.config.Profile" />
</data> </data>
<FrameLayout <ScrollView
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:padding="16dp"> android:padding="16dp">
<TextView <RelativeLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="wrap_content">
android:gravity="center"
android:text="@string/placeholder_text"
android:visibility="@{profile == null ? View.VISIBLE : View.GONE}" />
<ScrollView <TextView
android:layout_width="match_parent" android:id="@+id/profile_name_label"
android:layout_height="match_parent"
android:visibility="@{profile == null ? View.GONE : View.VISIBLE}">
<RelativeLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:labelFor="@+id/profile_name_text"
android:text="@string/profile_name" />
<TextView <TextView
android:id="@+id/profile_name_label" android:id="@+id/profile_name_text"
android:layout_width="match_parent" style="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content" android:layout_width="match_parent"
android:layout_alignParentTop="true" android:layout_height="wrap_content"
android:labelFor="@+id/profile_name_text" android:layout_below="@+id/profile_name_label"
android:text="@string/profile_name" /> android:text="@{profile.name}" />
<TextView <TextView
android:id="@+id/profile_name_text" android:id="@+id/public_key_label"
style="?android:attr/textAppearanceMedium" android:layout_width="match_parent"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:layout_below="@+id/profile_name_text"
android:layout_below="@+id/profile_name_label" android:labelFor="@+id/public_key_text"
android:text="@{profile.name}" /> android:text="@string/public_key" />
<TextView <TextView
android:id="@+id/public_key_label" android:id="@+id/public_key_text"
android:layout_width="match_parent" style="?android:attr/textAppearanceMedium"
android:layout_height="wrap_content" android:layout_width="match_parent"
android:layout_below="@+id/profile_name_text" android:layout_height="wrap_content"
android:labelFor="@+id/public_key_text" android:layout_below="@+id/public_key_label"
android:text="@string/public_key" /> android:ellipsize="end"
android:maxLines="1"
<TextView android:text="@{profile.interface.publicKey}" />
android:id="@+id/public_key_text" </RelativeLayout>
style="?android:attr/textAppearanceMedium" </ScrollView>
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>
</layout> </layout>

View File

@ -1,21 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?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_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:baselineAligned="false" android:tag="list" />
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>

View File

@ -3,6 +3,7 @@
<string name="app_name">WireGuard</string> <string name="app_name">WireGuard</string>
<string name="connected">Connected</string> <string name="connected">Connected</string>
<string name="disconnected">Disconnected</string> <string name="disconnected">Disconnected</string>
<string name="edit">Edit</string>
<string name="placeholder_text">No profile selected</string> <string name="placeholder_text">No profile selected</string>
<string name="profile_name">Profile name</string> <string name="profile_name">Profile name</string>
<string name="public_key">Public key</string> <string name="public_key">Public key</string>