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; 20 import android.content.res.TypedArray; 21 import android.graphics.drawable.ColorDrawable; 22 import android.graphics.drawable.Drawable; 23 import android.graphics.drawable.InsetDrawable; 24 import android.util.AttributeSet; 25 import android.view.View; 26 import android.widget.FrameLayout; 27 28 import com.android.launcher3.allapps.AllAppsContainerView; 29 import com.android.launcher3.config.FeatureFlags; 30 31 /** 32 * A base container view, which supports resizing. 33 */ 34 public abstract class BaseContainerView extends FrameLayout 35 implements DeviceProfile.LauncherLayoutChangeListener { 36 37 protected int mContainerPaddingLeft; 38 protected int mContainerPaddingRight; 39 protected int mContainerPaddingTop; 40 protected int mContainerPaddingBottom; 41 42 private InsetDrawable mRevealDrawable; 43 protected final Drawable mBaseDrawable; 44 45 private View mRevealView; 46 private View mContent; 47 BaseContainerView(Context context)48 public BaseContainerView(Context context) { 49 this(context, null); 50 } 51 BaseContainerView(Context context, AttributeSet attrs)52 public BaseContainerView(Context context, AttributeSet attrs) { 53 this(context, attrs, 0); 54 } 55 BaseContainerView(Context context, AttributeSet attrs, int defStyleAttr)56 public BaseContainerView(Context context, AttributeSet attrs, int defStyleAttr) { 57 super(context, attrs, defStyleAttr); 58 59 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && this instanceof AllAppsContainerView) { 60 mBaseDrawable = new ColorDrawable(); 61 } else { 62 TypedArray a = context.obtainStyledAttributes(attrs, 63 R.styleable.BaseContainerView, defStyleAttr, 0); 64 mBaseDrawable = a.getDrawable(R.styleable.BaseContainerView_revealBackground); 65 a.recycle(); 66 } 67 } 68 69 @Override onAttachedToWindow()70 protected void onAttachedToWindow() { 71 super.onAttachedToWindow(); 72 73 DeviceProfile grid = Launcher.getLauncher(getContext()).getDeviceProfile(); 74 grid.addLauncherLayoutChangedListener(this); 75 } 76 77 @Override onDetachedFromWindow()78 protected void onDetachedFromWindow() { 79 super.onDetachedFromWindow(); 80 81 DeviceProfile grid = Launcher.getLauncher(getContext()).getDeviceProfile(); 82 grid.removeLauncherLayoutChangedListener(this); 83 } 84 85 @Override onFinishInflate()86 protected void onFinishInflate() { 87 super.onFinishInflate(); 88 89 mContent = findViewById(R.id.main_content); 90 mRevealView = findViewById(R.id.reveal_view); 91 92 updatePaddings(); 93 } 94 95 @Override onLauncherLayoutChanged()96 public void onLauncherLayoutChanged() { 97 updatePaddings(); 98 } 99 setRevealDrawableColor(int color)100 public void setRevealDrawableColor(int color) { 101 ((ColorDrawable) mBaseDrawable).setColor(color); 102 } 103 getContentView()104 public final View getContentView() { 105 return mContent; 106 } 107 getRevealView()108 public final View getRevealView() { 109 return mRevealView; 110 } 111 updatePaddings()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 } 142 } 143 } 144