package com.rhomobile.rhodes.webview;

import java.lang.reflect.Method;

import android.annotation.SuppressLint;
import android.webkit.WebSettings;

import com.rhomobile.rhodes.Logger;
import com.rhomobile.rhodes.extmanager.IRhoConfig;
import com.rhomobile.rhodes.RhoConf;

public class WebSettingsProviderBase implements IWebSettingsProvider {
    protected static final String TAG = "WebSettingsProvider";
    
    @SuppressLint("SetJavaScriptEnabled")
    public void fillSettings(WebSettings settings, IRhoConfig config) {
        
        boolean enableZoom = config == null || config.getBool(WebViewConfig.ENABLE_ZOOM);
        boolean enableCache = config == null || config.getBool(WebViewConfig.ENABLE_CACHE);
        
        settings.setSavePassword(false);
        settings.setSaveFormData(false);
        settings.setJavaScriptEnabled(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(false);
        settings.setSupportZoom(enableZoom);
        settings.setBuiltInZoomControls(enableZoom);
        settings.setStandardFontFamily(RhoConf.getString("fontFamily"));
        
        Logger.T(TAG, "Enable Zoom: " + enableZoom);
        
        if (enableCache) {
            settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        } else {
            settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        }
        Logger.T(TAG, "Enable Cache: " + enableCache);

        settings.setSupportMultipleWindows(false);

        setWebPlugins(settings, config);
    }
    
    protected void setWebPlugins(WebSettings settings, IRhoConfig config) {
        
        try {
            Class<WebSettings> clazz = WebSettings.class;
            Method methodSetPlugins = clazz.getMethod("setPluginsEnabled", boolean.class);
            methodSetPlugins.invoke(settings, config != null && config.getBool(WebViewConfig.ENABLE_WEB_PLUGINS));
        } catch (Throwable e) {
            Logger.W(TAG, "WebSettings.setPluginsEnabled(bool) is removed from API by Google.");
        }
    }
}

@@ -64,16 +64,41 @@
 .Method;
+%0Aimport java.util.Locale;
 %0A%0Aimport
@@ -131,16 +131,41 @@
 ssLint;%0A
+import android.os.Build;%0A
 import a
@@ -753,32 +753,102 @@
 .ENABLE_CACHE);%0A
+        String customUA = config.getString(WebViewConfig.USER_AGENT);%0A
         %0A       
@@ -1200,24 +1200,157 @@
 tFamily%22));%0A
+        customUA = updateRevesionOfCustomUA(settings.getUserAgentString(), customUA);%0A        settings.setUserAgentString(customUA);%0A
         %0A   
@@ -2237,16 +2237,16 @@
 gle.%22);%0A
-
         
@@ -2253,10 +2253,1294 @@
 %7D%0A    %7D%0A
+    %0A    private String updateRevesionOfCustomUA(String defaultUA, String customUA) %7B%0A    %09String deviceInfo = android.os.Build.BRAND + %22,%22%0A%09%09%09%09+ android.os.Build.VERSION.RELEASE + %22; %22%0A%09%09%09%09+ Locale.getDefault().toString() + %22; %22 + android.os.Build.MODEL;%0A%09%09int index = Build.FINGERPRINT.indexOf(android.os.Build.VERSION.RELEASE);%0A%09%09String%5B%5D buildInfo = Build.FINGERPRINT.substring(index).split(%22/%22);%0A%09%09deviceInfo = deviceInfo + %22 Build/%22 + buildInfo%5B1%5D;%0A%09%09customUA = customUA.replace(%22U; /%25p%22, %22U; %22 + deviceInfo);%0A%09%09%0A%09%09%0A%09%09int start = defaultUA.indexOf(%22AppleWebKit/%22) + 12;%0A%09%09String temp = defaultUA.substring(start, defaultUA.length());%0A%09%09customUA = customUA.replace(%0A%09%09%09%09%22AppleWebKit/%25w%22,%0A%09%09%09%09%22AppleWebKit/%22%0A%09%09%09%09%09%09+ defaultUA.substring(start, start + temp.indexOf(%22(%22) - 1));%0A%0A%09%09int start1 = defaultUA.indexOf(%22Version/%22) + 8;%0A%09%09String temp1 = defaultUA.substring(start1, defaultUA.length());%0A%09%09customUA = customUA%0A%09%09%09%09.replace(%0A%09%09%09%09%09%09%22Version/%25e%22,%0A%09%09%09%09%09%09%22Version/%22%0A%09%09%09%09%09%09%09%09+ defaultUA.substring(start1,%0A%09%09%09%09%09%09%09%09%09%09start1 + temp1.indexOf(%22 %22)));%0A%0A%09%09int start2 = defaultUA.indexOf(%22Safari/%22) + 7;%0A%09%09defaultUA.substring(start2, defaultUA.length());%0A%09%09customUA = customUA.replace(%22Safari/%25w%22,%0A%09%09%09%09%22Safari/%22 + defaultUA.substring(start2, defaultUA.length()));%0A%09%09return customUA;%0A    %7D%0A
 %7D%0A

Fix for EMBPD00174164.