blob: bd1c625e34bf5595c346870c301b81671d0d6f85 [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;
22import android.widget.FrameLayout;
23
24/**
25 * A base container view, which supports resizing.
26 */
27public class BaseContainerView extends FrameLayout implements Insettable {
28
29 protected Rect mInsets = new Rect();
30 protected Rect mFixedBounds = new Rect();
31 protected int mFixedBoundsContainerInset;
32
33 public BaseContainerView(Context context) {
34 this(context, null);
35 }
36
37 public BaseContainerView(Context context, AttributeSet attrs) {
38 this(context, attrs, 0);
39 }
40
41 public BaseContainerView(Context context, AttributeSet attrs, int defStyleAttr) {
42 super(context, attrs, defStyleAttr);
43 mFixedBoundsContainerInset = context.getResources().getDimensionPixelSize(
44 R.dimen.container_fixed_bounds_inset);
45 }
46
47 @Override
48 final public void setInsets(Rect insets) {
49 mInsets.set(insets);
50 onUpdateBackgrounds();
51 onUpdatePaddings();
52 }
53
54 /**
55 * Sets the fixed bounds for this container view.
56 */
57 final public void setFixedBounds(Rect fixedBounds) {
58 if (!fixedBounds.isEmpty() && !fixedBounds.equals(mFixedBounds)) {
59 mFixedBounds.set(fixedBounds);
60 if (Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
61 mFixedBounds.top = mInsets.top;
Winson Chung13eb5272015-05-11 16:30:13 -070062 mFixedBounds.bottom = mInsets.bottom;
Winson Chung94804152015-05-08 13:06:44 -070063 }
64 // To ensure that the child RecyclerView has the full width to handle touches right to
65 // the edge of the screen, we only apply the top and bottom padding to the bounds
Winson Chung13eb5272015-05-11 16:30:13 -070066 mFixedBounds.top += mFixedBoundsContainerInset;
67 mFixedBounds.bottom += mFixedBoundsContainerInset;
Winson Chung94804152015-05-08 13:06:44 -070068 onFixedBoundsUpdated();
69 }
70 // Post the updates since they can trigger a relayout, and this call can be triggered from
71 // a layout pass itself.
72 post(new Runnable() {
73 @Override
74 public void run() {
75 onUpdateBackgrounds();
76 onUpdatePaddings();
77 }
78 });
79 }
80
81 /**
82 * Update the UI in response to a change in the fixed bounds.
83 */
84 protected void onFixedBoundsUpdated() {
85 // Do nothing
86 }
87
88 /**
89 * Update the paddings in response to a change in the bounds or insets.
90 */
91 protected void onUpdatePaddings() {
92 // Do nothing
93 }
94
95 /**
96 * Update the backgrounds in response to a change in the bounds or insets.
97 */
98 protected void onUpdateBackgrounds() {
99 // Do nothing
100 }
101}