blob: 4b7b977757cf07db7b5086634d6d9be0a0b6f287 [file] [log] [blame]
Winson Chung94804152015-05-08 13:06:44 -07001/*
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
17package com.android.launcher3;
18
19import android.content.Context;
20import android.graphics.Rect;
21import android.util.AttributeSet;
Winson Chungef7f8742015-06-04 17:18:17 -070022import android.widget.LinearLayout;
Winson Chung94804152015-05-08 13:06:44 -070023
24/**
25 * A base container view, which supports resizing.
26 */
Winson Chungef7f8742015-06-04 17:18:17 -070027public abstract class BaseContainerView extends LinearLayout implements Insettable {
Winson Chung94804152015-05-08 13:06:44 -070028
Winson Chungef7f8742015-06-04 17:18:17 -070029 // 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 Chung94804152015-05-08 13:06:44 -070041
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 Chungef7f8742015-06-04 17:18:17 -070052 mContainerBoundsInset = getResources().getDimensionPixelSize(R.dimen.container_bounds_inset);
Winson Chung94804152015-05-08 13:06:44 -070053 }
54
55 @Override
56 final public void setInsets(Rect insets) {
57 mInsets.set(insets);
Winson Chungef7f8742015-06-04 17:18:17 -070058 updateBackgroundAndPaddings();
59 }
60
61 protected void setHasSearchBar() {
62 mHasSearchBar = true;
Winson Chung94804152015-05-08 13:06:44 -070063 }
64
65 /**
Winson Chungef7f8742015-06-04 17:18:17 -070066 * Sets the search bar bounds for this container view to match.
Winson Chung94804152015-05-08 13:06:44 -070067 */
Winson Chungef7f8742015-06-04 17:18:17 -070068 final public void setSearchBarBounds(Rect bounds) {
69 mSearchBarBounds.set(bounds);
70
Winson Chung94804152015-05-08 13:06:44 -070071 // 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 Chungef7f8742015-06-04 17:18:17 -070076 updateBackgroundAndPaddings();
Winson Chung94804152015-05-08 13:06:44 -070077 }
78 });
79 }
80
81 /**
Winson Chungef7f8742015-06-04 17:18:17 -070082 * Update the backgrounds and padding in response to a change in the bounds or insets.
Winson Chung94804152015-05-08 13:06:44 -070083 */
Winson Chungef7f8742015-06-04 17:18:17 -070084 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 Chung94804152015-05-08 13:06:44 -0700115 }
116
117 /**
Winson Chungef7f8742015-06-04 17:18:17 -0700118 * To be implemented by container views to update themselves when the bounds changes.
Winson Chung94804152015-05-08 13:06:44 -0700119 */
Winson Chungef7f8742015-06-04 17:18:17 -0700120 protected abstract void onUpdateBackgroundAndPaddings(Rect searchBarBounds, Rect padding);
Winson Chung94804152015-05-08 13:06:44 -0700121}