1 /* 2 * Copyright (C) 2021 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.systemui.car.hvac; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.view.KeyEvent; 22 import android.view.MotionEvent; 23 24 import androidx.annotation.Nullable; 25 import androidx.constraintlayout.widget.ConstraintLayout; 26 27 /** A view that contains interfaces for the HVAC Panel application and intercepts the key events. */ 28 public class HvacPanelView extends ConstraintLayout { 29 private KeyEventHandler mKeyEventHandler; 30 private MotionEventHandler mMotionEventHandler; 31 HvacPanelView(Context context)32 public HvacPanelView(Context context) { 33 super(context); 34 } 35 HvacPanelView(Context context, @Nullable AttributeSet attrs)36 public HvacPanelView(Context context, @Nullable AttributeSet attrs) { 37 super(context, attrs); 38 } 39 HvacPanelView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)40 public HvacPanelView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 41 super(context, attrs, defStyleAttr); 42 } 43 HvacPanelView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)44 public HvacPanelView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 45 super(context, attrs, defStyleAttr, defStyleRes); 46 } 47 48 @Override onInterceptTouchEvent(MotionEvent ev)49 public boolean onInterceptTouchEvent(MotionEvent ev) { 50 boolean result = super.onInterceptTouchEvent(ev); 51 52 if (mMotionEventHandler != null) { 53 mMotionEventHandler.onInterceptTouchEvent(ev); 54 } 55 56 return result; 57 } 58 59 @Override dispatchKeyEvent(KeyEvent event)60 public boolean dispatchKeyEvent(KeyEvent event) { 61 if (super.dispatchKeyEvent(event)) { 62 return true; 63 } 64 65 if (mKeyEventHandler != null) { 66 return mKeyEventHandler.dispatchKeyEvent(event); 67 } 68 69 return false; 70 } 71 72 /** Sets a {@link KeyEventHandler} to help interact with the HVAC panel. */ setKeyEventHandler(KeyEventHandler keyEventHandler)73 public void setKeyEventHandler(KeyEventHandler keyEventHandler) { 74 mKeyEventHandler = keyEventHandler; 75 } 76 77 /** Gets the {@link KeyEventHandler} of the HVAC panel. */ getKeyEventHandler()78 public KeyEventHandler getKeyEventHandler() { 79 return mKeyEventHandler; 80 } 81 82 /** An interface to help interact with the HVAC panel. */ 83 public interface KeyEventHandler { 84 /** 85 * Allows handling of a {@link KeyEvent} if it wasn't already handled by the superclass. 86 * Returns {@code true} if the event was handled and false otherwise. 87 */ dispatchKeyEvent(KeyEvent event)88 boolean dispatchKeyEvent(KeyEvent event); 89 } 90 91 /** Sets a {@link MotionEventHandler} to help process motion events in the HVAC panel. */ setMotionEventHandler(MotionEventHandler motionEventHandler)92 public void setMotionEventHandler(MotionEventHandler motionEventHandler) { 93 mMotionEventHandler = motionEventHandler; 94 } 95 96 /** An interface to help process motion events in the HVAC panel. */ 97 public interface MotionEventHandler { 98 /** 99 * Intercepts incoming events of type {@link MotionEvent}. This can be used to know when the 100 * last interaction with the HVAC panel might have happened. 101 */ onInterceptTouchEvent(MotionEvent event)102 void onInterceptTouchEvent(MotionEvent event); 103 } 104 } 105