/** --| ADAPTIVE RUNTIME PLATFORM |---------------------------------------------------------------------------------------- (C) Copyright 2013-2015 Carlos Lozano Diez t/a Adaptive.me <http://adaptive.me>. 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 appli- -cable 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. Original author: * Carlos Lozano Diez <http://github.com/carloslozano> <http://twitter.com/adaptivecoder> <mailto:carlos@adaptive.me> Contributors: * Ferran Vila Conesa <http://github.com/fnva> <http://twitter.com/ferran_vila> <mailto:ferran.vila.conesa@gmail.com> * See source code files for contributors. Release: * @version v2.0.3 -------------------------------------------| aut inveniam viam aut faciam |-------------------------------------------- */ package me.adaptive.arp.impl; import android.webkit.JavascriptInterface; import android.webkit.WebView; import java.util.ArrayList; import java.util.List; import me.adaptive.arp.api.AppRegistryBridge; import me.adaptive.arp.api.IAppContextWebview; import me.adaptive.arp.api.ILogging; import me.adaptive.arp.api.ILoggingLogLevel; /** * Interface for webview context management purposes * Auto-generated implementation of IAppContextWebview specification. */ public class AppContextWebviewDelegate implements IAppContextWebview { // logger private static final String LOG_TAG = "MainActivity"; private ILogging logger; private WebView primaryView; private List<Object> views; private String userAgent; /** * Default Constructor. */ public AppContextWebviewDelegate() { super(); logger = AppRegistryBridge.getInstance().getLoggingBridge(); views = new ArrayList<>(); } /** * Additional views may be added to an application - a separate activity - and if these will make calls to the * ARP methods, they must be registered by adding them to the context. When they are added to the context, ARP * methods are bound to the webview so that they're callable from the HTML application. The primary webview should * not be added using this method. * * @param webView Platform specific webview reference (WebView, UIWebView, WKWebView,etc.) * @since ARP1.0 */ public void addWebview(Object webView) { views.add(webView); ((WebView) webView).addJavascriptInterface(this, "quirksMode"); } /** * Evaluate the specified javascript on the main webview of the application. * * @param javaScriptText The javascript expression to execute on the webview. */ public void executeJavaScript(String javaScriptText) { final String js = javaScriptText; primaryView.post(new Runnable() { @Override public void run() { primaryView.evaluateJavascript(js, null); } }); } /** * Evaluate the specified javascript on the specified webview of the application. * * @param javaScriptText The javascript expression to execute on the webview. * @param webViewReference The target webview on which to execute the expression. */ public void executeJavaScript(String javaScriptText, Object webViewReference) { final String js = javaScriptText; final WebView wv = (WebView) webViewReference; wv.post(new Runnable() { @Override public void run() { wv.evaluateJavascript(js, null); } }); } /** * Returns a reference to the main application webview. This is the first application webview and can not be removed * with the removeWebview method. The object returned should be cast to the platform specific implementation * WebView, WKWebView, etc. * * @return Object representing the specific and primary webview instance of the application. * @since ARP1.0 */ public Object getWebviewPrimary() { return this.primaryView; } /** * Returns an array of webviews currently managed by the context - composed of primary and the list of those added. * This method will always return at least one element; the primary webview. * * @return Array with all the Webview instances being managed by ARP. * @since ARP1.0 */ public Object[] getWebviews() { List<Object> ret = views; // Add the primary webview ret.add(primaryView); return ret.toArray(); } /** * When a webview is disposed - no longer in use from an external activity - the webview should be removed to unbind * ARP functions and release resources. The primary webview can not be removed. * * @param webView The instance of the webview to be removed from the binding. * @since ARP1.0 */ public void removeWebview(Object webView) { if (views.contains(webView)) { views.remove(webView); } else { logger.log(ILoggingLogLevel.Error, LOG_TAG, "The webView you're trying to remove is not on the list"); } } /** * Getter for the primary webView * * @return The primary webView */ public WebView getPrimaryView() { return primaryView; } /** * Setter for the primary webView * * @param primaryView The primary webView */ public void setPrimaryView(WebView primaryView) { this.primaryView = primaryView; this.primaryView.addJavascriptInterface(this, "quirksMode"); } public void setUserAgent(String userAgent) { this.userAgent = userAgent; } public String getUserAgent(){ return userAgent; } /** * Binds quirks to the webview to customize request processing to the extent Android allows. * * @return true always, if bound to the DOM. */ @JavascriptInterface public boolean isQuirkEnabled() { return true; } } /** ------------------------------------| Engineered with ♥ in Barcelona, Catalonia |-------------------------------------- */