blob: 96942ee6f8c709265fdd1dec37f643dc83a548dc [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;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080020import android.content.res.TypedArray;
Hyunyoung Songa97c64b2016-06-30 11:53:44 -070021import android.graphics.drawable.ColorDrawable;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080022import android.graphics.drawable.Drawable;
23import android.graphics.drawable.InsetDrawable;
Winson Chung94804152015-05-08 13:06:44 -070024import android.util.AttributeSet;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080025import android.view.View;
26import android.widget.FrameLayout;
Winson Chung94804152015-05-08 13:06:44 -070027
Hyunyoung Song645764e2016-06-06 14:19:02 -070028import com.android.launcher3.allapps.AllAppsContainerView;
29import com.android.launcher3.config.FeatureFlags;
30
Winson Chung94804152015-05-08 13:06:44 -070031/**
32 * A base container view, which supports resizing.
33 */
Winson1f064272016-07-18 17:18:02 -070034public abstract class BaseContainerView extends FrameLayout
35 implements DeviceProfile.LauncherLayoutChangeListener {
Winson Chung94804152015-05-08 13:06:44 -070036
Winson1f064272016-07-18 17:18:02 -070037 protected int mContainerPaddingLeft;
38 protected int mContainerPaddingRight;
39 protected int mContainerPaddingTop;
40 protected int mContainerPaddingBottom;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080041
Winson1f064272016-07-18 17:18:02 -070042 private InsetDrawable mRevealDrawable;
43 protected final Drawable mBaseDrawable;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080044
45 private View mRevealView;
46 private View mContent;
Winson Chung94804152015-05-08 13:06:44 -070047
48 public BaseContainerView(Context context) {
49 this(context, null);
50 }
51
52 public BaseContainerView(Context context, AttributeSet attrs) {
53 this(context, attrs, 0);
54 }
55
56 public BaseContainerView(Context context, AttributeSet attrs, int defStyleAttr) {
57 super(context, attrs, defStyleAttr);
Sunny Goyal05c8c572016-03-17 11:57:24 -070058
Hyunyoung Songa97c64b2016-06-30 11:53:44 -070059 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && this instanceof AllAppsContainerView) {
Winson1f064272016-07-18 17:18:02 -070060 mBaseDrawable = new ColorDrawable();
Hyunyoung Songa97c64b2016-06-30 11:53:44 -070061 } else {
62 TypedArray a = context.obtainStyledAttributes(attrs,
63 R.styleable.BaseContainerView, defStyleAttr, 0);
Winson1f064272016-07-18 17:18:02 -070064 mBaseDrawable = a.getDrawable(R.styleable.BaseContainerView_revealBackground);
Hyunyoung Songa97c64b2016-06-30 11:53:44 -070065 a.recycle();
66 }
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080067 }
68
69 @Override
Winson1f064272016-07-18 17:18:02 -070070 protected void onAttachedToWindow() {
71 super.onAttachedToWindow();
72
73 DeviceProfile grid = Launcher.getLauncher(getContext()).getDeviceProfile();
74 grid.addLauncherLayoutChangedListener(this);
75 }
76
77 @Override
78 protected void onDetachedFromWindow() {
79 super.onDetachedFromWindow();
80
81 DeviceProfile grid = Launcher.getLauncher(getContext()).getDeviceProfile();
82 grid.removeLauncherLayoutChangedListener(this);
83 }
84
85 @Override
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080086 protected void onFinishInflate() {
87 super.onFinishInflate();
88
89 mContent = findViewById(R.id.main_content);
90 mRevealView = findViewById(R.id.reveal_view);
Winson Chung94804152015-05-08 13:06:44 -070091
Winson1f064272016-07-18 17:18:02 -070092 updatePaddings();
93 }
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080094
Winson1f064272016-07-18 17:18:02 -070095 @Override
96 public void onLauncherLayoutChanged() {
97 updatePaddings();
98 }
99
100 public void setRevealDrawableColor(int color) {
101 ((ColorDrawable) mBaseDrawable).setColor(color);
Winson Chung73f0b9b2015-07-08 14:13:08 -0700102 }
Sunny Goyal0ac7ede2016-01-29 13:14:14 -0800103
104 public final View getContentView() {
105 return mContent;
106 }
107
108 public final View getRevealView() {
109 return mRevealView;
110 }
Hyunyoung Songe4be3b32016-07-18 16:35:10 -0700111
Winson1f064272016-07-18 17:18:02 -0700112 private void updatePaddings() {
113 Context context = getContext();
114 Launcher launcher = Launcher.getLauncher(context);
115
116 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP &&
117 this instanceof AllAppsContainerView &&
118 !launcher.getDeviceProfile().isVerticalBarLayout()) {
119 mContainerPaddingLeft = mContainerPaddingRight = 0;
120 mContainerPaddingTop = mContainerPaddingBottom = 0;
121 } else {
122 DeviceProfile grid = launcher.getDeviceProfile();
123 int[] padding = grid.getContainerPadding(context);
124 mContainerPaddingLeft = padding[0] + grid.edgeMarginPx;
125 mContainerPaddingRight = padding[1] + grid.edgeMarginPx;
126 if (!launcher.getDeviceProfile().isVerticalBarLayout()) {
127 mContainerPaddingTop = mContainerPaddingBottom = grid.edgeMarginPx;
128 } else {
129 mContainerPaddingTop = mContainerPaddingBottom = 0;
130 }
131 }
132
133 mRevealDrawable = new InsetDrawable(mBaseDrawable,
134 mContainerPaddingLeft, mContainerPaddingTop, mContainerPaddingRight,
135 mContainerPaddingBottom);
136 mRevealView.setBackground(mRevealDrawable);
137 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && this instanceof AllAppsContainerView) {
138 // Skip updating the content background
139 } else {
140 mContent.setBackground(mRevealDrawable);
141 }
Hyunyoung Songe4be3b32016-07-18 16:35:10 -0700142 }
Andrew Sappersteinabef55a2016-06-19 12:49:00 -0700143}