Browse Source

refactor(Android): New project setup + kotlin events (#121)

Thibault Malbranche 5 years ago
parent
commit
fd736cf26b
No account linked to committer's email address

+ 4
- 0
.gitignore View File

48
 !.vscode/tasks.json
48
 !.vscode/tasks.json
49
 !.vscode/launch.json
49
 !.vscode/launch.json
50
 !.vscode/extensions.json
50
 !.vscode/extensions.json
51
+
52
+android/gradle
53
+android/gradlew
54
+android/gradlew.bat

+ 78
- 9
android/build.gradle View File

1
+buildscript {
2
+    ext.kotlin_version = '1.2.71'
3
+    repositories {
4
+        google()
5
+        jcenter()
6
+        maven {
7
+            url 'https://maven.fabric.io/public'
8
+        }
9
+    }
10
+    dependencies {
11
+        classpath 'com.android.tools.build:gradle:3.2.1'
12
+        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
13
+    }
14
+}
15
+
1
 apply plugin: 'com.android.library'
16
 apply plugin: 'com.android.library'
17
+apply plugin: 'kotlin-android'
2
 
18
 
3
-def DEFAULT_COMPILE_SDK_VERSION             = 27
4
-def DEFAULT_BUILD_TOOLS_VERSION             = "27.0.3"
5
-def DEFAULT_MIN_SDK_VERSION                 = 16
6
-def DEFAULT_TARGET_SDK_VERSION              = 26
7
 
19
 
8
-android {
9
-    compileSdkVersion rootProject.findProperty('compileSdkVersion') ?: DEFAULT_COMPILE_SDK_VERSION
10
-    buildToolsVersion rootProject.findProperty('buildToolsVersion') ?: DEFAULT_BUILD_TOOLS_VERSION
20
+def DEFAULT_COMPILE_SDK_VERSION = 27
21
+def DEFAULT_BUILD_TOOLS_VERSION = "28.0.3"
22
+def DEFAULT_TARGET_SDK_VERSION = 27
11
 
23
 
24
+android {
25
+    compileSdkVersion rootProject.hasProperty('compileSdkVersion') ? rootProject.compileSdkVersion : DEFAULT_COMPILE_SDK_VERSION
26
+    buildToolsVersion rootProject.hasProperty('buildToolsVersion') ? rootProject.buildToolsVersion : DEFAULT_BUILD_TOOLS_VERSION
12
     defaultConfig {
27
     defaultConfig {
13
-        minSdkVersion rootProject.findProperty('minSdkVersion') ?: DEFAULT_MIN_SDK_VERSION
14
-        targetSdkVersion rootProject.findProperty('targetSdkVersion') ?: DEFAULT_TARGET_SDK_VERSION
28
+        minSdkVersion 16
29
+        targetSdkVersion rootProject.hasProperty('targetSdkVersion') ? rootProject.targetSdkVersion : DEFAULT_TARGET_SDK_VERSION
15
         versionCode 1
30
         versionCode 1
16
         versionName "1.0"
31
         versionName "1.0"
17
     }
32
     }
33
+    buildTypes {
34
+        release {
35
+            minifyEnabled false
36
+        }
37
+    }
38
+    productFlavors {
39
+    }
40
+    lintOptions {
41
+        disable 'GradleCompatible'
42
+    }
43
+    compileOptions {
44
+        sourceCompatibility JavaVersion.VERSION_1_8
45
+        targetCompatibility JavaVersion.VERSION_1_8
46
+    }
47
+}
48
+
49
+repositories {
50
+    mavenCentral()
51
+    maven {
52
+        url 'https://maven.google.com/'
53
+        name 'Google'
54
+    }
55
+
56
+    // Stolen from react-native-firebase, thanks dudes!
57
+    def found = false
58
+    def parentDir = rootProject.projectDir
59
+    def reactNativeAndroidName = 'React Native (Node Modules)'
60
+
61
+    1.upto(4, {
62
+        if (found) return true
63
+        parentDir = parentDir.parentFile
64
+        def reactNativeAndroid = new File(
65
+                parentDir,
66
+                'node_modules/react-native/android'
67
+        )
68
+
69
+        if (reactNativeAndroid.exists()) {
70
+            maven {
71
+                url reactNativeAndroid.toString()
72
+                name reactNativeAndroidName
73
+            }
74
+
75
+            println "${project.name}: using React Native sources from ${reactNativeAndroid.toString()}"
76
+            found = true
77
+        }
78
+    })
79
+
80
+    if (!found) {
81
+        throw new GradleException(
82
+                "${project.name}: unable to locate React Native Android sources, " +
83
+                        "ensure you have you installed React Native as a dependency and try again."
84
+        )
85
+    }
18
 }
86
 }
19
 
87
 
20
 dependencies {
88
 dependencies {
21
     implementation 'com.facebook.react:react-native:+'
89
     implementation 'com.facebook.react:react-native:+'
90
+    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
22
 }
91
 }

+ 1
- 6
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java View File

56
 import com.reactnativecommunity.webview.events.TopLoadingStartEvent;
56
 import com.reactnativecommunity.webview.events.TopLoadingStartEvent;
57
 import com.reactnativecommunity.webview.events.TopMessageEvent;
57
 import com.reactnativecommunity.webview.events.TopMessageEvent;
58
 import com.reactnativecommunity.webview.events.TopLoadingProgressEvent;
58
 import com.reactnativecommunity.webview.events.TopLoadingProgressEvent;
59
-import java.io.UnsupportedEncodingException;
60
-import java.util.ArrayList;
61
-import java.util.HashMap;
62
-import java.util.Locale;
63
-import java.util.Map;
64
-import javax.annotation.Nullable;
65
 import org.json.JSONException;
59
 import org.json.JSONException;
66
 import org.json.JSONObject;
60
 import org.json.JSONObject;
67
 
61
 
510
     }
504
     }
511
   }
505
   }
512
 
506
 
507
+  @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
513
   @ReactProp(name = "mediaPlaybackRequiresUserAction")
508
   @ReactProp(name = "mediaPlaybackRequiresUserAction")
514
   public void setMediaPlaybackRequiresUserAction(WebView view, boolean requires) {
509
   public void setMediaPlaybackRequiresUserAction(WebView view, boolean requires) {
515
     view.getSettings().setMediaPlaybackRequiresUserGesture(requires);
510
     view.getSettings().setMediaPlaybackRequiresUserGesture(requires);

+ 0
- 40
android/src/main/java/com/reactnativecommunity/webview/events/TopLoadingErrorEvent.java View File

1
-package com.reactnativecommunity.webview.events;
2
-
3
-import com.facebook.react.bridge.WritableMap;
4
-import com.facebook.react.uimanager.events.Event;
5
-import com.facebook.react.uimanager.events.RCTEventEmitter;
6
-
7
-/**
8
- * Event emitted when there is an error in loading.
9
- */
10
-public class TopLoadingErrorEvent extends Event<TopLoadingErrorEvent> {
11
-
12
-  public static final String EVENT_NAME = "topLoadingError";
13
-  private WritableMap mEventData;
14
-
15
-  public TopLoadingErrorEvent(int viewId, WritableMap eventData) {
16
-    super(viewId);
17
-    mEventData = eventData;
18
-  }
19
-
20
-  @Override
21
-  public String getEventName() {
22
-    return EVENT_NAME;
23
-  }
24
-
25
-  @Override
26
-  public boolean canCoalesce() {
27
-    return false;
28
-  }
29
-
30
-  @Override
31
-  public short getCoalescingKey() {
32
-    // All events for a given view can be coalesced.
33
-    return 0;
34
-  }
35
-
36
-  @Override
37
-  public void dispatch(RCTEventEmitter rctEventEmitter) {
38
-    rctEventEmitter.receiveEvent(getViewTag(), getEventName(), mEventData);
39
-  }
40
-}

+ 25
- 0
android/src/main/java/com/reactnativecommunity/webview/events/TopLoadingErrorEvent.kt View File

1
+package com.reactnativecommunity.webview.events
2
+
3
+import com.facebook.react.bridge.WritableMap
4
+import com.facebook.react.uimanager.events.Event
5
+import com.facebook.react.uimanager.events.RCTEventEmitter
6
+
7
+/**
8
+ * Event emitted when there is an error in loading.
9
+ */
10
+class TopLoadingErrorEvent(viewId: Int, private val mEventData: WritableMap) :
11
+    Event<TopLoadingErrorEvent>(viewId) {
12
+    companion object {
13
+        const val EVENT_NAME = "topLoadingError"
14
+    }
15
+
16
+    override fun getEventName(): String = EVENT_NAME
17
+
18
+    override fun canCoalesce(): Boolean = false
19
+
20
+    override fun getCoalescingKey(): Short = 0
21
+
22
+    override fun dispatch(rctEventEmitter: RCTEventEmitter) =
23
+        rctEventEmitter.receiveEvent(viewTag, eventName, mEventData)
24
+
25
+}

+ 0
- 40
android/src/main/java/com/reactnativecommunity/webview/events/TopLoadingFinishEvent.java View File

1
-package com.reactnativecommunity.webview.events;
2
-
3
-import com.facebook.react.bridge.WritableMap;
4
-import com.facebook.react.uimanager.events.Event;
5
-import com.facebook.react.uimanager.events.RCTEventEmitter;
6
-
7
-/**
8
- * Event emitted when loading is completed.
9
- */
10
-public class TopLoadingFinishEvent extends Event<TopLoadingFinishEvent> {
11
-
12
-  public static final String EVENT_NAME = "topLoadingFinish";
13
-  private WritableMap mEventData;
14
-
15
-  public TopLoadingFinishEvent(int viewId, WritableMap eventData) {
16
-    super(viewId);
17
-    mEventData = eventData;
18
-  }
19
-
20
-  @Override
21
-  public String getEventName() {
22
-    return EVENT_NAME;
23
-  }
24
-
25
-  @Override
26
-  public boolean canCoalesce() {
27
-    return false;
28
-  }
29
-
30
-  @Override
31
-  public short getCoalescingKey() {
32
-    // All events for a given view can be coalesced.
33
-    return 0;
34
-  }
35
-
36
-  @Override
37
-  public void dispatch(RCTEventEmitter rctEventEmitter) {
38
-    rctEventEmitter.receiveEvent(getViewTag(), getEventName(), mEventData);
39
-  }
40
-}

+ 24
- 0
android/src/main/java/com/reactnativecommunity/webview/events/TopLoadingFinishEvent.kt View File

1
+package com.reactnativecommunity.webview.events
2
+
3
+import com.facebook.react.bridge.WritableMap
4
+import com.facebook.react.uimanager.events.Event
5
+import com.facebook.react.uimanager.events.RCTEventEmitter
6
+
7
+/**
8
+ * Event emitted when loading is completed.
9
+ */
10
+class TopLoadingFinishEvent(viewId: Int, private val mEventData: WritableMap) :
11
+    Event<TopLoadingFinishEvent>(viewId) {
12
+    companion object {
13
+        const val EVENT_NAME = "topLoadingFinish"
14
+    }
15
+
16
+    override fun getEventName(): String = EVENT_NAME
17
+
18
+    override fun canCoalesce(): Boolean = false
19
+
20
+    override fun getCoalescingKey(): Short = 0
21
+
22
+    override fun dispatch(rctEventEmitter: RCTEventEmitter) =
23
+        rctEventEmitter.receiveEvent(viewTag, eventName, mEventData)
24
+}

+ 0
- 36
android/src/main/java/com/reactnativecommunity/webview/events/TopLoadingProgressEvent.java View File

1
-package com.reactnativecommunity.webview.events;
2
-
3
-import com.facebook.react.bridge.WritableMap;
4
-import com.facebook.react.uimanager.events.Event;
5
-import com.facebook.react.uimanager.events.RCTEventEmitter;
6
-
7
-public class TopLoadingProgressEvent extends Event<TopLoadingProgressEvent> {
8
-    public static final String EVENT_NAME = "topLoadingProgress";
9
-    private WritableMap mEventData;
10
-
11
-    public TopLoadingProgressEvent(int viewId, WritableMap eventData) {
12
-        super(viewId);
13
-        mEventData = eventData;
14
-    }
15
-
16
-    @Override
17
-    public String getEventName() {
18
-        return EVENT_NAME;
19
-    }
20
-
21
-    @Override
22
-    public boolean canCoalesce() {
23
-        return false;
24
-    }
25
-
26
-    @Override
27
-    public short getCoalescingKey() {
28
-        // All events for a given view can be coalesced.
29
-        return 0;
30
-    }
31
-
32
-    @Override
33
-    public void dispatch(RCTEventEmitter rctEventEmitter) {
34
-        rctEventEmitter.receiveEvent(getViewTag(), getEventName(), mEventData);
35
-    }
36
-}

+ 24
- 0
android/src/main/java/com/reactnativecommunity/webview/events/TopLoadingProgressEvent.kt View File

1
+package com.reactnativecommunity.webview.events
2
+
3
+import com.facebook.react.bridge.WritableMap
4
+import com.facebook.react.uimanager.events.Event
5
+import com.facebook.react.uimanager.events.RCTEventEmitter
6
+
7
+/**
8
+ * Event emitted when there is a loading progress event.
9
+ */
10
+class TopLoadingProgressEvent(viewId: Int, private val mEventData: WritableMap) :
11
+    Event<TopLoadingProgressEvent>(viewId) {
12
+    companion object {
13
+        const val EVENT_NAME = "topLoadingProgress"
14
+    }
15
+
16
+    override fun getEventName(): String = EVENT_NAME
17
+
18
+    override fun canCoalesce(): Boolean = false
19
+
20
+    override fun getCoalescingKey(): Short = 0
21
+
22
+    override fun dispatch(rctEventEmitter: RCTEventEmitter) =
23
+        rctEventEmitter.receiveEvent(viewTag, eventName, mEventData)
24
+}

+ 0
- 40
android/src/main/java/com/reactnativecommunity/webview/events/TopLoadingStartEvent.java View File

1
-package com.reactnativecommunity.webview.events;
2
-
3
-import com.facebook.react.bridge.WritableMap;
4
-import com.facebook.react.uimanager.events.Event;
5
-import com.facebook.react.uimanager.events.RCTEventEmitter;
6
-
7
-/**
8
- * Event emitted when loading has started
9
- */
10
-public class TopLoadingStartEvent extends Event<TopLoadingStartEvent> {
11
-
12
-  public static final String EVENT_NAME = "topLoadingStart";
13
-  private WritableMap mEventData;
14
-
15
-  public TopLoadingStartEvent(int viewId, WritableMap eventData) {
16
-    super(viewId);
17
-    mEventData = eventData;
18
-  }
19
-
20
-  @Override
21
-  public String getEventName() {
22
-    return EVENT_NAME;
23
-  }
24
-
25
-  @Override
26
-  public boolean canCoalesce() {
27
-    return false;
28
-  }
29
-
30
-  @Override
31
-  public short getCoalescingKey() {
32
-    // All events for a given view can be coalesced.
33
-    return 0;
34
-  }
35
-
36
-  @Override
37
-  public void dispatch(RCTEventEmitter rctEventEmitter) {
38
-    rctEventEmitter.receiveEvent(getViewTag(), getEventName(), mEventData);
39
-  }
40
-}

+ 25
- 0
android/src/main/java/com/reactnativecommunity/webview/events/TopLoadingStartEvent.kt View File

1
+package com.reactnativecommunity.webview.events
2
+
3
+import com.facebook.react.bridge.WritableMap
4
+import com.facebook.react.uimanager.events.Event
5
+import com.facebook.react.uimanager.events.RCTEventEmitter
6
+
7
+/**
8
+ * Event emitted when loading has started
9
+ */
10
+class TopLoadingStartEvent(viewId: Int, private val mEventData: WritableMap) :
11
+    Event<TopLoadingStartEvent>(viewId) {
12
+    companion object {
13
+        const val EVENT_NAME = "topLoadingStart"
14
+    }
15
+
16
+    override fun getEventName(): String = EVENT_NAME
17
+
18
+    override fun canCoalesce(): Boolean = false
19
+
20
+    override fun getCoalescingKey(): Short = 0
21
+
22
+    override fun dispatch(rctEventEmitter: RCTEventEmitter) =
23
+        rctEventEmitter.receiveEvent(viewTag, eventName, mEventData)
24
+
25
+}

+ 0
- 43
android/src/main/java/com/reactnativecommunity/webview/events/TopMessageEvent.java View File

1
-package com.reactnativecommunity.webview.events;
2
-
3
-import com.facebook.react.bridge.WritableMap;
4
-import com.facebook.react.bridge.Arguments;
5
-import com.facebook.react.uimanager.events.Event;
6
-import com.facebook.react.uimanager.events.RCTEventEmitter;
7
-
8
-/**
9
- * Event emitted when there is an error in loading.
10
- */
11
-public class TopMessageEvent extends Event<TopMessageEvent> {
12
-
13
-  public static final String EVENT_NAME = "topMessage";
14
-  private final String mData;
15
-
16
-  public TopMessageEvent(int viewId, String data) {
17
-    super(viewId);
18
-    mData = data;
19
-  }
20
-
21
-  @Override
22
-  public String getEventName() {
23
-    return EVENT_NAME;
24
-  }
25
-
26
-  @Override
27
-  public boolean canCoalesce() {
28
-    return false;
29
-  }
30
-
31
-  @Override
32
-  public short getCoalescingKey() {
33
-    // All events for a given view can be coalesced.
34
-    return 0;
35
-  }
36
-
37
-  @Override
38
-  public void dispatch(RCTEventEmitter rctEventEmitter) {
39
-    WritableMap data = Arguments.createMap();
40
-    data.putString("data", mData);
41
-    rctEventEmitter.receiveEvent(getViewTag(), EVENT_NAME, data);
42
-  }
43
-}

+ 26
- 0
android/src/main/java/com/reactnativecommunity/webview/events/TopMessageEvent.kt View File

1
+package com.reactnativecommunity.webview.events
2
+
3
+import com.facebook.react.bridge.Arguments
4
+import com.facebook.react.uimanager.events.Event
5
+import com.facebook.react.uimanager.events.RCTEventEmitter
6
+
7
+/**
8
+ * Event emitted when there is an error in loading.
9
+ */
10
+class TopMessageEvent(viewId: Int, private val mData: String) : Event<TopMessageEvent>(viewId) {
11
+    companion object {
12
+        const val EVENT_NAME = "topMessage"
13
+    }
14
+
15
+    override fun getEventName(): String = EVENT_NAME
16
+
17
+    override fun canCoalesce(): Boolean = false
18
+
19
+    override fun getCoalescingKey(): Short = 0
20
+
21
+    override fun dispatch(rctEventEmitter: RCTEventEmitter) {
22
+        val data = Arguments.createMap()
23
+        data.putString("data", mData)
24
+        rctEventEmitter.receiveEvent(viewTag, EVENT_NAME, data)
25
+    }
26
+}

+ 6
- 2
docs/Reference.md View File

110
 
110
 
111
 ### `mediaPlaybackRequiresUserAction`
111
 ### `mediaPlaybackRequiresUserAction`
112
 
112
 
113
-Boolean that determines whether HTML5 audio and video requires the user to tap them before they start playing. The default value is `true`.
113
+Boolean that determines whether HTML5 audio and video requires the user to tap them before they start playing. The default value is `true`. (Android API minimum version 17)
114
 
114
 
115
 | Type | Required |
115
 | Type | Required |
116
 | ---- | -------- |
116
 | ---- | -------- |
497
 | ------- | -------- | -------- |
497
 | ------- | -------- | -------- |
498
 | boolean | No       | iOS      |
498
 | boolean | No       | iOS      |
499
 
499
 
500
+---
501
+
500
 ### `allowFileAccess`
502
 ### `allowFileAccess`
501
 
503
 
502
 If true, this will allow access to the file system via `file://` URI's. The default value is `false`.
504
 If true, this will allow access to the file system via `file://` URI's. The default value is `false`.
505
 | ------- | -------- | -------- |
507
 | ------- | -------- | -------- |
506
 | boolean | No       | Android  |
508
 | boolean | No       | Android  |
507
 
509
 
510
+---
511
+
508
 ### `saveFormDataDisabled`
512
 ### `saveFormDataDisabled`
509
 
513
 
510
 Sets whether the WebView should disable saving form data. The default value is `false`. This function does not have any effect from Android API level 26 onwards as there is an Autofill feature which stores form data.
514
 Sets whether the WebView should disable saving form data. The default value is `false`. This function does not have any effect from Android API level 26 onwards as there is an Autofill feature which stores form data.
556
 ### `injectJavaScript(str)`
560
 ### `injectJavaScript(str)`
557
 
561
 
558
 ```javascript
562
 ```javascript
559
-injectJavaScript("... javascript string ...");
563
+injectJavaScript('... javascript string ...');
560
 ```
564
 ```
561
 
565
 
562
 Executes the JavaScript string.
566
 Executes the JavaScript string.