-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathindex.js
156 lines (143 loc) · 4.04 KB
/
index.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
* Sample React Native App
* https://github.com/facebook/react-native
*/
import React, { Component } from 'react';
import {
AppRegistry,
Button,
NativeEventEmitter,
NativeModules,
Platform,
StyleSheet,
Text,
TextInput,
View
} from 'react-native';
import BatchedBridge from "react-native/Libraries/BatchedBridge/BatchedBridge";
export class ExposedToJava {
extraMessage = "Be aware that this way of calling JavaScript is officially undocumented.\n\nIf possible, use events instead!";
setMessage(message) {
this.extraMessage = message;
}
/**
* If this is called from an activity that doesn't forward Android life-cycle events
* to React Native, the alert will appear to do nothing.
*/
alert(message) {
alert(message + "\n\n" + this.extraMessage);
}
}
const exposedToJava = new ExposedToJava();
BatchedBridge.registerCallableModule("JavaScriptVisibleToJava", exposedToJava);
const activityStarter = NativeModules.ActivityStarter;
const eventEmitterModule = NativeModules.EventEmitter;
export default class ActivityDemoComponent extends Component {
constructor(props) {
super(props);
this.state = { text: 'Demo text for custom edit menu' };
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native ({this.props.buildType})!
</Text>
<Text style={styles.instructions}>
<Text>To get started, edit </Text>
<Text style={styles.bold}>index.js</Text>
<Text>.</Text>
</Text>
<Text style={styles.instructions}>
Double tap R on your keyboard to reload,{'\n'}
Shake or press menu button for dev menu
</Text>
{
Platform.select({
android: (
<TextInput
style={styles.textInput}
value={this.state.text}
onChangeText={(text) => this.setState({text})}
/>)
})
}
<View style={styles.buttonContainer}>
<Button
onPress={() => activityStarter.navigateToExample()}
title='Start example activity'
/>
<Button
onPress={() => activityStarter.dialNumber('+1 (234) 567-8910')}
title='Dial +1 (234) 567-8910'
/>
<Button
onPress={() => activityStarter.getActivityName((name) => { alert(name); })}
title='Get activity name'
/>
<Button
onPress={async () => {
try {
var name = await activityStarter.getActivityNameAsPromise();
alert(name);
} catch (e) {
alert("Error: " + e.message);
}
}}
title='Get activity name as promise'
/>
<Button
onPress={() => NativeModules.Clipboard.setString("Copied to clipboard from JavaScript!")}
title='Copy to clipboard'
/>
<Button
onPress={() => activityStarter.callJavaScript()}
title='Call JavaScript from Java'
/>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
bold: {
fontWeight: "bold",
},
buttonContainer: {
height: 300,
width: "80%",
justifyContent: 'space-between',
marginTop: 30,
},
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#E5ECFF',
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
textInput: {
backgroundColor: 'white',
borderColor: 'gray',
borderWidth: 1,
height: 40,
marginTop: 20,
textAlign: "center",
width: "80%",
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
});
AppRegistry.registerComponent('ActivityDemoComponent', () => ActivityDemoComponent);
const eventEmitter = new NativeEventEmitter(eventEmitterModule);
eventEmitter.addListener(eventEmitterModule.MyEventName, (params) => {
exposedToJava.setMessage(params);
alert(params);
});