blob: 71ccd85fcd8e2560b9bdb31d8c78b410e8ea2d52 [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;
Adam Cohena6d04922014-10-23 17:28:30 -070012
13public class LauncherRootView extends InsettableFrameLayout {
Sunny Goyal0abb36f2015-06-23 10:53:59 -070014
15 private final Paint mOpaquePaint;
16 private boolean mDrawRightInsetBar;
17
Sunny Goyal93264612015-11-23 11:47:50 -080018 private View mAlignedView;
19
Adam Cohena6d04922014-10-23 17:28:30 -070020 public LauncherRootView(Context context, AttributeSet attrs) {
21 super(context, attrs);
Sunny Goyal0abb36f2015-06-23 10:53:59 -070022
23 mOpaquePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
24 mOpaquePaint.setColor(Color.BLACK);
25 mOpaquePaint.setStyle(Paint.Style.FILL);
Adam Cohena6d04922014-10-23 17:28:30 -070026 }
27
28 @Override
Sunny Goyal93264612015-11-23 11:47:50 -080029 protected void onFinishInflate() {
30 if (getChildCount() > 0) {
31 // LauncherRootView contains only one child, which should be aligned
32 // based on the horizontal insets.
33 mAlignedView = getChildAt(0);
34 }
35 super.onFinishInflate();
36 }
37
38 @TargetApi(23)
39 @Override
Adam Cohena6d04922014-10-23 17:28:30 -070040 protected boolean fitSystemWindows(Rect insets) {
Sunny Goyal93264612015-11-23 11:47:50 -080041 mDrawRightInsetBar = insets.right > 0 &&
42 (!Utilities.ATLEAST_MARSHMALLOW ||
43 getContext().getSystemService(ActivityManager.class).isLowRamDevice());
44 setInsets(mDrawRightInsetBar ? new Rect(0, insets.top, 0, insets.bottom) : insets);
45
46 if (mAlignedView != null) {
47 // Apply margins on aligned view to handle left/right insets.
48 MarginLayoutParams lp = (MarginLayoutParams) mAlignedView.getLayoutParams();
49 if (lp.leftMargin != insets.left || lp.rightMargin != insets.right) {
50 lp.leftMargin = insets.left;
51 lp.rightMargin = insets.right;
52 mAlignedView.setLayoutParams(lp);
53 }
54 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070055
Adam Cohena6d04922014-10-23 17:28:30 -070056 return true; // I'll take it from here
57 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070058
59 @Override
60 protected void dispatchDraw(Canvas canvas) {
61 super.dispatchDraw(canvas);
62
63 // If the right inset is opaque, draw a black rectangle to ensure that is stays opaque.
64 if (mDrawRightInsetBar) {
65 int width = getWidth();
66 canvas.drawRect(width - mInsets.right, 0, width, getHeight(), mOpaquePaint);
67 }
68 }
Adam Cohena6d04922014-10-23 17:28:30 -070069}