summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/base/views/SelectLocationEntry.java
blob: 554fe958404e8dd2fe2e32cda5e4eac74ae5080a (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
package se.leap.bitmaskclient.base.views;

import android.annotation.TargetApi;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;

import de.blinkt.openvpn.core.connection.Connection;
import se.leap.bitmaskclient.R;
import se.leap.bitmaskclient.base.models.Location;
import se.leap.bitmaskclient.eip.GatewaysManager.Load;

import static de.blinkt.openvpn.core.connection.Connection.TransportType.OBFS4;

public class SelectLocationEntry extends LinearLayout {

    private static final String TAG = SelectLocationEntry.class.getSimpleName();
    AppCompatTextView title;
    AppCompatTextView locationText;
    SimpleCheckBox selectedView;
    AppCompatImageView bridgesView;
    LocationIndicator locationIndicator;
    View divider;

    public SelectLocationEntry(Context context) {
        super(context);
        initLayout(context);
    }

    public SelectLocationEntry(Context context, AttributeSet attrs) {
        super(context, attrs);
        initLayout(context);
    }

    public SelectLocationEntry(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initLayout(context);
    }

     @TargetApi(21)
    public SelectLocationEntry(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initLayout(context);
    }

    private void initLayout(Context context) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rootview = inflater.inflate(R.layout.v_select_text_list_item, this, true);
        title = rootview.findViewById(R.id.title);
        locationIndicator = rootview.findViewById(R.id.quality);
        locationText = rootview.findViewById(R.id.location);
        bridgesView = rootview.findViewById(R.id.bridge_image);
        selectedView = rootview.findViewById(R.id.selected);
        divider = rootview.findViewById(R.id.divider);
    }

    public void setTitle(String text) {
        title.setText(text);
        title.setVisibility(text != null ? VISIBLE : GONE);
    }

    public void setLocation(Location location, Connection.TransportType transportType) {
        boolean hasData = location.hasLocationInfo();
        boolean supportsSelectedTransport = location.supportsTransport(transportType);
        locationText.setVisibility(hasData ? VISIBLE : GONE);
        locationIndicator.setVisibility(hasData ? VISIBLE : GONE);
        bridgesView.setVisibility(transportType.isPluggableTransport()  && supportsSelectedTransport ? VISIBLE : GONE);
        locationText.setText(location.getName());
        locationIndicator.setLoad(Load.getLoadByValue(location.getAverageLoad(transportType)));
        selectedView.setChecked(location.selected);
        locationText.setEnabled(supportsSelectedTransport);
        selectedView.setEnabled(supportsSelectedTransport);
        setEnabled(!hasData || supportsSelectedTransport);
    }

    public void showDivider(boolean show) {
        divider.setVisibility(show ? VISIBLE : GONE);
    }

    public void setSelected(boolean selected) {
        selectedView.setChecked(selected);
    }

    public boolean isSelected() {
        return selectedView.checkView.getVisibility() == VISIBLE;
    }

}