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