Add better version checks for accessibility

We would spam the debug output on devices with api < 16 with some
warnings that the super class a11y delegate could not be found and
others.
Instead check the runtime version before trying to load the JNI code and
only load the delegate if api is new enough.

Change-Id: I52286cb99924b034b9b58c53566f15030939b0c9
Reviewed-by: Eskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@digia.com>
This commit is contained in:
Frederik Gladhorn 2013-10-24 17:37:27 +02:00 committed by The Qt Project
parent 54ed14d5c6
commit 2c11a492fb
2 changed files with 32 additions and 15 deletions

View File

@ -117,21 +117,23 @@ public class QtSurface extends SurfaceView implements SurfaceHolder.Callback
// Initialize Accessibility
// The accessibility code depends on android API level 16, so dynamically resolve it
try {
final String a11yDelegateClassName = "org.qtproject.qt5.android.accessibility.QtAccessibilityDelegate";
Class<?> qtDelegateClass = Class.forName(a11yDelegateClassName);
Constructor constructor = qtDelegateClass.getConstructor(Class.forName("android.view.View"));
m_accessibilityDelegate = constructor.newInstance(this);
if (android.os.Build.VERSION.SDK_INT >= 16) {
try {
final String a11yDelegateClassName = "org.qtproject.qt5.android.accessibility.QtAccessibilityDelegate";
Class<?> qtDelegateClass = Class.forName(a11yDelegateClassName);
Constructor constructor = qtDelegateClass.getConstructor(Class.forName("android.view.View"));
m_accessibilityDelegate = constructor.newInstance(this);
Class a11yDelegateClass = Class.forName("android.view.View$AccessibilityDelegate");
Method setDelegateMethod = this.getClass().getMethod("setAccessibilityDelegate", a11yDelegateClass);
setDelegateMethod.invoke(this, m_accessibilityDelegate);
} 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());
Class a11yDelegateClass = Class.forName("android.view.View$AccessibilityDelegate");
Method setDelegateMethod = this.getClass().getMethod("setAccessibilityDelegate", a11yDelegateClass);
setDelegateMethod.invoke(this, m_accessibilityDelegate);
} 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());
}
}
}

View File

@ -834,6 +834,15 @@ static int registerNatives(JNIEnv *env)
return JNI_TRUE;
}
jint androidApiLevel(JNIEnv *env)
{
jclass clazz;
FIND_AND_CHECK_CLASS("android/os/Build$VERSION");
jfieldID fieldId;
GET_AND_CHECK_STATIC_FIELD(fieldId, clazz, "SDK_INT", "I");
return env->GetStaticIntField(clazz, fieldId);
}
Q_DECL_EXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void */*reserved*/)
{
typedef union {
@ -856,11 +865,17 @@ Q_DECL_EXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void */*reserved*/)
|| !QtAndroidInput::registerNatives(env)
|| !QtAndroidClipboard::registerNatives(env)
|| !QtAndroidMenu::registerNatives(env)
|| !QtAndroidAccessibility::registerNatives(env)) {
) {
__android_log_print(ANDROID_LOG_FATAL, "Qt", "registerNatives failed");
return -1;
}
jint apiLevel = androidApiLevel(env);
if (apiLevel >= 16 && !QtAndroidAccessibility::registerNatives(env)) {
__android_log_print(ANDROID_LOG_FATAL, "Qt A11y", "registerNatives failed");
return -1;
}
m_javaVM = vm;
return JNI_VERSION_1_4;
}