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;
|
|
|
|
import android.graphics.drawable.Icon;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.os.Bundle;
|
|
|
|
import android.util.Size;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.view.inputmethod.InlineSuggestion;
|
|
|
|
import android.view.inputmethod.InlineSuggestionsRequest;
|
|
|
|
import android.widget.HorizontalScrollView;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.inline.InlinePresentationSpec;
|
|
|
|
|
|
|
|
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-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
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
@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,
|
2023-12-29 15:27:47 +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,
|
2023-12-29 15:27:47 +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
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static HorizontalScrollView createView(List<InlineSuggestion> inlineSuggestions, Context context) {
|
|
|
|
|
|
|
|
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);
|
|
|
|
inlineSuggestionView.addView(container);
|
2023-12-29 12:51:46 +04:00
|
|
|
|
2024-01-08 12:18:16 +04:00
|
|
|
return inlineSuggestionView;
|
2023-12-29 12:51:46 +04:00
|
|
|
}
|
|
|
|
}
|