Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.graphics.Rect; |
| 21 | import android.util.AttributeSet; |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 22 | import android.widget.LinearLayout; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * A base container view, which supports resizing. |
| 26 | */ |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 27 | public abstract class BaseContainerView extends LinearLayout implements Insettable { |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 28 | |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 29 | // The window insets |
| 30 | private Rect mInsets = new Rect(); |
| 31 | // The bounds of the search bar. Only the left, top, right are used to inset the |
| 32 | // search bar and the height is determined by the measurement of the layout |
| 33 | private Rect mSearchBarBounds = new Rect(); |
| 34 | // The bounds of the container |
| 35 | protected Rect mContentBounds = new Rect(); |
| 36 | // The padding to apply to the container to achieve the bounds |
| 37 | protected Rect mContentPadding = new Rect(); |
| 38 | // The inset to apply to the edges and between the search bar and the container |
| 39 | private int mContainerBoundsInset; |
| 40 | private boolean mHasSearchBar; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 41 | |
| 42 | public BaseContainerView(Context context) { |
| 43 | this(context, null); |
| 44 | } |
| 45 | |
| 46 | public BaseContainerView(Context context, AttributeSet attrs) { |
| 47 | this(context, attrs, 0); |
| 48 | } |
| 49 | |
| 50 | public BaseContainerView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 51 | super(context, attrs, defStyleAttr); |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 52 | mContainerBoundsInset = getResources().getDimensionPixelSize(R.dimen.container_bounds_inset); |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Override |
| 56 | final public void setInsets(Rect insets) { |
| 57 | mInsets.set(insets); |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 58 | updateBackgroundAndPaddings(); |
| 59 | } |
| 60 | |
| 61 | protected void setHasSearchBar() { |
| 62 | mHasSearchBar = true; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 66 | * Sets the search bar bounds for this container view to match. |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 67 | */ |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 68 | final public void setSearchBarBounds(Rect bounds) { |
| 69 | mSearchBarBounds.set(bounds); |
| 70 | |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 71 | // Post the updates since they can trigger a relayout, and this call can be triggered from |
| 72 | // a layout pass itself. |
| 73 | post(new Runnable() { |
| 74 | @Override |
| 75 | public void run() { |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 76 | updateBackgroundAndPaddings(); |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 77 | } |
| 78 | }); |
| 79 | } |
| 80 | |
| 81 | /** |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 82 | * Update the backgrounds and padding in response to a change in the bounds or insets. |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 83 | */ |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 84 | protected void updateBackgroundAndPaddings() { |
| 85 | Rect padding; |
| 86 | Rect searchBarBounds = new Rect(mSearchBarBounds); |
| 87 | if (mSearchBarBounds.isEmpty()) { |
| 88 | // Use the normal bounds |
| 89 | padding = new Rect(mInsets.left + mContainerBoundsInset, |
| 90 | (mHasSearchBar ? 0 : (mInsets.top + mContainerBoundsInset)), |
| 91 | mInsets.right + mContainerBoundsInset, |
| 92 | mInsets.bottom + mContainerBoundsInset); |
| 93 | |
| 94 | // Special case -- we have the search bar, but no specific bounds, so just give it |
| 95 | // the inset bounds without a height. |
| 96 | searchBarBounds.set(mInsets.left + mContainerBoundsInset, |
| 97 | mInsets.top + mContainerBoundsInset, |
| 98 | getMeasuredWidth() - (mInsets.right + mContainerBoundsInset), 0); |
| 99 | } else { |
| 100 | // Use the search bounds, if there is a search bar, the bounds will contain |
| 101 | // the offsets for the insets so we can ignore that |
| 102 | padding = new Rect(mSearchBarBounds.left, |
| 103 | (mHasSearchBar ? 0 : (mInsets.top + mContainerBoundsInset)), |
| 104 | getMeasuredWidth() - mSearchBarBounds.right, |
| 105 | mInsets.bottom + mContainerBoundsInset); |
| 106 | } |
| 107 | if (!padding.equals(mContentPadding) || !searchBarBounds.equals(mSearchBarBounds)) { |
| 108 | mContentPadding.set(padding); |
| 109 | mContentBounds.set(padding.left, padding.top, |
| 110 | getMeasuredWidth() - padding.right, |
| 111 | getMeasuredHeight() - padding.bottom); |
| 112 | mSearchBarBounds.set(searchBarBounds); |
| 113 | onUpdateBackgroundAndPaddings(mSearchBarBounds, padding); |
| 114 | } |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 118 | * To be implemented by container views to update themselves when the bounds changes. |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 119 | */ |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 120 | protected abstract void onUpdateBackgroundAndPaddings(Rect searchBarBounds, Rect padding); |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 121 | } |