summaryrefslogtreecommitdiffhomepage
path: root/ui/src/main/java/com/wireguard/android/viewmodel/PeerDetail.kt
blob: 80b32fd59a53f4f32d629449ab8a9e1fd2afe82b (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
package com.wireguard.android.viewmodel

import android.util.Log
import androidx.databinding.BaseObservable
import androidx.databinding.Bindable
import androidx.databinding.Observable
import androidx.databinding.ObservableList
import androidx.databinding.ObservableArrayList

import com.wireguard.android.BR
import com.wireguard.config.InetEndpoint
import com.wireguard.config.InetNetwork
import com.wireguard.config.Peer
import com.wireguard.crypto.Key;

import java.util.Optional;

import kotlin.collections.LinkedHashSet


class PeerDetail : BaseObservable {
    var peer: Peer?
    private var owner: ConfigDetail? = null

    @get:Bindable
    var publicKey: Key

    @get:Bindable
    var allowedIps: ObservableList<InetNetwork> = ObservableArrayList<InetNetwork>()

    @get:Bindable
    var endpoint: Optional<InetEndpoint> = Optional.empty()
        get() {
            if (!field.isEmpty()) {
                return field
            } else {
                return Optional.ofNullable(peer?.endpoint?.get())
            }
        }

        set(value) {
            Log.i(TAG, "notifyPropertyChanged endpoint " + this + ", " + value)
            field = value
            notifyPropertyChanged(BR.endpoint)
        }

    @get:Bindable
    var persistentKeepalive: Optional<Int> = Optional.empty()

    constructor(other: Peer) {
        peer = other
        publicKey = other.getPublicKey()
        allowedIps.addAll(other.getAllowedIps())
        endpoint = other.getEndpoint();
        persistentKeepalive = other.getPersistentKeepalive()
    }

    constructor(publicKey: Key) {
        peer = null
        this.publicKey = publicKey
    }

    fun bind(owner: ConfigDetail) {
        this.owner = owner
    }

    override fun addOnPropertyChangedCallback (callback: Observable.OnPropertyChangedCallback) {
        Log.i(TAG, "addOnPropertyChangedCallback " + this + ", " + callback)
        super.addOnPropertyChangedCallback(callback)
    }
    
    /**
     * Converts the {@code Peer} into a string suitable for debugging purposes. The {@code Peer} is
     * identified by its public key and (if known) its endpoint.
     *
     * @return a concise single-line identifier for the {@code Peer}
     */
    override fun toString(): String {
        return "(Peer " + publicKey.toBase64() + ")"
    }

    companion object {
        private const val TAG = "WireGuard/PeerDetail"
    }
}