2014-10-23 17:28:30 -07:00
|
|
|
package com.android.launcher3;
|
|
|
|
|
|
2018-04-19 12:34:43 -07:00
|
|
|
import static com.android.launcher3.util.SystemUiController.FLAG_DARK_NAV;
|
|
|
|
|
import static com.android.launcher3.util.SystemUiController.UI_STATE_ROOT_VIEW;
|
|
|
|
|
|
2015-11-23 11:47:50 -08:00
|
|
|
import android.annotation.TargetApi;
|
|
|
|
|
import android.app.ActivityManager;
|
2014-10-23 17:28:30 -07:00
|
|
|
import android.content.Context;
|
2015-06-23 10:53:59 -07:00
|
|
|
import android.graphics.Canvas;
|
|
|
|
|
import android.graphics.Color;
|
|
|
|
|
import android.graphics.Paint;
|
2014-10-23 17:28:30 -07:00
|
|
|
import android.graphics.Rect;
|
2019-04-23 11:28:54 -07:00
|
|
|
import android.os.Build;
|
2014-10-23 17:28:30 -07:00
|
|
|
import android.util.AttributeSet;
|
2015-11-23 11:47:50 -08:00
|
|
|
import android.view.View;
|
2016-02-09 11:28:52 -08:00
|
|
|
import android.view.ViewDebug;
|
2014-10-23 17:28:30 -07:00
|
|
|
|
2019-04-23 11:28:54 -07:00
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
2014-10-23 17:28:30 -07:00
|
|
|
public class LauncherRootView extends InsettableFrameLayout {
|
2015-06-23 10:53:59 -07:00
|
|
|
|
2018-01-08 14:19:34 -08:00
|
|
|
private final Launcher mLauncher;
|
|
|
|
|
|
2015-06-23 10:53:59 -07:00
|
|
|
private final Paint mOpaquePaint;
|
2018-05-07 11:09:13 -07:00
|
|
|
|
2016-02-09 11:28:52 -08:00
|
|
|
@ViewDebug.ExportedProperty(category = "launcher")
|
2018-05-07 11:09:13 -07:00
|
|
|
private final Rect mConsumedInsets = new Rect();
|
2015-06-23 10:53:59 -07:00
|
|
|
|
2019-04-23 11:28:54 -07:00
|
|
|
@ViewDebug.ExportedProperty(category = "launcher")
|
|
|
|
|
private static final List<Rect> SYSTEM_GESTURE_EXCLUSION_RECT =
|
|
|
|
|
Collections.singletonList(new Rect());
|
|
|
|
|
|
2015-11-23 11:47:50 -08:00
|
|
|
private View mAlignedView;
|
2018-03-14 17:51:49 -07:00
|
|
|
private WindowStateListener mWindowStateListener;
|
2019-04-23 11:28:54 -07:00
|
|
|
@ViewDebug.ExportedProperty(category = "launcher")
|
|
|
|
|
private boolean mDisallowBackGesture;
|
2015-11-23 11:47:50 -08:00
|
|
|
|
2014-10-23 17:28:30 -07:00
|
|
|
public LauncherRootView(Context context, AttributeSet attrs) {
|
|
|
|
|
super(context, attrs);
|
2015-06-23 10:53:59 -07:00
|
|
|
|
|
|
|
|
mOpaquePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
|
|
|
|
|
mOpaquePaint.setColor(Color.BLACK);
|
|
|
|
|
mOpaquePaint.setStyle(Paint.Style.FILL);
|
2018-01-08 14:19:34 -08:00
|
|
|
|
|
|
|
|
mLauncher = Launcher.getLauncher(context);
|
2014-10-23 17:28:30 -07:00
|
|
|
}
|
|
|
|
|
|
2015-11-23 11:47:50 -08:00
|
|
|
@Override
|
|
|
|
|
protected void onFinishInflate() {
|
|
|
|
|
if (getChildCount() > 0) {
|
|
|
|
|
// LauncherRootView contains only one child, which should be aligned
|
|
|
|
|
// based on the horizontal insets.
|
|
|
|
|
mAlignedView = getChildAt(0);
|
|
|
|
|
}
|
|
|
|
|
super.onFinishInflate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@TargetApi(23)
|
2014-10-23 17:28:30 -07:00
|
|
|
@Override
|
|
|
|
|
protected boolean fitSystemWindows(Rect insets) {
|
2018-05-07 11:09:13 -07:00
|
|
|
mConsumedInsets.setEmpty();
|
|
|
|
|
boolean drawInsetBar = false;
|
2019-01-25 15:10:18 -08:00
|
|
|
if (mLauncher.isInMultiWindowMode()
|
2018-05-07 11:09:13 -07:00
|
|
|
&& (insets.left > 0 || insets.right > 0 || insets.bottom > 0)) {
|
|
|
|
|
mConsumedInsets.left = insets.left;
|
|
|
|
|
mConsumedInsets.right = insets.right;
|
|
|
|
|
mConsumedInsets.bottom = insets.bottom;
|
|
|
|
|
insets = new Rect(0, insets.top, 0, 0);
|
|
|
|
|
drawInsetBar = true;
|
|
|
|
|
} else if ((insets.right > 0 || insets.left > 0) &&
|
2019-01-25 15:10:18 -08:00
|
|
|
getContext().getSystemService(ActivityManager.class).isLowRamDevice()) {
|
2018-05-07 11:09:13 -07:00
|
|
|
mConsumedInsets.left = insets.left;
|
|
|
|
|
mConsumedInsets.right = insets.right;
|
2017-08-18 00:35:52 -07:00
|
|
|
insets = new Rect(0, insets.top, 0, insets.bottom);
|
2018-05-07 11:09:13 -07:00
|
|
|
drawInsetBar = true;
|
2017-08-18 00:35:52 -07:00
|
|
|
}
|
2018-05-07 11:09:13 -07:00
|
|
|
|
2018-01-08 14:19:34 -08:00
|
|
|
mLauncher.getSystemUiController().updateUiState(
|
2018-05-07 11:09:13 -07:00
|
|
|
UI_STATE_ROOT_VIEW, drawInsetBar ? FLAG_DARK_NAV : 0);
|
2017-08-18 00:35:52 -07:00
|
|
|
|
2018-01-08 14:19:34 -08:00
|
|
|
// Update device profile before notifying th children.
|
2019-04-30 12:04:37 -07:00
|
|
|
mLauncher.updateInsets(insets);
|
2018-01-18 14:02:47 -08:00
|
|
|
boolean resetState = !insets.equals(mInsets);
|
2017-08-18 00:35:52 -07:00
|
|
|
setInsets(insets);
|
2015-11-23 11:47:50 -08:00
|
|
|
|
2017-08-18 00:35:52 -07:00
|
|
|
if (mAlignedView != null) {
|
2018-05-07 11:09:13 -07:00
|
|
|
// Apply margins on aligned view to handle consumed insets.
|
2015-11-23 11:47:50 -08:00
|
|
|
MarginLayoutParams lp = (MarginLayoutParams) mAlignedView.getLayoutParams();
|
2018-05-07 11:09:13 -07:00
|
|
|
if (lp.leftMargin != mConsumedInsets.left || lp.rightMargin != mConsumedInsets.right ||
|
|
|
|
|
lp.bottomMargin != mConsumedInsets.bottom) {
|
|
|
|
|
lp.leftMargin = mConsumedInsets.left;
|
|
|
|
|
lp.rightMargin = mConsumedInsets.right;
|
|
|
|
|
lp.topMargin = mConsumedInsets.top;
|
|
|
|
|
lp.bottomMargin = mConsumedInsets.bottom;
|
2015-11-23 11:47:50 -08:00
|
|
|
mAlignedView.setLayoutParams(lp);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-18 14:02:47 -08:00
|
|
|
if (resetState) {
|
2018-04-19 12:34:43 -07:00
|
|
|
mLauncher.getStateManager().reapplyState(true /* cancelCurrentAnimation */);
|
2018-01-18 14:02:47 -08:00
|
|
|
}
|
2015-06-23 10:53:59 -07:00
|
|
|
|
2019-05-17 16:33:37 -07:00
|
|
|
return false; // Let children get the full insets
|
2014-10-23 17:28:30 -07:00
|
|
|
}
|
2015-06-23 10:53:59 -07:00
|
|
|
|
2018-01-18 11:35:10 -08:00
|
|
|
@Override
|
|
|
|
|
public void setInsets(Rect insets) {
|
2018-01-31 15:18:11 -08:00
|
|
|
// If the insets haven't changed, this is a no-op. Avoid unnecessary layout caused by
|
|
|
|
|
// modifying child layout params.
|
|
|
|
|
if (!insets.equals(mInsets)) {
|
|
|
|
|
super.setInsets(insets);
|
|
|
|
|
}
|
2018-01-18 11:35:10 -08:00
|
|
|
}
|
|
|
|
|
|
2018-01-08 14:19:34 -08:00
|
|
|
public void dispatchInsets() {
|
2019-04-30 12:04:37 -07:00
|
|
|
mLauncher.updateInsets(mInsets);
|
2018-01-31 15:18:11 -08:00
|
|
|
super.setInsets(mInsets);
|
2018-01-08 14:19:34 -08:00
|
|
|
}
|
|
|
|
|
|
2015-06-23 10:53:59 -07:00
|
|
|
@Override
|
|
|
|
|
protected void dispatchDraw(Canvas canvas) {
|
|
|
|
|
super.dispatchDraw(canvas);
|
|
|
|
|
|
|
|
|
|
// If the right inset is opaque, draw a black rectangle to ensure that is stays opaque.
|
2018-05-07 11:09:13 -07:00
|
|
|
if (mConsumedInsets.right > 0) {
|
|
|
|
|
int width = getWidth();
|
|
|
|
|
canvas.drawRect(width - mConsumedInsets.right, 0, width, getHeight(), mOpaquePaint);
|
|
|
|
|
}
|
|
|
|
|
if (mConsumedInsets.left > 0) {
|
|
|
|
|
canvas.drawRect(0, 0, mConsumedInsets.left, getHeight(), mOpaquePaint);
|
|
|
|
|
}
|
|
|
|
|
if (mConsumedInsets.bottom > 0) {
|
|
|
|
|
int height = getHeight();
|
|
|
|
|
canvas.drawRect(0, height - mConsumedInsets.bottom, getWidth(), height, mOpaquePaint);
|
2015-06-23 10:53:59 -07:00
|
|
|
}
|
|
|
|
|
}
|
2018-03-14 17:51:49 -07:00
|
|
|
|
|
|
|
|
public void setWindowStateListener(WindowStateListener listener) {
|
|
|
|
|
mWindowStateListener = listener;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onWindowFocusChanged(boolean hasWindowFocus) {
|
|
|
|
|
super.onWindowFocusChanged(hasWindowFocus);
|
|
|
|
|
if (mWindowStateListener != null) {
|
|
|
|
|
mWindowStateListener.onWindowFocusChanged(hasWindowFocus);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onWindowVisibilityChanged(int visibility) {
|
|
|
|
|
super.onWindowVisibilityChanged(visibility);
|
|
|
|
|
if (mWindowStateListener != null) {
|
|
|
|
|
mWindowStateListener.onWindowVisibilityChanged(visibility);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-23 11:28:54 -07:00
|
|
|
@Override
|
|
|
|
|
protected void onLayout(boolean changed, int l, int t, int r, int b) {
|
|
|
|
|
super.onLayout(changed, l, t, r, b);
|
|
|
|
|
SYSTEM_GESTURE_EXCLUSION_RECT.get(0).set(l, t, r, b);
|
|
|
|
|
setDisallowBackGesture(mDisallowBackGesture);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@TargetApi(Build.VERSION_CODES.Q)
|
|
|
|
|
public void setDisallowBackGesture(boolean disallowBackGesture) {
|
|
|
|
|
if (!Utilities.ATLEAST_Q) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
mDisallowBackGesture = disallowBackGesture;
|
|
|
|
|
setSystemGestureExclusionRects(mDisallowBackGesture
|
|
|
|
|
? SYSTEM_GESTURE_EXCLUSION_RECT
|
|
|
|
|
: Collections.emptyList());
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-14 17:51:49 -07:00
|
|
|
public interface WindowStateListener {
|
|
|
|
|
|
|
|
|
|
void onWindowFocusChanged(boolean hasFocus);
|
|
|
|
|
|
|
|
|
|
void onWindowVisibilityChanged(int visibility);
|
|
|
|
|
}
|
2014-10-23 17:28:30 -07:00
|
|
|
}
|