diff --git a/CallDetectionExample/ios/.DS_Store b/CallDetectionExample/ios/.DS_Store index 0b467373c..f3b44a461 100644 Binary files a/CallDetectionExample/ios/.DS_Store and b/CallDetectionExample/ios/.DS_Store differ diff --git a/CallDetectionExample/yarn.lock b/CallDetectionExample/yarn.lock index be9367a55..5773413a6 100644 --- a/CallDetectionExample/yarn.lock +++ b/CallDetectionExample/yarn.lock @@ -2666,8 +2666,8 @@ merge-stream@^1.0.1: readable-stream "^2.0.1" merge@^1.1.3: - version "1.2.0" - resolved "https://registry.yarnpkg.com/merge/-/merge-1.2.0.tgz#7531e39d4949c281a66b8c5a6e0265e8b05894da" + version "1.2.1" + resolved "https://registry.yarnpkg.com/merge/-/merge-1.2.1.tgz#38bebf80c3220a8a487b6fcfb3941bb11720c145" method-override@~2.3.5: version "2.3.9" diff --git a/README.md b/README.md index d25e88006..4ef5a2c0b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ ## React Native Call Detection 🎉 🎊 + [![npm version](https://badge.fury.io/js/react-native-call-detection.svg)](https://badge.fury.io/js/react-native-call-detection) 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 ``` ### For Android:- + Autolinking should work without manual changes ## Usage + 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. Its really easy to setup the package. Have a look at the following code snippet -``` javascript +```javascript import CallDetectorManager from 'react-native-call-detection' startListenerTapped() { @@ -90,56 +93,56 @@ Example project can be used to test out the package. In the example project upda ### How to run an example -1. Install `node` and `watchman` +1. Install `node` and `watchman` - ``` - brew install node - brew install watchman + ``` + brew install node + brew install watchman - ``` + ``` -2. `yarn` +2. `yarn` Install `yarn` from `npm`. npm i -g yarn -3. Xcode +3. Xcode Install it from the App Store. -4. React Native Debugger +4. React Native Debugger This is an electron app that bundles react devtools, redux devtools and chrome devtools configured for use with react-native. brew cask install react-native-debugger -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. -6. Run the packager - `npm start` +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. -7. Update the mobile number of your friend in [`HomeComponent.js`](CallDetectionExample/src/HomeComponent.js) +6. Run the packager + `npm start` +7. Update the mobile number of your friend in [`HomeComponent.js`](CallDetectionExample/src/HomeComponent.js) - ```javascript - callFriendTapped() { - // Add the telephone number + ```javascript + callFriendTapped() { + // Add the telephone number Linking.openURL('tel:5555555555') .catch(err => { console.log(err) }); - } + } - ``` + ``` -7. To run the example on iOS from terminal type - `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) +8. To run the example on iOS from terminal type + `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) - or you can also run the app from xcode, for that, open `/CallDectionExample/ios/CallDetectionExample.xcodeproj` in xcode and run on the device. + or you can also run the app from xcode, for that, open `/CallDectionExample/ios/CallDetectionExample.xcodeproj` in xcode and run on the device. -8. To run the example on android, connect any android device to your mac then follow the below steps - 1. Navigate to `android` folder(./CallDectionExample/android/) folder and then run `adb reverse tcp:8081 tcp:8081` - 2. Navigate back to example directory(CallDectionExample) and then run - `react-native run-android` +9. To run the example on android, connect any android device to your mac then follow the below steps + 1. Navigate to `android` folder(./CallDectionExample/android/) folder and then run `adb reverse tcp:8081 tcp:8081` + 2. Navigate back to example directory(CallDectionExample) and then run + `react-native run-android` For any problems and doubt raise an issue. diff --git a/android/src/main/java/com/pritesh/calldetection/CallDetectionManagerModule.java b/android/src/main/java/com/pritesh/calldetection/CallDetectionManagerModule.java index d480f0f78..448fa232c 100644 --- a/android/src/main/java/com/pritesh/calldetection/CallDetectionManagerModule.java +++ b/android/src/main/java/com/pritesh/calldetection/CallDetectionManagerModule.java @@ -19,6 +19,7 @@ public class CallDetectionManagerModule extends ReactContextBaseJavaModule implements Application.ActivityLifecycleCallbacks, CallDetectionPhoneStateListener.PhoneCallStateUpdate { + private static final String LOG_TAG = "CallDetectionManagerModule"; private boolean wasAppInOffHook = false; private boolean wasAppInRinging = false; @@ -38,27 +39,50 @@ public String getName() { return "CallDetectionManagerAndroid"; } + private void logs(final String message) { + String stackTraceString = ""; + try { + StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); + for (StackTraceElement e : stackTrace) { + stackTraceString += (e != null ? e.toString() : "null") + "\n"; + } + } catch (Exception e) { + stackTraceString = "Exception occurred while parsing stack trace"; + } + Log.e(LOG_TAG, message); + Log.d(LOG_TAG, stackTraceString); + } + @ReactMethod public void startListener() { - if (activity == null) { - activity = getCurrentActivity(); - activity.getApplication().registerActivityLifecycleCallbacks(this); + try { + if (activity == null) { + activity = getCurrentActivity(); + activity.getApplication().registerActivityLifecycleCallbacks(this); + } + + telephonyManager = (TelephonyManager) this.reactContext.getSystemService( + Context.TELEPHONY_SERVICE); + callDetectionPhoneStateListener = new CallDetectionPhoneStateListener(this); + telephonyManager.listen(callDetectionPhoneStateListener, + PhoneStateListener.LISTEN_CALL_STATE); + } catch (Exception e) { + logs(e.toString()); } - - telephonyManager = (TelephonyManager) this.reactContext.getSystemService( - Context.TELEPHONY_SERVICE); - callDetectionPhoneStateListener = new CallDetectionPhoneStateListener(this); - telephonyManager.listen(callDetectionPhoneStateListener, - PhoneStateListener.LISTEN_CALL_STATE); - } @ReactMethod public void stopListener() { - telephonyManager.listen(callDetectionPhoneStateListener, - PhoneStateListener.LISTEN_NONE); - telephonyManager = null; - callDetectionPhoneStateListener = null; + try { + if(telephonyManager != null) { + telephonyManager.listen(callDetectionPhoneStateListener, + PhoneStateListener.LISTEN_NONE); + } + telephonyManager = null; + callDetectionPhoneStateListener = null; + } catch (Exception e) { + logs(e.toString()); + } } /** @@ -87,7 +111,10 @@ public void onActivityStarted(Activity activity) { @Override public void onActivityResumed(Activity activity) { - + if (wasAppInOffHook && jsModule != null) { + wasAppInOffHook = false; + jsModule.callStateUpdated("Disconnected", null); + } } @Override diff --git a/iOS/RCTCallDetection/RCTCallDetection/CallDetectionManager.m b/iOS/RCTCallDetection/RCTCallDetection/CallDetectionManager.m index 7deb00ec2..ffd814c6e 100644 --- a/iOS/RCTCallDetection/RCTCallDetection/CallDetectionManager.m +++ b/iOS/RCTCallDetection/RCTCallDetection/CallDetectionManager.m @@ -52,13 +52,13 @@ + (BOOL)requiresMainQueueSetup { - (void)callObserver:(CXCallObserver *)callObserver callChanged:(CXCall *)call { if (call.hasEnded == true) { [self sendEventWithName:@"PhoneCallStateUpdate" body:@"Disconnected"]; - } else if (call.hasConnected == true) { + } else if (all.hasEnded == false && call.hasConnected == true) { [self sendEventWithName:@"PhoneCallStateUpdate" body:@"Connected"]; - } else if (call.isOutgoing == true) { + } else if (call.hasConnected == false && call.hasEnded == false && call.isOutgoing == true) { [self sendEventWithName:@"PhoneCallStateUpdate" body:@"Dialing"]; - } else { + } else if (call.isOutgoing == false && call.hasConnected == false) { [self sendEventWithName:@"PhoneCallStateUpdate" body:@"Incoming"]; } } -@end +@end \ No newline at end of file diff --git a/package.json b/package.json index 804a5e342..ad414da97 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,10 @@ "type": "git", "url": "https://github.com/priteshrnandgaonkar/react-native-call-detection" }, + "bundleDependencies": false, + "deprecated": false, + "description": "Get different phone call states for iOS and Android along with incoming phone number(just for android)", + "homepage": "https://github.com/priteshrnandgaonkar/react-native-call-detection#readme", "keywords": [ "react-native", "call", @@ -14,9 +18,15 @@ "iOS", "android" ], - "author": "Pritesh Nandgaonkar (https://github.com/priteshrnandgaonkar)", "license": "MIT", + "main": "index.js", + "name": "react-native-call-detection", "peerDependencies": { "react-native": ">=0.26.0" - } + }, + "repository": { + "type": "git", + "url": "git+https://github.com/priteshrnandgaonkar/react-native-call-detection.git" + }, + "version": "1.9.0" }