diff --git a/README.md b/README.md index 4c00fd4..6af9d4b 100644 --- a/README.md +++ b/README.md @@ -60,32 +60,21 @@ dependencies { } ``` -* Register Module (in MainActivity.java) +* Register Module (in MainApplication.java) ```java import cn.tuofeng.rnwebview.RNWebViewPackage; // <--- import -public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler { +public class MainApplication extends Application implements ReactApplication { ...... @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - mReactRootView = new ReactRootView(this); - - mReactInstanceManager = ReactInstanceManager.builder() - .setApplication(getApplication()) - .setBundleAssetName("index.android.bundle") - .setJSMainModuleName("index.android") - .addPackage(new MainReactPackage()) - .addPackage(new RNWebViewPackage(this)) // <------ add this line to yout MainActivity class - .setUseDeveloperSupport(BuildConfig.DEBUG) - .setInitialLifecycleState(LifecycleState.RESUMED) - .build(); - - mReactRootView.startReactApplication(mReactInstanceManager, "AndroidRNSample", null); - - setContentView(mReactRootView); + protected List getPackages() { + return Arrays.asList( + new MainReactPackage(), + ..... + new RNWebViewPackage()// <------ add this line + ); } ...... @@ -95,8 +84,13 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand ## Example ```javascript -var React = require('react-native'); -var { StyleSheet } = React; +import React, { Component } from 'react'; +import { + AppRegistry, + StyleSheet, + Text, + View +} from 'react-native'; var WebViewAndroid = require('react-native-crosswalk-android'); @@ -153,6 +147,7 @@ var styles = StyleSheet.create({ flex: 1, } }); +AppRegistry.registerComponent('your component', () => WebViewAndroidExample); ``` ## Tips for Video (HTML5) inside WebView @@ -162,4 +157,4 @@ To work with some html5 video player inside your Webview, I recommend you to set More info here: http://stackoverflow.com/questions/17259636/enabling-html5-video-playback-in-android-webview ## License -MIT +MIT \ No newline at end of file diff --git a/build.gradle b/build.gradle index 0bd7569..585ec72 100644 --- a/build.gradle +++ b/build.gradle @@ -7,8 +7,8 @@ android { defaultConfig { minSdkVersion 16 targetSdkVersion 22 - versionCode 5 - versionName "1.5" + versionCode 6 + versionName "1.6" } } @@ -23,6 +23,6 @@ repositories { dependencies { provided 'com.android.support:appcompat-v7:23.0.1' - provided 'com.facebook.react:react-native:0.13.+' + provided 'com.facebook.react:react-native:+' compile(name:'xwalk_core_library-17.46.448.10', ext:'aar') } diff --git a/index.js b/index.js index aa2b93b..43fe280 100644 --- a/index.js +++ b/index.js @@ -1,42 +1,68 @@ /** * @providesModule WebViewAndroid */ -'use strict'; + 'use strict'; +try { + var React = require('react'); +} catch(ex) { + var React = require('react-native'); +} -var React = require('react-native'); -var { requireNativeComponent, PropTypes } = React; -var RCTUIManager = React.NativeModules.UIManager; +var RN = require("react-native"); + +var { requireNativeComponent, NativeModules } = require('react-native'); +var RCTUIManager = NativeModules.UIManager; var WEBVIEW_REF = 'androidWebView'; var WebViewAndroid = React.createClass({ propTypes: { - url: PropTypes.string, - baseUrl: PropTypes.string, - html: PropTypes.string, - htmlCharset: PropTypes.string, - userAgent: PropTypes.string, - injectedJavaScript: PropTypes.string, - disablePlugins: PropTypes.bool, - disableCookies: PropTypes.bool, - javaScriptEnabled: PropTypes.bool, - geolocationEnabled: PropTypes.bool, - builtInZoomControls: PropTypes.bool, - onNavigationStateChange: PropTypes.func + url: React.PropTypes.string, + source: React.PropTypes.object, + baseUrl: React.PropTypes.string, + html: React.PropTypes.string, + htmlCharset: React.PropTypes.string, + userAgent: React.PropTypes.string, + injectedJavaScript: React.PropTypes.string, + disablePlugins: React.PropTypes.bool, + disableCookies: React.PropTypes.bool, + javaScriptEnabled: React.PropTypes.bool, + geolocationEnabled: React.PropTypes.bool, + allowUrlRedirect: React.PropTypes.bool, + builtInZoomControls: React.PropTypes.bool, + onNavigationStateChange: React.PropTypes.func }, _onNavigationStateChange: function(event) { + if (this.props.onNavigationStateChange) { + this.props.onNavigationStateChange(event.nativeEvent); + } }, goBack: function() { + RCTUIManager.dispatchViewManagerCommand( + this._getWebViewHandle(), + RCTUIManager.RNWebViewAndroid.Commands.goBack, + null + ); }, goForward: function() { + RCTUIManager.dispatchViewManagerCommand( + this._getWebViewHandle(), + RCTUIManager.RNWebViewAndroid.Commands.goForward, + null + ); }, reload: function() { + RCTUIManager.dispatchViewManagerCommand( + this._getWebViewHandle(), + RCTUIManager.RNWebViewAndroid.Commands.reload, + null + ); }, render: function() { return ; }, _getWebViewHandle: function() { - return React.findNodeHandle(this.refs[WEBVIEW_REF]); + return RN.findNodeHandle(this.refs[WEBVIEW_REF]); }, }); diff --git a/package.json b/package.json index ba53300..2aa7333 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-crosswalk-android", - "version": "0.0.5", + "version": "0.0.6", "description": "Simple React Native Android module to use Crosswalk's WebView inside your app", "main": "index.js", "scripts": { diff --git a/src/main/java/cn/tuofeng/rnwebview/RNWebViewManager.java b/src/main/java/cn/tuofeng/rnwebview/RNWebViewManager.java index da20b8e..7fd6f4b 100644 --- a/src/main/java/cn/tuofeng/rnwebview/RNWebViewManager.java +++ b/src/main/java/cn/tuofeng/rnwebview/RNWebViewManager.java @@ -2,13 +2,10 @@ import android.app.Activity; import javax.annotation.Nullable; -import android.util.AttributeSet; - -import org.xwalk.core.XWalkView; - +import com.facebook.react.uimanager.annotations.ReactProp; +import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.uimanager.SimpleViewManager; import com.facebook.react.uimanager.ThemedReactContext; -import com.facebook.react.uimanager.ReactProp; import com.facebook.react.common.annotations.VisibleForTesting; public class RNWebViewManager extends SimpleViewManager { @@ -19,11 +16,10 @@ public class RNWebViewManager extends SimpleViewManager { public static final int GO_FORWARD = 2; public static final int RELOAD = 3; - Activity mActivity; + ReactApplicationContext rContext; - public RNWebViewManager(Activity activity) { - super(); - mActivity = activity; + public RNWebViewManager(ReactApplicationContext reactApplicationContext) { + rContext = reactApplicationContext; } @VisibleForTesting @@ -36,6 +32,7 @@ public String getName() { @Override public RNWebView createViewInstance(ThemedReactContext context) { + Activity mActivity = rContext.getCurrentActivity(); return new RNWebView(context, mActivity); //xWalkWebView.setX(0); //xWalkWebView.setY(0); diff --git a/src/main/java/cn/tuofeng/rnwebview/RNWebViewPackage.java b/src/main/java/cn/tuofeng/rnwebview/RNWebViewPackage.java index be19fd9..dcdfa34 100644 --- a/src/main/java/cn/tuofeng/rnwebview/RNWebViewPackage.java +++ b/src/main/java/cn/tuofeng/rnwebview/RNWebViewPackage.java @@ -1,7 +1,5 @@ package cn.tuofeng.rnwebview; -import android.app.Activity; - import com.facebook.react.ReactPackage; import com.facebook.react.bridge.JavaScriptModule; import com.facebook.react.bridge.NativeModule; @@ -12,12 +10,6 @@ public class RNWebViewPackage implements ReactPackage { - Activity mActivity; - - public RNWebViewPackage(Activity activity) { - mActivity = activity; - } - @Override public List createNativeModules(ReactApplicationContext reactContext) { return Collections.emptyList(); @@ -30,6 +22,6 @@ public List> createJSModules() { @Override public List createViewManagers(ReactApplicationContext reactContext) { - return Arrays.asList(new RNWebViewManager(mActivity)); + return Arrays.asList(new RNWebViewManager(reactContext)); } }