summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/android/ProfileFragment.java
blob: 0e9092ec3e5040f0c5bcf61aa770733bb92370f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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;
    }

    protected void onCachedProfileChanged(Profile cachedProfile) {
    }

    @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)
            setProfile(savedInstanceState.getString(ProfileActivity.KEY_PROFILE_NAME));
        else if (getArguments() != null)
            setProfile(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);
        updateCachedProfile(service);
    }

    public void setProfile(String profile) {
        this.profile = profile;
        updateCachedProfile(getService());
    }

    private void updateCachedProfile(ProfileServiceInterface service) {
        final Profile newCachedProfile = service != null
                ? service.getProfiles().get(profile) : null;
        if (newCachedProfile != cachedProfile) {
            cachedProfile = newCachedProfile;
            onCachedProfileChanged(newCachedProfile);
        }
    }
}