Android(安卓)Browser 支持屏蔽webaudio的功能
16lz
2021-12-04
#############################################
本文为极度寒冰原创,转载请注明出处 #############################################在android浏览器中,我们都知道可以设置屏蔽image,即选择image是否加载。 同样的道理,那既然在webkit中提供了webaudio的接口,那我们也可以实现这个功能。 具体实现如下: Package/app/Browser中的修改如下:
diff --git a/res/values/strings.xml b/res/values/strings.xmlindex 41e2fd9..7389f34 100644--- a/res/values/strings.xml+++ b/res/values/strings.xml@@ -338,6 +338,10 @@ <string name="pref_allow_apptabs">Allow multiple tabs per app</string> <!-- Settings label --> <string name="pref_content_load_images">Load images</string>+ <!--chao begin -->+ <string name="pref_content_load_audio">Load audio</string>+ <string name="pref_content_load_audio_summary">Play audio on web pages</string>+ <!--chao --> <!-- Settings label --> <string name="pref_content_load_images_summary">Display images on web pages</string> <!-- Settings label -->diff --git a/res/xml/bandwidth_preferences.xml b/res/xml/bandwidth_preferences.xmlindex 1762b80..ae00881 100644--- a/res/xml/bandwidth_preferences.xml+++ b/res/xml/bandwidth_preferences.xml@@ -37,7 +37,13 @@ android:defaultValue="true" android:title="@string/pref_content_load_images" android:summary="@string/pref_content_load_images_summary" />-+<!-- add by chao-->+ <CheckBoxPreference+ android:key="load_audio"+ android:defaultValue="true"+ android:title="@string/pref_content_load_audio"+ android:summary="@string/pref_content_load_audio_summary" />+<!-- add by chao --> <CheckBoxPreference android:key="video_playback" android:defaultValue="true"diff --git a/src/com/android/browser/BrowserSettings.java b/src/com/android/browser/BrowserSettings.javaindex bf7a519..d426a8d 100644--- a/src/com/android/browser/BrowserSettings.java+++ b/src/com/android/browser/BrowserSettings.java@@ -91,6 +91,7 @@ import java.util.Iterator; import java.util.LinkedList; import java.util.WeakHashMap; +import android.util.Log; /** * Class for managing settings */@@ -330,6 +331,9 @@ public class BrowserSettings implements OnSharedPreferenceChangeListener, settings.setLayoutAlgorithm(getLayoutAlgorithm()); settings.setJavaScriptCanOpenWindowsAutomatically(!blockPopupWindows()); settings.setLoadsImagesAutomatically(loadImages());+ // -- add by chao+ settings.setLoadsAudioAutomatically(loadAudio());+ // add by chao -- settings.setLoadWithOverviewMode(loadPageInOverviewMode()); settings.setSavePassword(rememberPasswords()); settings.setSaveFormData(saveFormdata());@@ -757,9 +761,17 @@ public class BrowserSettings implements OnSharedPreferenceChangeListener, } public boolean loadImages() {+ Log.e("chao","loadImages"); return mPrefs.getBoolean(PREF_LOAD_IMAGES, true); } + // -- add by chao+ public boolean loadAudio()+ {+ Log.d("chao","loadAudio");+ return mPrefs.getBoolean(PREF_LOAD_AUDIO, true);+ }+ // add by chao -- public boolean videoPlayback() { return mPrefs.getBoolean(PREF_VIDEO_PLAYBACK, true); }diff --git a/src/com/android/browser/PreferenceKeys.java b/src/com/android/browser/PreferenceKeys.javaindex 145b805..43ec640 100644--- a/src/com/android/browser/PreferenceKeys.java+++ b/src/com/android/browser/PreferenceKeys.java@@ -109,6 +109,9 @@ public interface PreferenceKeys { static final String PREF_LOAD_IMAGES = "load_images"; static final String PREF_VIDEO_PLAYBACK = "video_playback"; + // -- add by chao+ static final String PREF_LOAD_AUDIO = "load_audio";+ // add by chao -- // ---------------------- // Keys for browser recovery // ----------------------
在framework中的修改如下:
diff --git a/core/java/android/webkit/WebSettings.java b/core/java/android/webkit/WebSettings.javaindex aa68904..da2c61e 100644--- a/core/java/android/webkit/WebSettings.java+++ b/core/java/android/webkit/WebSettings.java@@ -848,6 +848,12 @@ public abstract class WebSettings { throw new MustOverrideException(); } + // -- add by chao + public synchronized void setLoadsAudioAutomatically(boolean flag) {+ throw new MustOverrideException();+ }++ // add by chao /** * Gets whether the WebView loads image resources. This includes * images embedded using the data URI scheme.*/@@ -859,6 +865,12 @@ public abstract class WebSettings { throw new MustOverrideException(); } + // -- add by chao+ public synchronized boolean getLoadsAudioAutomatically() {+ throw new MustOverrideException();+ }+ // add by chao --+ /** * Sets whether the WebView should not load image resources from the * network (resources accessed via http and https URI schemes). Note*/diff --git a/core/java/android/webkit/WebSettingsClassic.java b/core/java/android/webkit/WebSettingsClassic.javaindex 73968a7..0614c5a 100644--- a/core/java/android/webkit/WebSettingsClassic.java+++ b/core/java/android/webkit/WebSettingsClassic.java@@ -62,6 +62,7 @@ import android.os.Message; import android.provider.Settings; import android.provider.Settings.SettingNotFoundException; import android.util.EventLog;+import android.util.Log; import android.os.SystemProperties; import java.util.Locale; @@ -116,6 +117,9 @@ public class WebSettingsClassic extends WebSettings { private int mDefaultFixedFontSize = 13; private int mPageCacheCapacity = 0; private boolean mLoadsImagesAutomatically = true;+ // -- add by chao+ private boolean mLoadsAudioAutomatically = true;+ // add by chao -- private boolean mBlockNetworkImage = false; private boolean mBlockNetworkLoads; private boolean mJavaScriptEnabled = false;@@ -1142,6 +1146,16 @@ public class WebSettingsClassic extends WebSettings { } } + // -- add by chao+ @Override+ public synchronized void setLoadsAudioAutomatically(boolean flag) {+ if (mLoadsAudioAutomatically != flag) {+ Log.d("chao","setLoadsAudioAutomatically flag = " + flag);+ mLoadsAudioAutomatically = flag;+ postSync();+ }+ }+ // add by chao -- /** * @see android.webkit.WebSettings#getLoadsImagesAutomatically() */@@ -1149,6 +1163,13 @@ public class WebSettingsClassic extends WebSettings { public synchronized boolean getLoadsImagesAutomatically() { return mLoadsImagesAutomatically; }+ + // -- add by chao+ @Override+ public synchronized boolean getLoadsAudioAutomatically() {+ return mLoadsAudioAutomatically;+ }+ // add by chao /** * @see android.webkit.WebSettings#setBlockNetworkImage(boolean)
extern中的修改如下:
diff --git a/Source/WebCore/page/Settings.cpp b/Source/WebCore/page/Settings.cppindex 79e38d1..6dfc878 100644--- a/Source/WebCore/page/Settings.cpp+++ b/Source/WebCore/page/Settings.cpp@@ -40,7 +40,7 @@ #include "ResourceHandle.h" #include "StorageMap.h" #include <limits>-+#include "AndroidLog.h" using namespace std; namespace WebCore {@@ -917,6 +917,7 @@ void Settings::setShouldUseHighResolutionTimers(bool shouldUseHighResolutionTime void Settings::setWebAudioEnabled(bool enabled) {+ ALOGE("setWebAudioEnable = %d --chao\n", enabled); m_webAudioEnabled = enabled; } diff --git a/Source/WebKit/android/jni/WebSettings.cpp b/Source/WebKit/android/jni/WebSettings.cppindex fe5ae0b..8348f6b 100644--- a/Source/WebKit/android/jni/WebSettings.cpp+++ b/Source/WebKit/android/jni/WebSettings.cpp@@ -61,6 +61,7 @@ #include <config.h> #include <wtf/Platform.h> +#include "AndroidLog.h" #include "ApplicationCacheStorage.h" #include "BitmapAllocatorAndroid.h" #include "CachedResourceLoader.h"@@ -127,6 +128,9 @@ struct FieldIds { mDefaultFontSize = env->GetFieldID(clazz, "mDefaultFontSize", "I"); mDefaultFixedFontSize = env->GetFieldID(clazz, "mDefaultFixedFontSize", "I"); mLoadsImagesAutomatically = env->GetFieldID(clazz, "mLoadsImagesAutomatically", "Z");+// add by chao+ mLoadsAudioAutomatically = env->GetFieldID(clazz, "mLoadsAudioAutomatically", "Z");+// add by chao end #ifdef ANDROID_BLOCK_NETWORK_IMAGE mBlockNetworkImage = env->GetFieldID(clazz, "mBlockNetworkImage", "Z"); #endif@@ -216,6 +220,9 @@ struct FieldIds { ALOG_ASSERT(mDefaultFontSize, "Could not find field mDefaultFontSize"); ALOG_ASSERT(mDefaultFixedFontSize, "Could not find field mDefaultFixedFontSize"); ALOG_ASSERT(mLoadsImagesAutomatically, "Could not find field mLoadsImagesAutomatically");+// add by chao + ALOG_ASSERT(mLoadsAudioAutomatically, "Could not find field mLoadsAudioAutomatically");+// add by chao -- #ifdef ANDROID_BLOCK_NETWORK_IMAGE ALOG_ASSERT(mBlockNetworkImage, "Could not find field mBlockNetworkImage"); #endif@@ -277,6 +284,9 @@ struct FieldIds { jfieldID mDefaultFontSize; jfieldID mDefaultFixedFontSize; jfieldID mLoadsImagesAutomatically;+// --add by chao+ jfieldID mLoadsAudioAutomatically;+// add by chao -- #ifdef ANDROID_BLOCK_NETWORK_IMAGE jfieldID mBlockNetworkImage; #endif@@ -471,6 +481,11 @@ public: if (flag) cachedResourceLoader->setAutoLoadImages(true); +// -- add by chao+ flag = env->GetBooleanField(obj, gFieldIds->mLoadsAudioAutomatically);+ ALOGE("setWebAudioEnabled = %d --chao \n", flag);+ s->setWebAudioEnabled(flag);+// add by chao -- #ifdef ANDROID_BLOCK_NETWORK_IMAGE flag = env->GetBooleanField(obj, gFieldIds->mBlockNetworkImage); s->setBlockNetworkImage(flag);
在browser的设置中,我们就可以看到并使用这个选项了。
更多相关文章
- Android(安卓)TextView属性大全
- 解决 android如何设置全屏模式
- android实现TextView多行文本滚动
- Android实现全屏显示的方法
- Android(安卓)滚动条属性
- Android中Dialog样式的设置
- Android(安卓)设置颜色的方法总结
- Android(安卓)中两种方法设置android:gravity 和 android:layout
- Android(安卓)如何将一个Activity设置成窗口样式