2018-01-01 09:06:37 +01:00
|
|
|
package com.wireguard.android.fragment;
|
|
|
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
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.model.Tunnel;
|
2018-04-28 04:45:17 +02:00
|
|
|
import com.wireguard.config.Config;
|
2018-01-01 09:06:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fragment that shows details about a specific tunnel.
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class TunnelDetailFragment extends BaseFragment {
|
|
|
|
private TunnelDetailFragmentBinding binding;
|
|
|
|
private boolean isViewStateRestored;
|
2018-04-28 04:45:17 +02:00
|
|
|
private String originalName;
|
2018-01-01 09:06:37 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(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 View onCreateView(final LayoutInflater inflater, final ViewGroup container,
|
|
|
|
final Bundle savedInstanceState) {
|
|
|
|
super.onCreateView(inflater, container, savedInstanceState);
|
|
|
|
binding = TunnelDetailFragmentBinding.inflate(inflater, container, false);
|
|
|
|
binding.executePendingBindings();
|
|
|
|
return binding.getRoot();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onDestroyView() {
|
|
|
|
binding = null;
|
|
|
|
super.onDestroyView();
|
|
|
|
}
|
|
|
|
|
2018-04-28 04:45:17 +02:00
|
|
|
private void onConfigLoaded(Config config) {
|
|
|
|
binding.setConfig(new Config.Observable(config, originalName));
|
|
|
|
}
|
|
|
|
|
2018-01-01 09:06:37 +01:00
|
|
|
@Override
|
|
|
|
public void onSelectedTunnelChanged(final Tunnel oldTunnel, final Tunnel newTunnel) {
|
2018-04-28 04:45:17 +02:00
|
|
|
if (binding != null && isViewStateRestored) {
|
2018-01-01 09:06:37 +01:00
|
|
|
binding.setTunnel(newTunnel);
|
2018-04-28 04:45:17 +02:00
|
|
|
if (newTunnel == null)
|
|
|
|
binding.setConfig(null);
|
|
|
|
else {
|
|
|
|
originalName = newTunnel.getName();
|
|
|
|
newTunnel.getConfigAsync().thenAccept(this::onConfigLoaded);
|
|
|
|
}
|
|
|
|
}
|
2018-01-01 09:06:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onViewStateRestored(final Bundle savedInstanceState) {
|
|
|
|
super.onViewStateRestored(savedInstanceState);
|
2018-04-28 04:45:17 +02:00
|
|
|
Tunnel tunnel = getSelectedTunnel();
|
|
|
|
binding.setTunnel(tunnel);
|
|
|
|
if (tunnel == null)
|
|
|
|
binding.setConfig(null);
|
|
|
|
else {
|
|
|
|
originalName = tunnel.getName();
|
|
|
|
tunnel.getConfigAsync().thenAccept(this::onConfigLoaded);
|
|
|
|
}
|
2018-01-01 09:06:37 +01:00
|
|
|
isViewStateRestored = true;
|
|
|
|
}
|
|
|
|
}
|