Skip to content

Commit

Permalink
Add canGoBack/-Forward() and goBack/-Forward() methods. Refs #4.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonatansberg committed Jan 30, 2015
1 parent 3200ac0 commit 551d6ff
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 10 deletions.
41 changes: 34 additions & 7 deletions android/src/com/universalavenue/ticrosswalk/WebView.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import android.graphics.Color;

import org.xwalk.core.XWalkView;
import org.xwalk.core.XWalkNavigationHistory.Direction;

public class WebView extends TiUIView implements OnLifecycleEvent
{
Expand Down Expand Up @@ -105,17 +106,43 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
super.propertyChanged(key, oldValue, newValue, proxy);
}

public XWalkView getXWalkView()
{
return (XWalkView) getNativeView();
}

public boolean canGoBack()
{
return getXWalkView().getNavigationHistory().canGoBack();
}

public boolean canGoForward()
{
return getXWalkView().getNavigationHistory().canGoForward();
}

public void goBack()
{
getXWalkView().getNavigationHistory().navigate(Direction.BACKWARD, 1);
}

public void goForward()
{
getXWalkView().getNavigationHistory().navigate(Direction.FORWARD, 1);
}


public void setUrl(String url)
{
Log.d(LCAT, "Loading new url: " + url);
XWalkView view = (XWalkView)getNativeView();
XWalkView view = getXWalkView();
view.load(url, null);
}

public void setHtml(String html)
{
Log.d(LCAT, "Loading html string");
XWalkView view = (XWalkView)getNativeView();
XWalkView view = getXWalkView();
view.load(null, html);
}

Expand All @@ -132,7 +159,7 @@ public void onStart(Activity activity) {
@Override
public void onPause(Activity activity) {
Log.i(LCAT, "onPause");
XWalkView view = (XWalkView) getNativeView();
XWalkView view = getXWalkView();
if (view != null) {
Log.i(LCAT, "onPause view found");
view.pauseTimers();
Expand All @@ -143,7 +170,7 @@ public void onPause(Activity activity) {
@Override
public void onResume(Activity activity) {
Log.i(LCAT, "onResume");
XWalkView view = (XWalkView) getNativeView();
XWalkView view = getXWalkView();
if (view != null) {
Log.i(LCAT, "onResume view found");
view.resumeTimers();
Expand All @@ -154,7 +181,7 @@ public void onResume(Activity activity) {
@Override
public void onDestroy(Activity activity) {
Log.i(LCAT, "onDestroy");
XWalkView view = (XWalkView) getNativeView();
XWalkView view = getXWalkView();
if (view != null) {
Log.i(LCAT, "onDestroy view found");
view.onDestroy();
Expand All @@ -167,7 +194,7 @@ public void onStop(Activity activity) {
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
XWalkView view = (XWalkView) getNativeView();
XWalkView view = getXWalkView();
Log.i(LCAT, "onActivityResult");
if (view != null) {
Log.i(LCAT, "onActivityResult view found");
Expand All @@ -177,7 +204,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {

public void onNewIntent(Intent intent) {
Log.i(LCAT, "onNewIntent");
XWalkView view = (XWalkView) getNativeView();
XWalkView view = getXWalkView();
if (view != null) {
Log.i(LCAT, "onNewIntent view found");
view.onNewIntent(intent);
Expand Down
47 changes: 44 additions & 3 deletions android/src/com/universalavenue/ticrosswalk/WebViewProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public TiUIView createView(Activity activity)
return view;
}

public WebView getWebView()
{
return (WebView) peekView();
}

// Handle creation options
@Override
public void handleCreationDict(KrollDict options)
Expand All @@ -73,6 +78,42 @@ public void handleCreationArgs(KrollModule createdInModule, Object[] args)
super.handleCreationArgs(createdInModule, args);
}

@Kroll.method
public boolean canGoBack()
{
WebView view = getWebView();
if (view != null) {
return view.canGoBack();
}
return false;
}

@Kroll.method
public boolean canGoForward()
{
WebView view = getWebView();
if (view != null) {
return view.canGoForward();
}
return false;
}

@Kroll.method
public void goBack()
{
if (canGoBack()) {
getWebView().goBack();
}
}

@Kroll.method
public void goForward()
{
if (canGoForward()) {
getWebView().goForward();
}
}

@Kroll.setProperty @Kroll.method
public void setUrl(String url) {
setPropertyAndFire("url", url);
Expand All @@ -85,7 +126,7 @@ public void setHtml(String html) {

@Kroll.method
public Object evalJS(String code) {
WebView view = (WebView) peekView();
WebView view = getWebView();

if (view == null) {
Log.e(LCAT, "evalJS failed, view not available");
Expand All @@ -98,7 +139,7 @@ public Object evalJS(String code) {

@Kroll.method
public void evalAsync(final String code, @Kroll.argument(optional=true) KrollFunction callback) {
WebView view = (WebView) peekView();
WebView view = getWebView();

if (view == null) {
Log.e(LCAT, "evalAsync failed, view not available");
Expand All @@ -123,7 +164,7 @@ public void onReceiveValue(String s) {
getActivity().runOnUiThread(new Runnable () {
@Override
public void run() {
WebView view = (WebView) peekView();
WebView view = getWebView();
if (view != null) {
XWalkView webView = (XWalkView) view.getNativeView();
if (webView != null) {
Expand Down

0 comments on commit 551d6ff

Please sign in to comment.