2020-12-22 12:40:09 -06:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2021 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
package com.android.launcher3.taskbar;
|
|
|
|
|
|
2022-01-10 22:30:14 +00:00
|
|
|
import static android.view.KeyEvent.ACTION_UP;
|
|
|
|
|
import static android.view.KeyEvent.KEYCODE_BACK;
|
|
|
|
|
|
2020-12-22 12:40:09 -06:00
|
|
|
import android.content.Context;
|
2021-04-07 11:06:40 -07:00
|
|
|
import android.graphics.Canvas;
|
2020-12-22 12:40:09 -06:00
|
|
|
import android.util.AttributeSet;
|
2022-01-10 22:30:14 +00:00
|
|
|
import android.view.KeyEvent;
|
2021-05-26 13:03:20 +01:00
|
|
|
import android.view.MotionEvent;
|
2021-02-02 17:12:08 -08:00
|
|
|
import android.view.View;
|
2020-12-22 12:40:09 -06:00
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
|
|
2022-01-10 22:30:14 +00:00
|
|
|
import com.android.launcher3.AbstractFloatingView;
|
2021-05-26 13:03:20 +01:00
|
|
|
import com.android.launcher3.testing.TestLogging;
|
2022-07-26 13:54:31 -07:00
|
|
|
import com.android.launcher3.testing.shared.TestProtocol;
|
2021-02-02 17:12:08 -08:00
|
|
|
import com.android.launcher3.views.BaseDragLayer;
|
2021-02-02 11:04:25 -08:00
|
|
|
import com.android.systemui.shared.system.ViewTreeObserverWrapper;
|
2021-05-20 20:18:47 +00:00
|
|
|
import com.android.systemui.shared.system.ViewTreeObserverWrapper.InsetsInfo;
|
|
|
|
|
import com.android.systemui.shared.system.ViewTreeObserverWrapper.OnComputeInsetsListener;
|
2021-02-02 11:04:25 -08:00
|
|
|
|
2020-12-22 12:40:09 -06:00
|
|
|
/**
|
|
|
|
|
* Top-level ViewGroup that hosts the TaskbarView as well as Views created by it such as Folder.
|
|
|
|
|
*/
|
2021-05-21 14:41:30 -07:00
|
|
|
public class TaskbarDragLayer extends BaseDragLayer<TaskbarActivityContext> {
|
2021-02-02 17:12:08 -08:00
|
|
|
|
2022-04-02 08:28:40 -07:00
|
|
|
private final TaskbarBackgroundRenderer mBackgroundRenderer;
|
2021-06-01 16:54:07 -07:00
|
|
|
private final OnComputeInsetsListener mTaskbarInsetsComputer = this::onComputeTaskbarInsets;
|
2021-02-02 17:12:08 -08:00
|
|
|
|
2021-08-13 17:03:55 -07:00
|
|
|
// Initialized in init.
|
2021-06-08 20:03:43 -07:00
|
|
|
private TaskbarDragLayerController.TaskbarDragLayerCallbacks mControllerCallbacks;
|
2021-05-20 20:18:47 +00:00
|
|
|
|
2021-06-01 16:54:07 -07:00
|
|
|
private float mTaskbarBackgroundOffset;
|
2021-02-02 11:04:25 -08:00
|
|
|
|
2021-05-21 14:41:30 -07:00
|
|
|
public TaskbarDragLayer(@NonNull Context context) {
|
2020-12-22 12:40:09 -06:00
|
|
|
this(context, null);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-21 14:41:30 -07:00
|
|
|
public TaskbarDragLayer(@NonNull Context context, @Nullable AttributeSet attrs) {
|
2020-12-22 12:40:09 -06:00
|
|
|
this(context, attrs, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-21 14:41:30 -07:00
|
|
|
public TaskbarDragLayer(@NonNull Context context, @Nullable AttributeSet attrs,
|
2020-12-22 12:40:09 -06:00
|
|
|
int defStyleAttr) {
|
|
|
|
|
this(context, attrs, defStyleAttr, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-21 14:41:30 -07:00
|
|
|
public TaskbarDragLayer(@NonNull Context context, @Nullable AttributeSet attrs,
|
2020-12-22 12:40:09 -06:00
|
|
|
int defStyleAttr, int defStyleRes) {
|
2021-02-02 17:12:08 -08:00
|
|
|
super(context, attrs, 1 /* alphaChannelCount */);
|
2022-04-02 08:28:40 -07:00
|
|
|
mBackgroundRenderer = new TaskbarBackgroundRenderer(mActivity);
|
|
|
|
|
mBackgroundRenderer.getPaint().setAlpha(0);
|
2021-06-08 20:03:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void init(TaskbarDragLayerController.TaskbarDragLayerCallbacks callbacks) {
|
|
|
|
|
mControllerCallbacks = callbacks;
|
2021-08-13 17:03:55 -07:00
|
|
|
|
2021-05-20 20:18:47 +00:00
|
|
|
recreateControllers();
|
2021-02-02 17:12:08 -08:00
|
|
|
}
|
|
|
|
|
|
2021-05-20 20:18:47 +00:00
|
|
|
@Override
|
|
|
|
|
public void recreateControllers() {
|
2021-12-01 14:51:51 +00:00
|
|
|
mControllers = mControllerCallbacks.getTouchControllers();
|
2020-12-22 12:40:09 -06:00
|
|
|
}
|
2021-02-02 11:04:25 -08:00
|
|
|
|
2021-05-20 20:18:47 +00:00
|
|
|
private void onComputeTaskbarInsets(InsetsInfo insetsInfo) {
|
|
|
|
|
if (mControllerCallbacks != null) {
|
|
|
|
|
mControllerCallbacks.updateInsetsTouchability(insetsInfo);
|
|
|
|
|
}
|
2021-02-02 11:04:25 -08:00
|
|
|
}
|
|
|
|
|
|
2021-05-20 20:18:47 +00:00
|
|
|
protected void onDestroy() {
|
2021-02-02 11:04:25 -08:00
|
|
|
ViewTreeObserverWrapper.removeOnComputeInsetsListener(mTaskbarInsetsComputer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onAttachedToWindow() {
|
|
|
|
|
super.onAttachedToWindow();
|
|
|
|
|
ViewTreeObserverWrapper.addOnComputeInsetsListener(getViewTreeObserver(),
|
|
|
|
|
mTaskbarInsetsComputer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onDetachedFromWindow() {
|
|
|
|
|
super.onDetachedFromWindow();
|
|
|
|
|
|
2021-05-20 20:18:47 +00:00
|
|
|
onDestroy();
|
2021-02-02 11:04:25 -08:00
|
|
|
}
|
2021-02-02 17:12:08 -08:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected boolean canFindActiveController() {
|
|
|
|
|
// Unlike super class, we want to be able to find controllers when touches occur in the
|
|
|
|
|
// gesture area. For example, this allows Folder to close itself when touching the Taskbar.
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onViewRemoved(View child) {
|
|
|
|
|
super.onViewRemoved(child);
|
2021-05-20 20:18:47 +00:00
|
|
|
if (mControllerCallbacks != null) {
|
2021-05-21 14:41:30 -07:00
|
|
|
mControllerCallbacks.onDragLayerViewRemoved();
|
2021-05-20 20:18:47 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void dispatchDraw(Canvas canvas) {
|
2021-06-01 16:54:07 -07:00
|
|
|
float backgroundHeight = mControllerCallbacks.getTaskbarBackgroundHeight()
|
|
|
|
|
* (1f - mTaskbarBackgroundOffset);
|
2022-04-02 08:28:40 -07:00
|
|
|
mBackgroundRenderer.setBackgroundHeight(backgroundHeight);
|
|
|
|
|
mBackgroundRenderer.draw(canvas);
|
2021-05-20 20:18:47 +00:00
|
|
|
super.dispatchDraw(canvas);
|
2021-02-02 17:12:08 -08:00
|
|
|
}
|
|
|
|
|
|
2021-04-07 11:06:40 -07:00
|
|
|
/**
|
|
|
|
|
* Sets the alpha of the background color behind all the Taskbar contents.
|
|
|
|
|
* @param alpha 0 is fully transparent, 1 is fully opaque.
|
|
|
|
|
*/
|
|
|
|
|
protected void setTaskbarBackgroundAlpha(float alpha) {
|
2022-04-02 08:28:40 -07:00
|
|
|
mBackgroundRenderer.getPaint().setAlpha((int) (alpha * 255));
|
2021-04-07 11:06:40 -07:00
|
|
|
invalidate();
|
|
|
|
|
}
|
2021-05-26 13:03:20 +01:00
|
|
|
|
2021-06-01 16:54:07 -07:00
|
|
|
/**
|
|
|
|
|
* Sets the translation of the background color behind all the Taskbar contents.
|
|
|
|
|
* @param offset 0 is fully onscreen, 1 is fully offscreen.
|
|
|
|
|
*/
|
|
|
|
|
protected void setTaskbarBackgroundOffset(float offset) {
|
|
|
|
|
mTaskbarBackgroundOffset = offset;
|
|
|
|
|
invalidate();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-26 13:03:20 +01:00
|
|
|
@Override
|
|
|
|
|
public boolean dispatchTouchEvent(MotionEvent ev) {
|
|
|
|
|
TestLogging.recordMotionEvent(TestProtocol.SEQUENCE_MAIN, "Touch event", ev);
|
|
|
|
|
return super.dispatchTouchEvent(ev);
|
|
|
|
|
}
|
2022-01-10 22:30:14 +00:00
|
|
|
|
|
|
|
|
/** Called while Taskbar window is focusable, e.g. when pressing back while a folder is open */
|
|
|
|
|
@Override
|
|
|
|
|
public boolean dispatchKeyEvent(KeyEvent event) {
|
|
|
|
|
if (event.getAction() == ACTION_UP && event.getKeyCode() == KEYCODE_BACK) {
|
|
|
|
|
AbstractFloatingView topView = AbstractFloatingView.getTopOpenView(mActivity);
|
|
|
|
|
if (topView != null && topView.onBackPressed()) {
|
|
|
|
|
// Handled by the floating view.
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return super.dispatchKeyEvent(event);
|
|
|
|
|
}
|
2020-12-22 12:40:09 -06:00
|
|
|
}
|