-
Notifications
You must be signed in to change notification settings - Fork 112
/
HomeComponent.js
115 lines (103 loc) · 2.9 KB
/
HomeComponent.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
/**
* @providesModule HomeComponent
*
*/
import React, { Component } from 'react';
import {
StyleSheet,
Text,
View,
Button,
Linking,
ListView
} from 'react-native';
var callDetector = undefined
import CallDetectorManager from 'react-native-call-detection'
export default class HomeComponent extends Component {
constructor(props) {
super(props)
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {callStates : [], ds: ds} //call states
this.startListenerTapped = this.startListenerTapped.bind(this);
}
startListenerTapped() {
callDetector = new CallDetectorManager((event, number) => {
var updatedCallStates = this.state.callStates
updatedCallStates.push(event + ' - ' + number)
var previousDS = this.state.ds
this.setState({ callStates: updatedCallStates, ds: previousDS.cloneWithRows(updatedCallStates)});
},
false, // if you want to read the phone number of the incoming call [ANDROID], otherwise false
()=>{}, // callback if your permission got denied [ANDROID] [only if you want to read incoming number] default: console.error
{
title: 'Phone State Permission',
message: 'This app needs access to your phone state in order to react and/or to adapt to incoming calls.'
} // a custom permission request message to explain to your user, why you need the permission [recommended] - this is the default one
)
}
callFriendTapped() {
Linking.openURL('tel:5555555555')
.catch(err => {
console.log(err)
});
}
stopListenerTapped() {
callDetector && callDetector.dispose();
}
render() {
console.log(this.state.callStates)
return (
<View style={styles.container}>
<Button
onPress={this.startListenerTapped}
title="Start Listener"
color="#841584"
style = {styles.bottomMargin}
/>
<Button
onPress={this.callFriendTapped}
title="Call your friend"
color="#341584"
style = {styles.bottomMargin}
/>
<Button
onPress={this.stopListenerTapped}
title="Stop Listener"
color="#841584"
style = {styles.bottomMargin}
/>
<Text style = {styles.text}>
Call State Logs
</Text>
<ListView
dataSource={this.state.ds}
renderRow={(rowData) => <Text style = {styles.callLogs}>{rowData}</Text>}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
paddingTop: 40,
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
bottomMargin: {
marginBottom: 10
},
text: {
marginTop: 30,
textAlign:'center',
fontSize: 20,
color: '#341584'
},
callLogs: {
textAlign:'center',
fontSize: 15,
color: '#333333',
marginBottom: 5
}
});