• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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.google.android.setupcompat.template;
18 
19 import android.annotation.SuppressLint;
20 import android.content.Context;
21 import android.util.AttributeSet;
22 import android.view.MotionEvent;
23 import android.view.View;
24 import android.widget.Button;
25 import androidx.annotation.Nullable;
26 
27 /** Button that can react to touch when disabled. */
28 public class FooterActionButton extends Button {
29 
30   @Nullable private FooterButton footerButton;
31   private boolean isPrimaryButtonStyle = false;
32 
FooterActionButton(Context context, AttributeSet attrs)33   public FooterActionButton(Context context, AttributeSet attrs) {
34     super(context, attrs);
35   }
36 
setFooterButton(FooterButton footerButton)37   void setFooterButton(FooterButton footerButton) {
38     this.footerButton = footerButton;
39   }
40 
41   // getOnClickListenerWhenDisabled is responsible for handling accessibility correctly, calling
42   // performClick if necessary.
43   @SuppressLint("ClickableViewAccessibility")
44   @Override
onTouchEvent(MotionEvent event)45   public boolean onTouchEvent(MotionEvent event) {
46     if (event.getAction() == MotionEvent.ACTION_DOWN) {
47       if (footerButton != null
48           && !footerButton.isEnabled()
49           && footerButton.getVisibility() == View.VISIBLE) {
50         OnClickListener listener = footerButton.getOnClickListenerWhenDisabled();
51         if (listener != null) {
52           listener.onClick(this);
53         }
54       }
55     }
56     return super.onTouchEvent(event);
57   }
58 
59   /**
60    * Sets this footer button is primary button style.
61    *
62    * @param isPrimaryButtonStyle True if this button is primary button style.
63    */
setPrimaryButtonStyle(boolean isPrimaryButtonStyle)64   void setPrimaryButtonStyle(boolean isPrimaryButtonStyle) {
65     this.isPrimaryButtonStyle = isPrimaryButtonStyle;
66   }
67 
68   /** Returns true when the footer button is primary button style. */
isPrimaryButtonStyle()69   public boolean isPrimaryButtonStyle() {
70     return isPrimaryButtonStyle;
71   }
72 }
73