summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/overlays/ui/OverlayDialogButton.java
blob: 8b7bc872b463997d635989afef31ea7b3e5b6f25 (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
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.overlays.ui;

import org.mozilla.gecko.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * A button in the share overlay, such as the "Add to Reading List" button.
 * Has an associated icon and label, and two states: enabled and disabled.
 *
 * When disabled, tapping results in a "pop" animation causing the icon to pulse. When enabled,
 * tapping calls the OnClickListener set by the consumer in the usual way.
 */
public class OverlayDialogButton extends LinearLayout {
    private static final String LOGTAG = "GeckoOverlayDialogButton";

    // We can't use super.isEnabled(), since we want to stay clickable in disabled state.
    private boolean isEnabled = true;

    private final ImageView iconView;
    private final TextView labelView;

    private String enabledText = "";
    private String disabledText = "";

    private OnClickListener enabledOnClickListener;

    public OverlayDialogButton(Context context) {
        this(context, null);
    }

    public OverlayDialogButton(Context context, AttributeSet attrs) {
        super(context, attrs);

        setOrientation(LinearLayout.HORIZONTAL);

        LayoutInflater.from(context).inflate(R.layout.overlay_share_button, this);

        iconView = (ImageView) findViewById(R.id.overlaybtn_icon);
        labelView = (TextView) findViewById(R.id.overlaybtn_label);

        super.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (isEnabled) {
                    if (enabledOnClickListener != null) {
                        enabledOnClickListener.onClick(v);
                    } else {
                        Log.e(LOGTAG, "enabledOnClickListener is null.");
                    }
                } else {
                    Animation anim = AnimationUtils.loadAnimation(getContext(), R.anim.overlay_pop);
                    iconView.startAnimation(anim);
                }
            }
        });

        final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.OverlayDialogButton);

        Drawable drawable = typedArray.getDrawable(R.styleable.OverlayDialogButton_drawable);
        if (drawable != null) {
            setDrawable(drawable);
        }

        String disabledText = typedArray.getString(R.styleable.OverlayDialogButton_disabledText);
        if (disabledText != null) {
            this.disabledText = disabledText;
        }

        String enabledText = typedArray.getString(R.styleable.OverlayDialogButton_enabledText);
        if (enabledText != null) {
            this.enabledText = enabledText;
        }

        typedArray.recycle();

        setEnabled(true);
    }

    public void setDrawable(Drawable drawable) {
        iconView.setImageDrawable(drawable);
    }

    public void setText(String text) {
        labelView.setText(text);
    }

    @Override
    public void setOnClickListener(OnClickListener listener) {
        enabledOnClickListener = listener;
    }

    /**
     * Set the enabledness state of this view. We don't call super.setEnabled, as we want to remain
     * clickable even in the disabled state (but with a different click listener).
     */
    @Override
    public void setEnabled(boolean enabled) {
        isEnabled = enabled;
        iconView.setEnabled(enabled);
        labelView.setEnabled(enabled);

        if (enabled) {
            setText(enabledText);
        } else {
            setText(disabledText);
        }
    }

}