blob: 1a1bec68439735a9ee28805ec69b99ee4de0ebf5 [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
Sunny Goyal906c6b22017-08-18 00:35:52 -070014import static com.android.launcher3.util.SystemUiController.FLAG_DARK_NAV;
15import static com.android.launcher3.util.SystemUiController.UI_STATE_ROOT_VIEW;
16
Adam Cohena6d04922014-10-23 17:28:30 -070017public class LauncherRootView extends InsettableFrameLayout {
Sunny Goyal0abb36f2015-06-23 10:53:59 -070018
Sunny Goyal07b69292018-01-08 14:19:34 -080019 private final Launcher mLauncher;
20
Sunny Goyal0abb36f2015-06-23 10:53:59 -070021 private final Paint mOpaquePaint;
Sunny Goyal4ffec482016-02-09 11:28:52 -080022 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyal6c2975e2016-07-06 09:47:56 -070023 private boolean mDrawSideInsetBar;
24 @ViewDebug.ExportedProperty(category = "launcher")
25 private int mLeftInsetBarWidth;
Sunny Goyal4ffec482016-02-09 11:28:52 -080026 @ViewDebug.ExportedProperty(category = "launcher")
Sunny Goyalecdc24f2016-01-12 10:35:32 -080027 private int mRightInsetBarWidth;
Sunny Goyal0abb36f2015-06-23 10:53:59 -070028
Sunny Goyal93264612015-11-23 11:47:50 -080029 private View mAlignedView;
30
Adam Cohena6d04922014-10-23 17:28:30 -070031 public LauncherRootView(Context context, AttributeSet attrs) {
32 super(context, attrs);
Sunny Goyal0abb36f2015-06-23 10:53:59 -070033
34 mOpaquePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
35 mOpaquePaint.setColor(Color.BLACK);
36 mOpaquePaint.setStyle(Paint.Style.FILL);
Sunny Goyal07b69292018-01-08 14:19:34 -080037
38 mLauncher = Launcher.getLauncher(context);
Adam Cohena6d04922014-10-23 17:28:30 -070039 }
40
41 @Override
Sunny Goyal93264612015-11-23 11:47:50 -080042 protected void onFinishInflate() {
43 if (getChildCount() > 0) {
44 // LauncherRootView contains only one child, which should be aligned
45 // based on the horizontal insets.
46 mAlignedView = getChildAt(0);
47 }
48 super.onFinishInflate();
49 }
50
51 @TargetApi(23)
52 @Override
Adam Cohena6d04922014-10-23 17:28:30 -070053 protected boolean fitSystemWindows(Rect insets) {
Sunny Goyal6c2975e2016-07-06 09:47:56 -070054 mDrawSideInsetBar = (insets.right > 0 || insets.left > 0) &&
Sunny Goyal93264612015-11-23 11:47:50 -080055 (!Utilities.ATLEAST_MARSHMALLOW ||
Sunny Goyal906c6b22017-08-18 00:35:52 -070056 getContext().getSystemService(ActivityManager.class).isLowRamDevice());
57 if (mDrawSideInsetBar) {
58 mLeftInsetBarWidth = insets.left;
59 mRightInsetBarWidth = insets.right;
60 insets = new Rect(0, insets.top, 0, insets.bottom);
61 } else {
62 mLeftInsetBarWidth = mRightInsetBarWidth = 0;
63 }
Sunny Goyal07b69292018-01-08 14:19:34 -080064 mLauncher.getSystemUiController().updateUiState(
Sunny Goyal906c6b22017-08-18 00:35:52 -070065 UI_STATE_ROOT_VIEW, mDrawSideInsetBar ? FLAG_DARK_NAV : 0);
Sunny Goyal93264612015-11-23 11:47:50 -080066
Sunny Goyal07b69292018-01-08 14:19:34 -080067 // Update device profile before notifying th children.
68 mLauncher.getDeviceProfile().updateInsets(insets);
Sunny Goyal906c6b22017-08-18 00:35:52 -070069 setInsets(insets);
70
71 if (mAlignedView != null) {
Sunny Goyal93264612015-11-23 11:47:50 -080072 // Apply margins on aligned view to handle left/right insets.
73 MarginLayoutParams lp = (MarginLayoutParams) mAlignedView.getLayoutParams();
Sunny Goyal906c6b22017-08-18 00:35:52 -070074 if (lp.leftMargin != mLeftInsetBarWidth || lp.rightMargin != mRightInsetBarWidth) {
75 lp.leftMargin = mLeftInsetBarWidth;
76 lp.rightMargin = mRightInsetBarWidth;
Sunny Goyal93264612015-11-23 11:47:50 -080077 mAlignedView.setLayoutParams(lp);
78 }
79 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070080
Adam Cohena6d04922014-10-23 17:28:30 -070081 return true; // I'll take it from here
82 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -070083
Sunny Goyal07b69292018-01-08 14:19:34 -080084 public void dispatchInsets() {
85 fitSystemWindows(mInsets);
86 }
87
Sunny Goyal0abb36f2015-06-23 10:53:59 -070088 @Override
89 protected void dispatchDraw(Canvas canvas) {
90 super.dispatchDraw(canvas);
91
92 // If the right inset is opaque, draw a black rectangle to ensure that is stays opaque.
Sunny Goyal6c2975e2016-07-06 09:47:56 -070093 if (mDrawSideInsetBar) {
94 if (mRightInsetBarWidth > 0) {
95 int width = getWidth();
96 canvas.drawRect(width - mRightInsetBarWidth, 0, width, getHeight(), mOpaquePaint);
97 }
98 if (mLeftInsetBarWidth > 0) {
99 canvas.drawRect(0, 0, mLeftInsetBarWidth, getHeight(), mOpaquePaint);
100 }
Sunny Goyal0abb36f2015-06-23 10:53:59 -0700101 }
102 }
Adam Cohena6d04922014-10-23 17:28:30 -0700103}