Merge branch 'aosp-13' into 13-wip

This commit is contained in:
MrSluffy
2023-09-21 21:54:56 +08:00
1558 changed files with 103738 additions and 36700 deletions
+154 -19
View File
@@ -25,6 +25,7 @@ import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.InputType;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
@@ -55,11 +56,15 @@ public abstract class ButtonDropTarget extends TextView
private static final int[] sTempCords = new int[2];
private static final int DRAG_VIEW_DROP_DURATION = 285;
private static final float DRAG_VIEW_HOVER_OVER_OPACITY = 0.65f;
private static final int MAX_LINES_TEXT_MULTI_LINE = 2;
private static final int MAX_LINES_TEXT_SINGLE_LINE = 1;
public static final int TOOLTIP_DEFAULT = 0;
public static final int TOOLTIP_LEFT = 1;
public static final int TOOLTIP_RIGHT = 2;
private final Rect mTempRect = new Rect();
protected final Launcher mLauncher;
protected DropTargetBar mDropTargetBar;
@@ -68,7 +73,10 @@ public abstract class ButtonDropTarget extends TextView
protected boolean mActive;
/** Whether an accessible drag is in progress */
private boolean mAccessibleDrag;
/** An item must be dragged at least this many pixels before this drop target is enabled. */
/**
* An item must be dragged at least this many pixels before this drop target is
* enabled.
*/
private final int mDragDistanceThreshold;
/** The size of the drawable shown in the drop target. */
private final int mDrawableSize;
@@ -78,6 +86,8 @@ public abstract class ButtonDropTarget extends TextView
protected CharSequence mText;
protected Drawable mDrawable;
private boolean mTextVisible = true;
private boolean mIconVisible = true;
private boolean mTextMultiLine = true;
private PopupWindow mToolTip;
private int mToolTipLocation;
@@ -92,7 +102,7 @@ public abstract class ButtonDropTarget extends TextView
Resources resources = getResources();
mDragDistanceThreshold = resources.getDimensionPixelSize(R.dimen.drag_distanceThreshold);
mDrawableSize = resources.getDimensionPixelSize(R.dimen.drop_target_text_size);
mDrawableSize = resources.getDimensionPixelSize(R.dimen.drop_target_button_drawable_size);
mDrawablePadding = resources.getDimensionPixelSize(
R.dimen.drop_target_button_drawable_padding);
}
@@ -110,9 +120,9 @@ public abstract class ButtonDropTarget extends TextView
int normalColor = ColorTokens.WorkspaceAccentColor.resolveColor(getContext());
int selectedColor = Themes.getAttrColor(getContext(), R.attr.dropTargetHoverTextColor);
setTextColor(new ColorStateList(
new int[][]{new int[]{-android.R.attr.state_selected}, new int[]{android.R.attr.state_selected}},
new int[]{normalColor, selectedColor}
));
new int[][] { new int[] { -android.R.attr.state_selected },
new int[] { android.R.attr.state_selected } },
new int[] { normalColor, selectedColor }));
}
protected void updateText(int resId) {
@@ -122,12 +132,12 @@ public abstract class ButtonDropTarget extends TextView
}
protected void setDrawable(int resId) {
// We do not set the drawable in the xml as that inflates two drawables corresponding to
// We do not set the drawable in the xml as that inflates two drawables
// corresponding to
// drawableLeft and drawableStart.
mDrawable = getContext().getDrawable(resId).mutate();
mDrawable.setTintList(getTextColors());
centerIcon();
setCompoundDrawablesRelative(mDrawable, null, null, null);
updateIconVisibility();
}
public void setDropTargetBar(DropTargetBar dropTargetBar) {
@@ -157,7 +167,7 @@ public abstract class ButtonDropTarget extends TextView
y = -getMeasuredHeight();
message.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
if (mToolTipLocation == TOOLTIP_LEFT) {
x = - getMeasuredWidth() - message.getMeasuredWidth() / 2;
x = -getMeasuredWidth() - message.getMeasuredWidth() / 2;
} else {
x = getMeasuredWidth() / 2 + message.getMeasuredWidth() / 2;
}
@@ -192,7 +202,12 @@ public abstract class ButtonDropTarget extends TextView
@Override
public void onDragStart(DropTarget.DragObject dragObject, DragOptions options) {
mActive = !options.isKeyboardDrag && supportsDrop(dragObject.dragInfo);
if (options.isKeyboardDrag) {
mActive = false;
} else {
setupItemInfo(dragObject.dragInfo);
mActive = supportsDrop(dragObject.dragInfo);
}
setVisibility(mActive ? View.VISIBLE : View.GONE);
mAccessibleDrag = options.isAccessibleDrag;
@@ -204,6 +219,11 @@ public abstract class ButtonDropTarget extends TextView
return supportsDrop(dragObject.dragInfo);
}
/**
* Setups button for the specified ItemInfo.
*/
protected abstract void setupItemInfo(ItemInfo info);
protected abstract boolean supportsDrop(ItemInfo info);
public abstract boolean supportsAccessibilityDrop(ItemInfo info, View view);
@@ -235,6 +255,7 @@ public abstract class ButtonDropTarget extends TextView
final Rect to = getIconRect(d);
final float scale = (float) to.width() / dragView.getMeasuredWidth();
dragView.detachContentView(/* reattachToPreviousParent= */ true);
mDropTargetBar.deferOnDragEnd();
Runnable onAnimationEndRunnable = () -> {
@@ -252,7 +273,8 @@ public abstract class ButtonDropTarget extends TextView
public abstract int getAccessibilityAction();
@Override
public void prepareAccessibilityDrop() { }
public void prepareAccessibilityDrop() {
}
public abstract void onAccessibilityDrop(View view, ItemInfo item);
@@ -322,13 +344,50 @@ public abstract class ButtonDropTarget extends TextView
if (mTextVisible != isVisible || !TextUtils.equals(newText, getText())) {
mTextVisible = isVisible;
setText(newText);
centerIcon();
setCompoundDrawablesRelative(mDrawable, null, null, null);
int drawablePadding = mTextVisible ? mDrawablePadding : 0;
setCompoundDrawablePadding(drawablePadding);
updateIconVisibility();
}
}
/**
* Display button text over multiple lines when isMultiLine is true, single line
* otherwise.
*/
public void setTextMultiLine(boolean isMultiLine) {
if (mTextMultiLine != isMultiLine) {
mTextMultiLine = isMultiLine;
setSingleLine(!isMultiLine);
setMaxLines(isMultiLine ? MAX_LINES_TEXT_MULTI_LINE : MAX_LINES_TEXT_SINGLE_LINE);
int inputType = InputType.TYPE_CLASS_TEXT;
if (isMultiLine) {
inputType |= InputType.TYPE_TEXT_FLAG_MULTI_LINE;
}
setInputType(inputType);
}
}
protected boolean isTextMultiLine() {
return mTextMultiLine;
}
/**
* Sets the button icon visible when isVisible is true, hides it otherwise.
*/
public void setIconVisible(boolean isVisible) {
if (mIconVisible != isVisible) {
mIconVisible = isVisible;
updateIconVisibility();
}
}
private void updateIconVisibility() {
if (mIconVisible) {
centerIcon();
}
setCompoundDrawablesRelative(mIconVisible ? mDrawable : null, null, null, null);
setCompoundDrawablePadding(mIconVisible && mTextVisible ? mDrawablePadding : 0);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
@@ -340,11 +399,87 @@ public abstract class ButtonDropTarget extends TextView
hideTooltip();
}
/**
* Returns if the text will be truncated within the provided availableWidth.
*/
public boolean isTextTruncated(int availableWidth) {
availableWidth -= (getPaddingLeft() + getPaddingRight() + mDrawable.getIntrinsicWidth()
+ mDrawablePadding);
CharSequence displayedText = TextUtils.ellipsize(mText, getPaint(), availableWidth,
availableWidth -= getPaddingLeft() + getPaddingRight();
if (mIconVisible) {
availableWidth -= mDrawable.getIntrinsicWidth() + getCompoundDrawablePadding();
}
if (availableWidth <= 0) {
return true;
}
CharSequence firstLine = TextUtils.ellipsize(mText, getPaint(), availableWidth,
TextUtils.TruncateAt.END);
return !mText.equals(displayedText);
if (!mTextMultiLine) {
return !TextUtils.equals(mText, firstLine);
}
if (TextUtils.equals(mText, firstLine)) {
// When multi-line is active, if it can display as one line, then text is not
// truncated.
return false;
}
CharSequence secondLine = TextUtils.ellipsize(mText.subSequence(firstLine.length(), mText.length()),
getPaint(), availableWidth, TextUtils.TruncateAt.END);
return !(TextUtils.equals(mText.subSequence(0, firstLine.length()), firstLine)
&& TextUtils.equals(mText.subSequence(firstLine.length(), secondLine.length()),
secondLine));
}
/**
* Returns if the text will be clipped vertically within the provided
* availableHeight.
*/
private boolean isTextClippedVertically(int availableHeight) {
availableHeight -= getPaddingTop() + getPaddingBottom();
if (availableHeight <= 0) {
return true;
}
getPaint().getTextBounds(mText.toString(), 0, mText.length(), mTempRect);
// Add bounds bottom to height, as text bounds height measures from the text
// baseline and
// above, which characters can descend below
return mTempRect.bottom + mTempRect.height() >= availableHeight;
}
/**
* Reduce the size of the text until it fits the measured width or reaches a
* minimum.
*
* The minimum size is defined by
* {@code R.dimen.button_drop_target_min_text_size} and
* it diminishes by intervals defined by
* {@code R.dimen.button_drop_target_resize_text_increment}
* This functionality is very similar to the option
* {@link TextView#setAutoSizeTextTypeWithDefaults(int)} but can't be used in
* this view because
* the layout width is {@code WRAP_CONTENT}.
*
* @return The biggest text size in SP that makes the text fit or if the text
* can't fit returns
* the min available value
*/
public float resizeTextToFit() {
float minSize = Utilities.pxToSp(getResources()
.getDimensionPixelSize(R.dimen.button_drop_target_min_text_size));
float step = Utilities.pxToSp(getResources()
.getDimensionPixelSize(R.dimen.button_drop_target_resize_text_increment));
float textSize = Utilities.pxToSp(getTextSize());
int availableWidth = getMeasuredWidth();
int availableHeight = getMeasuredHeight();
while (isTextTruncated(availableWidth) || isTextClippedVertically(availableHeight)) {
textSize -= step;
if (textSize < minSize) {
textSize = minSize;
setTextSize(textSize);
break;
}
setTextSize(textSize);
}
return textSize;
}
}