blob: 55a512fb56832dd8a4f50fcf63b75a9c76d9a219 [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;
Sunny Goyalecdc24f2016-01-12 10:35:32 -080017 private int mRightInsetBarWidth;
Sunny Goyal0abb36f2015-06-23 10:53:59 -070018
Sunny Goyal93264612015-11-23 11:47:50 -080019 private View mAlignedView;
20
Adam Cohena6d04922014-10-23 17:28:30 -070021 public LauncherRootView(Context context, AttributeSet attrs) {
22 super(context, attrs);
Sunny Goyal0abb36f2015-06-23 10:53:59 -070023
24 mOpaquePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
25 mOpaquePaint.setColor(Color.BLACK);
26 mOpaquePaint.setStyle(Paint.Style.FILL);
Adam Cohena6d04922014-10-23 17:28:30 -070027 }
28
29 @Override
Sunny Goyal93264612015-11-23 11:47:50 -080030 protected void onFinishInflate() {
31 if (getChildCount() > 0) {
32 // LauncherRootView contains only one child, which should be aligned
33 // based on the horizontal insets.
34 mAlignedView = getChildAt(0);
35 }
36 super.onFinishInflate();
37 }
38
39 @TargetApi(23)
40 @Override
Adam Cohena6d04922014-10-23 17:28:30 -070041 protected boolean fitSystemWindows(Rect insets) {
Sunny Goyal93264612015-11-23 11:47:50 -080042 mDrawRightInsetBar = insets.right > 0 &&
43 (!Utilities.ATLEAST_MARSHMALLOW ||
44 getContext().getSystemService(ActivityManager.class).isLowRamDevice());
Sunny Goyalecdc24f2016-01-12 10:35:32 -080045 mRightInsetBarWidth = insets.right;
Sunny Goyal93264612015-11-23 11:47:50 -080046 setInsets(mDrawRightInsetBar ? new Rect(0, insets.top, 0, insets.bottom) : insets);
47
Sunny Goyalecdc24f2016-01-12 10:35:32 -080048 if (mAlignedView != null && mDrawRightInsetBar) {
Sunny Goyal93264612015-11-23 11:47:50 -080049 // Apply margins on aligned view to handle left/right insets.
50 MarginLayoutParams lp = (MarginLayoutParams) mAlignedView.getLayoutParams();
51 if (lp.leftMargin != insets.left || lp.rightMargin != insets.right) {
52 lp.leftMargin = insets.left;
53 lp.rightMargin = insets.right;
54 mAlignedView.setLayoutParams(lp);
55 }
56 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070057
Adam Cohena6d04922014-10-23 17:28:30 -070058 return true; // I'll take it from here
59 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070060
61 @Override
62 protected void dispatchDraw(Canvas canvas) {
63 super.dispatchDraw(canvas);
64
65 // If the right inset is opaque, draw a black rectangle to ensure that is stays opaque.
66 if (mDrawRightInsetBar) {
67 int width = getWidth();
Sunny Goyalecdc24f2016-01-12 10:35:32 -080068 canvas.drawRect(width - mRightInsetBarWidth, 0, width, getHeight(), mOpaquePaint);
Sunny Goyal0abb36f2015-06-23 10:53:59 -070069 }
70 }
Adam Cohena6d04922014-10-23 17:28:30 -070071}