• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.PagedView;
23 
24 /**
25  * {@link FocusIndicatorHelper} for a generic view group.
26  */
27 public class ViewGroupFocusHelper extends FocusIndicatorHelper {
28 
29     private final View mContainer;
30 
ViewGroupFocusHelper(View container)31     public ViewGroupFocusHelper(View container) {
32         super(container);
33         mContainer = container;
34     }
35 
36     @Override
viewToRect(View v, Rect outRect)37     public void viewToRect(View v, Rect outRect) {
38         outRect.left = 0;
39         outRect.top = 0;
40 
41         computeLocationRelativeToContainer(v, outRect);
42 
43         // If a view is scaled, its position will also shift accordingly. For optimization, only
44         // consider this for the last node.
45         outRect.left += (1 - v.getScaleX()) * v.getWidth() / 2;
46         outRect.top += (1 - v.getScaleY()) * v.getHeight() / 2;
47 
48         outRect.right = outRect.left + (int) (v.getScaleX() * v.getWidth());
49         outRect.bottom = outRect.top + (int) (v.getScaleY() * v.getHeight());
50     }
51 
computeLocationRelativeToContainer(View child, Rect outRect)52     private void computeLocationRelativeToContainer(View child, Rect outRect) {
53         View parent = (View) child.getParent();
54         outRect.left += child.getX();
55         outRect.top += child.getY();
56 
57         if (parent != mContainer) {
58             if (parent instanceof PagedView) {
59                 PagedView page = (PagedView) parent;
60                 outRect.left -= page.getScrollForPage(page.indexOfChild(child));
61             }
62 
63             computeLocationRelativeToContainer(parent, outRect);
64         }
65     }
66 }
67