Eric Erfanian | 938468d | 2017-10-24 14:05:52 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.newbubble; |
| 18 | |
| 19 | import android.content.Context; |
yueg | 84ac49b | 2017-11-01 16:22:28 -0700 | [diff] [blame^] | 20 | import android.content.res.ColorStateList; |
Eric Erfanian | 938468d | 2017-10-24 14:05:52 -0700 | [diff] [blame] | 21 | import android.support.v4.view.AccessibilityDelegateCompat; |
| 22 | import android.support.v4.view.ViewCompat; |
| 23 | import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat; |
| 24 | import android.support.v7.widget.AppCompatButton; |
| 25 | import android.util.AttributeSet; |
| 26 | import android.view.View; |
| 27 | import android.view.accessibility.AccessibilityEvent; |
| 28 | import android.widget.Checkable; |
| 29 | |
| 30 | /** |
| 31 | * A {@link android.widget.Button Button} that implements {@link Checkable} and propagates the |
| 32 | * checkable state |
| 33 | */ |
| 34 | public class NewCheckableButton extends AppCompatButton implements Checkable { |
| 35 | |
| 36 | private boolean mChecked; |
| 37 | |
| 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); |
| 48 | |
| 49 | ViewCompat.setAccessibilityDelegate( |
| 50 | this, |
| 51 | new AccessibilityDelegateCompat() { |
| 52 | @Override |
| 53 | public void onInitializeAccessibilityEvent(View host, AccessibilityEvent event) { |
| 54 | super.onInitializeAccessibilityEvent(host, event); |
| 55 | event.setChecked(isChecked()); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void onInitializeAccessibilityNodeInfo( |
| 60 | View host, AccessibilityNodeInfoCompat info) { |
| 61 | super.onInitializeAccessibilityNodeInfo(host, info); |
| 62 | info.setCheckable(true); |
| 63 | info.setChecked(isChecked()); |
| 64 | } |
| 65 | }); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void setChecked(boolean checked) { |
| 70 | if (mChecked != checked) { |
| 71 | mChecked = checked; |
yueg | 84ac49b | 2017-11-01 16:22:28 -0700 | [diff] [blame^] | 72 | int newColor = |
Eric Erfanian | 938468d | 2017-10-24 14:05:52 -0700 | [diff] [blame] | 73 | checked |
yueg | 84ac49b | 2017-11-01 16:22:28 -0700 | [diff] [blame^] | 74 | ? getContext().getColor(R.color.bubble_button_color_blue) |
| 75 | : getContext().getColor(R.color.bubble_button_color_grey); |
| 76 | setTextColor(newColor); |
| 77 | setCompoundDrawableTintList(ColorStateList.valueOf(newColor)); |
Eric Erfanian | 938468d | 2017-10-24 14:05:52 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public boolean isChecked() { |
| 83 | return mChecked; |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public void toggle() { |
| 88 | setChecked(!mChecked); |
| 89 | } |
| 90 | } |