summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/base/views/MainButton.java
blob: 586eb32128975990c0afcb5a1b75dfbf3f5f7999 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package se.leap.bitmaskclient.base.views;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.RelativeLayout;

import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;

import java.lang.ref.WeakReference;

import se.leap.bitmaskclient.R;

public class MainButton extends RelativeLayout {

    private static final String TAG = MainButton.class.getSimpleName();

    interface MainButtonListener {
        void onButtonClicked();
    }

    AppCompatImageView glow;
    AppCompatImageView shadowDark;
    AppCompatImageView shadowLight;
    AnimationDrawable glowAnimation;
    WeakReference<MainButtonListener> callback;

    private boolean isOn = false;
    private boolean isProcessing = false;
    private boolean isError = true;



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

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

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


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

    public void setMainButtonListener(MainButtonListener callback) {
        this.callback = new WeakReference<>(callback);
    }

    private void initLayout(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rootview = inflater.inflate(R.layout.v_main_btn, this, true);

        glow = rootview.findViewById(R.id.vpn_btn_glow);
        glowAnimation = (AnimationDrawable) glow.getBackground();
        shadowDark = rootview.findViewById(R.id.vpn_btn_shadow_dark);
        shadowLight = rootview.findViewById(R.id.vpn_btn_shadow_light);

        rootview.setOnGenericMotionListener(new OnGenericMotionListener() {
            @Override
            public boolean onGenericMotion(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_BUTTON_PRESS:
                        Log.d(TAG, "onbuttonPrees");
                        break;
                    case MotionEvent.ACTION_BUTTON_RELEASE:
                        Log.d(TAG, "onButtonRelease");
                        break;
                }
                return false;
            }
        });
        rootview.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        Log.d(TAG, "motion down");
                        Drawable drawableDown = context.getResources().getDrawable(R.drawable.on_off_btn_start_2_pressed);
                        shadowDark.setImageDrawable(drawableDown);
                        break;
                    case MotionEvent.ACTION_UP:
                        Log.d(TAG, "motion up");
                        Drawable drawableUp = isOn ?
                                context.getResources().getDrawable(R.drawable.on_off_btn_start_2_disabled) :
                                context.getResources().getDrawable(R.drawable.on_off_btn_start_2_enabled);
                        shadowDark.setImageDrawable(drawableUp);
                        break;
                    case MotionEvent.ACTION_CANCEL:
                        Log.d(TAG, "motion cancelled");
                        Drawable drawableRestoreState = isOn ?
                                context.getResources().getDrawable(R.drawable.on_off_btn_start_2_enabled) :
                                context.getResources().getDrawable(R.drawable.on_off_btn_start_2_disabled);
                        shadowDark.setImageDrawable(drawableRestoreState);
                        break;
                }
                return false;
            }
        });

    }


    private void stopGlowAnimation() {
        AlphaAnimation fadeOutAnimation = new AlphaAnimation(1.0f, 0.0f);
        fadeOutAnimation.setDuration(300);
        fadeOutAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}

            @Override
            public void onAnimationEnd(Animation animation) {
                glow.setVisibility(GONE);
                glowAnimation.stop();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {}
        });
        glow.startAnimation(fadeOutAnimation);
    }

    private void startGlowAnimation() {
        glow.setAlpha(1.0f);
        glow.setVisibility(VISIBLE);
        glowAnimation.start();
    }

    public void updateState(boolean isOn, boolean isProcessing, boolean isError) {
        if (this.isOn != isOn) {
            this.isOn = isOn;
            Drawable drawableRestoreState = isOn ?
                    getContext().getResources().getDrawable(R.drawable.on_off_btn_start_2_enabled) :
                    getContext().getResources().getDrawable(R.drawable.on_off_btn_start_2_disabled);
            shadowDark.setImageDrawable(drawableRestoreState);
            shadowLight.setVisibility(isOn ? VISIBLE : GONE);
        }

        if (this.isProcessing != isProcessing) {
            if (!isProcessing) {
               stopGlowAnimation();
            } else {
                startGlowAnimation();
            }
            this.isProcessing = isProcessing;
        }

        if (this.isError != isError) {
            @DrawableRes int drawableResource = isOn ? R.drawable.on_off_btn_start_2_enabled : R.drawable.on_off_btn_start_2_disabled;
            if (!isError) {
                setImageWithTint(shadowLight, drawableResource, R.color.colorSecondary);
            } else {
                setImageWithTint(shadowLight, drawableResource, R.color.colorWarning);
            }
            this.isError = isError;
        }
    }

    private void setImageWithTint(AppCompatImageView view, @DrawableRes int resourceId, @ColorRes int color) {
        view.setImageDrawable(ContextCompat.getDrawable(getContext(), resourceId));
        view.setColorFilter(ContextCompat.getColor(getContext(), color), PorterDuff.Mode.SRC_ATOP);
    }



}