summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/android/widget/MultiselectableRelativeLayout.java
blob: 0759903b28e9229d77a14f70c249f6289858a67d (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
/*
 * Copyright © 2017-2018 WireGuard LLC. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

package com.wireguard.android.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import com.wireguard.android.R;

public class MultiselectableRelativeLayout extends RelativeLayout {
    private static final int[] STATE_MULTISELECTED = {R.attr.state_multiselected};
    private boolean multiselected;

    public MultiselectableRelativeLayout(final Context context) {
        super(context);
    }

    public MultiselectableRelativeLayout(final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }

    public MultiselectableRelativeLayout(final Context context, final AttributeSet attrs, final int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MultiselectableRelativeLayout(final Context context, final AttributeSet attrs, final int defStyleAttr, final int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected int[] onCreateDrawableState(final int extraSpace) {
        if (multiselected) {
            final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
            mergeDrawableStates(drawableState, STATE_MULTISELECTED);
            return drawableState;
        }
        return super.onCreateDrawableState(extraSpace);
    }

    public void setMultiSelected(final boolean on) {
        if (!multiselected) {
            multiselected = true;
            refreshDrawableState();
        }
        setActivated(on);
    }

    public void setSingleSelected(final boolean on) {
        if (multiselected) {
            multiselected = false;
            refreshDrawableState();
        }
        setActivated(on);
    }
}