1 /* 2 * Copyright (C) 2016 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.incallui.answer.impl; 18 19 import android.content.Context; 20 import android.content.res.Configuration; 21 import android.support.annotation.Nullable; 22 import android.util.AttributeSet; 23 import android.view.MotionEvent; 24 import android.view.View; 25 import android.widget.FrameLayout; 26 import com.android.incallui.answer.impl.affordance.SwipeButtonHelper; 27 import com.android.incallui.answer.impl.affordance.SwipeButtonHelper.Callback; 28 import com.android.incallui.answer.impl.affordance.SwipeButtonView; 29 import com.android.incallui.util.AccessibilityUtil; 30 31 /** Layout that delegates touches to its SwipeButtonHelper */ 32 public class AffordanceHolderLayout extends FrameLayout { 33 34 private SwipeButtonHelper affordanceHelper; 35 36 private Callback affordanceCallback; 37 AffordanceHolderLayout(Context context)38 public AffordanceHolderLayout(Context context) { 39 this(context, null); 40 } 41 AffordanceHolderLayout(Context context, AttributeSet attrs)42 public AffordanceHolderLayout(Context context, AttributeSet attrs) { 43 this(context, attrs, 0); 44 } 45 AffordanceHolderLayout(Context context, AttributeSet attrs, int defStyleAttr)46 public AffordanceHolderLayout(Context context, AttributeSet attrs, int defStyleAttr) { 47 super(context, attrs, defStyleAttr); 48 49 affordanceHelper = 50 new SwipeButtonHelper( 51 new Callback() { 52 @Override 53 public void onAnimationToSideStarted( 54 boolean rightPage, float translation, float vel) { 55 if (affordanceCallback != null) { 56 affordanceCallback.onAnimationToSideStarted(rightPage, translation, vel); 57 } 58 } 59 60 @Override 61 public void onAnimationToSideEnded(boolean rightPage) { 62 if (affordanceCallback != null) { 63 affordanceCallback.onAnimationToSideEnded(rightPage); 64 } 65 } 66 67 @Override 68 public float getMaxTranslationDistance() { 69 if (affordanceCallback != null) { 70 return affordanceCallback.getMaxTranslationDistance(); 71 } 72 return 0; 73 } 74 75 @Override 76 public void onSwipingStarted(boolean rightIcon) { 77 if (affordanceCallback != null) { 78 affordanceCallback.onSwipingStarted(rightIcon); 79 } 80 } 81 82 @Override 83 public void onSwipingAborted() { 84 if (affordanceCallback != null) { 85 affordanceCallback.onSwipingAborted(); 86 } 87 } 88 89 @Override 90 public void onIconClicked(boolean rightIcon) { 91 if (affordanceCallback != null) { 92 affordanceCallback.onIconClicked(rightIcon); 93 } 94 } 95 96 @Nullable 97 @Override 98 public SwipeButtonView getLeftIcon() { 99 if (affordanceCallback != null) { 100 return affordanceCallback.getLeftIcon(); 101 } 102 return null; 103 } 104 105 @Nullable 106 @Override 107 public SwipeButtonView getRightIcon() { 108 if (affordanceCallback != null) { 109 return affordanceCallback.getRightIcon(); 110 } 111 return null; 112 } 113 114 @Nullable 115 @Override 116 public View getLeftPreview() { 117 if (affordanceCallback != null) { 118 return affordanceCallback.getLeftPreview(); 119 } 120 return null; 121 } 122 123 @Nullable 124 @Override 125 public View getRightPreview() { 126 if (affordanceCallback != null) { 127 affordanceCallback.getRightPreview(); 128 } 129 return null; 130 } 131 132 @Override 133 public float getAffordanceFalsingFactor() { 134 if (affordanceCallback != null) { 135 return affordanceCallback.getAffordanceFalsingFactor(); 136 } 137 return 1.0f; 138 } 139 }, 140 context); 141 } 142 setAffordanceCallback(@ullable Callback callback)143 public void setAffordanceCallback(@Nullable Callback callback) { 144 affordanceCallback = callback; 145 affordanceHelper.init(); 146 } 147 startHintAnimation(boolean rightIcon, @Nullable Runnable onFinishListener)148 public void startHintAnimation(boolean rightIcon, @Nullable Runnable onFinishListener) { 149 affordanceHelper.startHintAnimation(rightIcon, onFinishListener); 150 } 151 animateHideLeftRightIcon()152 public void animateHideLeftRightIcon() { 153 affordanceHelper.animateHideLeftRightIcon(); 154 } 155 reset(boolean animate)156 public void reset(boolean animate) { 157 affordanceHelper.reset(animate); 158 } 159 160 @Override onInterceptTouchEvent(MotionEvent event)161 public boolean onInterceptTouchEvent(MotionEvent event) { 162 if (AccessibilityUtil.isTouchExplorationEnabled(getContext())) { 163 return false; 164 } 165 return affordanceHelper.onTouchEvent(event) || super.onInterceptTouchEvent(event); 166 } 167 168 @Override onTouchEvent(MotionEvent event)169 public boolean onTouchEvent(MotionEvent event) { 170 return affordanceHelper.onTouchEvent(event) || super.onTouchEvent(event); 171 } 172 173 @Override onConfigurationChanged(Configuration newConfig)174 protected void onConfigurationChanged(Configuration newConfig) { 175 super.onConfigurationChanged(newConfig); 176 affordanceHelper.onConfigurationChanged(); 177 } 178 } 179