Set TextEntryKey for accessibility nodes

This commit is contained in:
Aleksandras Kostarevas 2024-05-28 16:10:20 +03:00
parent b0c33d6a93
commit 7a7bb242bf
2 changed files with 21 additions and 12 deletions

View File

@ -18,15 +18,17 @@ package org.futo.inputmethod.accessibility;
import android.content.Context;
import android.os.SystemClock;
import androidx.core.view.AccessibilityDelegateCompat;
import androidx.core.view.ViewCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewParent;
import android.view.accessibility.AccessibilityEvent;
import androidx.core.view.AccessibilityDelegateCompat;
import androidx.core.view.ViewCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
import androidx.core.view.accessibility.AccessibilityNodeProviderCompat;
import org.futo.inputmethod.keyboard.Key;
import org.futo.inputmethod.keyboard.KeyDetector;
import org.futo.inputmethod.keyboard.Keyboard;
@ -133,14 +135,14 @@ public class KeyboardAccessibilityDelegate<KV extends KeyboardView>
* @return The accessibility node provider for the current keyboard.
*/
@Override
public KeyboardAccessibilityNodeProvider<KV> getAccessibilityNodeProvider(final View host) {
public AccessibilityNodeProviderCompat getAccessibilityNodeProvider(final View host) {
return getAccessibilityNodeProvider();
}
/**
* @return A lazily-instantiated node provider for this view delegate.
*/
protected KeyboardAccessibilityNodeProvider<KV> getAccessibilityNodeProvider() {
protected AccessibilityNodeProviderCompat getAccessibilityNodeProvider() {
// Instantiate the provide only when requested. Since the system
// will call this method multiple times it is a good practice to
// cache the provider instance.
@ -288,7 +290,7 @@ public class KeyboardAccessibilityDelegate<KV extends KeyboardView>
}
key.onPressed();
mKeyboardView.invalidateKey(key);
final KeyboardAccessibilityNodeProvider<KV> provider = getAccessibilityNodeProvider();
final KeyboardAccessibilityNodeProvider<KV> provider = mAccessibilityNodeProvider;
provider.onHoverEnterTo(key);
provider.performActionForKey(key, AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS);
}
@ -311,7 +313,7 @@ public class KeyboardAccessibilityDelegate<KV extends KeyboardView>
}
key.onReleased();
mKeyboardView.invalidateKey(key);
final KeyboardAccessibilityNodeProvider<KV> provider = getAccessibilityNodeProvider();
final KeyboardAccessibilityNodeProvider<KV> provider = mAccessibilityNodeProvider;
provider.onHoverExitFrom(key);
}

View File

@ -18,6 +18,8 @@ package org.futo.inputmethod.accessibility;
import android.graphics.Rect;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.core.view.ViewCompat;
import androidx.core.view.accessibility.AccessibilityEventCompat;
import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
@ -218,11 +220,19 @@ final class KeyboardAccessibilityNodeProvider<KV extends KeyboardView>
return rootInfo;
}
// Obtain and initialize an AccessibilityNodeInfo with information about the virtual view.
final AccessibilityNodeInfoCompat info = AccessibilityNodeInfoCompat.obtain();
populateNodeForVirtualView(virtualViewId, info);
return info;
}
public void populateNodeForVirtualView(int virtualViewId, @NonNull AccessibilityNodeInfoCompat info) {
// Find the key that corresponds to the given virtual view id.
final Key key = getKeyOf(virtualViewId);
if (key == null) {
Log.e(TAG, "Invalid virtual view ID: " + virtualViewId);
return null;
return;
}
final String keyDescription = getKeyDescription(key);
final Rect boundsInParent = key.getHitBox();
@ -233,10 +243,7 @@ final class KeyboardAccessibilityNodeProvider<KV extends KeyboardView>
CoordinateUtils.x(mParentLocation), CoordinateUtils.y(mParentLocation));
final Rect boundsInScreen = mTempBoundsInScreen;
// Obtain and initialize an AccessibilityNodeInfo with information about the virtual view.
final AccessibilityNodeInfoCompat info = AccessibilityNodeInfoCompat.obtain();
info.setPackageName(mKeyboardView.getContext().getPackageName());
//info.setTextEntryKey(true);
info.setClassName(key.getClass().getName());
info.setContentDescription(keyDescription);
info.setBoundsInParent(boundsInParent);
@ -245,6 +252,7 @@ final class KeyboardAccessibilityNodeProvider<KV extends KeyboardView>
info.setSource(mKeyboardView, virtualViewId);
info.setEnabled(key.isEnabled());
info.setVisibleToUser(true);
info.setTextEntryKey(true);
info.addAction(AccessibilityNodeInfoCompat.ACTION_CLICK);
if (key.isLongPressEnabled()) {
info.addAction(AccessibilityNodeInfoCompat.ACTION_LONG_CLICK);
@ -255,7 +263,6 @@ final class KeyboardAccessibilityNodeProvider<KV extends KeyboardView>
} else {
info.addAction(AccessibilityNodeInfoCompat.ACTION_ACCESSIBILITY_FOCUS);
}
return info;
}
@Override