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; |
Hyunyoung Song | a97c64b | 2016-06-30 11:53:44 -0700 | [diff] [blame] | 21 | import android.graphics.drawable.ColorDrawable; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 22 | import android.graphics.drawable.Drawable; |
| 23 | import android.graphics.drawable.InsetDrawable; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 24 | import android.util.AttributeSet; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 25 | import android.view.View; |
| 26 | import android.widget.FrameLayout; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 27 | |
Hyunyoung Song | 645764e | 2016-06-06 14:19:02 -0700 | [diff] [blame] | 28 | import com.android.launcher3.allapps.AllAppsContainerView; |
| 29 | import com.android.launcher3.config.FeatureFlags; |
| 30 | |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 31 | /** |
| 32 | * A base container view, which supports resizing. |
| 33 | */ |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 34 | public abstract class BaseContainerView extends FrameLayout |
| 35 | implements DeviceProfile.LauncherLayoutChangeListener { |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 36 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 37 | protected int mContainerPaddingLeft; |
| 38 | protected int mContainerPaddingRight; |
| 39 | protected int mContainerPaddingTop; |
| 40 | protected int mContainerPaddingBottom; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 41 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 42 | private InsetDrawable mRevealDrawable; |
| 43 | protected final Drawable mBaseDrawable; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 44 | |
| 45 | private View mRevealView; |
| 46 | private View mContent; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 47 | |
| 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 Goyal | 05c8c57 | 2016-03-17 11:57:24 -0700 | [diff] [blame] | 58 | |
Hyunyoung Song | a97c64b | 2016-06-30 11:53:44 -0700 | [diff] [blame] | 59 | if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && this instanceof AllAppsContainerView) { |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 60 | mBaseDrawable = new ColorDrawable(); |
Hyunyoung Song | a97c64b | 2016-06-30 11:53:44 -0700 | [diff] [blame] | 61 | } else { |
| 62 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 63 | R.styleable.BaseContainerView, defStyleAttr, 0); |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 64 | mBaseDrawable = a.getDrawable(R.styleable.BaseContainerView_revealBackground); |
Hyunyoung Song | a97c64b | 2016-06-30 11:53:44 -0700 | [diff] [blame] | 65 | a.recycle(); |
| 66 | } |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Override |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 70 | 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 Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 86 | protected void onFinishInflate() { |
| 87 | super.onFinishInflate(); |
| 88 | |
| 89 | mContent = findViewById(R.id.main_content); |
| 90 | mRevealView = findViewById(R.id.reveal_view); |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 91 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 92 | updatePaddings(); |
| 93 | } |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 94 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 95 | @Override |
| 96 | public void onLauncherLayoutChanged() { |
| 97 | updatePaddings(); |
| 98 | } |
| 99 | |
| 100 | public void setRevealDrawableColor(int color) { |
| 101 | ((ColorDrawable) mBaseDrawable).setColor(color); |
Winson Chung | 73f0b9b | 2015-07-08 14:13:08 -0700 | [diff] [blame] | 102 | } |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 103 | |
| 104 | public final View getContentView() { |
| 105 | return mContent; |
| 106 | } |
| 107 | |
| 108 | public final View getRevealView() { |
| 109 | return mRevealView; |
| 110 | } |
Hyunyoung Song | e4be3b3 | 2016-07-18 16:35:10 -0700 | [diff] [blame] | 111 | |
Winson | 1f06427 | 2016-07-18 17:18:02 -0700 | [diff] [blame] | 112 | 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 Song | e4be3b3 | 2016-07-18 16:35:10 -0700 | [diff] [blame] | 142 | } |
Andrew Sapperstein | abef55a | 2016-06-19 12:49:00 -0700 | [diff] [blame] | 143 | } |