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.launcher3.keyboard; 18 19 import android.graphics.Rect; 20 import android.view.View; 21 import android.view.View.OnFocusChangeListener; 22 23 import com.android.launcher3.PagedView; 24 25 /** 26 * {@link FocusIndicatorHelper} for a generic view group. 27 */ 28 public class ViewGroupFocusHelper extends FocusIndicatorHelper { 29 30 private final View mContainer; 31 32 public ViewGroupFocusHelper(View container) { 33 super(container); 34 mContainer = container; 35 } 36 37 @Override 38 public void viewToRect(View v, Rect outRect) { 39 outRect.left = 0; 40 outRect.top = 0; 41 42 computeLocationRelativeToContainer(v, outRect); 43 44 // If a view is scaled, its position will also shift accordingly. For optimization, only 45 // consider this for the last node. 46 outRect.left += (1 - v.getScaleX()) * v.getWidth() / 2; 47 outRect.top += (1 - v.getScaleY()) * v.getHeight() / 2; 48 49 outRect.right = outRect.left + (int) (v.getScaleX() * v.getWidth()); 50 outRect.bottom = outRect.top + (int) (v.getScaleY() * v.getHeight()); 51 } 52 53 private void computeLocationRelativeToContainer(View child, Rect outRect) { 54 View parent = (View) child.getParent(); 55 outRect.left += child.getLeft(); 56 outRect.top += child.getTop(); 57 58 if (parent != mContainer) { 59 if (parent instanceof PagedView) { 60 PagedView page = (PagedView) parent; 61 outRect.left -= page.getScrollForPage(page.indexOfChild(child)); 62 } 63 64 computeLocationRelativeToContainer(parent, outRect); 65 } 66 } 67 68 /** 69 * Sets the alpha of this FocusIndicatorHelper to 0 when a view with this listener 70 * receives focus. 71 */ 72 public View.OnFocusChangeListener getHideIndicatorOnFocusListener() { 73 return new OnFocusChangeListener() { 74 @Override 75 public void onFocusChange(View v, boolean hasFocus) { 76 if (hasFocus) { 77 endCurrentAnimation(); 78 setCurrentView(null); 79 setAlpha(0); 80 invalidateDirty(); 81 } 82 } 83 }; 84 } 85 } 86