blob: fe951068a4b5a73b8c55fca58ada6c6274b60d29 [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;
Jon Miranda177ad2b2016-09-21 14:27:41 -070021import android.graphics.Rect;
Hyunyoung Songa97c64b2016-06-30 11:53:44 -070022import android.graphics.drawable.ColorDrawable;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080023import android.graphics.drawable.Drawable;
24import android.graphics.drawable.InsetDrawable;
Winson Chung94804152015-05-08 13:06:44 -070025import android.util.AttributeSet;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080026import android.view.View;
27import android.widget.FrameLayout;
Winson Chung94804152015-05-08 13:06:44 -070028
Hyunyoung Song645764e2016-06-06 14:19:02 -070029import com.android.launcher3.allapps.AllAppsContainerView;
30import com.android.launcher3.config.FeatureFlags;
Jon Miranda177ad2b2016-09-21 14:27:41 -070031import com.android.launcher3.util.TransformingTouchDelegate;
Hyunyoung Song645764e2016-06-06 14:19:02 -070032
Winson Chung94804152015-05-08 13:06:44 -070033/**
34 * A base container view, which supports resizing.
35 */
Winson1f064272016-07-18 17:18:02 -070036public abstract class BaseContainerView extends FrameLayout
37 implements DeviceProfile.LauncherLayoutChangeListener {
Winson Chung94804152015-05-08 13:06:44 -070038
Winson1f064272016-07-18 17:18:02 -070039 protected int mContainerPaddingLeft;
40 protected int mContainerPaddingRight;
41 protected int mContainerPaddingTop;
42 protected int mContainerPaddingBottom;
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080043
Winson1f064272016-07-18 17:18:02 -070044 protected final Drawable mBaseDrawable;
Jon Miranda177ad2b2016-09-21 14:27:41 -070045 private final Rect mBgPaddingRect = new Rect();
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080046
47 private View mRevealView;
48 private View mContent;
Winson Chung94804152015-05-08 13:06:44 -070049
Jon Miranda177ad2b2016-09-21 14:27:41 -070050 private TransformingTouchDelegate mTouchDelegate;
51
Winson Chung94804152015-05-08 13:06:44 -070052 public BaseContainerView(Context context) {
53 this(context, null);
54 }
55
56 public BaseContainerView(Context context, AttributeSet attrs) {
57 this(context, attrs, 0);
58 }
59
60 public BaseContainerView(Context context, AttributeSet attrs, int defStyleAttr) {
61 super(context, attrs, defStyleAttr);
Sunny Goyal05c8c572016-03-17 11:57:24 -070062
Hyunyoung Songa97c64b2016-06-30 11:53:44 -070063 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && this instanceof AllAppsContainerView) {
Winson1f064272016-07-18 17:18:02 -070064 mBaseDrawable = new ColorDrawable();
Hyunyoung Songa97c64b2016-06-30 11:53:44 -070065 } else {
66 TypedArray a = context.obtainStyledAttributes(attrs,
67 R.styleable.BaseContainerView, defStyleAttr, 0);
Winson1f064272016-07-18 17:18:02 -070068 mBaseDrawable = a.getDrawable(R.styleable.BaseContainerView_revealBackground);
Hyunyoung Songa97c64b2016-06-30 11:53:44 -070069 a.recycle();
70 }
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080071 }
72
73 @Override
Winson1f064272016-07-18 17:18:02 -070074 protected void onAttachedToWindow() {
75 super.onAttachedToWindow();
76
77 DeviceProfile grid = Launcher.getLauncher(getContext()).getDeviceProfile();
78 grid.addLauncherLayoutChangedListener(this);
Jon Miranda177ad2b2016-09-21 14:27:41 -070079
80 View touchDelegateTargetView = getTouchDelegateTargetView();
81 if (touchDelegateTargetView != null) {
82 mTouchDelegate = new TransformingTouchDelegate(touchDelegateTargetView);
83 ((View) touchDelegateTargetView.getParent()).setTouchDelegate(mTouchDelegate);
84 }
Winson1f064272016-07-18 17:18:02 -070085 }
86
87 @Override
88 protected void onDetachedFromWindow() {
89 super.onDetachedFromWindow();
90
91 DeviceProfile grid = Launcher.getLauncher(getContext()).getDeviceProfile();
92 grid.removeLauncherLayoutChangedListener(this);
93 }
94
95 @Override
Sunny Goyal0ac7ede2016-01-29 13:14:14 -080096 protected void onFinishInflate() {
97 super.onFinishInflate();
98
99 mContent = findViewById(R.id.main_content);
100 mRevealView = findViewById(R.id.reveal_view);
Winson Chung94804152015-05-08 13:06:44 -0700101
Winson1f064272016-07-18 17:18:02 -0700102 updatePaddings();
103 }
Sunny Goyal0ac7ede2016-01-29 13:14:14 -0800104
Winson1f064272016-07-18 17:18:02 -0700105 @Override
Jon Miranda177ad2b2016-09-21 14:27:41 -0700106 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
107 super.onLayout(changed, left, top, right, bottom);
108 getRevealView().getBackground().getPadding(mBgPaddingRect);
109
110 View touchDelegateTargetView = getTouchDelegateTargetView();
111 if (touchDelegateTargetView != null) {
112 mTouchDelegate.setBounds(
113 touchDelegateTargetView.getLeft() - mBgPaddingRect.left,
114 touchDelegateTargetView.getTop() - mBgPaddingRect.top,
115 touchDelegateTargetView.getRight() + mBgPaddingRect.right,
116 touchDelegateTargetView.getBottom() + mBgPaddingRect.bottom);
117 }
118 }
119
120 @Override
Winson1f064272016-07-18 17:18:02 -0700121 public void onLauncherLayoutChanged() {
122 updatePaddings();
123 }
124
125 public void setRevealDrawableColor(int color) {
126 ((ColorDrawable) mBaseDrawable).setColor(color);
Winson Chung73f0b9b2015-07-08 14:13:08 -0700127 }
Sunny Goyal0ac7ede2016-01-29 13:14:14 -0800128
129 public final View getContentView() {
130 return mContent;
131 }
132
133 public final View getRevealView() {
134 return mRevealView;
135 }
Hyunyoung Songe4be3b32016-07-18 16:35:10 -0700136
Winson1f064272016-07-18 17:18:02 -0700137 private void updatePaddings() {
138 Context context = getContext();
139 Launcher launcher = Launcher.getLauncher(context);
140
141 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP &&
142 this instanceof AllAppsContainerView &&
143 !launcher.getDeviceProfile().isVerticalBarLayout()) {
144 mContainerPaddingLeft = mContainerPaddingRight = 0;
145 mContainerPaddingTop = mContainerPaddingBottom = 0;
146 } else {
147 DeviceProfile grid = launcher.getDeviceProfile();
148 int[] padding = grid.getContainerPadding(context);
149 mContainerPaddingLeft = padding[0] + grid.edgeMarginPx;
150 mContainerPaddingRight = padding[1] + grid.edgeMarginPx;
151 if (!launcher.getDeviceProfile().isVerticalBarLayout()) {
152 mContainerPaddingTop = mContainerPaddingBottom = grid.edgeMarginPx;
153 } else {
154 mContainerPaddingTop = mContainerPaddingBottom = 0;
155 }
156 }
157
Jon Miranda177ad2b2016-09-21 14:27:41 -0700158 InsetDrawable revealDrawable = new InsetDrawable(mBaseDrawable,
Winson1f064272016-07-18 17:18:02 -0700159 mContainerPaddingLeft, mContainerPaddingTop, mContainerPaddingRight,
160 mContainerPaddingBottom);
Jon Miranda177ad2b2016-09-21 14:27:41 -0700161 mRevealView.setBackground(revealDrawable);
162 mContent.setBackground(revealDrawable);
Hyunyoung Songe4be3b32016-07-18 16:35:10 -0700163 }
Jon Miranda177ad2b2016-09-21 14:27:41 -0700164
165 public abstract View getTouchDelegateTargetView();
Andrew Sappersteinabef55a2016-06-19 12:49:00 -0700166}