summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/android/BaseConfigFragment.java
blob: 4a754b63f2bd20271e11cdb3112ddc322292e72b (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
package com.wireguard.android;

import android.app.Fragment;
import android.os.Bundle;

import com.wireguard.config.Config;

/**
 * Base class for fragments that need to remember the current configuration.
 */

abstract class BaseConfigFragment extends Fragment {
    private static final String KEY_CURRENT_CONFIG = "currentConfig";

    private Config currentConfig;

    protected Config getCurrentConfig() {
        return currentConfig;
    }

    protected abstract void onCurrentConfigChanged(Config config);

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Restore the saved configuration if there is one; otherwise grab it from the arguments.
        String initialConfig = null;
        if (savedInstanceState != null)
            initialConfig = savedInstanceState.getString(KEY_CURRENT_CONFIG);
        else if (getArguments() != null)
            initialConfig = getArguments().getString(KEY_CURRENT_CONFIG);
        if (initialConfig != null && currentConfig == null)
            setCurrentConfig(VpnService.getInstance().get(initialConfig));
    }

    @Override
    public void onSaveInstanceState(final Bundle outState) {
        super.onSaveInstanceState(outState);
        if (currentConfig != null)
            outState.putString(KEY_CURRENT_CONFIG, currentConfig.getName());
    }

    public void setCurrentConfig(final Config config) {
        currentConfig = config;
        onCurrentConfigChanged(currentConfig);
    }
}