blob: 7bcf5d00e29d4946feb3c04f8fa4c6df3de18a33 [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) {
Sunny Goyal6c2975e2016-07-06 09:47:56 -070047 mDrawSideInsetBar = (insets.right > 0 || insets.left > 0) &&
Sunny Goyal93264612015-11-23 11:47:50 -080048 (!Utilities.ATLEAST_MARSHMALLOW ||
49 getContext().getSystemService(ActivityManager.class).isLowRamDevice());
Sunny Goyalecdc24f2016-01-12 10:35:32 -080050 mRightInsetBarWidth = insets.right;
Sunny Goyal6c2975e2016-07-06 09:47:56 -070051 mLeftInsetBarWidth = insets.left;
52 setInsets(mDrawSideInsetBar ? new Rect(0, insets.top, 0, insets.bottom) : insets);
Sunny Goyal93264612015-11-23 11:47:50 -080053
Sunny Goyal6c2975e2016-07-06 09:47:56 -070054 if (mAlignedView != null && mDrawSideInsetBar) {
Sunny Goyal93264612015-11-23 11:47:50 -080055 // Apply margins on aligned view to handle left/right insets.
56 MarginLayoutParams lp = (MarginLayoutParams) mAlignedView.getLayoutParams();
57 if (lp.leftMargin != insets.left || lp.rightMargin != insets.right) {
58 lp.leftMargin = insets.left;
59 lp.rightMargin = insets.right;
60 mAlignedView.setLayoutParams(lp);
61 }
62 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070063
Adam Cohena6d04922014-10-23 17:28:30 -070064 return true; // I'll take it from here
65 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070066
67 @Override
68 protected void dispatchDraw(Canvas canvas) {
69 super.dispatchDraw(canvas);
70
71 // If the right inset is opaque, draw a black rectangle to ensure that is stays opaque.
Sunny Goyal6c2975e2016-07-06 09:47:56 -070072 if (mDrawSideInsetBar) {
73 if (mRightInsetBarWidth > 0) {
74 int width = getWidth();
75 canvas.drawRect(width - mRightInsetBarWidth, 0, width, getHeight(), mOpaquePaint);
76 }
77 if (mLeftInsetBarWidth > 0) {
78 canvas.drawRect(0, 0, mLeftInsetBarWidth, getHeight(), mOpaquePaint);
79 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070080 }
81 }
Adam Cohena6d04922014-10-23 17:28:30 -070082}