Files
wirevpn/app/src/main/java/com/wireguard/android/ProfileFragment.java
T
Samuel Holland 2c4f605b66 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>
2017-08-09 07:18:31 -05:00

54 lines
1.6 KiB
Java

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