/* * Copyright (C) 2007 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package android.webkit; import android.annotation.SystemApi; import android.content.Context; /** * Manages settings state for a WebView. When a WebView is first created, it * obtains a set of default settings. These default settings will be returned * from any getter call. A WebSettings object obtained from * WebView.getSettings() is tied to the life of the WebView. If a WebView has * been destroyed, any method call on WebSettings will throw an * IllegalStateException. */ // This is an abstract base class: concrete WebViewProviders must // create a class derived from this, and return an instance of it in the // WebViewProvider.getWebSettingsProvider() method implementation. public abstract class WebSettings { /** * Enum for controlling the layout of html. *
* The built-in mechanisms are the only currently supported zoom * mechanisms, so it is recommended that this setting is always enabled. * * @param enabled whether the WebView should use its built-in zoom mechanisms */ // This method was intended to select between the built-in zoom mechanisms // and the separate zoom controls. The latter were obtained using // {@link WebView#getZoomControls}, which is now hidden. public abstract void setBuiltInZoomControls(boolean enabled); /** * Gets whether the zoom mechanisms built into WebView are being used. * * @return true if the zoom mechanisms built into WebView are being used * @see #setBuiltInZoomControls */ public abstract boolean getBuiltInZoomControls(); /** * Sets whether the WebView should display on-screen zoom controls when * using the built-in zoom mechanisms. See {@link #setBuiltInZoomControls}. * The default is true. * * @param enabled whether the WebView should display on-screen zoom controls */ public abstract void setDisplayZoomControls(boolean enabled); /** * Gets whether the WebView displays on-screen zoom controls when using * the built-in zoom mechanisms. * * @return true if the WebView displays on-screen zoom controls when using * the built-in zoom mechanisms * @see #setDisplayZoomControls */ public abstract boolean getDisplayZoomControls(); /** * Enables or disables file access within WebView. File access is enabled by * default. Note that this enables or disables file system access only. * Assets and resources are still accessible using file:///android_asset and * file:///android_res. */ public abstract void setAllowFileAccess(boolean allow); /** * Gets whether this WebView supports file access. * * @see #setAllowFileAccess */ public abstract boolean getAllowFileAccess(); /** * Enables or disables content URL access within WebView. Content URL * access allows WebView to load content from a content provider installed * in the system. The default is enabled. */ public abstract void setAllowContentAccess(boolean allow); /** * Gets whether this WebView supports content URL access. * * @see #setAllowContentAccess */ public abstract boolean getAllowContentAccess(); /** * Sets whether the WebView loads pages in overview mode, that is, * zooms out the content to fit on screen by width. This setting is * taken into account when the content width is greater than the width * of the WebView control, for example, when {@link #getUseWideViewPort} * is enabled. The default is false. */ public abstract void setLoadWithOverviewMode(boolean overview); /** * Gets whether this WebView loads pages in overview mode. * * @return whether this WebView loads pages in overview mode * @see #setLoadWithOverviewMode */ public abstract boolean getLoadWithOverviewMode(); /** * Sets whether the WebView will enable smooth transition while panning or * zooming or while the window hosting the WebView does not have focus. * If it is true, WebView will choose a solution to maximize the performance. * e.g. the WebView's content may not be updated during the transition. * If it is false, WebView will keep its fidelity. The default value is false. * * @deprecated This method is now obsolete, and will become a no-op in future. */ @Deprecated public abstract void setEnableSmoothTransition(boolean enable); /** * Gets whether the WebView enables smooth transition while panning or * zooming. * * @see #setEnableSmoothTransition * * @deprecated This method is now obsolete, and will become a no-op in future. */ @Deprecated public abstract boolean enableSmoothTransition(); /** * Sets whether the WebView uses its background for over scroll background. * If true, it will use the WebView's background. If false, it will use an * internal pattern. Default is true. * * @deprecated This method is now obsolete. * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1} */ @SystemApi @Deprecated public abstract void setUseWebViewBackgroundForOverscrollBackground(boolean view); /** * Gets whether this WebView uses WebView's background instead of * internal pattern for over scroll background. * * @see #setUseWebViewBackgroundForOverscrollBackground * @deprecated This method is now obsolete. * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1} */ @SystemApi @Deprecated public abstract boolean getUseWebViewBackgroundForOverscrollBackground(); /** * Sets whether the WebView should save form data. The default is true. */ public abstract void setSaveFormData(boolean save); /** * Gets whether the WebView saves form data. * * @return whether the WebView saves form data * @see #setSaveFormData */ public abstract boolean getSaveFormData(); /** * Sets whether the WebView should save passwords. The default is true. * @deprecated Saving passwords in WebView will not be supported in future versions. */ @Deprecated public abstract void setSavePassword(boolean save); /** * Gets whether the WebView saves passwords. * * @return whether the WebView saves passwords * @see #setSavePassword * @deprecated Saving passwords in WebView will not be supported in future versions. */ @Deprecated public abstract boolean getSavePassword(); /** * Sets the text zoom of the page in percent. The default is 100. * * @param textZoom the text zoom in percent */ public abstract void setTextZoom(int textZoom); /** * Gets the text zoom of the page in percent. * * @return the text zoom of the page in percent * @see #setTextZoom */ public abstract int getTextZoom(); /** * Sets policy for third party cookies. * Developers should access this via {@link CookieManager#setShouldAcceptThirdPartyCookies}. * @hide Internal API. */ @SystemApi public abstract void setAcceptThirdPartyCookies(boolean accept); /** * Gets policy for third party cookies. * Developers should access this via {@link CookieManager#getShouldAcceptThirdPartyCookies}. * @hide Internal API */ @SystemApi public abstract boolean getAcceptThirdPartyCookies(); /** * Sets the text size of the page. The default is {@link TextSize#NORMAL}. * * @param t the text size as a {@link TextSize} value * @deprecated Use {@link #setTextZoom} instead. */ public synchronized void setTextSize(TextSize t) { setTextZoom(t.value); } /** * Gets the text size of the page. If the text size was previously specified * in percent using {@link #setTextZoom}, this will return the closest * matching {@link TextSize}. * * @return the text size as a {@link TextSize} value * @see #setTextSize * @deprecated Use {@link #getTextZoom} instead. */ public synchronized TextSize getTextSize() { TextSize closestSize = null; int smallestDelta = Integer.MAX_VALUE; int textSize = getTextZoom(); for (TextSize size : TextSize.values()) { int delta = Math.abs(textSize - size.value); if (delta == 0) { return size; } if (delta < smallestDelta) { smallestDelta = delta; closestSize = size; } } return closestSize != null ? closestSize : TextSize.NORMAL; } /** * Sets the default zoom density of the page. This must be called from the UI * thread. The default is {@link ZoomDensity#MEDIUM}. * * This setting is not recommended for use in new applications. If the WebView * is utilized to display mobile-oriented pages, the desired effect can be achieved by * adjusting 'width' and 'initial-scale' attributes of page's 'meta viewport' * tag. For pages lacking the tag, {@link android.webkit.WebView#setInitialScale} * and {@link #setUseWideViewPort} can be used. * * @param zoom the zoom density * @deprecated This method is no longer supported, see the function documentation for * recommended alternatives. */ @Deprecated public abstract void setDefaultZoom(ZoomDensity zoom); /** * Gets the default zoom density of the page. This should be called from * the UI thread. * * This setting is not recommended for use in new applications. * * @return the zoom density * @see #setDefaultZoom * @deprecated Will only return the default value. */ public abstract ZoomDensity getDefaultZoom(); /** * Enables using light touches to make a selection and activate mouseovers. * @deprecated From {@link android.os.Build.VERSION_CODES#JELLY_BEAN} this * setting is obsolete and has no effect. */ @Deprecated public abstract void setLightTouchEnabled(boolean enabled); /** * Gets whether light touches are enabled. * @see #setLightTouchEnabled * @deprecated This setting is obsolete. */ @Deprecated public abstract boolean getLightTouchEnabled(); /** * Controlled a rendering optimization that is no longer present. Setting * it now has no effect. * * @deprecated This setting now has no effect. * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1} */ @Deprecated public void setUseDoubleTree(boolean use) { // Specified to do nothing, so no need for derived classes to override. } /** * Controlled a rendering optimization that is no longer present. Setting * it now has no effect. * * @deprecated This setting now has no effect. * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1} */ @Deprecated public boolean getUseDoubleTree() { // Returns false unconditionally, so no need for derived classes to override. return false; } /** * Sets the user-agent string using an integer code. *
* The default value is true for API level * {@link android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH_MR1} and below, * and false for API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN} * and above. * * @param flag whether JavaScript running in the context of a file scheme * URL should be allowed to access content from any origin */ public abstract void setAllowUniversalAccessFromFileURLs(boolean flag); /** * Sets whether JavaScript running in the context of a file scheme URL * should be allowed to access content from other file scheme URLs. To * enable the most restrictive, and therefore secure policy, this setting * should be disabled. Note that the value of this setting is ignored if * the value of {@link #getAllowUniversalAccessFromFileURLs} is true. * Note too, that this setting affects only JavaScript access to file scheme * resources. Other access to such resources, for example, from image HTML * elements, is unaffected. *
* The default value is true for API level * {@link android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH_MR1} and below, * and false for API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN} * and above. * * @param flag whether JavaScript running in the context of a file scheme * URL should be allowed to access content from other file * scheme URLs */ public abstract void setAllowFileAccessFromFileURLs(boolean flag); /** * Sets whether the WebView should enable plugins. The default is false. * * @param flag true if plugins should be enabled * @deprecated This method has been deprecated in favor of * {@link #setPluginState} * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2} */ @SystemApi @Deprecated public abstract void setPluginsEnabled(boolean flag); /** * Tells the WebView to enable, disable, or have plugins on demand. On * demand mode means that if a plugin exists that can handle the embedded * content, a placeholder icon will be shown instead of the plugin. When * the placeholder is clicked, the plugin will be enabled. The default is * {@link PluginState#OFF}. * * @param state a PluginState value * @deprecated Plugins will not be supported in future, and should not be used. */ @Deprecated public abstract void setPluginState(PluginState state); /** * Sets a custom path to plugins used by the WebView. This method is * obsolete since each plugin is now loaded from its own package. * * @param pluginsPath a String path to the directory containing plugins * @deprecated This method is no longer used as plugins are loaded from * their own APK via the system's package manager. * @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2} */ @Deprecated public void setPluginsPath(String pluginsPath) { // Specified to do nothing, so no need for derived classes to override. } /** * Sets the path to where database storage API databases should be saved. * In order for the database storage API to function correctly, this method * must be called with a path to which the application can write. This * method should only be called once: repeated calls are ignored. * * @param databasePath a path to the directory where databases should be * saved. * @deprecated Database paths are managed by the implementation and calling this method * will have no effect. */ // This will update WebCore when the Sync runs in the C++ side. // Note that the WebCore Database Tracker only allows the path to be set // once. @Deprecated public abstract void setDatabasePath(String databasePath); /** * Sets the path where the Geolocation databases should be saved. In order * for Geolocation permissions and cached positions to be persisted, this * method must be called with a path to which the application can write. * * @param databasePath a path to the directory where databases should be * saved. */ // This will update WebCore when the Sync runs in the C++ side. public abstract void setGeolocationDatabasePath(String databasePath); /** * Sets whether the Application Caches API should be enabled. The default * is false. Note that in order for the Application Caches API to be * enabled, a valid database path must also be supplied to * {@link #setAppCachePath}. * * @param flag true if the WebView should enable Application Caches */ public abstract void setAppCacheEnabled(boolean flag); /** * Sets the path to the Application Caches files. In order for the * Application Caches API to be enabled, this method must be called with a * path to which the application can write. This method should only be * called once: repeated calls are ignored. * * @param appCachePath a String path to the directory containing * Application Caches files. * @see #setAppCacheEnabled */ public abstract void setAppCachePath(String appCachePath); /** * Sets the maximum size for the Application Cache content. The passed size * will be rounded to the nearest value that the database can support, so * this should be viewed as a guide, not a hard limit. Setting the * size to a value less than current database size does not cause the * database to be trimmed. The default size is {@link Long#MAX_VALUE}. * It is recommended to leave the maximum size set to the default value. * * @param appCacheMaxSize the maximum size in bytes * @deprecated In future quota will be managed automatically. */ @Deprecated public abstract void setAppCacheMaxSize(long appCacheMaxSize); /** * Sets whether the database storage API is enabled. The default value is * false. See also {@link #setDatabasePath} for how to correctly set up the * database storage API. * * This setting is global in effect, across all WebView instances in a process. * Note you should only modify this setting prior to making any WebView * page load within a given process, as the WebView implementation may ignore * changes to this setting after that point. * * @param flag true if the WebView should use the database storage API */ public abstract void setDatabaseEnabled(boolean flag); /** * Sets whether the DOM storage API is enabled. The default value is false. * * @param flag true if the WebView should use the DOM storage API */ public abstract void setDomStorageEnabled(boolean flag); /** * Gets whether the DOM Storage APIs are enabled. * * @return true if the DOM Storage APIs are enabled * @see #setDomStorageEnabled */ public abstract boolean getDomStorageEnabled(); /** * Gets the path to where database storage API databases are saved. * * @return the String path to the database storage API databases * @see #setDatabasePath * @deprecated Database paths are managed by the implementation this method is obsolete. */ @Deprecated public abstract String getDatabasePath(); /** * Gets whether the database storage API is enabled. * * @return true if the database storage API is enabled * @see #setDatabaseEnabled */ public abstract boolean getDatabaseEnabled(); /** * Sets whether Geolocation is enabled. The default is true. *
* Please note that in order for the Geolocation API to be usable * by a page in the WebView, the following requirements must be met: *
* As an option, it is possible to store previous locations and web origin
* permissions in a database. See {@link #setGeolocationDatabasePath}.
*
* @param flag whether Geolocation should be enabled
*/
public abstract void setGeolocationEnabled(boolean flag);
/**
* Gets whether JavaScript is enabled.
*
* @return true if JavaScript is enabled
* @see #setJavaScriptEnabled
*/
public abstract boolean getJavaScriptEnabled();
/**
* Gets whether JavaScript running in the context of a file scheme URL can
* access content from any origin. This includes access to content from
* other file scheme URLs.
*
* @return whether JavaScript running in the context of a file scheme URL
* can access content from any origin
* @see #setAllowUniversalAccessFromFileURLs
*/
public abstract boolean getAllowUniversalAccessFromFileURLs();
/**
* Gets whether JavaScript running in the context of a file scheme URL can
* access content from other file scheme URLs.
*
* @return whether JavaScript running in the context of a file scheme URL
* can access content from other file scheme URLs
* @see #setAllowFileAccessFromFileURLs
*/
public abstract boolean getAllowFileAccessFromFileURLs();
/**
* Gets whether plugins are enabled.
*
* @return true if plugins are enabled
* @see #setPluginsEnabled
* @deprecated This method has been replaced by {@link #getPluginState}
* @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}
*/
@SystemApi
@Deprecated
public abstract boolean getPluginsEnabled();
/**
* Gets the current state regarding whether plugins are enabled.
*
* @return the plugin state as a {@link PluginState} value
* @see #setPluginState
* @deprecated Plugins will not be supported in future, and should not be used.
*/
@Deprecated
public abstract PluginState getPluginState();
/**
* Gets the directory that contains the plugin libraries. This method is
* obsolete since each plugin is now loaded from its own package.
*
* @return an empty string
* @deprecated This method is no longer used as plugins are loaded from
* their own APK via the system's package manager.
* @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR2}
*/
@Deprecated
public String getPluginsPath() {
// Unconditionally returns empty string, so no need for derived classes to override.
return "";
}
/**
* Tells JavaScript to open windows automatically. This applies to the
* JavaScript function window.open(). The default is false.
*
* @param flag true if JavaScript can open windows automatically
*/
public abstract void setJavaScriptCanOpenWindowsAutomatically(boolean flag);
/**
* Gets whether JavaScript can open windows automatically.
*
* @return true if JavaScript can open windows automatically during
* window.open()
* @see #setJavaScriptCanOpenWindowsAutomatically
*/
public abstract boolean getJavaScriptCanOpenWindowsAutomatically();
/**
* Sets the default text encoding name to use when decoding html pages.
* The default is "UTF-8".
*
* @param encoding the text encoding name
*/
public abstract void setDefaultTextEncodingName(String encoding);
/**
* Gets the default text encoding name.
*
* @return the default text encoding name as a string
* @see #setDefaultTextEncodingName
*/
public abstract String getDefaultTextEncodingName();
/**
* Sets the WebView's user-agent string. If the string is null or empty,
* the system default value will be used.
*/
public abstract void setUserAgentString(String ua);
/**
* Gets the WebView's user-agent string.
*
* @return the WebView's user-agent string
* @see #setUserAgentString
*/
public abstract String getUserAgentString();
/**
* Returns the default User-Agent used by a WebView.
* An instance of WebView could use a different User-Agent if a call
* is made to {@link WebSettings#setUserAgentString(String)}.
*
* @param context a Context object used to access application assets
*/
public static String getDefaultUserAgent(Context context) {
return WebViewFactory.getProvider().getStatics().getDefaultUserAgent(context);
}
/**
* Tells the WebView whether it needs to set a node to have focus when
* {@link WebView#requestFocus(int, android.graphics.Rect)} is called. The
* default value is true.
*
* @param flag whether the WebView needs to set a node
*/
public abstract void setNeedInitialFocus(boolean flag);
/**
* Sets the priority of the Render thread. Unlike the other settings, this
* one only needs to be called once per process. The default value is
* {@link RenderPriority#NORMAL}.
*
* @param priority the priority
* @deprecated It is not recommended to adjust thread priorities, and this will
* not be supported in future versions.
*/
@Deprecated
public abstract void setRenderPriority(RenderPriority priority);
/**
* Overrides the way the cache is used. The way the cache is used is based
* on the navigation type. For a normal page load, the cache is checked
* and content is re-validated as needed. When navigating back, content is
* not revalidated, instead the content is just retrieved from the cache.
* This method allows the client to override this behavior by specifying
* one of {@link #LOAD_DEFAULT},
* {@link #LOAD_CACHE_ELSE_NETWORK}, {@link #LOAD_NO_CACHE} or
* {@link #LOAD_CACHE_ONLY}. The default value is {@link #LOAD_DEFAULT}.
*
* @param mode the mode to use
*/
public abstract void setCacheMode(int mode);
/**
* Gets the current setting for overriding the cache mode.
*
* @return the current setting for overriding the cache mode
* @see #setCacheMode
*/
public abstract int getCacheMode();
/**
* Configures the WebView's behavior when a secure origin attempts to load a resource from an
* insecure origin.
*
* By default, apps that target {@link android.os.Build.VERSION_CODES#KITKAT} or below default
* to {@link #MIXED_CONTENT_ALWAYS_ALLOW}. Apps targeting
* {@link android.os.Build.VERSION_CODES#LOLLIPOP} default to {@link #MIXED_CONTENT_NEVER_ALLOW}.
*
* The preferred and most secure mode of operation for the WebView is
* {@link #MIXED_CONTENT_NEVER_ALLOW} and use of {@link #MIXED_CONTENT_ALWAYS_ALLOW} is
* strongly discouraged.
*
* @param mode The mixed content mode to use. One of {@link #MIXED_CONTENT_NEVER_ALLOW},
* {@link #MIXED_CONTENT_NEVER_ALLOW} or {@link #MIXED_CONTENT_COMPATIBILITY_MODE}.
*/
public abstract void setMixedContentMode(int mode);
/**
* Gets the current behavior of the WebView with regard to loading insecure content from a
* secure origin.
* @return The current setting, one of {@link #MIXED_CONTENT_NEVER_ALLOW},
* {@link #MIXED_CONTENT_NEVER_ALLOW} or {@link #MIXED_CONTENT_COMPATIBILITY_MODE}.
*/
public abstract int getMixedContentMode();
/**
* Sets whether to use a video overlay for embedded encrypted video.
* In API levels prior to {@link android.os.Build.VERSION_CODES#LOLLIPOP}, encrypted video can
* only be rendered directly on a secure video surface, so it had been a hard problem to play
* encrypted video in HTML. When this flag is on, WebView can play encrypted video (MSE/EME)
* by using a video overlay (aka hole-punching) for videos embedded using HTML <video>
* tag.
* Caution: This setting is intended for use only in a narrow set of circumstances and apps
* should only enable it if they require playback of encrypted video content. It will impose
* the following limitations on the WebView:
*