blob: 643d48adfce3eeedd8b1dccdb0986167075b1d8e [file] [log] [blame]
Adam Cohena6d04922014-10-23 17:28:30 -07001package com.android.launcher3;
2
Sunny Goyal93264612015-11-23 11:47:50 -08003import android.annotation.TargetApi;
4import android.app.ActivityManager;
Adam Cohena6d04922014-10-23 17:28:30 -07005import android.content.Context;
Sunny Goyal0abb36f2015-06-23 10:53:59 -07006import android.graphics.Canvas;
7import android.graphics.Color;
8import android.graphics.Paint;
Adam Cohena6d04922014-10-23 17:28:30 -07009import android.graphics.Rect;
10import android.util.AttributeSet;
Sunny Goyal93264612015-11-23 11:47:50 -080011import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -080012import android.view.ViewDebug;
Adam Cohena6d04922014-10-23 17:28:30 -070013
14public class LauncherRootView extends InsettableFrameLayout {
Sunny Goyal0abb36f2015-06-23 10:53:59 -070015
16 private final Paint mOpaquePaint;
Sunny Goyal4ffec482016-02-09 11:28:52 -080017 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal6c2975e2016-07-06 09:47:56 -070018 private boolean mDrawSideInsetBar;
19 @ViewDebug.ExportedProperty(category = "launcher")
20 private int mLeftInsetBarWidth;
Sunny Goyal4ffec482016-02-09 11:28:52 -080021 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalecdc24f2016-01-12 10:35:32 -080022 private int mRightInsetBarWidth;
Sunny Goyal0abb36f2015-06-23 10:53:59 -070023
Sunny Goyal93264612015-11-23 11:47:50 -080024 private View mAlignedView;
25
Adam Cohena6d04922014-10-23 17:28:30 -070026 public LauncherRootView(Context context, AttributeSet attrs) {
27 super(context, attrs);
Sunny Goyal0abb36f2015-06-23 10:53:59 -070028
29 mOpaquePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
30 mOpaquePaint.setColor(Color.BLACK);
31 mOpaquePaint.setStyle(Paint.Style.FILL);
Adam Cohena6d04922014-10-23 17:28:30 -070032 }
33
34 @Override
Sunny Goyal93264612015-11-23 11:47:50 -080035 protected void onFinishInflate() {
36 if (getChildCount() > 0) {
37 // LauncherRootView contains only one child, which should be aligned
38 // based on the horizontal insets.
39 mAlignedView = getChildAt(0);
40 }
41 super.onFinishInflate();
42 }
43
44 @TargetApi(23)
45 @Override
Adam Cohena6d04922014-10-23 17:28:30 -070046 protected boolean fitSystemWindows(Rect insets) {
Winson1f064272016-07-18 17:18:02 -070047 boolean rawInsetsChanged = !mInsets.equals(insets);
Sunny Goyal6c2975e2016-07-06 09:47:56 -070048 mDrawSideInsetBar = (insets.right > 0 || insets.left > 0) &&
Sunny Goyal93264612015-11-23 11:47:50 -080049 (!Utilities.ATLEAST_MARSHMALLOW ||
50 getContext().getSystemService(ActivityManager.class).isLowRamDevice());
Sunny Goyalecdc24f2016-01-12 10:35:32 -080051 mRightInsetBarWidth = insets.right;
Sunny Goyal6c2975e2016-07-06 09:47:56 -070052 mLeftInsetBarWidth = insets.left;
53 setInsets(mDrawSideInsetBar ? new Rect(0, insets.top, 0, insets.bottom) : insets);
Sunny Goyal93264612015-11-23 11:47:50 -080054
Sunny Goyal6c2975e2016-07-06 09:47:56 -070055 if (mAlignedView != null && mDrawSideInsetBar) {
Sunny Goyal93264612015-11-23 11:47:50 -080056 // Apply margins on aligned view to handle left/right insets.
57 MarginLayoutParams lp = (MarginLayoutParams) mAlignedView.getLayoutParams();
58 if (lp.leftMargin != insets.left || lp.rightMargin != insets.right) {
59 lp.leftMargin = insets.left;
60 lp.rightMargin = insets.right;
61 mAlignedView.setLayoutParams(lp);
62 }
63 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070064
Winson1f064272016-07-18 17:18:02 -070065 if (rawInsetsChanged) {
66 // Update the grid again
67 Launcher launcher = Launcher.getLauncher(getContext());
68 launcher.onInsetsChanged(insets);
69 }
70
Adam Cohena6d04922014-10-23 17:28:30 -070071 return true; // I'll take it from here
72 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070073
74 @Override
75 protected void dispatchDraw(Canvas canvas) {
76 super.dispatchDraw(canvas);
77
78 // If the right inset is opaque, draw a black rectangle to ensure that is stays opaque.
Sunny Goyal6c2975e2016-07-06 09:47:56 -070079 if (mDrawSideInsetBar) {
80 if (mRightInsetBarWidth > 0) {
81 int width = getWidth();
82 canvas.drawRect(width - mRightInsetBarWidth, 0, width, getHeight(), mOpaquePaint);
83 }
84 if (mLeftInsetBarWidth > 0) {
85 canvas.drawRect(0, 0, mLeftInsetBarWidth, getHeight(), mOpaquePaint);
86 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070087 }
88 }
Adam Cohena6d04922014-10-23 17:28:30 -070089}