diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/WebViewProxy.java b/android/modules/ui/src/java/ti/modules/titanium/ui/WebViewProxy.java index 4407523e6d4..57a7159eb16 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/WebViewProxy.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/WebViewProxy.java @@ -38,7 +38,7 @@ TiC.PROPERTY_CACHE_MODE, TiC.PROPERTY_LIGHT_TOUCH_ENABLED }) -public class WebViewProxy extends ViewProxy +public class WebViewProxy extends ViewProxy implements Handler.Callback, OnLifecycleEvent, interceptOnBackPressedEvent { private static final String TAG = "WebViewProxy"; @@ -56,13 +56,14 @@ public class WebViewProxy extends ViewProxy private static final int MSG_RELEASE = MSG_FIRST_ID + 110; private static final int MSG_PAUSE = MSG_FIRST_ID + 111; private static final int MSG_RESUME = MSG_FIRST_ID + 112; + private static final int MSG_SET_HEADER = MSG_FIRST_ID + 113; protected static final int MSG_LAST_ID = MSG_FIRST_ID + 999; private static String fusername; private static String fpassword; private Message postCreateMessage; - + public static final String OPTIONS_IN_SETHTML = "optionsInSetHtml"; public WebViewProxy() @@ -158,6 +159,9 @@ public boolean handleMessage(Message msg) case MSG_STOP_LOADING: getWebView().stopLoading(); return true; + case MSG_SET_HEADER: + getWebView().setHeader((HashMap)msg.obj); + return true; case MSG_SET_USER_AGENT: getWebView().setUserAgentString(msg.obj.toString()); return true; @@ -213,6 +217,23 @@ public void setBasicAuthentication(String username, String password) } + @Kroll.method + public void setHeader(HashMap d) + { + TiUIWebView currWebView = getWebView(); + if (currWebView != null) { + if (TiApplication.isUIThread()) { + currWebView.setHeader(d); + } else { + // + Message message = getMainHandler().obtainMessage(MSG_SET_HEADER); + message.obj = d; + message.sendToTarget(); + } + } + } + + @Kroll.method @Kroll.setProperty public void setUserAgent(String userAgent) { @@ -319,7 +340,7 @@ public void setPluginState(int pluginState) } @Kroll.method - public void pause() + public void pause() { if (peekView() != null) { if (TiApplication.isUIThread()) { @@ -364,7 +385,7 @@ public void clearBasicAuthentication() fusername = null; fpassword = null; } - + public String getBasicAuthenticationUserName() { return fusername; diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiUIWebView.java b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiUIWebView.java index 61ed480db84..fa68681ba27 100644 --- a/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiUIWebView.java +++ b/android/modules/ui/src/java/ti/modules/titanium/ui/widget/webview/TiUIWebView.java @@ -15,6 +15,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; +import java.util.Map; import org.appcelerator.kroll.KrollDict; import org.appcelerator.kroll.KrollProxy; @@ -54,6 +55,7 @@ public class TiUIWebView extends TiUIView private TiWebChromeClient chromeClient; private boolean bindingCodeInjected = false; private boolean isLocalHTML = false; + private HashMap extraHeaders = new HashMap(); private static Enum enumPluginStateOff; private static Enum enumPluginStateOn; @@ -69,10 +71,10 @@ public class TiUIWebView extends TiUIView private static enum reloadTypes { DEFAULT, DATA, HTML, URL } - + private reloadTypes reloadMethod = reloadTypes.DEFAULT; private Object reloadData = null; - + private class TiWebView extends WebView { public TiWebViewClient client; @@ -94,7 +96,7 @@ public void destroy() @Override public boolean onTouchEvent(MotionEvent ev) { - + boolean handled = false; // In Android WebView, all the click events are directly sent to WebKit. As a result, OnClickListener() is @@ -119,9 +121,9 @@ public boolean onTouchEvent(MotionEvent ev) } // Don't return here -- must call super.onTouchEvent() - + boolean superHandled = super.onTouchEvent(ev); - + return (superHandled || handled || swipeHandled); } @@ -132,7 +134,7 @@ protected void onLayout(boolean changed, int left, int top, int right, int botto TiUIHelper.firePostLayoutEvent(proxy); } } - + //TIMOB-16952. Overriding onCheckIsTextEditor crashes HTC Sense devices private class NonHTCWebView extends TiWebView { @@ -140,13 +142,13 @@ public NonHTCWebView(Context context) { super(context); } - + @Override public boolean onCheckIsTextEditor() { if (proxy.hasProperty(TiC.PROPERTY_SOFT_KEYBOARD_ON_FOCUS)) { int value = TiConvert.toInt(proxy.getProperty(TiC.PROPERTY_SOFT_KEYBOARD_ON_FOCUS), TiUIView.SOFT_KEYBOARD_DEFAULT_ON_FOCUS); - + if (value == TiUIView.SOFT_KEYBOARD_HIDE_ON_FOCUS) { return false; } else if (value == TiUIView.SOFT_KEYBOARD_SHOW_ON_FOCUS) { @@ -156,13 +158,13 @@ public boolean onCheckIsTextEditor() return super.onCheckIsTextEditor(); } } - + private boolean isHTCSenseDevice() { boolean isHTC = false; - + FeatureInfo[] features = TiApplication.getInstance().getApplicationContext().getPackageManager().getSystemAvailableFeatures(); - if(features == null) { + if(features == null) { return isHTC; } for (FeatureInfo f : features) { @@ -175,15 +177,15 @@ private boolean isHTCSenseDevice() } } } - + return isHTC; } - + public TiUIWebView(TiViewProxy proxy) { super(proxy); - + TiWebView webView = isHTCSenseDevice() ? new TiWebView(proxy.getActivity()) : new NonHTCWebView(proxy.getActivity()); webView.setVerticalScrollbarOverlay(true); @@ -199,7 +201,7 @@ public TiUIWebView(TiViewProxy proxy) settings.setDatabasePath(path.getAbsolutePath()); settings.setDatabaseEnabled(true); } - + File cacheDir = TiApplication.getInstance().getCacheDir(); if (cacheDir != null) { settings.setAppCacheEnabled(true); @@ -300,7 +302,7 @@ public void processProperties(KrollDict d) WebSettings settings = getWebView().getSettings(); settings.setLoadWithOverviewMode(TiConvert.toBoolean(d, TiC.PROPERTY_SCALES_PAGE_TO_FIT)); } - + if (d.containsKey(TiC.PROPERTY_CACHE_MODE)) { int mode = TiConvert.toInt(d.get(TiC.PROPERTY_CACHE_MODE), AndroidModule.WEBVIEW_LOAD_DEFAULT); getWebView().getSettings().setCacheMode(mode); @@ -316,7 +318,7 @@ public void processProperties(KrollDict d) setData((TiBlob) value); } } - + if (d.containsKey(TiC.PROPERTY_LIGHT_TOUCH_ENABLED)) { WebSettings settings = getWebView().getSettings(); settings.setLightTouchEnabled(TiConvert.toBoolean(d,TiC.PROPERTY_LIGHT_TOUCH_ENABLED)); @@ -332,7 +334,7 @@ public void processProperties(KrollDict d) if (d.containsKey(TiC.PROPERTY_PLUGIN_STATE)) { setPluginState(TiConvert.toInt(d, TiC.PROPERTY_PLUGIN_STATE)); } - + if (d.containsKey(TiC.PROPERTY_OVER_SCROLL_MODE)) { if (Build.VERSION.SDK_INT >= 9) { nativeView.setOverScrollMode(TiConvert.toInt(d.get(TiC.PROPERTY_OVER_SCROLL_MODE), View.OVER_SCROLL_ALWAYS)); @@ -358,7 +360,7 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP if (Build.VERSION.SDK_INT >= 9) { nativeView.setOverScrollMode(TiConvert.toInt(newValue, View.OVER_SCROLL_ALWAYS)); } - } else if (TiC.PROPERTY_CACHE_MODE.equals(key)) { + } else if (TiC.PROPERTY_CACHE_MODE.equals(key)) { getWebView().getSettings().setCacheMode(TiConvert.toInt(newValue)); } else if (TiC.PROPERTY_LIGHT_TOUCH_ENABLED.equals(key)) { WebSettings settings = getWebView().getSettings(); @@ -388,6 +390,13 @@ private boolean mightBeHtml(String url) } } + public void setHeader(HashMap items){ + Map map = items; + for(Map.Entry item : map.entrySet()){ + extraHeaders.put(item.getKey().toString(), item.getValue().toString()); + } + } + public void setUrl(String url) { reloadMethod = reloadTypes.URL; @@ -459,7 +468,11 @@ public void setUrl(String url) getWebView().getSettings().setLoadWithOverviewMode(true); } isLocalHTML = false; - getWebView().loadUrl(finalUrl); + if (extraHeaders.size()>0){ + getWebView().loadUrl(finalUrl, extraHeaders); + } else { + getWebView().loadUrl(finalUrl); + } } public void changeProxyUrl(String url) @@ -504,27 +517,27 @@ public void setHtml(String html, HashMap d) setHtml(html); return; } - + reloadMethod = reloadTypes.HTML; reloadData = d; String baseUrl = TiC.URL_ANDROID_ASSET_RESOURCES; String mimeType = "text/html"; if (d.containsKey(TiC.PROPERTY_BASE_URL_WEBVIEW)) { baseUrl = TiConvert.toString(d.get(TiC.PROPERTY_BASE_URL_WEBVIEW)); - } + } if (d.containsKey(TiC.PROPERTY_MIMETYPE)) { mimeType = TiConvert.toString(d.get(TiC.PROPERTY_MIMETYPE)); } - + setHtmlInternal(html, baseUrl, mimeType); } /** - * Loads HTML content into the web view. Note that the "historyUrl" property - * must be set to non null in order for the web view history to work correctly - * when working with local files (IE: goBack() and goForward() will not work if + * Loads HTML content into the web view. Note that the "historyUrl" property + * must be set to non null in order for the web view history to work correctly + * when working with local files (IE: goBack() and goForward() will not work if * null is used) - * + * * @param html HTML data to load into the web view * @param baseUrl url to associate with the data being loaded * @param mimeType mime type of the data being loaded @@ -584,14 +597,14 @@ public void setData(TiBlob blob) reloadMethod = reloadTypes.DATA; reloadData = blob; String mimeType = "text/html"; - + // iOS parity: for whatever reason, in setData, the iOS implementation // explicitly sets the native webview's setScalesPageToFit to YES if the // Ti scalesPageToFit property has _not_ been set. if (!proxy.hasProperty(TiC.PROPERTY_SCALES_PAGE_TO_FIT)) { getWebView().getSettings().setLoadWithOverviewMode(true); } - + if (blob.getType() == TiBlob.TYPE_FILE) { String fullPath = blob.getNativePath(); if (fullPath != null) { @@ -599,7 +612,7 @@ public void setData(TiBlob blob) return; } } - + if (blob.getMimeType() != null) { mimeType = blob.getMimeType(); } @@ -737,7 +750,7 @@ public void reload() getWebView().reload(); } break; - + case HTML: if (reloadData == null || (reloadData instanceof HashMap) ) { setHtml(TiConvert.toString(getProxy().getProperty(TiC.PROPERTY_HTML)), (HashMap)reloadData); @@ -746,7 +759,7 @@ public void reload() getWebView().reload(); } break; - + case URL: if (reloadData != null && reloadData instanceof String) { setUrl((String) reloadData); @@ -755,7 +768,7 @@ public void reload() getWebView().reload(); } break; - + default: getWebView().reload(); }