blob: 104af5280bf18f3e1b0d98407bee8730235f0921 [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 Goyal0abb36f2015-06-23 10:53:59 -070018 private boolean mDrawRightInsetBar;
Sunny Goyal4ffec482016-02-09 11:28:52 -080019 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalecdc24f2016-01-12 10:35:32 -080020 private int mRightInsetBarWidth;
Sunny Goyal0abb36f2015-06-23 10:53:59 -070021
Sunny Goyal93264612015-11-23 11:47:50 -080022 private View mAlignedView;
23
Adam Cohena6d04922014-10-23 17:28:30 -070024 public LauncherRootView(Context context, AttributeSet attrs) {
25 super(context, attrs);
Sunny Goyal0abb36f2015-06-23 10:53:59 -070026
27 mOpaquePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
28 mOpaquePaint.setColor(Color.BLACK);
29 mOpaquePaint.setStyle(Paint.Style.FILL);
Adam Cohena6d04922014-10-23 17:28:30 -070030 }
31
32 @Override
Sunny Goyal93264612015-11-23 11:47:50 -080033 protected void onFinishInflate() {
34 if (getChildCount() > 0) {
35 // LauncherRootView contains only one child, which should be aligned
36 // based on the horizontal insets.
37 mAlignedView = getChildAt(0);
38 }
39 super.onFinishInflate();
40 }
41
42 @TargetApi(23)
43 @Override
Adam Cohena6d04922014-10-23 17:28:30 -070044 protected boolean fitSystemWindows(Rect insets) {
Sunny Goyal93264612015-11-23 11:47:50 -080045 mDrawRightInsetBar = insets.right > 0 &&
46 (!Utilities.ATLEAST_MARSHMALLOW ||
47 getContext().getSystemService(ActivityManager.class).isLowRamDevice());
Sunny Goyalecdc24f2016-01-12 10:35:32 -080048 mRightInsetBarWidth = insets.right;
Sunny Goyal93264612015-11-23 11:47:50 -080049 setInsets(mDrawRightInsetBar ? new Rect(0, insets.top, 0, insets.bottom) : insets);
50
Sunny Goyalecdc24f2016-01-12 10:35:32 -080051 if (mAlignedView != null && mDrawRightInsetBar) {
Sunny Goyal93264612015-11-23 11:47:50 -080052 // Apply margins on aligned view to handle left/right insets.
53 MarginLayoutParams lp = (MarginLayoutParams) mAlignedView.getLayoutParams();
54 if (lp.leftMargin != insets.left || lp.rightMargin != insets.right) {
55 lp.leftMargin = insets.left;
56 lp.rightMargin = insets.right;
57 mAlignedView.setLayoutParams(lp);
58 }
59 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070060
Adam Cohena6d04922014-10-23 17:28:30 -070061 return true; // I'll take it from here
62 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070063
64 @Override
65 protected void dispatchDraw(Canvas canvas) {
66 super.dispatchDraw(canvas);
67
68 // If the right inset is opaque, draw a black rectangle to ensure that is stays opaque.
69 if (mDrawRightInsetBar) {
70 int width = getWidth();
Sunny Goyalecdc24f2016-01-12 10:35:32 -080071 canvas.drawRect(width - mRightInsetBarWidth, 0, width, getHeight(), mOpaquePaint);
Sunny Goyal0abb36f2015-06-23 10:53:59 -070072 }
73 }
Adam Cohena6d04922014-10-23 17:28:30 -070074}