Do not initialize accessibility code by default on startup.

Instead listen for if the accessibility service is running or not by
implementing AccessibilityStateChangeListener.
This seems to work smoothly when turning on or off TalkBack.

This should also improve startup time.

Change-Id: I3eb7d6cb9c9c1618afbb59675e4e089c9159019c
Reviewed-by: Christian Stromme <christian.stromme@theqtcompany.com>
bb10
Jan Arve Saether 2014-12-05 10:24:09 +01:00 committed by Jan Arve Sæther
parent a6436ff559
commit 5a6735316a
4 changed files with 86 additions and 47 deletions

View File

@ -4,7 +4,8 @@ API_VERSION = android-16
PATHPREFIX = $$PWD/src/org/qtproject/qt5/android/accessibility
JAVACLASSPATH += $$PWD/src/
JAVACLASSPATH += $$PWD/src/ \
$$DESTDIR/QtAndroid-bundled.jar
JAVASOURCES += \
$$PATHPREFIX/QtAccessibilityDelegate.java \
$$PATHPREFIX/QtNativeAccessibility.java

View File

@ -43,6 +43,7 @@
package org.qtproject.qt5.android.accessibility;
import android.accessibilityservice.AccessibilityService;
import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
@ -60,6 +61,8 @@ import android.content.Context;
import java.util.LinkedList;
import java.util.List;
import org.qtproject.qt5.android.QtActivityDelegate;
public class QtAccessibilityDelegate extends View.AccessibilityDelegate
{
private static final String TAG = "Qt A11Y";
@ -72,8 +75,11 @@ public class QtAccessibilityDelegate extends View.AccessibilityDelegate
// Pretend to be an inner class of the QtSurface.
private static final String DEFAULT_CLASS_NAME = "$VirtualChild";
private final View m_view;
private final AccessibilityManager m_manager;
private View m_view = null;
private AccessibilityManager m_manager;
private QtActivityDelegate m_activityDelegate;
private Activity m_activity;
private ViewGroup m_layout;
// The accessible object that currently has the "accessibility focus"
// usually indicated by a yellow rectangle on screen.
@ -95,17 +101,71 @@ public class QtAccessibilityDelegate extends View.AccessibilityDelegate
}
}
public QtAccessibilityDelegate(View host)
public QtAccessibilityDelegate(Activity activity, ViewGroup layout, QtActivityDelegate activityDelegate)
{
m_view = host;
m_view.setOnHoverListener(new HoverEventListener());
m_manager = (AccessibilityManager) host.getContext()
.getSystemService(Context.ACCESSIBILITY_SERVICE);
m_activity = activity;
m_layout = layout;
m_activityDelegate = activityDelegate;
m_manager = (AccessibilityManager) m_activity.getSystemService(Context.ACCESSIBILITY_SERVICE);
if (m_manager != null) {
AccessibilityManagerListener accServiceListener = new AccessibilityManagerListener();
if (!m_manager.addAccessibilityStateChangeListener(accServiceListener))
Log.w("Qt A11y", "Could not register a11y state change listener");
if (m_manager.isEnabled())
accServiceListener.onAccessibilityStateChanged(true);
}
// Enable Qt Accessibility so that notifications are enabled
QtNativeAccessibility.setActive(true);
}
private class AccessibilityManagerListener implements AccessibilityManager.AccessibilityStateChangeListener
{
@Override
public void onAccessibilityStateChanged(boolean enabled)
{
if (enabled) {
// The accessibility code depends on android API level 16, so dynamically resolve it
if (android.os.Build.VERSION.SDK_INT >= 16) {
try {
View view = m_view;
if (view == null) {
view = new View(m_activity);
view.setId(View.NO_ID);
}
// ### Keep this for debugging for a while. It allows us to visually see that our View
// ### is on top of the surface(s)
// ColorDrawable color = new ColorDrawable(0x80ff8080); //0xAARRGGBB
// view.setBackground(color);
view.setAccessibilityDelegate(QtAccessibilityDelegate.this);
// if all is fine, add it to the layout
if (m_view == null) {
//m_layout.addAccessibilityView(view);
m_layout.addView(view, m_activityDelegate.getSurfaceCount(),
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
}
m_view = view;
m_view.setOnHoverListener(new HoverEventListener());
} catch (Exception e) {
// Unknown exception means something went wrong.
Log.w("Qt A11y", "Unknown exception: " + e.toString());
}
}
} else {
if (m_view != null) {
m_layout.removeView(m_view);
m_view = null;
}
}
}
}
@Override
public AccessibilityNodeProvider getAccessibilityNodeProvider(View host)
{

View File

@ -1,2 +1,3 @@
TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS = jar java templates accessibility

View File

@ -127,7 +127,6 @@ public class QtActivityDelegate
private boolean m_quitApp = true;
private Process m_debuggerProcess = null; // debugger process
private View m_dummyView = null;
private View m_accView = null;
private boolean m_keyboardIsVisible = false;
public boolean m_backKeyPressedSent = false;
private long m_showHideTimeStamp = System.nanoTime();
@ -804,6 +803,22 @@ public class QtActivityDelegate
m_nativeViews = new HashMap<Integer, View>();
m_activity.registerForContextMenu(m_layout);
// Initialize accessibility
try {
final String a11yDelegateClassName = "org.qtproject.qt5.android.accessibility.QtAccessibilityDelegate";
Class<?> qtDelegateClass = Class.forName(a11yDelegateClassName);
Constructor constructor = qtDelegateClass.getConstructor(android.app.Activity.class,
android.view.ViewGroup.class,
this.getClass());
Object accessibilityDelegate = constructor.newInstance(m_activity, m_layout, this);
} catch (ClassNotFoundException e) {
// Class not found is fine since we are compatible with Android API < 16, but the function will
// only be available with that API level.
} catch (Exception e) {
// Unknown exception means something went wrong.
Log.w("Qt A11y", "Unknown exception: " + e.toString());
}
m_activity.setContentView(m_layout,
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
@ -1184,44 +1199,6 @@ public class QtActivityDelegate
m_layout.addView(surface, surfaceCount);
m_surfaces.put(id, surface);
// Initialize Accessibility
// The accessibility code depends on android API level 16, so dynamically resolve it
if (android.os.Build.VERSION.SDK_INT >= 16) {
if (m_accView == null) {
try {
View accView = new View(m_activity);
accView.setId(View.NO_ID);
// ### Keep this for debugging for a while. It allows us to visually see that our View
// ### is on top of the surface(s)
// ColorDrawable color = new ColorDrawable(0x80ff8080); //0xAARRGGBB
// accView.setBackground(color);
final String a11yDelegateClassName = "org.qtproject.qt5.android.accessibility.QtAccessibilityDelegate";
Class<?> qtDelegateClass = Class.forName(a11yDelegateClassName);
Constructor constructor = qtDelegateClass.getConstructor(Class.forName("android.view.View"));
Object accessibilityDelegate = constructor.newInstance(accView);
Class a11yDelegateClass = Class.forName("android.view.View$AccessibilityDelegate");
Method setDelegateMethod = accView.getClass().getMethod("setAccessibilityDelegate", a11yDelegateClass);
setDelegateMethod.invoke(accView, accessibilityDelegate);
// if all is fine, add it to the layout
m_layout.addView(accView, surfaceCount + 1,
new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
m_accView = accView;
} catch (ClassNotFoundException e) {
// Class not found is fine since we are compatible with Android API < 16, but the function will
// only be available with that API level.
} catch (Exception e) {
// Unknown exception means something went wrong.
Log.w("Qt A11y", "Unknown exception: " + e.toString());
}
}
}
}
public void setSurfaceGeometry(int id, int x, int y, int w, int h) {