blob: c11824054c8a9fb35b7b322453fb290063aeb00e [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 Chung73f0b9b2015-07-08 14:13:08 -070022import android.util.Log;
Winson Chungef7f8742015-06-04 17:18:17 -070023import android.widget.LinearLayout;
Winson Chung94804152015-05-08 13:06:44 -070024
25/**
26 * A base container view, which supports resizing.
27 */
Winson Chungef7f8742015-06-04 17:18:17 -070028public abstract class BaseContainerView extends LinearLayout implements Insettable {
Winson Chung94804152015-05-08 13:06:44 -070029
Winson Chung73f0b9b2015-07-08 14:13:08 -070030 private final static String TAG = "BaseContainerView";
31
Winson Chungef7f8742015-06-04 17:18:17 -070032 // 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 Chung73f0b9b2015-07-08 14:13:08 -070036 private Rect mFixedSearchBarBounds = new Rect();
Winsonb0ca1a22015-08-06 14:42:50 -070037 // The computed bounds of the search bar
38 private Rect mSearchBarBounds = new Rect();
39 // The computed bounds of the container
Winson Chungef7f8742015-06-04 17:18:17 -070040 protected Rect mContentBounds = new Rect();
Winsonb0ca1a22015-08-06 14:42:50 -070041 // The computed padding to apply to the container to achieve the container bounds
42 private Rect mContentPadding = new Rect();
Winson Chungef7f8742015-06-04 17:18:17 -070043 // The inset to apply to the edges and between the search bar and the container
44 private int mContainerBoundsInset;
45 private boolean mHasSearchBar;
Winson Chung94804152015-05-08 13:06:44 -070046
47 public BaseContainerView(Context context) {
48 this(context, null);
49 }
50
51 public BaseContainerView(Context context, AttributeSet attrs) {
52 this(context, attrs, 0);
53 }
54
55 public BaseContainerView(Context context, AttributeSet attrs, int defStyleAttr) {
56 super(context, attrs, defStyleAttr);
Winson Chungef7f8742015-06-04 17:18:17 -070057 mContainerBoundsInset = getResources().getDimensionPixelSize(R.dimen.container_bounds_inset);
Winson Chung94804152015-05-08 13:06:44 -070058 }
59
60 @Override
61 final public void setInsets(Rect insets) {
62 mInsets.set(insets);
Winson Chungef7f8742015-06-04 17:18:17 -070063 updateBackgroundAndPaddings();
64 }
65
66 protected void setHasSearchBar() {
67 mHasSearchBar = true;
Winson Chung94804152015-05-08 13:06:44 -070068 }
69
70 /**
Winson Chungef7f8742015-06-04 17:18:17 -070071 * Sets the search bar bounds for this container view to match.
Winson Chung94804152015-05-08 13:06:44 -070072 */
Winson Chungef7f8742015-06-04 17:18:17 -070073 final public void setSearchBarBounds(Rect bounds) {
Winson Chung73f0b9b2015-07-08 14:13:08 -070074 if (LauncherAppState.isDogfoodBuild() && !isValidSearchBarBounds(bounds)) {
75 Log.e(TAG, "Invalid search bar bounds: " + bounds);
76 }
77
78 mFixedSearchBarBounds.set(bounds);
Winson Chungef7f8742015-06-04 17:18:17 -070079
Winson Chung94804152015-05-08 13:06:44 -070080 // Post the updates since they can trigger a relayout, and this call can be triggered from
81 // a layout pass itself.
82 post(new Runnable() {
83 @Override
84 public void run() {
Winson Chungef7f8742015-06-04 17:18:17 -070085 updateBackgroundAndPaddings();
Winson Chung94804152015-05-08 13:06:44 -070086 }
87 });
88 }
89
90 /**
Winson Chungef7f8742015-06-04 17:18:17 -070091 * Update the backgrounds and padding in response to a change in the bounds or insets.
Winson Chung94804152015-05-08 13:06:44 -070092 */
Winson Chungef7f8742015-06-04 17:18:17 -070093 protected void updateBackgroundAndPaddings() {
94 Rect padding;
Winsonb0ca1a22015-08-06 14:42:50 -070095 Rect searchBarBounds = new Rect();
Winson Chung73f0b9b2015-07-08 14:13:08 -070096 if (!isValidSearchBarBounds(mFixedSearchBarBounds)) {
97 // Use the default bounds
Winson Chungef7f8742015-06-04 17:18:17 -070098 padding = new Rect(mInsets.left + mContainerBoundsInset,
99 (mHasSearchBar ? 0 : (mInsets.top + mContainerBoundsInset)),
100 mInsets.right + mContainerBoundsInset,
101 mInsets.bottom + mContainerBoundsInset);
102
103 // Special case -- we have the search bar, but no specific bounds, so just give it
104 // the inset bounds without a height.
105 searchBarBounds.set(mInsets.left + mContainerBoundsInset,
106 mInsets.top + mContainerBoundsInset,
107 getMeasuredWidth() - (mInsets.right + mContainerBoundsInset), 0);
108 } else {
109 // Use the search bounds, if there is a search bar, the bounds will contain
110 // the offsets for the insets so we can ignore that
Winson Chung73f0b9b2015-07-08 14:13:08 -0700111 padding = new Rect(mFixedSearchBarBounds.left,
Winson Chungef7f8742015-06-04 17:18:17 -0700112 (mHasSearchBar ? 0 : (mInsets.top + mContainerBoundsInset)),
Winson Chung73f0b9b2015-07-08 14:13:08 -0700113 getMeasuredWidth() - mFixedSearchBarBounds.right,
Winson Chungef7f8742015-06-04 17:18:17 -0700114 mInsets.bottom + mContainerBoundsInset);
Winsonb0ca1a22015-08-06 14:42:50 -0700115
116 // Use the search bounds
117 searchBarBounds.set(mFixedSearchBarBounds);
Winson Chungef7f8742015-06-04 17:18:17 -0700118 }
Winsonb0ca1a22015-08-06 14:42:50 -0700119
120 // If either the computed container padding has changed, or the computed search bar bounds
121 // has changed, then notify the container
122 if (!padding.equals(mContentPadding) || !searchBarBounds.equals(mSearchBarBounds)) {
Winson Chungef7f8742015-06-04 17:18:17 -0700123 mContentPadding.set(padding);
124 mContentBounds.set(padding.left, padding.top,
125 getMeasuredWidth() - padding.right,
126 getMeasuredHeight() - padding.bottom);
Winsonb0ca1a22015-08-06 14:42:50 -0700127 mSearchBarBounds.set(searchBarBounds);
128 onUpdateBackgroundAndPaddings(mSearchBarBounds, padding);
Winson Chungef7f8742015-06-04 17:18:17 -0700129 }
Winson Chung94804152015-05-08 13:06:44 -0700130 }
131
132 /**
Winson Chungef7f8742015-06-04 17:18:17 -0700133 * To be implemented by container views to update themselves when the bounds changes.
Winson Chung94804152015-05-08 13:06:44 -0700134 */
Winson Chungef7f8742015-06-04 17:18:17 -0700135 protected abstract void onUpdateBackgroundAndPaddings(Rect searchBarBounds, Rect padding);
Winson Chung73f0b9b2015-07-08 14:13:08 -0700136
137 /**
138 * Returns whether the search bar bounds we got are considered valid.
139 */
140 private boolean isValidSearchBarBounds(Rect searchBarBounds) {
141 return !searchBarBounds.isEmpty() &&
142 searchBarBounds.right <= getMeasuredWidth() &&
143 searchBarBounds.bottom <= getMeasuredHeight();
144 }
Winson Chung94804152015-05-08 13:06:44 -0700145}