summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/android/viewmodel/ConfigProxy.java
blob: abe8cbcfab35c8011df9bda1e54aff18a6226089 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * Copyright © 2017-2018 WireGuard LLC. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

package com.wireguard.android.viewmodel;

import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
import android.os.Parcel;
import android.os.Parcelable;

import com.wireguard.config.Config;
import com.wireguard.config.ParseException;
import com.wireguard.config.Peer;

import java.util.ArrayList;
import java.util.Collection;

public class ConfigProxy implements Parcelable {
    public static final Parcelable.Creator<ConfigProxy> CREATOR = new ConfigProxyCreator();

    private final InterfaceProxy interfaze;
    private final ObservableList<PeerProxy> peers = new ObservableArrayList<>();

    private ConfigProxy(final Parcel in) {
        interfaze = in.readParcelable(InterfaceProxy.class.getClassLoader());
        in.readTypedList(peers, PeerProxy.CREATOR);
        for (final PeerProxy proxy : peers)
            proxy.bind(this);
    }

    public ConfigProxy(final Config other) {
        interfaze = new InterfaceProxy(other.getInterface());
        for (final Peer peer : other.getPeers()) {
            final PeerProxy proxy = new PeerProxy(peer);
            peers.add(proxy);
            proxy.bind(this);
        }
    }

    public ConfigProxy() {
        interfaze = new InterfaceProxy();
    }

    public PeerProxy addPeer() {
        final PeerProxy proxy = new PeerProxy();
        peers.add(proxy);
        proxy.bind(this);
        return proxy;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public InterfaceProxy getInterface() {
        return interfaze;
    }

    public ObservableList<PeerProxy> getPeers() {
        return peers;
    }

    public Config resolve() throws ParseException {
        final Collection<Peer> resolvedPeers = new ArrayList<>();
        for (final PeerProxy proxy : peers)
            resolvedPeers.add(proxy.resolve());
        return new Config.Builder()
                .setInterface(interfaze.resolve())
                .addPeers(resolvedPeers)
                .build();
    }

    @Override
    public void writeToParcel(final Parcel dest, final int flags) {
        dest.writeParcelable(interfaze, flags);
        dest.writeTypedList(peers);
    }

    private static class ConfigProxyCreator implements Parcelable.Creator<ConfigProxy> {
        @Override
        public ConfigProxy createFromParcel(final Parcel in) {
            return new ConfigProxy(in);
        }

        @Override
        public ConfigProxy[] newArray(final int size) {
            return new ConfigProxy[size];
        }
    }
}