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