/* * Copyright 2013 Gleb Godonoga. * * 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.andrada.sitracker.ui.widget; import android.graphics.Rect; import android.view.MotionEvent; import android.view.TouchDelegate; import android.view.View; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; public class TouchDelegateGroup extends TouchDelegate { private static final Rect USELESS_HACKY_RECT = new Rect(); @Nullable private ArrayList<TouchDelegate> mTouchDelegates; @Nullable private TouchDelegate mCurrentTouchDelegate; public TouchDelegateGroup(@NotNull View uselessHackyView) { // I know this is pretty hacky. Unfortunately there is no other way to // create a TouchDelegate containing TouchDelegates since TouchDelegate // is not an interface ... super(USELESS_HACKY_RECT, uselessHackyView); } public void addTouchDelegate(TouchDelegate touchDelegate) { if (mTouchDelegates == null) { mTouchDelegates = new ArrayList<TouchDelegate>(); } mTouchDelegates.add(touchDelegate); } public void removeTouchDelegate(TouchDelegate touchDelegate) { if (mTouchDelegates != null) { mTouchDelegates.remove(touchDelegate); if (mTouchDelegates.isEmpty()) { mTouchDelegates = null; } } } public void clearTouchDelegates() { if (mTouchDelegates != null) { mTouchDelegates.clear(); } mCurrentTouchDelegate = null; } @Override public boolean onTouchEvent(@NotNull MotionEvent event) { TouchDelegate delegate = null; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if (mTouchDelegates != null) { for (TouchDelegate touchDelegate : mTouchDelegates) { if (touchDelegate != null) { if (touchDelegate.onTouchEvent(event)) { mCurrentTouchDelegate = touchDelegate; return true; } } } } break; case MotionEvent.ACTION_MOVE: delegate = mCurrentTouchDelegate; break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: delegate = mCurrentTouchDelegate; mCurrentTouchDelegate = null; break; } return delegate != null && delegate.onTouchEvent(event); } }