Skip to content

Commit 5aff118

Browse files
fix: Attempt to invoke virtual method 'void android.telephony.TelephonyManager.listen(android.telephony.PhoneStateListener, int)' on a null object reference
1 parent cdf7b41 commit 5aff118

File tree

5 files changed

+86
-45
lines changed

5 files changed

+86
-45
lines changed

CallDetectionExample/ios/.DS_Store

2 KB
Binary file not shown.

README.md

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## React Native Call Detection 🎉 🎊
2+
23
[![npm version](https://badge.fury.io/js/react-native-call-detection.svg)](https://badge.fury.io/js/react-native-call-detection)
34

45
This package helps to detect different call states like `Incoming`, `Disconnected`, `Dialing` and `Connected` for iOS. For android, this package will give following states, `Offhook`, `Incoming`, `Disconnected` and `Missed`. In the case of `Incoming` for android, the package will also provide with the incoming phone number.
@@ -13,14 +14,16 @@ yarn add react-native-call-detection
1314
```
1415

1516
### For Android:-
17+
1618
Autolinking should work without manual changes
1719

1820
## Usage
21+
1922
There are different hooks that you may get depending on the platform. Since for android you could also request the package to provide you with phone number of the caller, you will have to provide the necessary request message and the corresponding error callback. The package will request for `READ_PHONE_STATE` permission in android.
2023

2124
Its really easy to setup the package. Have a look at the following code snippet
2225

23-
``` javascript
26+
```javascript
2427
import CallDetectorManager from 'react-native-call-detection'
2528

2629
startListenerTapped() {
@@ -90,56 +93,56 @@ Example project can be used to test out the package. In the example project upda
9093

9194
### How to run an example
9295

93-
1. Install `node` and `watchman`
96+
1. Install `node` and `watchman`
9497

95-
```
96-
brew install node
97-
brew install watchman
98+
```
99+
brew install node
100+
brew install watchman
98101
99-
```
102+
```
100103
101-
2. `yarn`
104+
2. `yarn`
102105
103106
Install `yarn` from `npm`.
104107
105108
npm i -g yarn
106109
107-
3. Xcode
110+
3. Xcode
108111
109112
Install it from the App Store.
110113
111-
4. React Native Debugger
114+
4. React Native Debugger
112115
113116
This is an electron app that bundles react devtools, redux devtools and chrome devtools configured for use with react-native.
114117
115118
brew cask install react-native-debugger
116-
5. Once you have done all the above steps, navigate to `CallDetectionExample` folder and run `yarn` or `npm install`, it will fetch all the dependencies in the `node_modules` folder.
117119
118-
6. Run the packager
119-
`npm start`
120+
5. Once you have done all the above steps, navigate to `CallDetectionExample` folder and run `yarn` or `npm install`, it will fetch all the dependencies in the `node_modules` folder.
120121
121-
7. Update the mobile number of your friend in [`HomeComponent.js`](CallDetectionExample/src/HomeComponent.js)
122+
6. Run the packager
123+
`npm start`
122124
125+
7. Update the mobile number of your friend in [`HomeComponent.js`](CallDetectionExample/src/HomeComponent.js)
123126
124-
```javascript
125-
callFriendTapped() {
126-
// Add the telephone number
127+
```javascript
128+
callFriendTapped() {
129+
// Add the telephone number
127130
Linking.openURL('tel:5555555555')
128131
.catch(err => {
129132
console.log(err)
130133
});
131-
}
134+
}
132135
133-
```
136+
```
134137
135-
7. To run the example on iOS from terminal type
136-
`react-native run-ios` (This will open the simulator, since simulator doesnt have the support for calling I will advice you to connect your iOS device and the follow the below procedure for running the app through xcode)
138+
8. To run the example on iOS from terminal type
139+
`react-native run-ios` (This will open the simulator, since simulator doesnt have the support for calling I will advice you to connect your iOS device and the follow the below procedure for running the app through xcode)
137140
138-
or you can also run the app from xcode, for that, open `/CallDectionExample/ios/CallDetectionExample.xcodeproj` in xcode and run on the device.
141+
or you can also run the app from xcode, for that, open `/CallDectionExample/ios/CallDetectionExample.xcodeproj` in xcode and run on the device.
139142
140-
8. To run the example on android, connect any android device to your mac then follow the below steps
141-
1. Navigate to `android` folder(./CallDectionExample/android/) folder and then run `adb reverse tcp:8081 tcp:8081`
142-
2. Navigate back to example directory(CallDectionExample) and then run
143-
`react-native run-android`
143+
9. To run the example on android, connect any android device to your mac then follow the below steps
144+
1. Navigate to `android` folder(./CallDectionExample/android/) folder and then run `adb reverse tcp:8081 tcp:8081`
145+
2. Navigate back to example directory(CallDectionExample) and then run
146+
`react-native run-android`
144147
145148
For any problems and doubt raise an issue.

android/src/main/java/com/pritesh/calldetection/CallDetectionManagerModule.java

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class CallDetectionManagerModule
1919
extends ReactContextBaseJavaModule
2020
implements Application.ActivityLifecycleCallbacks,
2121
CallDetectionPhoneStateListener.PhoneCallStateUpdate {
22+
private static final String LOG_TAG = "CallDetectionManagerModule";
2223

2324
private boolean wasAppInOffHook = false;
2425
private boolean wasAppInRinging = false;
@@ -38,27 +39,50 @@ public String getName() {
3839
return "CallDetectionManagerAndroid";
3940
}
4041

42+
private void logs(final String message) {
43+
String stackTraceString = "";
44+
try {
45+
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
46+
for (StackTraceElement e : stackTrace) {
47+
stackTraceString += (e != null ? e.toString() : "null") + "\n";
48+
}
49+
} catch (Exception e) {
50+
stackTraceString = "Exception occurred while parsing stack trace";
51+
}
52+
Log.e(LOG_TAG, message);
53+
Log.d(LOG_TAG, stackTraceString);
54+
}
55+
4156
@ReactMethod
4257
public void startListener() {
43-
if (activity == null) {
44-
activity = getCurrentActivity();
45-
activity.getApplication().registerActivityLifecycleCallbacks(this);
58+
try {
59+
if (activity == null) {
60+
activity = getCurrentActivity();
61+
activity.getApplication().registerActivityLifecycleCallbacks(this);
62+
}
63+
64+
telephonyManager = (TelephonyManager) this.reactContext.getSystemService(
65+
Context.TELEPHONY_SERVICE);
66+
callDetectionPhoneStateListener = new CallDetectionPhoneStateListener(this);
67+
telephonyManager.listen(callDetectionPhoneStateListener,
68+
PhoneStateListener.LISTEN_CALL_STATE);
69+
} catch (Exception e) {
70+
logs(e.toString());
4671
}
47-
48-
telephonyManager = (TelephonyManager) this.reactContext.getSystemService(
49-
Context.TELEPHONY_SERVICE);
50-
callDetectionPhoneStateListener = new CallDetectionPhoneStateListener(this);
51-
telephonyManager.listen(callDetectionPhoneStateListener,
52-
PhoneStateListener.LISTEN_CALL_STATE);
53-
5472
}
5573

5674
@ReactMethod
5775
public void stopListener() {
58-
telephonyManager.listen(callDetectionPhoneStateListener,
59-
PhoneStateListener.LISTEN_NONE);
60-
telephonyManager = null;
61-
callDetectionPhoneStateListener = null;
76+
try {
77+
if(telephonyManager != null) {
78+
telephonyManager.listen(callDetectionPhoneStateListener,
79+
PhoneStateListener.LISTEN_NONE);
80+
}
81+
telephonyManager = null;
82+
callDetectionPhoneStateListener = null;
83+
} catch (Exception e) {
84+
logs(e.toString());
85+
}
6286
}
6387

6488
/**
@@ -87,7 +111,10 @@ public void onActivityStarted(Activity activity) {
87111

88112
@Override
89113
public void onActivityResumed(Activity activity) {
90-
114+
if (wasAppInOffHook && jsModule != null) {
115+
wasAppInOffHook = false;
116+
jsModule.callStateUpdated("Disconnected", null);
117+
}
91118
}
92119

93120
@Override

iOS/RCTCallDetection/RCTCallDetection/CallDetectionManager.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ + (BOOL)requiresMainQueueSetup {
5252
- (void)callObserver:(CXCallObserver *)callObserver callChanged:(CXCall *)call {
5353
if (call.hasEnded == true) {
5454
[self sendEventWithName:@"PhoneCallStateUpdate" body:@"Disconnected"];
55-
} else if (call.hasConnected == true) {
55+
} else if (all.hasEnded == false && call.hasConnected == true) {
5656
[self sendEventWithName:@"PhoneCallStateUpdate" body:@"Connected"];
57-
} else if (call.isOutgoing == true) {
57+
} else if (call.hasConnected == false && call.hasEnded == false && call.isOutgoing == true) {
5858
[self sendEventWithName:@"PhoneCallStateUpdate" body:@"Dialing"];
59-
} else {
59+
} else if (call.isOutgoing == false && call.hasConnected == false) {
6060
[self sendEventWithName:@"PhoneCallStateUpdate" body:@"Incoming"];
6161
}
6262
}
6363

64-
@end
64+
@end

package.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
"type": "git",
88
"url": "https://github.com/priteshrnandgaonkar/react-native-call-detection"
99
},
10+
"bundleDependencies": false,
11+
"deprecated": false,
12+
"description": "Get different phone call states for iOS and Android along with incoming phone number(just for android)",
13+
"homepage": "https://github.com/priteshrnandgaonkar/react-native-call-detection#readme",
1014
"keywords": [
1115
"react-native",
1216
"call",
@@ -16,7 +20,14 @@
1620
],
1721
"author": "Pritesh Nandgaonkar <prit.nandgaonkar@gmail.com> (https://github.com/priteshrnandgaonkar)",
1822
"license": "MIT",
23+
"main": "index.js",
24+
"name": "react-native-call-detection",
1925
"peerDependencies": {
2026
"react-native": ">=0.26.0"
21-
}
27+
},
28+
"repository": {
29+
"type": "git",
30+
"url": "git+https://github.com/priteshrnandgaonkar/react-native-call-detection.git"
31+
},
32+
"version": "1.9.0"
2233
}

0 commit comments

Comments
 (0)