Skip to content

Commit

Permalink
Merge pull request #29 from qluan/master
Browse files Browse the repository at this point in the history
[Fix] 部分4.2.1&4.2.2偶现crash
  • Loading branch information
qluan authored Dec 22, 2016
2 parents d7b9f8c + 55de8d0 commit 41e6074
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Demo 中使用 github 的 raw 文件服务提供一个简单的路由表文件 r
#### gradle

```groovy
compile 'com.douban.rexxar:core:0.1.6'
compile 'com.douban.rexxar:core:0.1.7'
```


Expand Down
2 changes: 1 addition & 1 deletion core/gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PROJ_GROUP=com.douban.rexxar
PROJ_VERSION=0.1.6
PROJ_VERSION=0.1.7
PROJ_NAME=rexxar-android
PROJ_WEBSITEURL=https://github.com/douban/rexxar-android
PROJ_ISSUETRACKERURL=https://github.com/douban/rexxar-android/issues
Expand Down
64 changes: 64 additions & 0 deletions core/src/main/java/com/douban/rexxar/utils/WebViewCompatUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.douban.rexxar.utils;

import android.content.Context;
import android.os.Build;
import android.view.accessibility.AccessibilityManager;
import android.webkit.WebSettings;

import java.lang.reflect.Method;

/**
* Created by luanqian on 16/12/22.
*/

public class WebViewCompatUtils {

/**
* fix bug: NPE in android.webkit.AccessibilityInjector$TextToSpeechWrapper (Android 4.2.1)
* see: https://code.google.com/p/android/issues/detail?id=40944
*
* @param context
* @param webSettings
*/
public static void enableJavaScriptForWebView(Context context, WebSettings webSettings) {
if (null == webSettings) {
return;
}
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
disableAccessibility(context);
}
try {
webSettings.setJavaScriptEnabled(true);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* m:lorss
* 关闭辅助功能,针对4.2.1和4.2.2 崩溃问题
* java.lang.NullPointerException
* at android.webkit.AccessibilityInjector$TextToSpeechWrapper$1.onInit(AccessibilityInjector.java:753)
* ... ...
* at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:321)
*/
private static void disableAccessibility(Context context) {
if (Build.VERSION.SDK_INT == 17/*4.2 (Build.VERSION_CODES.JELLY_BEAN_MR1)*/) {
if (context != null) {
try {
AccessibilityManager am = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
if (!am.isEnabled()) {
//Not need to disable accessibility
return;
}

Method setState = am.getClass().getDeclaredMethod("setState", int.class);
setState.setAccessible(true);
setState.invoke(am, 0);/**{@link AccessibilityManager#STATE_FLAG_ACCESSIBILITY_ENABLED}*/
} catch (Exception ignored) {
ignored.printStackTrace();
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.douban.rexxar.route.RouteManager;
import com.douban.rexxar.utils.LogUtils;
import com.douban.rexxar.utils.Utils;
import com.douban.rexxar.utils.WebViewCompatUtils;

import java.io.IOException;

Expand Down Expand Up @@ -177,6 +178,7 @@ private void setup() {
@SuppressLint("SetJavaScriptEnabled")
protected void setupWebSettings(WebSettings ws) {
ws.setAppCacheEnabled(true);
WebViewCompatUtils.enableJavaScriptForWebView(getContext(), ws);
ws.setJavaScriptEnabled(true);
ws.setGeolocationEnabled(true);
ws.setBuiltInZoomControls(true);
Expand Down

0 comments on commit 41e6074

Please sign in to comment.