| /* |
| * Copyright (C) 2020 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| package com.android.car.ui.baselayout; |
| |
| import static com.android.car.ui.core.CarUi.MIN_TARGET_API; |
| |
| import android.annotation.SuppressLint; |
| import android.annotation.TargetApi; |
| import android.content.Context; |
| import android.util.AttributeSet; |
| import android.view.MotionEvent; |
| import android.view.View; |
| |
| import androidx.annotation.Nullable; |
| |
| /** |
| * A view that doesn't allow any touches to pass through it to views below. |
| * |
| * <p>Used in baselayouts to prevent clicking through the toolbar. |
| */ |
| @SuppressLint("ClickableViewAccessibility") |
| @TargetApi(MIN_TARGET_API) |
| public class ClickBlockingView extends View { |
| |
| private boolean mEatingTouch = false; |
| private boolean mEatingHover = false; |
| |
| public ClickBlockingView(Context context) { |
| super(context); |
| } |
| |
| public ClickBlockingView(Context context, |
| @Nullable AttributeSet attrs) { |
| super(context, attrs); |
| } |
| |
| public ClickBlockingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { |
| super(context, attrs, defStyleAttr); |
| } |
| |
| public ClickBlockingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, |
| int defStyleRes) { |
| super(context, attrs, defStyleAttr, defStyleRes); |
| } |
| |
| @Override |
| public boolean onTouchEvent(MotionEvent ev) { |
| // Copied from androidx.appcompat.widget.Toolbar |
| |
| // We always eat touch events, but should still respect the touch event dispatch |
| // contract. If the normal View implementation doesn't want the events, we'll just silently |
| // eat the rest of the gesture without reporting the events to the default implementation |
| // since that's what it expects. |
| |
| final int action = ev.getActionMasked(); |
| if (action == MotionEvent.ACTION_DOWN) { |
| mEatingTouch = false; |
| } |
| |
| if (!mEatingTouch) { |
| final boolean handled = super.onTouchEvent(ev); |
| if (action == MotionEvent.ACTION_DOWN && !handled) { |
| mEatingTouch = true; |
| } |
| } |
| |
| if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) { |
| mEatingTouch = false; |
| } |
| |
| return true; |
| } |
| |
| @Override |
| public boolean onHoverEvent(MotionEvent ev) { |
| // Copied from androidx.appcompat.widget.Toolbar |
| |
| // Same deal as onTouchEvent() above. Eat all hover events, but still |
| // respect the touch event dispatch contract. |
| |
| final int action = ev.getActionMasked(); |
| if (action == MotionEvent.ACTION_HOVER_ENTER) { |
| mEatingHover = false; |
| } |
| |
| if (!mEatingHover) { |
| final boolean handled = super.onHoverEvent(ev); |
| if (action == MotionEvent.ACTION_HOVER_ENTER && !handled) { |
| mEatingHover = true; |
| } |
| } |
| |
| if (action == MotionEvent.ACTION_HOVER_EXIT || action == MotionEvent.ACTION_CANCEL) { |
| mEatingHover = false; |
| } |
| |
| return true; |
| } |
| } |