Skip to content

Commit

Permalink
push setHeader in android webview
Browse files Browse the repository at this point in the history
  • Loading branch information
m1ga committed Jan 24, 2016
1 parent 66d4161 commit 9555725
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 37 deletions.
Expand Up @@ -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";
Expand All @@ -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()
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -319,7 +340,7 @@ public void setPluginState(int pluginState)
}

@Kroll.method
public void pause()
public void pause()
{
if (peekView() != null) {
if (TiApplication.isUIThread()) {
Expand Down Expand Up @@ -364,7 +385,7 @@ public void clearBasicAuthentication()
fusername = null;
fpassword = null;
}

public String getBasicAuthenticationUserName()
{
return fusername;
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -54,6 +55,7 @@ public class TiUIWebView extends TiUIView
private TiWebChromeClient chromeClient;
private boolean bindingCodeInjected = false;
private boolean isLocalHTML = false;
private HashMap<String, String> extraHeaders = new HashMap<String, String>();

private static Enum<?> enumPluginStateOff;
private static Enum<?> enumPluginStateOn;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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);
}

Expand All @@ -132,21 +134,21 @@ 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
{
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) {
Expand All @@ -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) {
Expand All @@ -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);

Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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();
Expand Down Expand Up @@ -388,6 +390,13 @@ private boolean mightBeHtml(String url)
}
}

public void setHeader(HashMap items){
Map<String, String> map = items;
for(Map.Entry<String, String> item : map.entrySet()){
extraHeaders.put(item.getKey().toString(), item.getValue().toString());
}
}

public void setUrl(String url)
{
reloadMethod = reloadTypes.URL;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -504,27 +517,27 @@ public void setHtml(String html, HashMap<String, Object> 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
Expand Down Expand Up @@ -584,22 +597,22 @@ 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) {
setUrl(fullPath);
return;
}
}

if (blob.getMimeType() != null) {
mimeType = blob.getMimeType();
}
Expand Down Expand Up @@ -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<String,Object>)reloadData);
Expand All @@ -746,7 +759,7 @@ public void reload()
getWebView().reload();
}
break;

case URL:
if (reloadData != null && reloadData instanceof String) {
setUrl((String) reloadData);
Expand All @@ -755,7 +768,7 @@ public void reload()
getWebView().reload();
}
break;

default:
getWebView().reload();
}
Expand Down

0 comments on commit 9555725

Please sign in to comment.