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 22 import com.android.launcher3.Flags; 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 private static final Rect sTempRect = new Rect(); 32 ViewGroupFocusHelper(View container)33 public ViewGroupFocusHelper(View container) { 34 super(container); 35 mContainer = container; 36 } 37 38 @Override shouldDraw(View item)39 protected boolean shouldDraw(View item) { 40 if (Flags.enableFocusOutline()) { 41 // Not draw outline in page transition because the outline just remains fully 42 // persistent during the transition and does not look smooth 43 return super.shouldDraw(item) && !isInPageTransition(item); 44 } else { 45 return super.shouldDraw(item); 46 } 47 } 48 isInPageTransition(View view)49 private boolean isInPageTransition(View view) { 50 if (view == null || !(view.getParent() instanceof View)) { 51 return false; 52 } 53 boolean isInTransition = false; 54 if (view instanceof PagedView) { 55 isInTransition = ((PagedView<?>) view).isPageInTransition(); 56 } 57 return isInTransition || isInPageTransition((View) view.getParent()); 58 } 59 60 @Override viewToRect(View v, Rect outRect)61 public void viewToRect(View v, Rect outRect) { 62 // Using FocusedRect here allows views to provide their custom rect for drawing outline, 63 // e.g. making the Rect bigger than the content to leave some padding between view and 64 // outline 65 v.getFocusedRect(sTempRect); 66 outRect.left = sTempRect.left; 67 outRect.top = sTempRect.top; 68 69 computeLocationRelativeToContainer(v, outRect); 70 71 // If a view is scaled, its position will also shift accordingly. For optimization, only 72 // consider this for the last node. 73 outRect.left = (int) (outRect.left + (1 - v.getScaleX()) * sTempRect.width() / 2); 74 outRect.top = (int) (outRect.top + (1 - v.getScaleY()) * sTempRect.height() / 2); 75 76 outRect.right = outRect.left + (int) (v.getScaleX() * sTempRect.width()); 77 outRect.bottom = outRect.top + (int) (v.getScaleY() * sTempRect.height()); 78 } 79 computeLocationRelativeToContainer(View child, Rect outRect)80 private void computeLocationRelativeToContainer(View child, Rect outRect) { 81 if (child == null) { 82 return; 83 } 84 85 outRect.left += child.getX(); 86 outRect.top += child.getY(); 87 88 if (child.getParent() == null || !(child.getParent() instanceof View)) { 89 return; 90 } 91 92 View parent = (View) child.getParent(); 93 if (parent != mContainer) { 94 if (parent instanceof PagedView) { 95 PagedView page = (PagedView) parent; 96 outRect.left -= page.getScrollForPage(page.indexOfChild(child)); 97 } 98 99 computeLocationRelativeToContainer(parent, outRect); 100 } 101 } 102 } 103