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 | |
Jon Miranda | 3298d0f | 2016-09-22 16:50:17 -0700 | [diff] [blame^] | 19 | import android.annotation.SuppressLint; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 21 | import android.content.res.TypedArray; |
Jon Miranda | 3298d0f | 2016-09-22 16:50:17 -0700 | [diff] [blame^] | 22 | import android.graphics.Point; |
| 23 | import android.graphics.PointF; |
Jon Miranda | 177ad2b | 2016-09-21 14:27:41 -0700 | [diff] [blame] | 24 | import android.graphics.Rect; |
Hyunyoung Song | a97c64b | 2016-06-30 11:53:44 -0700 | [diff] [blame] | 25 | import android.graphics.drawable.ColorDrawable; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 26 | import android.graphics.drawable.Drawable; |
| 27 | import android.graphics.drawable.InsetDrawable; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 28 | import android.util.AttributeSet; |
Jon Miranda | 3298d0f | 2016-09-22 16:50:17 -0700 | [diff] [blame^] | 29 | import android.view.MotionEvent; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 30 | import android.view.View; |
Jon Miranda | 3298d0f | 2016-09-22 16:50:17 -0700 | [diff] [blame^] | 31 | import android.view.ViewConfiguration; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 32 | import android.widget.FrameLayout; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 33 | |
Hyunyoung Song | 645764e | 2016-06-06 14:19:02 -0700 | [diff] [blame] | 34 | import com.android.launcher3.allapps.AllAppsContainerView; |
| 35 | import com.android.launcher3.config.FeatureFlags; |
Jon Miranda | 177ad2b | 2016-09-21 14:27:41 -0700 | [diff] [blame] | 36 | import com.android.launcher3.util.TransformingTouchDelegate; |
Hyunyoung Song | 645764e | 2016-06-06 14:19:02 -0700 | [diff] [blame] | 37 | |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 38 | /** |
| 39 | * A base container view, which supports resizing. |
| 40 | */ |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 41 | public abstract class BaseContainerView extends FrameLayout |
| 42 | implements DeviceProfile.LauncherLayoutChangeListener { |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 43 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 44 | protected int mContainerPaddingLeft; |
| 45 | protected int mContainerPaddingRight; |
| 46 | protected int mContainerPaddingTop; |
| 47 | protected int mContainerPaddingBottom; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 48 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 49 | protected final Drawable mBaseDrawable; |
Jon Miranda | 177ad2b | 2016-09-21 14:27:41 -0700 | [diff] [blame] | 50 | private final Rect mBgPaddingRect = new Rect(); |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 51 | |
| 52 | private View mRevealView; |
| 53 | private View mContent; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 54 | |
Jon Miranda | 177ad2b | 2016-09-21 14:27:41 -0700 | [diff] [blame] | 55 | private TransformingTouchDelegate mTouchDelegate; |
| 56 | |
Jon Miranda | 3298d0f | 2016-09-22 16:50:17 -0700 | [diff] [blame^] | 57 | private final PointF mLastTouchDownPosPx = new PointF(-1.0f, -1.0f); |
| 58 | |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 59 | public BaseContainerView(Context context) { |
| 60 | this(context, null); |
| 61 | } |
| 62 | |
| 63 | public BaseContainerView(Context context, AttributeSet attrs) { |
| 64 | this(context, attrs, 0); |
| 65 | } |
| 66 | |
| 67 | public BaseContainerView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 68 | super(context, attrs, defStyleAttr); |
Sunny Goyal | 05c8c57 | 2016-03-17 11:57:24 -0700 | [diff] [blame] | 69 | |
Hyunyoung Song | a97c64b | 2016-06-30 11:53:44 -0700 | [diff] [blame] | 70 | if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && this instanceof AllAppsContainerView) { |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 71 | mBaseDrawable = new ColorDrawable(); |
Hyunyoung Song | a97c64b | 2016-06-30 11:53:44 -0700 | [diff] [blame] | 72 | } else { |
| 73 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 74 | R.styleable.BaseContainerView, defStyleAttr, 0); |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 75 | mBaseDrawable = a.getDrawable(R.styleable.BaseContainerView_revealBackground); |
Hyunyoung Song | a97c64b | 2016-06-30 11:53:44 -0700 | [diff] [blame] | 76 | a.recycle(); |
| 77 | } |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 81 | protected void onAttachedToWindow() { |
| 82 | super.onAttachedToWindow(); |
| 83 | |
| 84 | DeviceProfile grid = Launcher.getLauncher(getContext()).getDeviceProfile(); |
| 85 | grid.addLauncherLayoutChangedListener(this); |
Jon Miranda | 177ad2b | 2016-09-21 14:27:41 -0700 | [diff] [blame] | 86 | |
| 87 | View touchDelegateTargetView = getTouchDelegateTargetView(); |
| 88 | if (touchDelegateTargetView != null) { |
| 89 | mTouchDelegate = new TransformingTouchDelegate(touchDelegateTargetView); |
| 90 | ((View) touchDelegateTargetView.getParent()).setTouchDelegate(mTouchDelegate); |
| 91 | } |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
| 95 | protected void onDetachedFromWindow() { |
| 96 | super.onDetachedFromWindow(); |
| 97 | |
| 98 | DeviceProfile grid = Launcher.getLauncher(getContext()).getDeviceProfile(); |
| 99 | grid.removeLauncherLayoutChangedListener(this); |
| 100 | } |
| 101 | |
| 102 | @Override |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 103 | protected void onFinishInflate() { |
| 104 | super.onFinishInflate(); |
| 105 | |
| 106 | mContent = findViewById(R.id.main_content); |
| 107 | mRevealView = findViewById(R.id.reveal_view); |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 108 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 109 | updatePaddings(); |
| 110 | } |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 111 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 112 | @Override |
Jon Miranda | 177ad2b | 2016-09-21 14:27:41 -0700 | [diff] [blame] | 113 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 114 | super.onLayout(changed, left, top, right, bottom); |
| 115 | getRevealView().getBackground().getPadding(mBgPaddingRect); |
| 116 | |
| 117 | View touchDelegateTargetView = getTouchDelegateTargetView(); |
| 118 | if (touchDelegateTargetView != null) { |
| 119 | mTouchDelegate.setBounds( |
| 120 | touchDelegateTargetView.getLeft() - mBgPaddingRect.left, |
| 121 | touchDelegateTargetView.getTop() - mBgPaddingRect.top, |
| 122 | touchDelegateTargetView.getRight() + mBgPaddingRect.right, |
| 123 | touchDelegateTargetView.getBottom() + mBgPaddingRect.bottom); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | @Override |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 128 | public void onLauncherLayoutChanged() { |
| 129 | updatePaddings(); |
| 130 | } |
| 131 | |
Jon Miranda | 3298d0f | 2016-09-22 16:50:17 -0700 | [diff] [blame^] | 132 | @Override |
| 133 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 134 | return handleTouchEvent(ev); |
| 135 | } |
| 136 | |
| 137 | @SuppressLint("ClickableViewAccessibility") |
| 138 | @Override |
| 139 | public boolean onTouchEvent(MotionEvent ev) { |
| 140 | return handleTouchEvent(ev); |
| 141 | } |
| 142 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 143 | public void setRevealDrawableColor(int color) { |
| 144 | ((ColorDrawable) mBaseDrawable).setColor(color); |
Winson Chung | 73f0b9b | 2015-07-08 14:13:08 -0700 | [diff] [blame] | 145 | } |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 146 | |
| 147 | public final View getContentView() { |
| 148 | return mContent; |
| 149 | } |
| 150 | |
| 151 | public final View getRevealView() { |
| 152 | return mRevealView; |
| 153 | } |
Hyunyoung Song | e4be3b3 | 2016-07-18 16:35:10 -0700 | [diff] [blame] | 154 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 155 | private void updatePaddings() { |
| 156 | Context context = getContext(); |
| 157 | Launcher launcher = Launcher.getLauncher(context); |
| 158 | |
| 159 | if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && |
| 160 | this instanceof AllAppsContainerView && |
| 161 | !launcher.getDeviceProfile().isVerticalBarLayout()) { |
| 162 | mContainerPaddingLeft = mContainerPaddingRight = 0; |
| 163 | mContainerPaddingTop = mContainerPaddingBottom = 0; |
| 164 | } else { |
| 165 | DeviceProfile grid = launcher.getDeviceProfile(); |
| 166 | int[] padding = grid.getContainerPadding(context); |
| 167 | mContainerPaddingLeft = padding[0] + grid.edgeMarginPx; |
| 168 | mContainerPaddingRight = padding[1] + grid.edgeMarginPx; |
| 169 | if (!launcher.getDeviceProfile().isVerticalBarLayout()) { |
| 170 | mContainerPaddingTop = mContainerPaddingBottom = grid.edgeMarginPx; |
| 171 | } else { |
| 172 | mContainerPaddingTop = mContainerPaddingBottom = 0; |
| 173 | } |
| 174 | } |
| 175 | |
Jon Miranda | 177ad2b | 2016-09-21 14:27:41 -0700 | [diff] [blame] | 176 | InsetDrawable revealDrawable = new InsetDrawable(mBaseDrawable, |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 177 | mContainerPaddingLeft, mContainerPaddingTop, mContainerPaddingRight, |
| 178 | mContainerPaddingBottom); |
Jon Miranda | 177ad2b | 2016-09-21 14:27:41 -0700 | [diff] [blame] | 179 | mRevealView.setBackground(revealDrawable); |
| 180 | mContent.setBackground(revealDrawable); |
Hyunyoung Song | e4be3b3 | 2016-07-18 16:35:10 -0700 | [diff] [blame] | 181 | } |
Jon Miranda | 177ad2b | 2016-09-21 14:27:41 -0700 | [diff] [blame] | 182 | |
Jon Miranda | 3298d0f | 2016-09-22 16:50:17 -0700 | [diff] [blame^] | 183 | /** |
| 184 | * Handles the touch events that shows the workspace when clicking outside the bounds of the |
| 185 | * touch delegate target view. |
| 186 | */ |
| 187 | private boolean handleTouchEvent(MotionEvent ev) { |
| 188 | switch (ev.getAction()) { |
| 189 | case MotionEvent.ACTION_DOWN: |
| 190 | // Check if the touch is outside touch delegate target view |
| 191 | View touchDelegateTargetView = getTouchDelegateTargetView(); |
| 192 | float leftBoundPx = touchDelegateTargetView.getLeft(); |
| 193 | if (ev.getX() < leftBoundPx || |
| 194 | ev.getX() > (touchDelegateTargetView.getWidth() + leftBoundPx)) { |
| 195 | mLastTouchDownPosPx.set((int) ev.getX(), (int) ev.getY()); |
| 196 | } |
| 197 | break; |
| 198 | case MotionEvent.ACTION_UP: |
| 199 | if (mLastTouchDownPosPx.x > -1) { |
| 200 | ViewConfiguration viewConfig = ViewConfiguration.get(getContext()); |
| 201 | float dx = ev.getX() - mLastTouchDownPosPx.x; |
| 202 | float dy = ev.getY() - mLastTouchDownPosPx.y; |
| 203 | float distance = PointF.length(dx, dy); |
| 204 | if (distance < viewConfig.getScaledTouchSlop()) { |
| 205 | // The background was clicked, so just go home |
| 206 | Launcher.getLauncher(getContext()).showWorkspace(true); |
| 207 | return true; |
| 208 | } |
| 209 | } |
| 210 | // Fall through |
| 211 | case MotionEvent.ACTION_CANCEL: |
| 212 | mLastTouchDownPosPx.set(-1, -1); |
| 213 | break; |
| 214 | } |
| 215 | return false; |
| 216 | } |
| 217 | |
Jon Miranda | 177ad2b | 2016-09-21 14:27:41 -0700 | [diff] [blame] | 218 | public abstract View getTouchDelegateTargetView(); |
Andrew Sapperstein | abef55a | 2016-06-19 12:49:00 -0700 | [diff] [blame] | 219 | } |