-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from qluan/master
[Fix] 部分4.2.1&4.2.2偶现crash
- Loading branch information
Showing
4 changed files
with
68 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
core/src/main/java/com/douban/rexxar/utils/WebViewCompatUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters