blob: fda0ddc6eadf4ce5fe1a94d0bc384063a26cc685 [file] [log] [blame]
Eric Erfanian938468d2017-10-24 14:05:52 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.newbubble;
18
19import android.content.Context;
yueg84ac49b2017-11-01 16:22:28 -070020import android.content.res.ColorStateList;
Eric Erfanian938468d2017-10-24 14:05:52 -070021import android.support.v4.view.AccessibilityDelegateCompat;
22import android.support.v4.view.ViewCompat;
23import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
24import android.support.v7.widget.AppCompatButton;
25import android.util.AttributeSet;
26import android.view.View;
27import android.view.accessibility.AccessibilityEvent;
28import android.widget.Checkable;
29
30/**
31 * A {@link android.widget.Button Button} that implements {@link Checkable} and propagates the
32 * checkable state
33 */
34public class NewCheckableButton extends AppCompatButton implements Checkable {
35
linyuh183cb712017-12-27 17:02:37 -080036 private boolean checked;
Eric Erfanian938468d2017-10-24 14:05:52 -070037
38 public NewCheckableButton(Context context) {
39 this(context, null);
40 }
41
42 public NewCheckableButton(Context context, AttributeSet attrs) {
43 this(context, attrs, android.R.attr.imageButtonStyle);
44 }
45
46 public NewCheckableButton(Context context, AttributeSet attrs, int defStyleAttr) {
47 super(context, attrs, defStyleAttr);
yueg2a422f72018-01-24 15:20:10 -080048 }
Eric Erfanian938468d2017-10-24 14:05:52 -070049
yueg2a422f72018-01-24 15:20:10 -080050 public void setCheckable(boolean checkable) {
Eric Erfanian938468d2017-10-24 14:05:52 -070051 ViewCompat.setAccessibilityDelegate(
52 this,
53 new AccessibilityDelegateCompat() {
54 @Override
55 public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) {
56 super.onInitializeAccessibilityEvent(host, event);
yueg2a422f72018-01-24 15:20:10 -080057 if (checkable) {
58 event.setChecked(isChecked());
59 }
Eric Erfanian938468d2017-10-24 14:05:52 -070060 }
61
62 @Override
63 public void onInitializeAccessibilityNodeInfo(
64 View host, AccessibilityNodeInfoCompat info) {
65 super.onInitializeAccessibilityNodeInfo(host, info);
yueg2a422f72018-01-24 15:20:10 -080066 info.setCheckable(checkable);
67 if (checkable) {
68 info.setChecked(isChecked());
69 }
Eric Erfanian938468d2017-10-24 14:05:52 -070070 }
71 });
72 }
73
74 @Override
75 public void setChecked(boolean checked) {
linyuh183cb712017-12-27 17:02:37 -080076 if (this.checked != checked) {
77 this.checked = checked;
yueg84ac49b2017-11-01 16:22:28 -070078 int newColor =
Eric Erfanian938468d2017-10-24 14:05:52 -070079 checked
yueg84ac49b2017-11-01 16:22:28 -070080 ? getContext().getColor(R.color.bubble_button_color_blue)
81 : getContext().getColor(R.color.bubble_button_color_grey);
82 setTextColor(newColor);
83 setCompoundDrawableTintList(ColorStateList.valueOf(newColor));
Eric Erfanian938468d2017-10-24 14:05:52 -070084 }
85 }
86
87 @Override
88 public boolean isChecked() {
linyuh183cb712017-12-27 17:02:37 -080089 return checked;
Eric Erfanian938468d2017-10-24 14:05:52 -070090 }
91
92 @Override
93 public void toggle() {
linyuh183cb712017-12-27 17:02:37 -080094 setChecked(!checked);
Eric Erfanian938468d2017-10-24 14:05:52 -070095 }
96}