Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e6e1366a authored by Michael W's avatar Michael W Committed by Michael Bestas
Browse files

LatinIME: Implement delete key repetition on emoji keyboard

Currently it only allows deleting one character (or emoji) at a time

Fixes: https://gitlab.com/LineageOS/issues/android/-/issues/6300
Change-Id: I0f01c0cd8132607fd04e842fa97a0999895126fb
parent 1c636a46
Loading
Loading
Loading
Loading
+28 −13
Original line number Original line Diff line number Diff line
/*
/*
 * Copyright (C) 2013 The Android Open Source Project
 * Copyright (C) 2013 The Android Open Source Project
 * Copyright (C) 2023-2025 The LineageOS Project
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * you may not use this file except in compliance with the License.
@@ -22,6 +23,8 @@ import android.content.Context;
import android.content.res.Resources;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Color;
import android.os.Handler;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.preference.PreferenceManager;
import androidx.viewpager2.widget.ViewPager2;
import androidx.viewpager2.widget.ViewPager2;
import android.util.AttributeSet;
import android.util.AttributeSet;
@@ -432,9 +435,21 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange
    }
    }


    private static class DeleteKeyOnTouchListener implements OnTouchListener {
    private static class DeleteKeyOnTouchListener implements OnTouchListener {
        private static final int INITIAL_DELAY = 400;
        private static final int REPEAT_DELAY = 100;

        private KeyboardActionListener mKeyboardActionListener =
        private KeyboardActionListener mKeyboardActionListener =
                KeyboardActionListener.EMPTY_LISTENER;
                KeyboardActionListener.EMPTY_LISTENER;


        private final Handler mRepeatHandler = new Handler(Looper.getMainLooper());
        private final Runnable mRepeatRunnable = new Runnable() {
            @Override
            public void run() {
                mRepeatHandler.postDelayed(this, REPEAT_DELAY);
                onClick();
            }
        };

        public void setKeyboardActionListener(final KeyboardActionListener listener) {
        public void setKeyboardActionListener(final KeyboardActionListener listener) {
            mKeyboardActionListener = listener;
            mKeyboardActionListener = listener;
        }
        }
@@ -443,39 +458,39 @@ public final class EmojiPalettesView extends LinearLayout implements OnTabChange
        public boolean onTouch(final View v, final MotionEvent event) {
        public boolean onTouch(final View v, final MotionEvent event) {
            switch (event.getActionMasked()) {
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_DOWN:
                    onTouchDown(v);
                    mRepeatHandler.removeCallbacks(mRepeatRunnable);
                    mRepeatHandler.postDelayed(mRepeatRunnable, INITIAL_DELAY);
                    v.setPressed(true /* pressed */);
                    onClick();
                    return true;
                    return true;
                case MotionEvent.ACTION_MOVE:
                case MotionEvent.ACTION_MOVE:
                    final float x = event.getX();
                    final float x = event.getX();
                    final float y = event.getY();
                    final float y = event.getY();
                    if (x < 0.0f || v.getWidth() < x || y < 0.0f || v.getHeight() < y) {
                    if (x < 0.0f || v.getWidth() < x || y < 0.0f || v.getHeight() < y) {
                        // Stop generating key events once the finger moves away from the view area.
                        // Stop generating key events once the finger moves away from the view area.
                        mRepeatHandler.removeCallbacks(mRepeatRunnable);
                        onTouchCanceled(v);
                        onTouchCanceled(v);
                    }
                    }
                    return true;
                    return true;
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_UP:
                    onTouchUp(v);
                    mRepeatHandler.removeCallbacks(mRepeatRunnable);
                    v.setPressed(false /* pressed */);
                    return true;
                    return true;
            }
            }
            return false;
            return false;
        }
        }


        private void onTouchDown(final View v) {
        private void onTouchCanceled(final View v) {
            mKeyboardActionListener.onPressKey(Constants.CODE_DELETE,
            v.setBackgroundColor(Color.TRANSPARENT);
                    0 /* repeatCount */, true /* isSinglePointer */);
            v.setPressed(true /* pressed */);
        }
        }


        private void onTouchUp(final View v) {
        private void onClick() {
            mKeyboardActionListener.onPressKey(Constants.CODE_DELETE,
                    0 /* repeatCount */, true /* isSinglePointer */);
            mKeyboardActionListener.onCodeInput(Constants.CODE_DELETE,
            mKeyboardActionListener.onCodeInput(Constants.CODE_DELETE,
                    NOT_A_COORDINATE, NOT_A_COORDINATE, false /* isKeyRepeat */);
                    NOT_A_COORDINATE, NOT_A_COORDINATE, false /* isKeyRepeat */);
            mKeyboardActionListener.onReleaseKey(Constants.CODE_DELETE, false /* withSliding */);
            mKeyboardActionListener.onReleaseKey(Constants.CODE_DELETE, false /* withSliding */);
            v.setPressed(false /* pressed */);
        }

        private void onTouchCanceled(final View v) {
            v.setBackgroundColor(Color.TRANSPARENT);
        }
        }
    }
    }
}
}