Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher3; |
| 18 | |
| 19 | import android.content.Context; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 20 | import android.content.res.TypedArray; |
Jon Miranda | 177ad2b | 2016-09-21 14:27:41 -0700 | [diff] [blame^] | 21 | import android.graphics.Rect; |
Hyunyoung Song | a97c64b | 2016-06-30 11:53:44 -0700 | [diff] [blame] | 22 | import android.graphics.drawable.ColorDrawable; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 23 | import android.graphics.drawable.Drawable; |
| 24 | import android.graphics.drawable.InsetDrawable; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 25 | import android.util.AttributeSet; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 26 | import android.view.View; |
| 27 | import android.widget.FrameLayout; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 28 | |
Hyunyoung Song | 645764e | 2016-06-06 14:19:02 -0700 | [diff] [blame] | 29 | import com.android.launcher3.allapps.AllAppsContainerView; |
| 30 | import com.android.launcher3.config.FeatureFlags; |
Jon Miranda | 177ad2b | 2016-09-21 14:27:41 -0700 | [diff] [blame^] | 31 | import com.android.launcher3.util.TransformingTouchDelegate; |
Hyunyoung Song | 645764e | 2016-06-06 14:19:02 -0700 | [diff] [blame] | 32 | |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 33 | /** |
| 34 | * A base container view, which supports resizing. |
| 35 | */ |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 36 | public abstract class BaseContainerView extends FrameLayout |
| 37 | implements DeviceProfile.LauncherLayoutChangeListener { |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 38 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 39 | protected int mContainerPaddingLeft; |
| 40 | protected int mContainerPaddingRight; |
| 41 | protected int mContainerPaddingTop; |
| 42 | protected int mContainerPaddingBottom; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 43 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 44 | protected final Drawable mBaseDrawable; |
Jon Miranda | 177ad2b | 2016-09-21 14:27:41 -0700 | [diff] [blame^] | 45 | private final Rect mBgPaddingRect = new Rect(); |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 46 | |
| 47 | private View mRevealView; |
| 48 | private View mContent; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 49 | |
Jon Miranda | 177ad2b | 2016-09-21 14:27:41 -0700 | [diff] [blame^] | 50 | private TransformingTouchDelegate mTouchDelegate; |
| 51 | |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 52 | 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 Goyal | 05c8c57 | 2016-03-17 11:57:24 -0700 | [diff] [blame] | 62 | |
Hyunyoung Song | a97c64b | 2016-06-30 11:53:44 -0700 | [diff] [blame] | 63 | if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && this instanceof AllAppsContainerView) { |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 64 | mBaseDrawable = new ColorDrawable(); |
Hyunyoung Song | a97c64b | 2016-06-30 11:53:44 -0700 | [diff] [blame] | 65 | } else { |
| 66 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 67 | R.styleable.BaseContainerView, defStyleAttr, 0); |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 68 | mBaseDrawable = a.getDrawable(R.styleable.BaseContainerView_revealBackground); |
Hyunyoung Song | a97c64b | 2016-06-30 11:53:44 -0700 | [diff] [blame] | 69 | a.recycle(); |
| 70 | } |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 74 | protected void onAttachedToWindow() { |
| 75 | super.onAttachedToWindow(); |
| 76 | |
| 77 | DeviceProfile grid = Launcher.getLauncher(getContext()).getDeviceProfile(); |
| 78 | grid.addLauncherLayoutChangedListener(this); |
Jon Miranda | 177ad2b | 2016-09-21 14:27:41 -0700 | [diff] [blame^] | 79 | |
| 80 | View touchDelegateTargetView = getTouchDelegateTargetView(); |
| 81 | if (touchDelegateTargetView != null) { |
| 82 | mTouchDelegate = new TransformingTouchDelegate(touchDelegateTargetView); |
| 83 | ((View) touchDelegateTargetView.getParent()).setTouchDelegate(mTouchDelegate); |
| 84 | } |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 85 | } |
| 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 Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 96 | protected void onFinishInflate() { |
| 97 | super.onFinishInflate(); |
| 98 | |
| 99 | mContent = findViewById(R.id.main_content); |
| 100 | mRevealView = findViewById(R.id.reveal_view); |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 101 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 102 | updatePaddings(); |
| 103 | } |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 104 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 105 | @Override |
Jon Miranda | 177ad2b | 2016-09-21 14:27:41 -0700 | [diff] [blame^] | 106 | 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 |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 121 | public void onLauncherLayoutChanged() { |
| 122 | updatePaddings(); |
| 123 | } |
| 124 | |
| 125 | public void setRevealDrawableColor(int color) { |
| 126 | ((ColorDrawable) mBaseDrawable).setColor(color); |
Winson Chung | 73f0b9b | 2015-07-08 14:13:08 -0700 | [diff] [blame] | 127 | } |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 128 | |
| 129 | public final View getContentView() { |
| 130 | return mContent; |
| 131 | } |
| 132 | |
| 133 | public final View getRevealView() { |
| 134 | return mRevealView; |
| 135 | } |
Hyunyoung Song | e4be3b3 | 2016-07-18 16:35:10 -0700 | [diff] [blame] | 136 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 137 | 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 Miranda | 177ad2b | 2016-09-21 14:27:41 -0700 | [diff] [blame^] | 158 | InsetDrawable revealDrawable = new InsetDrawable(mBaseDrawable, |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 159 | mContainerPaddingLeft, mContainerPaddingTop, mContainerPaddingRight, |
| 160 | mContainerPaddingBottom); |
Jon Miranda | 177ad2b | 2016-09-21 14:27:41 -0700 | [diff] [blame^] | 161 | mRevealView.setBackground(revealDrawable); |
| 162 | mContent.setBackground(revealDrawable); |
Hyunyoung Song | e4be3b3 | 2016-07-18 16:35:10 -0700 | [diff] [blame] | 163 | } |
Jon Miranda | 177ad2b | 2016-09-21 14:27:41 -0700 | [diff] [blame^] | 164 | |
| 165 | public abstract View getTouchDelegateTargetView(); |
Andrew Sapperstein | abef55a | 2016-06-19 12:49:00 -0700 | [diff] [blame] | 166 | } |