2023-12-29 12:51:46 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2019 The Android Open Source Project
|
|
|
|
* modified
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2024-01-31 18:32:43 +01:00
|
|
|
package helium314.keyboard.latin.utils;
|
2023-12-29 12:51:46 +04:00
|
|
|
|
|
|
|
import android.annotation.SuppressLint;
|
|
|
|
import android.content.Context;
|
2024-02-07 23:49:38 +04:00
|
|
|
import android.graphics.Canvas;
|
|
|
|
import android.graphics.PixelFormat;
|
|
|
|
import android.graphics.Rect;
|
2023-12-29 12:51:46 +04:00
|
|
|
import android.graphics.drawable.Icon;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.os.Bundle;
|
2024-02-07 23:49:38 +04:00
|
|
|
import android.util.AttributeSet;
|
2023-12-29 12:51:46 +04:00
|
|
|
import android.util.Size;
|
2024-02-07 23:49:38 +04:00
|
|
|
import android.view.Choreographer;
|
|
|
|
import android.view.Surface;
|
|
|
|
import android.view.SurfaceHolder;
|
|
|
|
import android.view.SurfaceView;
|
|
|
|
import android.view.View;
|
2023-12-29 12:51:46 +04:00
|
|
|
import android.view.ViewGroup;
|
2024-02-07 23:49:38 +04:00
|
|
|
import android.view.ViewTreeObserver;
|
2023-12-29 12:51:46 +04:00
|
|
|
import android.view.inputmethod.InlineSuggestion;
|
|
|
|
import android.view.inputmethod.InlineSuggestionsRequest;
|
2024-02-07 23:49:38 +04:00
|
|
|
import android.widget.FrameLayout;
|
2023-12-29 12:51:46 +04:00
|
|
|
import android.widget.HorizontalScrollView;
|
|
|
|
import android.widget.LinearLayout;
|
2024-02-07 23:49:38 +04:00
|
|
|
import android.widget.inline.InlineContentView;
|
2023-12-29 12:51:46 +04:00
|
|
|
import android.widget.inline.InlinePresentationSpec;
|
|
|
|
|
2024-02-07 23:49:38 +04:00
|
|
|
import androidx.annotation.AttrRes;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2023-12-29 12:51:46 +04:00
|
|
|
import androidx.annotation.RequiresApi;
|
|
|
|
import androidx.autofill.inline.UiVersions;
|
2024-01-08 12:18:16 +04:00
|
|
|
import androidx.autofill.inline.UiVersions.StylesBuilder;
|
2023-12-29 12:51:46 +04:00
|
|
|
import androidx.autofill.inline.common.ImageViewStyle;
|
|
|
|
import androidx.autofill.inline.common.TextViewStyle;
|
|
|
|
import androidx.autofill.inline.common.ViewStyle;
|
|
|
|
import androidx.autofill.inline.v1.InlineSuggestionUi;
|
2024-01-08 12:18:16 +04:00
|
|
|
import androidx.autofill.inline.v1.InlineSuggestionUi.Style;
|
2023-12-29 12:51:46 +04:00
|
|
|
|
2024-02-07 23:49:38 +04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2024-01-31 18:32:43 +01:00
|
|
|
import helium314.keyboard.latin.R;
|
|
|
|
import helium314.keyboard.latin.common.ColorType;
|
|
|
|
import helium314.keyboard.latin.common.Colors;
|
|
|
|
import helium314.keyboard.latin.settings.Settings;
|
2023-12-29 12:51:46 +04:00
|
|
|
|
|
|
|
@RequiresApi(api = Build.VERSION_CODES.R)
|
|
|
|
public class InlineAutofillUtils {
|
|
|
|
|
|
|
|
public static InlineSuggestionsRequest createInlineSuggestionRequest(Context context) {
|
|
|
|
|
2023-12-29 15:27:47 +04:00
|
|
|
final Colors colors = Settings.getInstance().getCurrent().mColors;
|
|
|
|
|
2024-01-08 12:18:16 +04:00
|
|
|
StylesBuilder stylesBuilder = UiVersions.newStylesBuilder();
|
|
|
|
@SuppressLint("RestrictedApi") Style style = InlineSuggestionUi.newStyleBuilder()
|
2023-12-29 12:51:46 +04:00
|
|
|
.setSingleIconChipStyle(
|
|
|
|
new ViewStyle.Builder()
|
|
|
|
.setBackground(
|
|
|
|
Icon.createWithResource(context,
|
2024-02-07 23:49:38 +04:00
|
|
|
androidx.autofill.R.drawable.autofill_inline_suggestion_chip_background)
|
2024-01-01 13:16:49 +01:00
|
|
|
.setTint(colors.get(ColorType.AUTOFILL_BACKGROUND_CHIP)))
|
2023-12-29 12:51:46 +04:00
|
|
|
.setPadding(0, 0, 0, 0)
|
|
|
|
.build())
|
|
|
|
.setChipStyle(
|
|
|
|
new ViewStyle.Builder()
|
|
|
|
.setBackground(
|
|
|
|
Icon.createWithResource(context,
|
2024-02-07 23:49:38 +04:00
|
|
|
androidx.autofill.R.drawable.autofill_inline_suggestion_chip_background)
|
2024-01-01 13:16:49 +01:00
|
|
|
.setTint(colors.get(ColorType.AUTOFILL_BACKGROUND_CHIP)))
|
2023-12-29 12:51:46 +04:00
|
|
|
.build())
|
|
|
|
.setStartIconStyle(new ImageViewStyle.Builder().setLayoutMargin(0, 0, 0, 0).build())
|
|
|
|
.setTitleStyle(
|
|
|
|
new TextViewStyle.Builder()
|
2023-12-29 15:27:47 +04:00
|
|
|
.setTextColor(colors.get(ColorType.KEY_TEXT))
|
2023-12-29 12:51:46 +04:00
|
|
|
.setTextSize(12)
|
|
|
|
.build())
|
|
|
|
.setSubtitleStyle(
|
|
|
|
new TextViewStyle.Builder()
|
2023-12-29 15:27:47 +04:00
|
|
|
.setTextColor(colors.get(ColorType.KEY_HINT_TEXT))
|
2023-12-29 12:51:46 +04:00
|
|
|
.setTextSize(10)
|
|
|
|
.build())
|
|
|
|
.setEndIconStyle(new ImageViewStyle.Builder().setLayoutMargin(0, 0, 0, 0).build())
|
|
|
|
.build();
|
|
|
|
stylesBuilder.addStyle(style);
|
|
|
|
Bundle stylesBundle = stylesBuilder.build();
|
|
|
|
|
2024-01-08 12:18:16 +04:00
|
|
|
final int height = context.getResources().getDimensionPixelSize(R.dimen.config_suggestions_strip_height);
|
|
|
|
final Size min = new Size(100, height);
|
|
|
|
final Size max = new Size(740, height);
|
2023-12-29 12:51:46 +04:00
|
|
|
|
2024-02-07 23:49:38 +04:00
|
|
|
// Three InlinePresentationSpec are required for some password managers
|
2023-12-29 12:51:46 +04:00
|
|
|
final ArrayList<InlinePresentationSpec> presentationSpecs = new ArrayList<>();
|
|
|
|
presentationSpecs.add(new InlinePresentationSpec.Builder(min, max).setStyle(stylesBundle).build());
|
|
|
|
presentationSpecs.add(new InlinePresentationSpec.Builder(min, max).setStyle(stylesBundle).build());
|
|
|
|
presentationSpecs.add(new InlinePresentationSpec.Builder(min, max).setStyle(stylesBundle).build());
|
|
|
|
|
|
|
|
return new InlineSuggestionsRequest.Builder(presentationSpecs)
|
|
|
|
.setMaxSuggestionCount(6)
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
2024-02-07 23:49:38 +04:00
|
|
|
public static InlineContentClipView createView(List<InlineSuggestion> inlineSuggestions, Context context) {
|
2023-12-29 12:51:46 +04:00
|
|
|
final int totalSuggestionsCount = inlineSuggestions.size();
|
|
|
|
|
|
|
|
LinearLayout container = new LinearLayout(context);
|
|
|
|
|
|
|
|
for (int i = 0; i < totalSuggestionsCount; i++) {
|
|
|
|
final InlineSuggestion inlineSuggestion = inlineSuggestions.get(i);
|
|
|
|
|
|
|
|
inlineSuggestion.inflate(context, new Size(ViewGroup.LayoutParams.WRAP_CONTENT,
|
|
|
|
ViewGroup.LayoutParams.WRAP_CONTENT), context.getMainExecutor(), (view) -> {
|
|
|
|
if (view != null)
|
|
|
|
container.addView(view);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-01-08 12:18:16 +04:00
|
|
|
HorizontalScrollView inlineSuggestionView = new HorizontalScrollView(context);
|
|
|
|
inlineSuggestionView.setHorizontalScrollBarEnabled(false);
|
2024-02-07 23:49:38 +04:00
|
|
|
inlineSuggestionView.setOverScrollMode(View.OVER_SCROLL_NEVER);
|
|
|
|
|
2024-01-08 12:18:16 +04:00
|
|
|
inlineSuggestionView.addView(container);
|
2023-12-29 12:51:46 +04:00
|
|
|
|
2024-02-07 23:49:38 +04:00
|
|
|
InlineContentClipView mScrollableSuggestionsClip = new InlineContentClipView(context);
|
|
|
|
mScrollableSuggestionsClip.addView(inlineSuggestionView);
|
|
|
|
|
|
|
|
return mScrollableSuggestionsClip;
|
2023-12-29 12:51:46 +04:00
|
|
|
}
|
2024-02-07 23:49:38 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class is a container for showing {@link InlineContentView}s for cases
|
|
|
|
* where you want to ensure they appear only in a given area in your app. An
|
|
|
|
* example is having a scrollable list of items. Note that without this container
|
|
|
|
* the InlineContentViews' surfaces would cover parts of your app as these surfaces
|
|
|
|
* are owned by another process and always appearing on top of your app.
|
|
|
|
*/
|
|
|
|
private static class InlineContentClipView extends FrameLayout {
|
|
|
|
@NonNull
|
|
|
|
private final ViewTreeObserver.OnDrawListener mOnDrawListener =
|
|
|
|
this::clipDescendantInlineContentViews;
|
|
|
|
@NonNull
|
|
|
|
private final Rect mParentBounds = new Rect();
|
|
|
|
@NonNull
|
|
|
|
private final Rect mContentBounds = new Rect();
|
|
|
|
@NonNull
|
|
|
|
private final SurfaceView mBackgroundView;
|
|
|
|
private int mBackgroundColor;
|
|
|
|
public InlineContentClipView(@NonNull Context context) {
|
|
|
|
this(context, /*attrs*/ null);
|
|
|
|
}
|
|
|
|
public InlineContentClipView(@NonNull Context context, @Nullable AttributeSet attrs) {
|
|
|
|
this(context, attrs, /*defStyleAttr*/ 0);
|
|
|
|
}
|
|
|
|
public InlineContentClipView(@NonNull Context context, @Nullable AttributeSet attrs,
|
|
|
|
@AttrRes int defStyleAttr) {
|
|
|
|
super(context, attrs, defStyleAttr);
|
|
|
|
mBackgroundView = new SurfaceView(context);
|
|
|
|
mBackgroundView.setZOrderOnTop(true);
|
|
|
|
mBackgroundView.getHolder().setFormat(PixelFormat.TRANSPARENT);
|
|
|
|
mBackgroundView.setLayoutParams(new ViewGroup.LayoutParams(
|
|
|
|
ViewGroup.LayoutParams.WRAP_CONTENT,
|
|
|
|
ViewGroup.LayoutParams.WRAP_CONTENT));
|
|
|
|
mBackgroundView.getHolder().addCallback(new SurfaceHolder.Callback() {
|
|
|
|
@Override
|
|
|
|
public void surfaceCreated(@NonNull SurfaceHolder holder) {
|
|
|
|
drawBackgroundColorIfReady();
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width,
|
|
|
|
int height) { /*do nothing*/ }
|
|
|
|
@Override
|
|
|
|
public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
|
|
|
|
/*do nothing*/
|
|
|
|
}
|
|
|
|
});
|
|
|
|
addView(mBackgroundView);
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
protected void onAttachedToWindow() {
|
|
|
|
super.onAttachedToWindow();
|
|
|
|
getViewTreeObserver().addOnDrawListener(mOnDrawListener);
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
protected void onDetachedFromWindow() {
|
|
|
|
super.onDetachedFromWindow();
|
|
|
|
getViewTreeObserver().removeOnDrawListener(mOnDrawListener);
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void setBackgroundColor(int color) {
|
|
|
|
mBackgroundColor = color;
|
|
|
|
Choreographer.getInstance().postFrameCallback((frameTimeNanos) ->
|
|
|
|
drawBackgroundColorIfReady());
|
|
|
|
}
|
|
|
|
private void drawBackgroundColorIfReady() {
|
|
|
|
final Surface surface = mBackgroundView.getHolder().getSurface();
|
|
|
|
if (surface.isValid()) {
|
|
|
|
final Canvas canvas = surface.lockCanvas(null);
|
|
|
|
try {
|
|
|
|
canvas.drawColor(mBackgroundColor);
|
|
|
|
} finally {
|
|
|
|
surface.unlockCanvasAndPost(canvas);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void clipDescendantInlineContentViews() {
|
|
|
|
mParentBounds.right = getWidth();
|
|
|
|
mParentBounds.bottom = getHeight();
|
|
|
|
clipDescendantInlineContentViews(this);
|
|
|
|
}
|
|
|
|
private void clipDescendantInlineContentViews(@Nullable View root) {
|
|
|
|
if (root == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (root instanceof InlineContentView inlineContentView) {
|
|
|
|
mContentBounds.set(mParentBounds);
|
|
|
|
offsetRectIntoDescendantCoords(inlineContentView, mContentBounds);
|
|
|
|
inlineContentView.setClipBounds(mContentBounds);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (root instanceof ViewGroup rootGroup) {
|
|
|
|
final int childCount = rootGroup.getChildCount();
|
|
|
|
for (int i = 0; i < childCount; i++) {
|
|
|
|
final View child = rootGroup.getChildAt(i);
|
|
|
|
clipDescendantInlineContentViews(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|