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; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 21 | import android.graphics.Rect; |
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; |
Winson Chung | 73f0b9b | 2015-07-08 14:13:08 -0700 | [diff] [blame] | 25 | import android.util.Log; |
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 | |
Sunny Goyal | 6c56c68 | 2015-07-16 14:09:05 -0700 | [diff] [blame] | 29 | import com.android.launcher3.config.ProviderConfig; |
| 30 | |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 31 | /** |
| 32 | * A base container view, which supports resizing. |
| 33 | */ |
Sunny Goyal | 05c8c57 | 2016-03-17 11:57:24 -0700 | [diff] [blame^] | 34 | public abstract class BaseContainerView extends FrameLayout { |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 35 | |
Sunny Goyal | 05c8c57 | 2016-03-17 11:57:24 -0700 | [diff] [blame^] | 36 | protected final int mHorizontalPadding; |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 37 | |
| 38 | private final Drawable mRevealDrawable; |
| 39 | |
| 40 | private View mRevealView; |
| 41 | private View mContent; |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 42 | |
| 43 | public BaseContainerView(Context context) { |
| 44 | this(context, null); |
| 45 | } |
| 46 | |
| 47 | public BaseContainerView(Context context, AttributeSet attrs) { |
| 48 | this(context, attrs, 0); |
| 49 | } |
| 50 | |
| 51 | public BaseContainerView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 52 | super(context, attrs, defStyleAttr); |
Sunny Goyal | 05c8c57 | 2016-03-17 11:57:24 -0700 | [diff] [blame^] | 53 | |
| 54 | int width = ((Launcher) context).getDeviceProfile().availableWidthPx; |
| 55 | mHorizontalPadding = Math.max( |
| 56 | getResources().getDimensionPixelSize(R.dimen.container_min_margin), |
| 57 | (int) getResources().getFraction(R.fraction.container_margin, width, 1)); |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 58 | |
| 59 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 60 | R.styleable.BaseContainerView, defStyleAttr, 0); |
Sunny Goyal | 05c8c57 | 2016-03-17 11:57:24 -0700 | [diff] [blame^] | 61 | mRevealDrawable = new InsetDrawable( |
| 62 | a.getDrawable(R.styleable.BaseContainerView_revealBackground), |
| 63 | mHorizontalPadding, 0, mHorizontalPadding, 0); |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 64 | a.recycle(); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | protected void onFinishInflate() { |
| 69 | super.onFinishInflate(); |
| 70 | |
| 71 | mContent = findViewById(R.id.main_content); |
| 72 | mRevealView = findViewById(R.id.reveal_view); |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 73 | |
Sunny Goyal | 05c8c57 | 2016-03-17 11:57:24 -0700 | [diff] [blame^] | 74 | mRevealView.setBackground(mRevealDrawable.getConstantState().newDrawable()); |
| 75 | mContent.setBackground(mRevealDrawable); |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 76 | |
Sunny Goyal | a6194d3 | 2016-02-19 09:34:09 -0800 | [diff] [blame] | 77 | // We let the content have a intent background, but still have full width. |
| 78 | // This allows the scroll bar to be used responsive outside the background bounds as well. |
| 79 | mContent.setPadding(0, 0, 0, 0); |
Winson Chung | 73f0b9b | 2015-07-08 14:13:08 -0700 | [diff] [blame] | 80 | } |
Sunny Goyal | 0ac7ede | 2016-01-29 13:14:14 -0800 | [diff] [blame] | 81 | |
| 82 | public final View getContentView() { |
| 83 | return mContent; |
| 84 | } |
| 85 | |
| 86 | public final View getRevealView() { |
| 87 | return mRevealView; |
| 88 | } |
Winson Chung | 9480415 | 2015-05-08 13:06:44 -0700 | [diff] [blame] | 89 | } |