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; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame^] | 20 | import android.content.res.TypedArray; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 21 | import android.graphics.Rect; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame^] | 22 | import android.graphics.drawable.Drawable; |
| 23 | import android.graphics.drawable.InsetDrawable; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 24 | import android.util.AttributeSet; |
Winson Chung | 73f0b9b | 2015-07-08 14:13:08 -0700 | [diff] [blame] | 25 | import android.util.Log; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame^] | 26 | import android.view.View; |
| 27 | import android.widget.FrameLayout; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * A base container view, which supports resizing. |
| 31 | */ |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame^] | 32 | public abstract class BaseContainerView extends FrameLayout implements Insettable { |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 33 | |
Winson Chung | 73f0b9b | 2015-07-08 14:13:08 -0700 | [diff] [blame] | 34 | private final static String TAG = "BaseContainerView"; |
| 35 | |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 36 | // The window insets |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame^] | 37 | private final Rect mInsets = new Rect(); |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 38 | // The bounds of the search bar. Only the left, top, right are used to inset the |
| 39 | // search bar and the height is determined by the measurement of the layout |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame^] | 40 | private final Rect mFixedSearchBarBounds = new Rect(); |
Winson | b0ca1a2 | 2015-08-06 14:42:50 -0700 | [diff] [blame] | 41 | // The computed bounds of the container |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame^] | 42 | protected final Rect mContentBounds = new Rect(); |
Winson | b0ca1a2 | 2015-08-06 14:42:50 -0700 | [diff] [blame] | 43 | // The computed padding to apply to the container to achieve the container bounds |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame^] | 44 | private final Rect mContentPadding = new Rect(); |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 45 | // The inset to apply to the edges and between the search bar and the container |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame^] | 46 | private final int mContainerBoundsInset; |
| 47 | |
| 48 | private final Drawable mRevealDrawable; |
| 49 | |
| 50 | private View mRevealView; |
| 51 | private View mContent; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 52 | |
| 53 | public BaseContainerView(Context context) { |
| 54 | this(context, null); |
| 55 | } |
| 56 | |
| 57 | public BaseContainerView(Context context, AttributeSet attrs) { |
| 58 | this(context, attrs, 0); |
| 59 | } |
| 60 | |
| 61 | public BaseContainerView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 62 | super(context, attrs, defStyleAttr); |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 63 | mContainerBoundsInset = getResources().getDimensionPixelSize(R.dimen.container_bounds_inset); |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame^] | 64 | |
| 65 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 66 | R.styleable.BaseContainerView, defStyleAttr, 0); |
| 67 | mRevealDrawable = a.getDrawable(R.styleable.BaseContainerView_revealBackground); |
| 68 | a.recycle(); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | protected void onFinishInflate() { |
| 73 | super.onFinishInflate(); |
| 74 | |
| 75 | mContent = findViewById(R.id.main_content); |
| 76 | mRevealView = findViewById(R.id.reveal_view); |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Override |
| 80 | final public void setInsets(Rect insets) { |
| 81 | mInsets.set(insets); |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 82 | updateBackgroundAndPaddings(); |
| 83 | } |
| 84 | |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 85 | /** |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 86 | * Sets the search bar bounds for this container view to match. |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 87 | */ |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 88 | final public void setSearchBarBounds(Rect bounds) { |
Winson Chung | 73f0b9b | 2015-07-08 14:13:08 -0700 | [diff] [blame] | 89 | if (LauncherAppState.isDogfoodBuild() && !isValidSearchBarBounds(bounds)) { |
| 90 | Log.e(TAG, "Invalid search bar bounds: " + bounds); |
| 91 | } |
| 92 | |
| 93 | mFixedSearchBarBounds.set(bounds); |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 94 | |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 95 | // Post the updates since they can trigger a relayout, and this call can be triggered from |
| 96 | // a layout pass itself. |
| 97 | post(new Runnable() { |
| 98 | @Override |
| 99 | public void run() { |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 100 | updateBackgroundAndPaddings(); |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 101 | } |
| 102 | }); |
| 103 | } |
| 104 | |
| 105 | /** |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 106 | * 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] | 107 | */ |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 108 | protected void updateBackgroundAndPaddings() { |
| 109 | Rect padding; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame^] | 110 | if (isValidSearchBarBounds(mFixedSearchBarBounds)) { |
| 111 | padding = new Rect( |
| 112 | mFixedSearchBarBounds.left, |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 113 | mInsets.top + mContainerBoundsInset, |
Winson Chung | 73f0b9b | 2015-07-08 14:13:08 -0700 | [diff] [blame] | 114 | getMeasuredWidth() - mFixedSearchBarBounds.right, |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame^] | 115 | mInsets.bottom + mContainerBoundsInset |
| 116 | ); |
| 117 | } else { |
| 118 | padding = new Rect( |
| 119 | mInsets.left + mContainerBoundsInset, |
| 120 | mInsets.top + mContainerBoundsInset, |
| 121 | mInsets.right + mContainerBoundsInset, |
| 122 | mInsets.bottom + mContainerBoundsInset |
| 123 | ); |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 124 | } |
Winson | b0ca1a2 | 2015-08-06 14:42:50 -0700 | [diff] [blame] | 125 | |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame^] | 126 | // The container padding changed, notify the container. |
| 127 | if (!padding.equals(mContentPadding)) { |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 128 | mContentPadding.set(padding); |
| 129 | mContentBounds.set(padding.left, padding.top, |
| 130 | getMeasuredWidth() - padding.right, |
| 131 | getMeasuredHeight() - padding.bottom); |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame^] | 132 | onUpdateBackgroundAndPaddings(padding); |
Winson Chung | ef7f874 | 2015-06-04 17:18:17 -0700 | [diff] [blame] | 133 | } |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame^] | 136 | private void onUpdateBackgroundAndPaddings(Rect padding) { |
| 137 | // Apply the top-bottom padding to itself so that the launcher transition is |
| 138 | // clipped correctly |
| 139 | setPadding(0, padding.top, 0, padding.bottom); |
| 140 | |
| 141 | InsetDrawable background = new InsetDrawable(mRevealDrawable, |
| 142 | padding.left, 0, padding.right, 0); |
| 143 | mRevealView.setBackground(background.getConstantState().newDrawable()); |
| 144 | mContent.setBackground(background); |
| 145 | |
| 146 | Rect bgPadding = new Rect(); |
| 147 | background.getPadding(bgPadding); |
| 148 | onUpdateBgPadding(padding, bgPadding); |
| 149 | } |
| 150 | |
| 151 | protected abstract void onUpdateBgPadding(Rect padding, Rect bgPadding); |
Winson Chung | 73f0b9b | 2015-07-08 14:13:08 -0700 | [diff] [blame] | 152 | |
| 153 | /** |
| 154 | * Returns whether the search bar bounds we got are considered valid. |
| 155 | */ |
| 156 | private boolean isValidSearchBarBounds(Rect searchBarBounds) { |
| 157 | return !searchBarBounds.isEmpty() && |
| 158 | searchBarBounds.right <= getMeasuredWidth() && |
| 159 | searchBarBounds.bottom <= getMeasuredHeight(); |
| 160 | } |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame^] | 161 | |
| 162 | public final View getContentView() { |
| 163 | return mContent; |
| 164 | } |
| 165 | |
| 166 | public final View getRevealView() { |
| 167 | return mRevealView; |
| 168 | } |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 169 | } |