2023-01-24 15:27:00 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2023 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;
|
|
|
|
|
|
|
|
|
|
import static com.android.quickstep.util.BorderAnimator.DEFAULT_BORDER_COLOR;
|
|
|
|
|
|
|
|
|
|
import android.animation.Animator;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.res.Resources;
|
2023-05-12 12:09:36 -07:00
|
|
|
import android.content.res.TypedArray;
|
2023-01-24 15:27:00 -08:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
|
import android.graphics.Canvas;
|
2024-03-25 12:31:28 -04:00
|
|
|
import android.graphics.Color;
|
2024-03-13 14:04:02 -04:00
|
|
|
import android.graphics.drawable.Drawable;
|
2023-01-24 15:27:00 -08:00
|
|
|
import android.util.AttributeSet;
|
2023-04-06 14:04:48 -07:00
|
|
|
import android.view.View;
|
2023-01-24 15:27:00 -08:00
|
|
|
import android.widget.ImageView;
|
|
|
|
|
|
2023-05-18 10:35:16 -07:00
|
|
|
import androidx.annotation.ColorInt;
|
2023-01-24 15:27:00 -08:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
|
import androidx.constraintlayout.widget.ConstraintLayout;
|
|
|
|
|
|
|
|
|
|
import com.android.launcher3.R;
|
2023-05-18 10:35:16 -07:00
|
|
|
import com.android.launcher3.util.Preconditions;
|
2024-08-28 11:31:39 -07:00
|
|
|
import com.android.launcher3.util.SplitConfigurationOptions.SplitBounds;
|
2023-01-24 15:27:00 -08:00
|
|
|
import com.android.quickstep.util.BorderAnimator;
|
|
|
|
|
import com.android.systemui.shared.recents.model.Task;
|
|
|
|
|
import com.android.systemui.shared.recents.model.ThumbnailData;
|
|
|
|
|
|
2023-09-27 13:19:33 -04:00
|
|
|
import kotlin.Unit;
|
|
|
|
|
|
2024-08-28 11:31:39 -07:00
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
|
2023-01-24 15:27:00 -08:00
|
|
|
/**
|
|
|
|
|
* A view that displays a recent task during a keyboard quick switch.
|
|
|
|
|
*/
|
|
|
|
|
public class KeyboardQuickSwitchTaskView extends ConstraintLayout {
|
|
|
|
|
|
2024-03-13 14:04:02 -04:00
|
|
|
private static final float THUMBNAIL_BLUR_RADIUS = 1f;
|
2024-07-11 11:38:51 -04:00
|
|
|
private static final int INVALID_BORDER_RADIUS = -1;
|
2024-03-13 14:04:02 -04:00
|
|
|
|
2023-05-18 10:35:16 -07:00
|
|
|
@ColorInt private final int mBorderColor;
|
2024-07-11 11:38:51 -04:00
|
|
|
@ColorInt private final int mBorderRadius;
|
2023-05-18 10:35:16 -07:00
|
|
|
|
|
|
|
|
@Nullable private BorderAnimator mBorderAnimator;
|
2023-01-24 15:27:00 -08:00
|
|
|
|
|
|
|
|
@Nullable private ImageView mThumbnailView1;
|
|
|
|
|
@Nullable private ImageView mThumbnailView2;
|
2023-05-03 12:02:58 -07:00
|
|
|
@Nullable private ImageView mIcon1;
|
|
|
|
|
@Nullable private ImageView mIcon2;
|
2023-04-06 14:04:48 -07:00
|
|
|
@Nullable private View mContent;
|
2023-01-24 15:27:00 -08:00
|
|
|
|
|
|
|
|
public KeyboardQuickSwitchTaskView(@NonNull Context context) {
|
|
|
|
|
this(context, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public KeyboardQuickSwitchTaskView(@NonNull Context context, @Nullable AttributeSet attrs) {
|
|
|
|
|
this(context, attrs, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public KeyboardQuickSwitchTaskView(
|
|
|
|
|
@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
|
|
|
|
|
this(context, attrs, defStyleAttr, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public KeyboardQuickSwitchTaskView(
|
|
|
|
|
@NonNull Context context,
|
|
|
|
|
@Nullable AttributeSet attrs,
|
|
|
|
|
int defStyleAttr,
|
|
|
|
|
int defStyleRes) {
|
|
|
|
|
super(context, attrs, defStyleAttr, defStyleRes);
|
2023-05-12 12:09:36 -07:00
|
|
|
TypedArray ta = context.obtainStyledAttributes(
|
|
|
|
|
attrs, R.styleable.TaskView, defStyleAttr, defStyleRes);
|
|
|
|
|
|
2023-01-24 15:27:00 -08:00
|
|
|
setWillNotDraw(false);
|
2023-05-18 10:35:16 -07:00
|
|
|
|
|
|
|
|
mBorderColor = ta.getColor(
|
2023-07-14 15:44:16 +01:00
|
|
|
R.styleable.TaskView_focusBorderColor, DEFAULT_BORDER_COLOR);
|
2024-07-11 11:38:51 -04:00
|
|
|
mBorderRadius = ta.getDimensionPixelSize(
|
|
|
|
|
R.styleable.TaskView_focusBorderRadius, INVALID_BORDER_RADIUS);
|
2023-05-12 12:09:36 -07:00
|
|
|
ta.recycle();
|
2023-01-24 15:27:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onFinishInflate() {
|
|
|
|
|
super.onFinishInflate();
|
2024-03-13 14:04:02 -04:00
|
|
|
mThumbnailView1 = findViewById(R.id.thumbnail_1);
|
|
|
|
|
mThumbnailView2 = findViewById(R.id.thumbnail_2);
|
|
|
|
|
mIcon1 = findViewById(R.id.icon_1);
|
|
|
|
|
mIcon2 = findViewById(R.id.icon_2);
|
2023-04-06 14:04:48 -07:00
|
|
|
mContent = findViewById(R.id.content);
|
2023-05-18 10:35:16 -07:00
|
|
|
|
|
|
|
|
Preconditions.assertNotNull(mContent);
|
2025-02-13 11:39:51 -05:00
|
|
|
|
|
|
|
|
TypefaceUtils.setTypeface(
|
|
|
|
|
mContent.findViewById(R.id.large_text),
|
|
|
|
|
TypefaceUtils.FONT_FAMILY_HEADLINE_LARGE_EMPHASIZED
|
|
|
|
|
);
|
|
|
|
|
TypefaceUtils.setTypeface(
|
|
|
|
|
mContent.findViewById(R.id.small_text),
|
|
|
|
|
TypefaceUtils.FONT_FAMILY_LABEL_LARGE_BASELINE
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Resources resources = mContext.getResources();
|
2023-09-27 13:19:33 -04:00
|
|
|
mBorderAnimator = BorderAnimator.createScalingBorderAnimator(
|
2024-07-11 11:38:51 -04:00
|
|
|
/* borderRadiusPx= */ mBorderRadius != INVALID_BORDER_RADIUS
|
|
|
|
|
? mBorderRadius
|
|
|
|
|
: resources.getDimensionPixelSize(
|
|
|
|
|
R.dimen.keyboard_quick_switch_task_view_radius),
|
2023-09-27 13:19:33 -04:00
|
|
|
/* borderWidthPx= */ resources.getDimensionPixelSize(
|
2023-05-18 10:35:16 -07:00
|
|
|
R.dimen.keyboard_quick_switch_border_width),
|
2023-09-27 13:19:33 -04:00
|
|
|
/* boundsBuilder= */ bounds -> {
|
|
|
|
|
bounds.set(0, 0, getWidth(), getHeight());
|
|
|
|
|
return Unit.INSTANCE;
|
|
|
|
|
},
|
|
|
|
|
/* targetView= */ this,
|
|
|
|
|
/* contentView= */ mContent,
|
|
|
|
|
/* borderColor= */ mBorderColor);
|
2023-01-24 15:27:00 -08:00
|
|
|
}
|
|
|
|
|
|
2023-05-18 10:35:16 -07:00
|
|
|
@Nullable
|
2023-01-24 15:27:00 -08:00
|
|
|
protected Animator getFocusAnimator(boolean focused) {
|
2023-05-18 10:35:16 -07:00
|
|
|
return mBorderAnimator == null ? null : mBorderAnimator.buildAnimator(focused);
|
2023-01-24 15:27:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void draw(Canvas canvas) {
|
|
|
|
|
super.draw(canvas);
|
2023-05-18 10:35:16 -07:00
|
|
|
if (mBorderAnimator != null) {
|
|
|
|
|
mBorderAnimator.drawBorder(canvas);
|
|
|
|
|
}
|
2023-01-24 15:27:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void setThumbnails(
|
|
|
|
|
@NonNull Task task1,
|
|
|
|
|
@Nullable Task task2,
|
|
|
|
|
@Nullable ThumbnailUpdateFunction thumbnailUpdateFunction,
|
2023-05-03 12:02:58 -07:00
|
|
|
@Nullable IconUpdateFunction iconUpdateFunction) {
|
2023-01-24 15:27:00 -08:00
|
|
|
applyThumbnail(mThumbnailView1, task1, thumbnailUpdateFunction);
|
|
|
|
|
applyThumbnail(mThumbnailView2, task2, thumbnailUpdateFunction);
|
|
|
|
|
|
2023-05-03 12:02:58 -07:00
|
|
|
if (iconUpdateFunction == null) {
|
2023-05-16 22:09:19 +00:00
|
|
|
applyIcon(mIcon1, task1);
|
|
|
|
|
applyIcon(mIcon2, task2);
|
2023-01-24 15:27:00 -08:00
|
|
|
setContentDescription(task2 == null
|
|
|
|
|
? task1.titleDescription
|
|
|
|
|
: getContext().getString(
|
|
|
|
|
R.string.quick_switch_split_task,
|
|
|
|
|
task1.titleDescription,
|
|
|
|
|
task2.titleDescription));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-05-03 12:02:58 -07:00
|
|
|
iconUpdateFunction.updateIconInBackground(task1, t -> {
|
|
|
|
|
applyIcon(mIcon1, task1);
|
|
|
|
|
if (task2 != null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setContentDescription(task1.titleDescription);
|
|
|
|
|
});
|
2023-01-24 15:27:00 -08:00
|
|
|
if (task2 == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-05-03 12:02:58 -07:00
|
|
|
iconUpdateFunction.updateIconInBackground(task2, t -> {
|
|
|
|
|
applyIcon(mIcon2, task2);
|
|
|
|
|
setContentDescription(getContext().getString(
|
|
|
|
|
R.string.quick_switch_split_task,
|
|
|
|
|
task1.titleDescription,
|
|
|
|
|
task2.titleDescription));
|
|
|
|
|
});
|
2023-01-24 15:27:00 -08:00
|
|
|
}
|
|
|
|
|
|
2024-08-28 11:31:39 -07:00
|
|
|
protected void setThumbnailsForSplitTasks(
|
|
|
|
|
@NonNull Task task1,
|
|
|
|
|
@Nullable Task task2,
|
|
|
|
|
@Nullable ThumbnailUpdateFunction thumbnailUpdateFunction,
|
|
|
|
|
@Nullable IconUpdateFunction iconUpdateFunction,
|
|
|
|
|
@Nullable SplitBounds splitBounds) {
|
|
|
|
|
setThumbnails(task1, task2, thumbnailUpdateFunction, iconUpdateFunction);
|
|
|
|
|
|
|
|
|
|
if (splitBounds == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final boolean isLeftRightSplit = !splitBounds.appsStackedVertically;
|
2025-02-19 15:38:27 -08:00
|
|
|
final float leftOrTopTaskPercent = splitBounds.getLeftTopTaskPercent();
|
2024-08-28 11:31:39 -07:00
|
|
|
|
|
|
|
|
ConstraintLayout.LayoutParams leftTopParams = (ConstraintLayout.LayoutParams)
|
|
|
|
|
mThumbnailView1.getLayoutParams();
|
|
|
|
|
ConstraintLayout.LayoutParams rightBottomParams = (ConstraintLayout.LayoutParams)
|
|
|
|
|
mThumbnailView2.getLayoutParams();
|
|
|
|
|
|
|
|
|
|
if (isLeftRightSplit) {
|
|
|
|
|
// Set thumbnail view ratio in left right split mode.
|
|
|
|
|
leftTopParams.width = 0; // Set width to 0dp, so it uses the constraint dimension ratio.
|
|
|
|
|
leftTopParams.height = ConstraintLayout.LayoutParams.MATCH_PARENT;
|
|
|
|
|
leftTopParams.matchConstraintPercentWidth = leftOrTopTaskPercent;
|
|
|
|
|
leftTopParams.leftToLeft = ConstraintLayout.LayoutParams.PARENT_ID;
|
|
|
|
|
leftTopParams.rightToLeft = R.id.thumbnail_2;
|
|
|
|
|
mThumbnailView1.setLayoutParams(leftTopParams);
|
|
|
|
|
|
|
|
|
|
rightBottomParams.width = 0;
|
|
|
|
|
rightBottomParams.height = ConstraintLayout.LayoutParams.MATCH_PARENT;
|
|
|
|
|
rightBottomParams.matchConstraintPercentWidth = 1 - leftOrTopTaskPercent;
|
|
|
|
|
rightBottomParams.leftToRight = R.id.thumbnail_1;
|
|
|
|
|
rightBottomParams.rightToRight = ConstraintLayout.LayoutParams.PARENT_ID;
|
|
|
|
|
mThumbnailView2.setLayoutParams(rightBottomParams);
|
|
|
|
|
} else {
|
|
|
|
|
// Set thumbnail view ratio in top bottom split mode.
|
|
|
|
|
leftTopParams.height = 0;
|
|
|
|
|
leftTopParams.width = ConstraintLayout.LayoutParams.MATCH_PARENT;
|
|
|
|
|
leftTopParams.matchConstraintPercentHeight = leftOrTopTaskPercent;
|
|
|
|
|
leftTopParams.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
|
|
|
|
|
leftTopParams.bottomToTop = R.id.thumbnail_2;
|
|
|
|
|
mThumbnailView1.setLayoutParams(leftTopParams);
|
|
|
|
|
|
|
|
|
|
rightBottomParams.height = 0;
|
|
|
|
|
rightBottomParams.width = ConstraintLayout.LayoutParams.MATCH_PARENT;
|
|
|
|
|
rightBottomParams.matchConstraintPercentHeight = 1 - leftOrTopTaskPercent;
|
|
|
|
|
rightBottomParams.topToBottom = R.id.thumbnail_1;
|
|
|
|
|
rightBottomParams.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID;
|
|
|
|
|
mThumbnailView2.setLayoutParams(rightBottomParams);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-24 15:27:00 -08:00
|
|
|
private void applyThumbnail(
|
|
|
|
|
@Nullable ImageView thumbnailView,
|
|
|
|
|
@Nullable Task task,
|
|
|
|
|
@Nullable ThumbnailUpdateFunction updateFunction) {
|
2024-03-13 14:04:02 -04:00
|
|
|
if (thumbnailView == null || task == null) {
|
2023-01-24 15:27:00 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (updateFunction == null) {
|
2024-03-25 12:31:28 -04:00
|
|
|
applyThumbnail(thumbnailView, task.colorBackground, task.thumbnail);
|
2023-01-24 15:27:00 -08:00
|
|
|
return;
|
|
|
|
|
}
|
2024-03-25 12:31:28 -04:00
|
|
|
updateFunction.updateThumbnailInBackground(task, thumbnailData ->
|
|
|
|
|
applyThumbnail(thumbnailView, task.colorBackground, thumbnailData));
|
2023-01-24 15:27:00 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void applyThumbnail(
|
2024-03-13 14:04:02 -04:00
|
|
|
@NonNull ImageView thumbnailView,
|
2024-03-25 12:31:28 -04:00
|
|
|
@ColorInt int backgroundColor,
|
|
|
|
|
@Nullable ThumbnailData thumbnailData) {
|
2024-05-16 11:26:18 +00:00
|
|
|
Bitmap bm = thumbnailData == null ? null : thumbnailData.getThumbnail();
|
2023-01-24 15:27:00 -08:00
|
|
|
|
2024-03-13 14:04:02 -04:00
|
|
|
if (thumbnailView.getVisibility() != VISIBLE) {
|
|
|
|
|
thumbnailView.setVisibility(VISIBLE);
|
|
|
|
|
}
|
2024-03-25 12:31:28 -04:00
|
|
|
thumbnailView.getBackground().setTint(bm == null ? backgroundColor : Color.TRANSPARENT);
|
2024-03-13 14:04:02 -04:00
|
|
|
thumbnailView.setImageDrawable(new BlurredBitmapDrawable(bm, THUMBNAIL_BLUR_RADIUS));
|
2023-01-24 15:27:00 -08:00
|
|
|
}
|
|
|
|
|
|
2023-05-16 22:09:19 +00:00
|
|
|
private void applyIcon(@Nullable ImageView iconView, @Nullable Task task) {
|
2024-03-13 14:04:02 -04:00
|
|
|
if (iconView == null || task == null || task.icon == null) {
|
2023-05-03 12:02:58 -07:00
|
|
|
return;
|
|
|
|
|
}
|
2024-03-13 14:04:02 -04:00
|
|
|
Drawable.ConstantState constantState = task.icon.getConstantState();
|
|
|
|
|
if (constantState == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (iconView.getVisibility() != VISIBLE) {
|
|
|
|
|
iconView.setVisibility(VISIBLE);
|
|
|
|
|
}
|
|
|
|
|
// Use the bitmap directly since the drawable's scale can change
|
|
|
|
|
iconView.setImageDrawable(
|
|
|
|
|
constantState.newDrawable(getResources(), getContext().getTheme()));
|
2023-05-03 12:02:58 -07:00
|
|
|
}
|
|
|
|
|
|
2023-01-24 15:27:00 -08:00
|
|
|
protected interface ThumbnailUpdateFunction {
|
|
|
|
|
|
|
|
|
|
void updateThumbnailInBackground(Task task, Consumer<ThumbnailData> callback);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-03 12:02:58 -07:00
|
|
|
protected interface IconUpdateFunction {
|
2023-01-24 15:27:00 -08:00
|
|
|
|
2023-05-03 12:02:58 -07:00
|
|
|
void updateIconInBackground(Task task, Consumer<Task> callback);
|
2023-01-24 15:27:00 -08:00
|
|
|
}
|
|
|
|
|
}
|