DialpadView.java

 1/*
 2 * Copyright 2012-2015 the original author or authors.
 3 *
 4 * This program is free software: you can redistribute it and/or modify
 5 * it under the terms of the GNU General Public License as published by
 6 * the Free Software Foundation, either version 3 of the License, or
 7 * (at your option) any later version.
 8 *
 9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18package eu.siacs.conversations.ui.widget;
19
20import android.content.Context;
21import android.util.AttributeSet;
22import android.view.View;
23
24import androidx.constraintlayout.widget.ConstraintLayout;
25import eu.siacs.conversations.R;
26
27public class DialpadView extends ConstraintLayout implements View.OnClickListener {
28
29    protected Consumer<String> clickConsumer = null;
30
31    public DialpadView(Context context) {
32        super(context);
33        init();
34    }
35
36    public DialpadView(Context context, AttributeSet attrs) {
37        super(context, attrs);
38        init();
39    }
40
41    public DialpadView(Context context, AttributeSet attrs, int defStyleAttr) {
42        super(context, attrs, defStyleAttr);
43        init();
44    }
45
46    public void setClickConsumer(Consumer<String> clickConsumer) {
47        this.clickConsumer = clickConsumer;
48    }
49
50    private void init() {
51        inflate(getContext(), R.layout.dialpad, this);
52        initViews();
53    }
54
55    private void initViews() {
56        findViewById(R.id.dialpad_1_holder).setOnClickListener(this);
57        findViewById(R.id.dialpad_2_holder).setOnClickListener(this);
58        findViewById(R.id.dialpad_3_holder).setOnClickListener(this);
59        findViewById(R.id.dialpad_4_holder).setOnClickListener(this);
60        findViewById(R.id.dialpad_5_holder).setOnClickListener(this);
61        findViewById(R.id.dialpad_6_holder).setOnClickListener(this);
62        findViewById(R.id.dialpad_7_holder).setOnClickListener(this);
63        findViewById(R.id.dialpad_8_holder).setOnClickListener(this);
64        findViewById(R.id.dialpad_9_holder).setOnClickListener(this);
65        findViewById(R.id.dialpad_0_holder).setOnClickListener(this);
66        findViewById(R.id.dialpad_asterisk_holder).setOnClickListener(this);
67        findViewById(R.id.dialpad_pound_holder).setOnClickListener(this);
68    }
69
70    @Override
71    public void onClick(View v) {
72        clickConsumer.accept(v.getTag().toString());
73    }
74
75	// Based on java.util.function.Consumer to avoid Android 24 dependency
76	public interface Consumer<T> {
77		void accept(T t);
78	}
79}