blob: 668e74c0eb02ecbf3c6c5a4112e4bf1e519cd3fe (
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
|
package com.wireguard.android.fragment;
import android.app.AlertDialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.system.ErrnoException;
import android.system.OsConstants;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.commonsware.cwac.crossport.design.widget.Snackbar;
import com.wireguard.android.R;
import com.wireguard.android.databinding.TunnelDetailFragmentBinding;
import com.wireguard.android.databinding.TunnelListItemBinding;
import com.wireguard.android.model.Tunnel;
import com.wireguard.android.model.Tunnel.State;
import com.wireguard.android.util.ExceptionLoggers;
/**
* Helper method shared by TunnelListFragment and TunnelDetailFragment.
*/
public final class TunnelController {
private static final String TAG = TunnelController.class.getSimpleName();
private TunnelController() {
// Prevent instantiation.
}
public static void setTunnelState(final View view, final boolean checked) {
final ViewDataBinding binding = DataBindingUtil.findBinding(view);
final Tunnel tunnel;
if (binding instanceof TunnelDetailFragmentBinding)
tunnel = ((TunnelDetailFragmentBinding) binding).getTunnel();
else if (binding instanceof TunnelListItemBinding)
tunnel = ((TunnelListItemBinding) binding).getItem();
else
tunnel = null;
if (tunnel == null) {
Log.e(TAG, "setChecked() from a null tunnel", new IllegalStateException());
return;
}
tunnel.setState(State.of(checked)).whenComplete((state, throwable) -> {
if (throwable == null)
return;
Log.e(TAG, "Cannot set state of tunnel " + tunnel.getName(), throwable);
final Context context = view.getContext();
if (throwable instanceof ErrnoException
&& ((ErrnoException) throwable).errno == OsConstants.ENODEV) {
final String message = context.getString(R.string.not_supported_message);
final AlertDialog dialog = new AlertDialog.Builder(context)
.setMessage(Html.fromHtml(message))
.setPositiveButton(R.string.ok, null)
.setTitle(R.string.not_supported_title)
.show();
// Make links work.
((TextView) dialog.findViewById(android.R.id.message))
.setMovementMethod(LinkMovementMethod.getInstance());
} else {
final String message =
context.getString(checked ? R.string.error_up : R.string.error_down) + ": "
+ ExceptionLoggers.unwrap(throwable).getMessage();
Snackbar.make(view, message, Snackbar.LENGTH_LONG).show();
}
});
}
}
|