mirror of
https://gitee.com/shuto-github/phonegap-mobile-accessibility.git
synced 2026-05-08 00:00:02 +08:00
Fix issue #32 Doesn't work with cordova-plugin-crosswalk-webview
Use reflection for getView, getSettings, getTextSize/getTextZoom, and setTextSize/setTextZoom so that the View does not need to be cast as an android.webkit.WebView.
This commit is contained in:
+3
-3
@@ -23,9 +23,9 @@ package com.phonegap.plugin.mobileaccessibility;
|
||||
|
||||
import android.view.ViewParent;
|
||||
|
||||
public abstract class AbstractMobileAccessibilityHelper {
|
||||
protected MobileAccessibility mMobileAccessibility;
|
||||
protected ViewParent mParent;
|
||||
abstract class AbstractMobileAccessibilityHelper {
|
||||
MobileAccessibility mMobileAccessibility;
|
||||
ViewParent mParent;
|
||||
public abstract void initialize(MobileAccessibility mobileAccessibility);
|
||||
public abstract boolean isClosedCaptioningEnabled();
|
||||
public abstract boolean isScreenReaderRunning();
|
||||
|
||||
+45
-24
@@ -26,6 +26,7 @@ import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.view.accessibility.AccessibilityEvent;
|
||||
import android.view.accessibility.AccessibilityManager;
|
||||
import android.view.View;
|
||||
import android.webkit.WebSettings;
|
||||
import android.webkit.WebView;
|
||||
|
||||
@@ -36,19 +37,20 @@ import java.lang.reflect.Method;
|
||||
@TargetApi(Build.VERSION_CODES.DONUT)
|
||||
public class DonutMobileAccessibilityHelper extends
|
||||
AbstractMobileAccessibilityHelper {
|
||||
protected AccessibilityManager mAccessibilityManager;
|
||||
protected WebView mWebView;
|
||||
AccessibilityManager mAccessibilityManager;
|
||||
View mView;
|
||||
|
||||
@Override
|
||||
public void initialize(MobileAccessibility mobileAccessibility) {
|
||||
mMobileAccessibility = mobileAccessibility;
|
||||
|
||||
WebView view;
|
||||
try {
|
||||
mWebView = (WebView) mobileAccessibility.webView;
|
||||
view = (WebView) mobileAccessibility.webView;
|
||||
mView = view;
|
||||
} catch(ClassCastException ce) { // cordova-android 4.0+
|
||||
try {
|
||||
Method getView = mobileAccessibility.webView.getClass().getMethod("getView");
|
||||
mWebView = (WebView) getView.invoke(mobileAccessibility.webView);
|
||||
mView = (View) getView.invoke(mobileAccessibility.webView);
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
@@ -108,9 +110,9 @@ public class DonutMobileAccessibilityHelper extends
|
||||
final int eventType = AccessibilityEvent.TYPE_VIEW_FOCUSED;
|
||||
final AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
|
||||
event.getText().add(text);
|
||||
event.setEnabled(mWebView.isEnabled());
|
||||
event.setClassName(mWebView.getClass().getName());
|
||||
event.setPackageName(mWebView.getContext().getPackageName());
|
||||
event.setEnabled(mView.isEnabled());
|
||||
event.setClassName(mView.getClass().getName());
|
||||
event.setPackageName(mView.getContext().getPackageName());
|
||||
event.setContentDescription(null);
|
||||
|
||||
mAccessibilityManager.sendAccessibilityEvent(event);
|
||||
@@ -120,7 +122,23 @@ public class DonutMobileAccessibilityHelper extends
|
||||
@Override
|
||||
public double getTextZoom() {
|
||||
double zoom = 100;
|
||||
WebSettings.TextSize wTextSize = mWebView.getSettings().getTextSize();
|
||||
WebSettings.TextSize wTextSize = WebSettings.TextSize.NORMAL;
|
||||
|
||||
try {
|
||||
Method getSettings = mView.getClass().getMethod("getSettings");
|
||||
Object wSettings = getSettings.invoke(mView);
|
||||
Method getTextSize = wSettings.getClass().getMethod("getTextSize");
|
||||
wTextSize = (WebSettings.TextSize) getTextSize.invoke(wSettings);
|
||||
} catch(ClassCastException ce) {
|
||||
ce.printStackTrace();
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
switch (wTextSize) {
|
||||
case LARGEST:
|
||||
zoom = 200;
|
||||
@@ -128,39 +146,42 @@ public class DonutMobileAccessibilityHelper extends
|
||||
case LARGER:
|
||||
zoom = 150;
|
||||
break;
|
||||
case NORMAL:
|
||||
zoom = 100;
|
||||
break;
|
||||
case SMALLER:
|
||||
zoom = 75;
|
||||
break;
|
||||
case SMALLEST:
|
||||
zoom = 50;
|
||||
break;
|
||||
default:
|
||||
zoom = 100;
|
||||
break;
|
||||
}
|
||||
|
||||
return zoom;
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public void setTextZoom(double textZoom) {
|
||||
final double zoom = textZoom;
|
||||
WebSettings.TextSize wTextSize = WebSettings.TextSize.NORMAL;
|
||||
if (zoom > 115) {
|
||||
WebSettings.TextSize wTextSize = WebSettings.TextSize.SMALLEST;
|
||||
if (textZoom > 115) {
|
||||
wTextSize = WebSettings.TextSize.LARGEST;
|
||||
} else if (zoom > 100) {
|
||||
} else if (textZoom > 100) {
|
||||
wTextSize = WebSettings.TextSize.LARGER;
|
||||
} else if (zoom == 100) {
|
||||
} else if (textZoom == 100) {
|
||||
wTextSize = WebSettings.TextSize.NORMAL;
|
||||
} else if (zoom > 50) {
|
||||
} else if (textZoom > 50) {
|
||||
wTextSize = WebSettings.TextSize.SMALLER;
|
||||
} else {
|
||||
wTextSize = WebSettings.TextSize.SMALLEST;
|
||||
}
|
||||
//Log.i("MobileAccessibility", "fontScale = " + zoom + ", WebSettings.TextSize = " + wTextSize.toString());
|
||||
mWebView.getSettings().setTextSize(wTextSize);
|
||||
try {
|
||||
Method getSettings = mView.getClass().getMethod("getSettings");
|
||||
Object wSettings = getSettings.invoke(mView);
|
||||
Method setTextSize = wSettings.getClass().getMethod("setTextSize", WebSettings.TextSize.class);
|
||||
setTextSize.invoke(wSettings, wTextSize);
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+36
-5
@@ -26,10 +26,14 @@ import android.annotation.TargetApi;
|
||||
import android.os.Build;
|
||||
import android.view.accessibility.AccessibilityManager.AccessibilityStateChangeListener;
|
||||
|
||||
import java.lang.IllegalAccessException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
|
||||
public class IceCreamSandwichMobileAccessibilityHelper extends
|
||||
DonutMobileAccessibilityHelper {
|
||||
protected AccessibilityStateChangeListener mAccessibilityStateChangeListener;
|
||||
private AccessibilityStateChangeListener mAccessibilityStateChangeListener;
|
||||
|
||||
@Override
|
||||
public boolean isScreenReaderRunning() {
|
||||
@@ -52,17 +56,44 @@ public class IceCreamSandwichMobileAccessibilityHelper extends
|
||||
|
||||
@Override
|
||||
public double getTextZoom() {
|
||||
return mWebView.getSettings().getTextZoom();
|
||||
double zoom = 100;
|
||||
try {
|
||||
Method getSettings = mView.getClass().getMethod("getSettings");
|
||||
Object wSettings = getSettings.invoke(mView);
|
||||
Method getTextZoom = wSettings.getClass().getMethod("getTextZoom");
|
||||
zoom = Double.valueOf(getTextZoom.invoke(wSettings).toString());
|
||||
} catch (ClassCastException ce) {
|
||||
ce.printStackTrace();
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return zoom;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTextZoom(double textZoom) {
|
||||
final double zoom = textZoom;
|
||||
//Log.i("MobileAccessibility", "setTextZoom(" + zoom + ")");
|
||||
mWebView.getSettings().setTextZoom((int) zoom);
|
||||
try {
|
||||
Method getSettings = mView.getClass().getMethod("getSettings");
|
||||
Object wSettings = getSettings.invoke(mView);
|
||||
Method setTextZoom = wSettings.getClass().getMethod("setTextZoom", Integer.TYPE);
|
||||
setTextZoom.invoke(wSettings, (int) textZoom);
|
||||
} catch (ClassCastException ce) {
|
||||
ce.printStackTrace();
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
protected class InternalAccessibilityStateChangeListener
|
||||
private class InternalAccessibilityStateChangeListener
|
||||
implements AccessibilityStateChangeListener {
|
||||
|
||||
@Override
|
||||
|
||||
+3
-26
@@ -26,11 +26,6 @@ import com.phonegap.plugin.mobileaccessibility.MobileAccessibility;
|
||||
import android.annotation.TargetApi;
|
||||
import android.os.Build;
|
||||
import android.view.accessibility.AccessibilityEvent;
|
||||
import android.webkit.WebView;
|
||||
|
||||
import java.lang.IllegalAccessException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
|
||||
public class JellyBeanMobileAccessibilityHelper extends
|
||||
@@ -38,26 +33,8 @@ public class JellyBeanMobileAccessibilityHelper extends
|
||||
|
||||
@Override
|
||||
public void initialize(MobileAccessibility mobileAccessibility) {
|
||||
WebView view;
|
||||
super.initialize(mobileAccessibility);
|
||||
|
||||
try {
|
||||
view = (WebView) mobileAccessibility.webView;
|
||||
mParent = view.getParentForAccessibility();
|
||||
} catch(ClassCastException ce) { // cordova-android 4.0+
|
||||
try {
|
||||
Method getView = mobileAccessibility.webView.getClass().getMethod("getView");
|
||||
view = (WebView) getView.invoke(mobileAccessibility.webView);
|
||||
mParent = view.getParentForAccessibility();
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
mParent = mView.getParentForAccessibility();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -66,10 +43,10 @@ public class JellyBeanMobileAccessibilityHelper extends
|
||||
mAccessibilityManager.interrupt();
|
||||
AccessibilityEvent event = AccessibilityEvent.obtain(
|
||||
AccessibilityEvent.TYPE_ANNOUNCEMENT);
|
||||
mWebView.onInitializeAccessibilityEvent(event);
|
||||
mView.onInitializeAccessibilityEvent(event);
|
||||
event.getText().add(text);
|
||||
event.setContentDescription(null);
|
||||
mParent.requestSendAccessibilityEvent(mWebView, event);
|
||||
mParent.requestSendAccessibilityEvent(mView, event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+4
-5
@@ -31,9 +31,9 @@ import android.view.accessibility.CaptioningManager.CaptioningChangeListener;
|
||||
@TargetApi(19)
|
||||
public class KitKatMobileAccessibilityHelper extends
|
||||
JellyBeanMobileAccessibilityHelper {
|
||||
protected CaptioningManager mCaptioningManager;
|
||||
protected CaptioningChangeListener mCaptioningChangeListener;
|
||||
protected TouchExplorationStateChangeListener mTouchExplorationStateChangeListener;
|
||||
private CaptioningManager mCaptioningManager;
|
||||
private CaptioningChangeListener mCaptioningChangeListener;
|
||||
private TouchExplorationStateChangeListener mTouchExplorationStateChangeListener;
|
||||
|
||||
@Override
|
||||
public void initialize(MobileAccessibility mobileAccessibility) {
|
||||
@@ -61,7 +61,6 @@ public class KitKatMobileAccessibilityHelper extends
|
||||
super.addStateChangeListeners();
|
||||
if (mCaptioningChangeListener == null) {
|
||||
mCaptioningChangeListener = new CaptioningChangeListener() {
|
||||
/** @hide */
|
||||
@Override
|
||||
public void onEnabledChanged(boolean enabled) {
|
||||
onCaptioningEnabledChanged(enabled);
|
||||
@@ -89,7 +88,7 @@ public class KitKatMobileAccessibilityHelper extends
|
||||
}
|
||||
}
|
||||
|
||||
protected class InternalTouchExplorationStateChangeListener
|
||||
private class InternalTouchExplorationStateChangeListener
|
||||
implements TouchExplorationStateChangeListener {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -41,13 +41,13 @@ import java.lang.reflect.Method;
|
||||
* This class provides information on the status of native accessibility services to JavaScript.
|
||||
*/
|
||||
public class MobileAccessibility extends CordovaPlugin {
|
||||
protected AbstractMobileAccessibilityHelper mMobileAccessibilityHelper;
|
||||
protected CallbackContext mCallbackContext = null;
|
||||
protected boolean mIsScreenReaderRunning = false;
|
||||
protected boolean mClosedCaptioningEnabled = false;
|
||||
protected boolean mTouchExplorationEnabled = false;
|
||||
protected boolean mCachedIsScreenReaderRunning = false;
|
||||
protected float mFontScale = 1;
|
||||
private AbstractMobileAccessibilityHelper mMobileAccessibilityHelper;
|
||||
private CallbackContext mCallbackContext = null;
|
||||
private boolean mIsScreenReaderRunning = false;
|
||||
private boolean mClosedCaptioningEnabled = false;
|
||||
private boolean mTouchExplorationEnabled = false;
|
||||
private boolean mCachedIsScreenReaderRunning = false;
|
||||
private float mFontScale = 1;
|
||||
|
||||
@Override
|
||||
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
|
||||
@@ -144,8 +144,8 @@ public class MobileAccessibility extends CordovaPlugin {
|
||||
} catch(ClassCastException ce) { // cordova-android 4.0+
|
||||
try { // cordova-android 4.0+
|
||||
Method getView = webView.getClass().getMethod("getView");
|
||||
view = (WebView) getView.invoke(webView);
|
||||
view.reload();
|
||||
Method reload = getView.invoke(webView).getClass().getMethod("reload");
|
||||
reload.invoke(webView);
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
@@ -166,14 +166,13 @@ public class MobileAccessibility extends CordovaPlugin {
|
||||
stop();
|
||||
}
|
||||
|
||||
protected boolean isScreenReaderRunning(final CallbackContext callbackContext) {
|
||||
private void isScreenReaderRunning(final CallbackContext callbackContext) {
|
||||
mIsScreenReaderRunning = mMobileAccessibilityHelper.isScreenReaderRunning();
|
||||
cordova.getThreadPool().execute(new Runnable() {
|
||||
public void run() {
|
||||
callbackContext.success(mIsScreenReaderRunning ? 1 : 0);
|
||||
}
|
||||
});
|
||||
return mIsScreenReaderRunning;
|
||||
}
|
||||
|
||||
protected boolean isScreenReaderRunning() {
|
||||
@@ -181,14 +180,13 @@ public class MobileAccessibility extends CordovaPlugin {
|
||||
return mIsScreenReaderRunning;
|
||||
}
|
||||
|
||||
protected boolean isClosedCaptioningEnabled(final CallbackContext callbackContext) {
|
||||
private void isClosedCaptioningEnabled(final CallbackContext callbackContext) {
|
||||
mClosedCaptioningEnabled = mMobileAccessibilityHelper.isClosedCaptioningEnabled();
|
||||
cordova.getThreadPool().execute(new Runnable() {
|
||||
public void run() {
|
||||
callbackContext.success(mClosedCaptioningEnabled ? 1 : 0);
|
||||
}
|
||||
});
|
||||
return mClosedCaptioningEnabled;
|
||||
}
|
||||
|
||||
protected boolean isClosedCaptioningEnabled() {
|
||||
@@ -196,14 +194,13 @@ public class MobileAccessibility extends CordovaPlugin {
|
||||
return mClosedCaptioningEnabled;
|
||||
}
|
||||
|
||||
protected boolean isTouchExplorationEnabled(final CallbackContext callbackContext) {
|
||||
private void isTouchExplorationEnabled(final CallbackContext callbackContext) {
|
||||
mTouchExplorationEnabled= mMobileAccessibilityHelper.isTouchExplorationEnabled();
|
||||
cordova.getThreadPool().execute(new Runnable() {
|
||||
public void run() {
|
||||
callbackContext.success(mTouchExplorationEnabled ? 1 : 0);
|
||||
}
|
||||
});
|
||||
return mTouchExplorationEnabled;
|
||||
}
|
||||
|
||||
protected boolean isTouchExplorationEnabled() {
|
||||
@@ -211,7 +208,7 @@ public class MobileAccessibility extends CordovaPlugin {
|
||||
return mTouchExplorationEnabled;
|
||||
}
|
||||
|
||||
protected void announceForAccessibility(CharSequence text, final CallbackContext callbackContext) {
|
||||
private void announceForAccessibility(CharSequence text, final CallbackContext callbackContext) {
|
||||
mMobileAccessibilityHelper.announceForAccessibility(text);
|
||||
if (callbackContext != null) {
|
||||
JSONObject info = new JSONObject();
|
||||
@@ -252,7 +249,7 @@ public class MobileAccessibility extends CordovaPlugin {
|
||||
});
|
||||
}
|
||||
|
||||
public void getTextZoom(final CallbackContext callbackContext) {
|
||||
private void getTextZoom(final CallbackContext callbackContext) {
|
||||
cordova.getActivity().runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
final double textZoom = mMobileAccessibilityHelper.getTextZoom();
|
||||
@@ -263,13 +260,13 @@ public class MobileAccessibility extends CordovaPlugin {
|
||||
});
|
||||
}
|
||||
|
||||
public void setTextZoom(final double textZoom, final CallbackContext callbackContext) {
|
||||
private void setTextZoom(final double textZoom, final CallbackContext callbackContext) {
|
||||
cordova.getActivity().runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
mMobileAccessibilityHelper.setTextZoom(textZoom);
|
||||
if (callbackContext != null) {
|
||||
callbackContext.success((int) mMobileAccessibilityHelper.getTextZoom());
|
||||
};
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -282,7 +279,7 @@ public class MobileAccessibility extends CordovaPlugin {
|
||||
});
|
||||
}
|
||||
|
||||
public void updateTextZoom(final CallbackContext callbackContext) {
|
||||
private void updateTextZoom(final CallbackContext callbackContext) {
|
||||
float fontScale = cordova.getActivity().getResources().getConfiguration().fontScale;
|
||||
if (fontScale != mFontScale) {
|
||||
mFontScale = fontScale;
|
||||
@@ -291,7 +288,7 @@ public class MobileAccessibility extends CordovaPlugin {
|
||||
setTextZoom(textZoom, callbackContext);
|
||||
}
|
||||
|
||||
protected void sendMobileAccessibilityStatusChangedCallback() {
|
||||
private void sendMobileAccessibilityStatusChangedCallback() {
|
||||
if (this.mCallbackContext != null) {
|
||||
PluginResult result = new PluginResult(PluginResult.Status.OK, getMobileAccessibilityStatus());
|
||||
result.setKeepCallback(true);
|
||||
@@ -300,7 +297,7 @@ public class MobileAccessibility extends CordovaPlugin {
|
||||
}
|
||||
|
||||
/* Get the current mobile accessibility status. */
|
||||
protected JSONObject getMobileAccessibilityStatus() {
|
||||
private JSONObject getMobileAccessibilityStatus() {
|
||||
JSONObject status = new JSONObject();
|
||||
try {
|
||||
status.put("isScreenReaderRunning", mIsScreenReaderRunning);
|
||||
@@ -315,14 +312,14 @@ public class MobileAccessibility extends CordovaPlugin {
|
||||
return status;
|
||||
}
|
||||
|
||||
protected void start(CallbackContext callbackContext) {
|
||||
private void start(CallbackContext callbackContext) {
|
||||
//Log.i("MobileAccessibility", "MobileAccessibility.start");
|
||||
mCallbackContext = callbackContext;
|
||||
mMobileAccessibilityHelper.addStateChangeListeners();
|
||||
sendMobileAccessibilityStatusChangedCallback();
|
||||
}
|
||||
|
||||
protected void stop() {
|
||||
private void stop() {
|
||||
//Log.i("MobileAccessibility", "MobileAccessibility.stop");
|
||||
if (mCallbackContext != null) {
|
||||
sendMobileAccessibilityStatusChangedCallback();
|
||||
|
||||
Reference in New Issue
Block a user