Skip to content

Commit 7e66d50

Browse files
author
liulingfeng
committed
修改android原生暴露组件
1 parent fe908cd commit 7e66d50

File tree

5 files changed

+151
-101
lines changed

5 files changed

+151
-101
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package com.netease.alivedetected;
2+
3+
import android.content.Context;
4+
import android.util.Log;
5+
6+
import com.facebook.react.bridge.Arguments;
7+
import com.facebook.react.bridge.ReactContext;
8+
import com.facebook.react.bridge.WritableMap;
9+
import com.facebook.react.modules.core.DeviceEventManagerModule;
10+
import com.facebook.react.uimanager.events.RCTEventEmitter;
11+
import com.netease.nis.alivedetected.ActionType;
12+
import com.netease.nis.alivedetected.AliveDetector;
13+
import com.netease.nis.alivedetected.DetectedListener;
14+
import com.netease.nis.alivedetected.NISCameraPreview;
15+
16+
/**
17+
* Created by hzhuqi on 2020/8/31
18+
*/
19+
public class AliveHelper {
20+
public static final String TAG = "RNAlive";
21+
private ReactContext reactContext;
22+
private NISCameraPreview cameraPreview;
23+
private static final AliveHelper aliveHelper = new AliveHelper();
24+
25+
private AliveHelper() {
26+
}
27+
28+
public static AliveHelper getInstance() {
29+
return aliveHelper;
30+
}
31+
32+
public void setPreView(NISCameraPreview cameraPreview) {
33+
this.cameraPreview = cameraPreview;
34+
}
35+
36+
public void init(ReactContext reactContext, String businessId, int timeOut) {
37+
this.reactContext = reactContext;
38+
if (cameraPreview != null) {
39+
AliveDetector.getInstance().init(reactContext, cameraPreview, businessId);
40+
AliveDetector.getInstance().setTimeOut(timeOut);
41+
}
42+
}
43+
44+
public void startDetected() {
45+
AliveDetector.getInstance().startDetect();
46+
AliveDetector.getInstance().setDetectedListener(new DetectedListener() {
47+
@Override
48+
public void onReady(boolean b) {
49+
50+
}
51+
52+
@Override
53+
public void onActionCommands(ActionType[] actionTypes) {
54+
Log.d(TAG, "动作序列--------->" + buildActionCommand(actionTypes));
55+
WritableMap event = Arguments.createMap();
56+
event.putString("actions", buildActionCommand(actionTypes));
57+
sendEvent("onActionChange", event);
58+
}
59+
60+
@Override
61+
public void onStateTipChanged(ActionType actionType, String stateTip) {
62+
int currentIndex = Integer.parseInt(actionType.getActionID());
63+
if (currentIndex >= 0 && currentIndex <= 4) {
64+
Log.d(TAG, "动作类型--------->" + actionType.getActionTip());
65+
WritableMap event = Arguments.createMap();
66+
event.putString("message", actionType.getActionTip());
67+
event.putInt("currentStep", currentIndex);
68+
sendEvent("onStepChange", event);
69+
} else if (currentIndex == 5) {
70+
Log.d(TAG, "状态提示--------->" + stateTip);
71+
WritableMap event = Arguments.createMap();
72+
event.putString("message", stateTip);
73+
sendEvent("onWarnChange", event);
74+
}
75+
}
76+
77+
@Override
78+
public void onPassed(boolean isPassed, String token) {
79+
WritableMap event = Arguments.createMap();
80+
if (isPassed) {
81+
event.putString("message", "success");
82+
} else {
83+
event.putString("message", "failed");
84+
}
85+
event.putString("token", token);
86+
sendEvent("onResultChange", event);
87+
}
88+
89+
@Override
90+
public void onError(int code, String msg, String token) {
91+
WritableMap event = Arguments.createMap();
92+
event.putString("message", msg);
93+
event.putString("token", token);
94+
sendEvent("onWarnChange", event);
95+
}
96+
97+
@Override
98+
public void onOverTime() {
99+
WritableMap event = Arguments.createMap();
100+
event.putString("message", "操作超时,用户未在规定时间内完成动作");
101+
event.putString("token", "");
102+
sendEvent("onResultChange", event);
103+
}
104+
});
105+
}
106+
107+
public void stopDetected() {
108+
AliveDetector.getInstance().stopDetect();
109+
}
110+
111+
public void destroy() {
112+
AliveDetector.getInstance().destroy();
113+
}
114+
115+
private void sendEvent(String eventName, WritableMap event) {
116+
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(
117+
eventName, event);
118+
}
119+
120+
121+
private String buildActionCommand(ActionType[] actionCommands) {
122+
StringBuilder commands = new StringBuilder();
123+
for (ActionType actionType : actionCommands) {
124+
commands.append(actionType.getActionID());
125+
}
126+
return commands == null ? "" : commands.toString();
127+
}
128+
129+
}

android/src/main/java/com/netease/alivedetected/RNAliveViewManager.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77

88
import com.facebook.react.uimanager.SimpleViewManager;
99
import com.facebook.react.uimanager.ThemedReactContext;
10-
import com.netease.nis.alivedetected.AliveDetector;
10+
import com.netease.nis.alivedetected.NISCameraPreview;
1111

1212
/**
1313
* Created by hzhuqi on 2020/8/21
1414
*/
1515
public class RNAliveViewManager extends SimpleViewManager<FrameLayout> {
16-
private static final String NAME = "RNAliveView";
17-
public static final String TAG = "RNAlive";
16+
private static final String NAME = "NTESRNLiveDetect";
1817

1918
@Override
2019
public String getName() {
@@ -23,17 +22,20 @@ public String getName() {
2322

2423
@Override
2524
protected FrameLayout createViewInstance(ThemedReactContext reactContext) {
26-
Log.d(TAG, "======createViewInstance======");
25+
Log.d(AliveHelper.TAG, "======createViewInstance======");
2726
Context context = reactContext.getApplicationContext();
28-
return (FrameLayout) LayoutInflater.from(reactContext).inflate(getLayoutId(context), null);
27+
FrameLayout cameraPreview = (FrameLayout) LayoutInflater.from(reactContext).inflate(getLayoutId(context), null);
28+
NISCameraPreview aliveView = cameraPreview.findViewById(R.id.surface_view);
29+
AliveHelper.getInstance().setPreView(aliveView);
30+
return cameraPreview;
2931
}
3032

3133
@Override
3234
public void onDropViewInstance(FrameLayout preview) {
33-
Log.d(TAG, "======onDropViewInstance======");
35+
Log.d(AliveHelper.TAG, "======onDropViewInstance======");
3436

35-
AliveDetector.getInstance().stopDetect();
36-
AliveDetector.getInstance().destroy();
37+
AliveHelper.getInstance().stopDetected();
38+
AliveHelper.getInstance().destroy();
3739
}
3840

3941
private int getLayoutId(Context context) {
Lines changed: 7 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,30 @@
11

22
package com.netease.alivedetected;
33

4-
import android.content.Context;
5-
import android.util.Log;
6-
7-
import com.facebook.react.bridge.Arguments;
84
import com.facebook.react.bridge.ReactApplicationContext;
95
import com.facebook.react.bridge.ReactContext;
106
import com.facebook.react.bridge.ReactContextBaseJavaModule;
117
import com.facebook.react.bridge.ReactMethod;
12-
import com.facebook.react.bridge.WritableMap;
13-
import com.facebook.react.modules.core.DeviceEventManagerModule;
14-
import com.netease.nis.alivedetected.ActionType;
158
import com.netease.nis.alivedetected.AliveDetector;
16-
import com.netease.nis.alivedetected.DetectedListener;
17-
import com.netease.nis.alivedetected.NISCameraPreview;
189

1910
public class RNAliveViewModule extends ReactContextBaseJavaModule {
20-
private ReactContext reactContext;
21-
public static final String TAG = "RNAlive";
11+
private final ReactContext reactContext;
12+
private final AliveHelper aliveHelper;
2213

2314
public RNAliveViewModule(ReactApplicationContext reactContext) {
2415
super(reactContext);
2516
this.reactContext = reactContext;
17+
aliveHelper = AliveHelper.getInstance();
2618
}
2719

2820
@ReactMethod
29-
public void init(Context context, NISCameraPreview cameraPreview, String businessId, int timeOut) {
30-
AliveDetector.getInstance().init(context, cameraPreview, businessId);
31-
AliveDetector.getInstance().setTimeOut(timeOut * 1000);
21+
public void initWithBusinessID(String businessId, int timeOut) {
22+
aliveHelper.init(reactContext, businessId, timeOut);
3223
}
3324

3425
@ReactMethod
3526
public void startAlive() {
36-
AliveDetector.getInstance().startDetect();
37-
AliveDetector.getInstance().setDetectedListener(new DetectedListener() {
38-
@Override
39-
public void onReady(boolean b) {
40-
41-
}
42-
43-
@Override
44-
public void onActionCommands(ActionType[] actionTypes) {
45-
Log.d(TAG, "动作序列--------->" + buildActionCommand(actionTypes));
46-
WritableMap event = Arguments.createMap();
47-
event.putString("actions", buildActionCommand(actionTypes));
48-
sendEvent("onActionChange", event);
49-
}
50-
51-
@Override
52-
public void onStateTipChanged(ActionType actionType, String stateTip) {
53-
int currentIndex = Integer.parseInt(actionType.getActionID());
54-
if (currentIndex >= 0 && currentIndex <= 4) {
55-
Log.d(TAG, "动作类型--------->" + actionType.getActionTip());
56-
WritableMap event = Arguments.createMap();
57-
event.putString("message", actionType.getActionTip());
58-
event.putInt("currentStep", currentIndex);
59-
sendEvent("onStepChange", event);
60-
} else if (currentIndex == 5) {
61-
Log.d(TAG, "状态提示--------->" + stateTip);
62-
WritableMap event = Arguments.createMap();
63-
event.putString("message", stateTip);
64-
sendEvent("onWarnChange", event);
65-
}
66-
}
67-
68-
@Override
69-
public void onPassed(boolean isPassed, String token) {
70-
WritableMap event = Arguments.createMap();
71-
if (isPassed) {
72-
event.putString("message", "success");
73-
} else {
74-
event.putString("message", "failed");
75-
}
76-
event.putString("token", token);
77-
sendEvent("onResultChange", event);
78-
}
79-
80-
@Override
81-
public void onError(int code, String msg, String token) {
82-
WritableMap event = Arguments.createMap();
83-
event.putString("message", msg);
84-
event.putString("token", token);
85-
sendEvent("onWarnChange", event);
86-
}
87-
88-
@Override
89-
public void onOverTime() {
90-
WritableMap event = Arguments.createMap();
91-
event.putString("message", "操作超时,用户未在规定时间内完成动作");
92-
event.putString("token", "");
93-
sendEvent("onResultChange", event);
94-
}
95-
});
27+
aliveHelper.startDetected();
9628
}
9729

9830
@ReactMethod
@@ -102,19 +34,6 @@ public void stopAlive() {
10234

10335
@Override
10436
public String getName() {
105-
return "RNAliveView";
106-
}
107-
108-
private String buildActionCommand(ActionType[] actionCommands) {
109-
StringBuilder commands = new StringBuilder();
110-
for (ActionType actionType : actionCommands) {
111-
commands.append(actionType.getActionID());
112-
}
113-
return commands == null ? "" : commands.toString();
114-
}
115-
116-
private void sendEvent(String eventName, WritableMap event) {
117-
reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(
118-
eventName, event);
37+
return "AliveHelper";
11938
}
12039
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
1+
<com.netease.nis.alivedetected.NISCameraPreview xmlns:android="http://schemas.android.com/apk/res/android"
22
android:layout_width="match_parent"
33
android:layout_height="match_parent">
44

55
<com.netease.nis.alivedetected.NISCameraPreview
66
android:id="@+id/surface_view"
77
android:layout_width="match_parent"
88
android:layout_height="match_parent" />
9-
</FrameLayout>
9+
</com.netease.nis.alivedetected.NISCameraPreview>
1010

index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import { NativeModules } from 'react-native';
3+
import { requireNativeComponent } from 'react-native';
34

4-
const { RNAliveView } = NativeModules;
5-
6-
export default RNAliveView;
5+
export default NativeModules.AliveHelper;
6+
module.exports = requireNativeComponent('NTESRNLiveDetect');

0 commit comments

Comments
 (0)