blob: 2a84432219bbe4b93f75caef055ded91619cb280 [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;
62 mFixedBounds.bottom = getMeasuredHeight();
63 }
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
66 mFixedBounds.inset(0, mFixedBoundsContainerInset);
67 onFixedBoundsUpdated();
68 }
69 // Post the updates since they can trigger a relayout, and this call can be triggered from
70 // a layout pass itself.
71 post(new Runnable() {
72 @Override
73 public void run() {
74 onUpdateBackgrounds();
75 onUpdatePaddings();
76 }
77 });
78 }
79
80 /**
81 * Update the UI in response to a change in the fixed bounds.
82 */
83 protected void onFixedBoundsUpdated() {
84 // Do nothing
85 }
86
87 /**
88 * Update the paddings in response to a change in the bounds or insets.
89 */
90 protected void onUpdatePaddings() {
91 // Do nothing
92 }
93
94 /**
95 * Update the backgrounds in response to a change in the bounds or insets.
96 */
97 protected void onUpdateBackgrounds() {
98 // Do nothing
99 }
100}