summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard
diff options
context:
space:
mode:
authorSamuel Holland <samuel@sholland.org>2017-08-09 07:18:31 -0500
committerSamuel Holland <samuel@sholland.org>2017-08-09 07:18:31 -0500
commit2c4f605b6674e2ca9659c46f686b8597baa431ff (patch)
tree333967f5c4d7e1fa98f5a74f00fbb855de985b44 /app/src/main/java/com/wireguard
parent39ed03f758fdcb22dd01ca576e31b19057ca387a (diff)
ProfileFragment: Helper class to remember a fragment's profile
Caching the actual profile object is important when the fragment is on an activity's back stack: when it is shown again, onCreateView will be called with profile already set and the service already connected. In this case, there is no later notification from which to bind the profile, so the existing object needs to be bound there. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main/java/com/wireguard')
-rw-r--r--app/src/main/java/com/wireguard/android/ProfileDetailFragment.java25
-rw-r--r--app/src/main/java/com/wireguard/android/ProfileFragment.java53
2 files changed, 62 insertions, 16 deletions
diff --git a/app/src/main/java/com/wireguard/android/ProfileDetailFragment.java b/app/src/main/java/com/wireguard/android/ProfileDetailFragment.java
index 4af7fb25..2aa40c06 100644
--- a/app/src/main/java/com/wireguard/android/ProfileDetailFragment.java
+++ b/app/src/main/java/com/wireguard/android/ProfileDetailFragment.java
@@ -10,22 +10,15 @@ import android.view.ViewGroup;
import com.wireguard.android.databinding.ProfileDetailFragmentBinding;
/**
- * Fragment for viewing and editing a WireGuard profile.
+ * Fragment for viewing information about a WireGuard profile.
*/
-public class ProfileDetailFragment extends ServiceClientFragment<ProfileServiceInterface> {
+public class ProfileDetailFragment extends ProfileFragment {
private ProfileDetailFragmentBinding binding;
- private String name;
-
- public ProfileDetailFragment() {
- super();
- setArguments(new Bundle());
- }
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- name = getArguments().getString(ProfileActivity.KEY_PROFILE_NAME);
setHasOptionsMenu(true);
}
@@ -37,20 +30,20 @@ public class ProfileDetailFragment extends ServiceClientFragment<ProfileServiceI
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
binding = ProfileDetailFragmentBinding.inflate(inflater, parent, false);
+ binding.setProfile(getCachedProfile());
return binding.getRoot();
}
@Override
public void onServiceConnected(ProfileServiceInterface service) {
super.onServiceConnected(service);
- binding.setProfile(service.getProfiles().get(name));
+ binding.setProfile(service.getProfiles().get(getProfile()));
}
- public void setProfile(String name) {
- this.name = name;
- getArguments().putString(ProfileActivity.KEY_PROFILE_NAME, name);
- final ProfileServiceInterface service = getService();
- if (binding != null && service != null)
- binding.setProfile(service.getProfiles().get(name));
+ @Override
+ public void setProfile(String profile) {
+ super.setProfile(profile);
+ if (binding != null)
+ binding.setProfile(getCachedProfile());
}
}
diff --git a/app/src/main/java/com/wireguard/android/ProfileFragment.java b/app/src/main/java/com/wireguard/android/ProfileFragment.java
new file mode 100644
index 00000000..cf1c690e
--- /dev/null
+++ b/app/src/main/java/com/wireguard/android/ProfileFragment.java
@@ -0,0 +1,53 @@
+package com.wireguard.android;
+
+import android.os.Bundle;
+
+import com.wireguard.config.Profile;
+
+/**
+ * Base class for fragments that need to remember which profile they belong to.
+ */
+
+abstract class ProfileFragment extends ServiceClientFragment<ProfileServiceInterface> {
+ private Profile cachedProfile;
+ private String profile;
+
+ protected Profile getCachedProfile() {
+ return cachedProfile;
+ }
+
+ public String getProfile() {
+ return profile;
+ }
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ // Restore the saved profile if there is one; otherwise grab it from the arguments.
+ if (savedInstanceState != null)
+ profile = savedInstanceState.getString(ProfileActivity.KEY_PROFILE_NAME);
+ else if (getArguments() != null)
+ profile = getArguments().getString(ProfileActivity.KEY_PROFILE_NAME);
+ }
+
+ @Override
+ public void onSaveInstanceState(Bundle outState) {
+ super.onSaveInstanceState(outState);
+ outState.putString(ProfileActivity.KEY_PROFILE_NAME, profile);
+ }
+
+ @Override
+ public void onServiceConnected(ProfileServiceInterface service) {
+ super.onServiceConnected(service);
+ cachedProfile = service.getProfiles().get(profile);
+ }
+
+ public void setProfile(String profile) {
+ this.profile = profile;
+ final ProfileServiceInterface service = getService();
+ if (service != null)
+ cachedProfile = service.getProfiles().get(profile);
+ else
+ cachedProfile = null;
+ }
+}