-
Notifications
You must be signed in to change notification settings - Fork 454
Expand file tree
/
Copy pathYouTubeModule.java
More file actions
96 lines (83 loc) · 3.92 KB
/
Copy pathYouTubeModule.java
File metadata and controls
96 lines (83 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.inprogress.reactnativeyoutube;
import androidx.annotation.Nullable;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.uimanager.IllegalViewOperationException;
import com.facebook.react.uimanager.NativeViewHierarchyManager;
import com.facebook.react.uimanager.UIBlock;
import com.facebook.react.uimanager.UIManagerModule;
import com.google.android.youtube.player.YouTubePlayer;
import java.util.HashMap;
import java.util.Map;
public class YouTubeModule extends ReactContextBaseJavaModule {
private static final String E_MODULE_ERROR = "E_MODULE_ERROR";
private ReactApplicationContext mReactContext;
public YouTubeModule(ReactApplicationContext reactContext) {
super(reactContext);
mReactContext = reactContext;
}
@Override
public String getName() {
return "YouTubeModule";
}
@ReactMethod
public void getVideosIndex(final int reactTag, final Promise promise) {
try {
UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
uiManager.addUIBlock(new UIBlock() {
public void execute(NativeViewHierarchyManager nvhm) {
YouTubeView youTubeView = (YouTubeView) nvhm.resolveView(reactTag);
YouTubeManager youTubeManager = (YouTubeManager) nvhm.resolveViewManager(reactTag);
int index = youTubeManager.getVideosIndex(youTubeView);
promise.resolve(index);
}
});
} catch (IllegalViewOperationException e) {
promise.reject(E_MODULE_ERROR, e);
}
}
@ReactMethod
public void getCurrentTime(final int reactTag, final Promise promise) {
try {
UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
uiManager.addUIBlock(new UIBlock() {
public void execute(NativeViewHierarchyManager nvhm) {
YouTubeView youTubeView = (YouTubeView) nvhm.resolveView(reactTag);
YouTubeManager youTubeManager = (YouTubeManager) nvhm.resolveViewManager(reactTag);
int currentTime = youTubeManager.getCurrentTime(youTubeView);
promise.resolve(currentTime);
}
});
} catch (IllegalViewOperationException e) {
promise.reject(E_MODULE_ERROR, e);
}
}
@ReactMethod
public void getDuration(final int reactTag, final Promise promise) {
try {
UIManagerModule uiManager = mReactContext.getNativeModule(UIManagerModule.class);
uiManager.addUIBlock(new UIBlock() {
public void execute(NativeViewHierarchyManager nvhm) {
YouTubeView youTubeView = (YouTubeView) nvhm.resolveView(reactTag);
YouTubeManager youTubeManager = (YouTubeManager) nvhm.resolveViewManager(reactTag);
int duration = youTubeManager.getDuration(youTubeView);
promise.resolve(duration);
}
});
} catch (IllegalViewOperationException e) {
promise.reject(E_MODULE_ERROR, e);
}
}
@Nullable
@Override
public Map<String, Object> getConstants() {
Map<String, Object> constants = new HashMap<>();
constants.put("FULLSCREEN_FLAG_CONTROL_ORIENTATION", YouTubePlayer.FULLSCREEN_FLAG_CONTROL_ORIENTATION);
constants.put("FULLSCREEN_FLAG_CONTROL_SYSTEM_UI", YouTubePlayer.FULLSCREEN_FLAG_CONTROL_SYSTEM_UI);
constants.put("FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE", YouTubePlayer.FULLSCREEN_FLAG_ALWAYS_FULLSCREEN_IN_LANDSCAPE);
constants.put("FULLSCREEN_FLAG_CUSTOM_LAYOUT", YouTubePlayer.FULLSCREEN_FLAG_CUSTOM_LAYOUT);
return constants;
}
}