summaryrefslogtreecommitdiffhomepage
path: root/ui/src/main/java/com/wireguard/android/fragment/TunnelDetailFragment.java
blob: 8d90fa7e067299f8f65aafa075f75d3edb66dab4 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
 * Copyright © 2017-2019 WireGuard LLC. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

package com.wireguard.android.fragment;

import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wireguard.android.R;
import com.wireguard.android.databinding.TunnelDetailFragmentBinding;
import com.wireguard.android.databinding.TunnelDetailPeerBinding;
import com.wireguard.android.model.ObservableTunnel;
import com.wireguard.android.backend.Tunnel.State;
import com.wireguard.android.ui.EdgeToEdge;
import com.wireguard.crypto.Key;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Fragment that shows details about a specific tunnel.
 */

public class TunnelDetailFragment extends BaseFragment {
    @Nullable private TunnelDetailFragmentBinding binding;
    @Nullable private Timer timer;
    @Nullable private State lastState = State.TOGGLE;

    @Override
    public void onCreate(@Nullable final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
        inflater.inflate(R.menu.tunnel_detail, menu);
    }

    @Override
    public void onStop() {
        super.onStop();
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                updateStats();
            }
        }, 0, 1000);
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container,
                             @Nullable final Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        binding = TunnelDetailFragmentBinding.inflate(inflater, container, false);
        binding.executePendingBindings();
        EdgeToEdge.setUpRoot((ViewGroup) binding.getRoot());
        EdgeToEdge.setUpScrollingContent((ViewGroup) binding.getRoot(), null);
        return binding.getRoot();
    }

    @Override
    public void onDestroyView() {
        binding = null;
        super.onDestroyView();
    }

    @Override
    public void onSelectedTunnelChanged(@Nullable final ObservableTunnel oldTunnel, @Nullable final ObservableTunnel newTunnel) {
        if (binding == null)
            return;
        binding.setTunnel(newTunnel);
        if (newTunnel == null)
            binding.setConfig(null);
        else
            newTunnel.getConfigAsync().thenAccept(binding::setConfig);
        lastState = State.TOGGLE;
        updateStats();
    }

    @Override
    public void onViewStateRestored(@Nullable final Bundle savedInstanceState) {
        if (binding == null) {
            return;
        }

        binding.setFragment(this);
        onSelectedTunnelChanged(null, getSelectedTunnel());
        super.onViewStateRestored(savedInstanceState);
    }

    private String formatBytes(final long bytes) {
        if (bytes < 1024)
            return requireContext().getString(R.string.transfer_bytes, bytes);
        else if (bytes < 1024*1024)
            return requireContext().getString(R.string.transfer_kibibytes, bytes/1024.0);
        else if (bytes < 1024*1024*1024)
            return requireContext().getString(R.string.transfer_mibibytes, bytes/(1024.0*1024.0));
        else if (bytes < 1024*1024*1024*1024)
            return requireContext().getString(R.string.transfer_gibibytes, bytes/(1024.0*1024.0*1024.0));
        return requireContext().getString(R.string.transfer_tibibytes, bytes/(1024.0*1024.0*1024.0)/1024.0);
    }

    private void updateStats() {
        if (binding == null || !isResumed())
            return;
        final ObservableTunnel tunnel = binding.getTunnel();
        if (tunnel == null)
            return;
        final State state = tunnel.getState();
        if (state != State.UP && lastState == state)
            return;
        lastState = state;
        tunnel.getStatisticsAsync().whenComplete((statistics, throwable) -> {
            if (throwable != null) {
                for (int i = 0; i < binding.peersLayout.getChildCount(); ++i) {
                    final TunnelDetailPeerBinding peer = DataBindingUtil.getBinding(binding.peersLayout.getChildAt(i));
                    if (peer == null)
                        continue;
                    peer.transferLabel.setVisibility(View.GONE);
                    peer.transferText.setVisibility(View.GONE);
                }
                return;
            }
            for (int i = 0; i < binding.peersLayout.getChildCount(); ++i) {
                final TunnelDetailPeerBinding peer = DataBindingUtil.getBinding(binding.peersLayout.getChildAt(i));
                if (peer == null)
                    continue;
                final Key publicKey = peer.getItem().getPublicKey();
                final long rx = statistics.peerRx(publicKey);
                final long tx = statistics.peerTx(publicKey);
                if (rx == 0 && tx == 0) {
                    peer.transferLabel.setVisibility(View.GONE);
                    peer.transferText.setVisibility(View.GONE);
                    continue;
                }
                peer.transferText.setText(requireContext().getString(R.string.transfer_rx_tx, formatBytes(rx), formatBytes(tx)));
                peer.transferLabel.setVisibility(View.VISIBLE);
                peer.transferText.setVisibility(View.VISIBLE);
            }
        });
    }
}