From c4b9ca3d7c4f0c397614b0987e89e77e00b67205 Mon Sep 17 00:00:00 2001 From: Talia Segev Date: Mon, 16 Apr 2018 13:45:32 +0300 Subject: [PATCH 1/3] add failure callback for TelephonyModule --- .../java/com/telephony/TelephonyModule.java | 316 +++++++++--------- 1 file changed, 167 insertions(+), 149 deletions(-) diff --git a/android/app/src/main/java/com/telephony/TelephonyModule.java b/android/app/src/main/java/com/telephony/TelephonyModule.java index 445d50d..90ac5ed 100644 --- a/android/app/src/main/java/com/telephony/TelephonyModule.java +++ b/android/app/src/main/java/com/telephony/TelephonyModule.java @@ -97,136 +97,146 @@ private void sendEvent(String eventName, Object params) { @ReactMethod public void startListener(int events) { telephonyPhoneStateListener = new TelephonyListener(this); - telephonyManager.listen(telephonyPhoneStateListener, events); - + if (telephonyManager != null) { + telephonyManager.listen(telephonyPhoneStateListener, events); + } } @ReactMethod public void stopListener() { - telephonyManager.listen(telephonyPhoneStateListener, - PhoneStateListener.LISTEN_NONE); - telephonyManager = null; + if (telephonyManager != null) { + telephonyManager.listen(telephonyPhoneStateListener, + PhoneStateListener.LISTEN_NONE); + telephonyManager = null; + } telephonyPhoneStateListener = null; } @ReactMethod - public void isNetworkRoaming(Callback successCallback) { + public void isNetworkRoaming(Callback successCallback, Callback failureCallback) { + if (telephonyManager == null) { + failureCallback.invoke("telephonyManager is null"); + return; + } successCallback.invoke(telephonyManager.isNetworkRoaming()); } @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) @ReactMethod - public void getCellInfo(Callback successCallback) { - - WritableArray mapArray = Arguments.createArray(); + public void getCellInfo(Callback successCallback, Callback failureCallback) { + try { + WritableArray mapArray = Arguments.createArray(); - List cellInfo = telephonyManager.getAllCellInfo(); - - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1 || cellInfo == null) { - successCallback.invoke(mapArray); - return; - } - - int i = 0; + List cellInfo = telephonyManager.getAllCellInfo(); - for (CellInfo info : cellInfo) { - WritableMap mapCellIdentity = Arguments.createMap(); - WritableMap mapCellSignalStrength = Arguments.createMap(); - WritableMap map = Arguments.createMap(); - - map.putInt("key", i); - - if (info instanceof CellInfoGsm) { - CellIdentityGsm cellIdentity = ((CellInfoGsm) info).getCellIdentity(); - map.putString("connectionType", "GSM"); - - mapCellIdentity.putInt("cid", cellIdentity.getCid()); - mapCellIdentity.putInt("lac", cellIdentity.getLac()); - mapCellIdentity.putInt("mcc", cellIdentity.getMcc()); - mapCellIdentity.putInt("mnc", cellIdentity.getMnc()); - mapCellIdentity.putInt("psc", cellIdentity.getPsc()); - - CellSignalStrengthGsm cellSignalStrengthGsm = ((CellInfoGsm) info).getCellSignalStrength(); - mapCellSignalStrength.putInt("asuLevel", cellSignalStrengthGsm.getAsuLevel()); - mapCellSignalStrength.putInt("dBm", cellSignalStrengthGsm.getDbm()); - mapCellSignalStrength.putInt("level", cellSignalStrengthGsm.getLevel()); - } else if (info instanceof CellInfoWcdma && android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { - CellIdentityWcdma cellIdentity = ((CellInfoWcdma) info).getCellIdentity(); - map.putString("connectionType", "WCDMA"); - - mapCellIdentity.putInt("cid", cellIdentity.getCid()); - mapCellIdentity.putInt("lac", cellIdentity.getLac()); - mapCellIdentity.putInt("mcc", cellIdentity.getMcc()); - mapCellIdentity.putInt("mnc", cellIdentity.getMnc()); - mapCellIdentity.putInt("psc", cellIdentity.getPsc()); - - CellSignalStrengthWcdma cellSignalStrengthWcdma = ((CellInfoWcdma) info).getCellSignalStrength(); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1 || cellInfo == null) { + successCallback.invoke(mapArray); + return; + } - mapCellSignalStrength.putInt("asuLevel", cellSignalStrengthWcdma.getAsuLevel()); - mapCellSignalStrength.putInt("dBm", cellSignalStrengthWcdma.getDbm()); - mapCellSignalStrength.putInt("level", cellSignalStrengthWcdma.getLevel()); - } else if (info instanceof CellInfoLte) { - if(info.isRegistered()) { - mapCellIdentity.putBoolean("servingCellFlag", info.isRegistered()); - } else { - mapCellIdentity.putBoolean("servingCellFlag", info.isRegistered()); + int i = 0; + + for (CellInfo info : cellInfo) { + WritableMap mapCellIdentity = Arguments.createMap(); + WritableMap mapCellSignalStrength = Arguments.createMap(); + WritableMap map = Arguments.createMap(); + + map.putInt("key", i); + + if (info instanceof CellInfoGsm) { + CellIdentityGsm cellIdentity = ((CellInfoGsm) info).getCellIdentity(); + map.putString("connectionType", "GSM"); + + mapCellIdentity.putInt("cid", cellIdentity.getCid()); + mapCellIdentity.putInt("lac", cellIdentity.getLac()); + mapCellIdentity.putInt("mcc", cellIdentity.getMcc()); + mapCellIdentity.putInt("mnc", cellIdentity.getMnc()); + mapCellIdentity.putInt("psc", cellIdentity.getPsc()); + + CellSignalStrengthGsm cellSignalStrengthGsm = ((CellInfoGsm) info).getCellSignalStrength(); + mapCellSignalStrength.putInt("asuLevel", cellSignalStrengthGsm.getAsuLevel()); + mapCellSignalStrength.putInt("dBm", cellSignalStrengthGsm.getDbm()); + mapCellSignalStrength.putInt("level", cellSignalStrengthGsm.getLevel()); + } else if (info instanceof CellInfoWcdma && android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + CellIdentityWcdma cellIdentity = ((CellInfoWcdma) info).getCellIdentity(); + map.putString("connectionType", "WCDMA"); + + mapCellIdentity.putInt("cid", cellIdentity.getCid()); + mapCellIdentity.putInt("lac", cellIdentity.getLac()); + mapCellIdentity.putInt("mcc", cellIdentity.getMcc()); + mapCellIdentity.putInt("mnc", cellIdentity.getMnc()); + mapCellIdentity.putInt("psc", cellIdentity.getPsc()); + + CellSignalStrengthWcdma cellSignalStrengthWcdma = ((CellInfoWcdma) info).getCellSignalStrength(); + + mapCellSignalStrength.putInt("asuLevel", cellSignalStrengthWcdma.getAsuLevel()); + mapCellSignalStrength.putInt("dBm", cellSignalStrengthWcdma.getDbm()); + mapCellSignalStrength.putInt("level", cellSignalStrengthWcdma.getLevel()); + } else if (info instanceof CellInfoLte) { + if (info.isRegistered()) { + mapCellIdentity.putBoolean("servingCellFlag", info.isRegistered()); + } else { + mapCellIdentity.putBoolean("servingCellFlag", info.isRegistered()); + } + CellIdentityLte cellIdentity = ((CellInfoLte) info).getCellIdentity(); + map.putString("connectionType", "LTE"); + + mapCellIdentity.putInt("cid", cellIdentity.getCi()); + mapCellIdentity.putInt("tac", cellIdentity.getTac()); + mapCellIdentity.putInt("mcc", cellIdentity.getMcc()); + mapCellIdentity.putInt("mnc", cellIdentity.getMnc()); + mapCellIdentity.putInt("pci", cellIdentity.getPci()); + getEarfcn(mapCellIdentity, cellIdentity); + + String cellIdHex = decToHex(cellIdentity.getCi()); + boolean cellIdHexIsEmpty = cellIdHex == null || cellIdHex.isEmpty() || cellIdHex.length() < 2; + String eNodeBHex = cellIdHexIsEmpty ? "0" : cellIdHex.substring(0, cellIdHex.length() - 2); + mapCellIdentity.putInt("eNodeB", hexToDec(eNodeBHex)); + String localCellIdHex = cellIdHexIsEmpty ? "0" : cellIdHex.substring(cellIdHex.length() - 2, cellIdHex.length()); + mapCellIdentity.putInt("localCellId", hexToDec(localCellIdHex)); + + CellSignalStrengthLte cellSignalStrengthLte = ((CellInfoLte) info).getCellSignalStrength(); + + mapCellSignalStrength.putInt("asuLevel", cellSignalStrengthLte.getAsuLevel()); + mapCellSignalStrength.putInt("dBm", cellSignalStrengthLte.getDbm()); + mapCellSignalStrength.putInt("level", cellSignalStrengthLte.getLevel()); + mapCellSignalStrength.putInt("timingAdvance", cellSignalStrengthLte.getTimingAdvance()); + + } else if (info instanceof CellInfoCdma) { + CellIdentityCdma cellIdentity = ((CellInfoCdma) info).getCellIdentity(); + map.putString("connectionType", "CDMA"); + + mapCellIdentity.putInt("basestationId", cellIdentity.getBasestationId()); + mapCellIdentity.putInt("latitude", cellIdentity.getLatitude()); + mapCellIdentity.putInt("longitude", cellIdentity.getLongitude()); + mapCellIdentity.putInt("networkId", cellIdentity.getNetworkId()); + mapCellIdentity.putInt("systemId", cellIdentity.getSystemId()); + + CellSignalStrengthCdma cellSignalStrengthCdma = ((CellInfoCdma) info).getCellSignalStrength(); + + mapCellSignalStrength.putInt("asuLevel", cellSignalStrengthCdma.getAsuLevel()); + mapCellSignalStrength.putInt("cmdaDbm", cellSignalStrengthCdma.getCdmaDbm()); + mapCellSignalStrength.putInt("cmdaEcio", cellSignalStrengthCdma.getCdmaEcio()); + mapCellSignalStrength.putInt("cmdaLevl", cellSignalStrengthCdma.getCdmaLevel()); + mapCellSignalStrength.putInt("dBm", cellSignalStrengthCdma.getDbm()); + mapCellSignalStrength.putInt("evdoDbm", cellSignalStrengthCdma.getEvdoDbm()); + mapCellSignalStrength.putInt("evdoEcio", cellSignalStrengthCdma.getEvdoEcio()); + mapCellSignalStrength.putInt("evdoLevel", cellSignalStrengthCdma.getEvdoLevel()); + mapCellSignalStrength.putInt("evdoSnr", cellSignalStrengthCdma.getEvdoSnr()); + mapCellSignalStrength.putInt("level", cellSignalStrengthCdma.getLevel()); } - CellIdentityLte cellIdentity = ((CellInfoLte) info).getCellIdentity(); - map.putString("connectionType", "LTE"); - - mapCellIdentity.putInt("cid", cellIdentity.getCi()); - mapCellIdentity.putInt("tac", cellIdentity.getTac()); - mapCellIdentity.putInt("mcc", cellIdentity.getMcc()); - mapCellIdentity.putInt("mnc", cellIdentity.getMnc()); - mapCellIdentity.putInt("pci", cellIdentity.getPci()); - getEarfcn(mapCellIdentity, cellIdentity); - - String cellIdHex = decToHex(cellIdentity.getCi()); - boolean cellIdHexIsEmpty = cellIdHex == null || cellIdHex.isEmpty() || cellIdHex.length() < 2; - String eNodeBHex = cellIdHexIsEmpty ? "0" : cellIdHex.substring(0, cellIdHex.length() - 2); - mapCellIdentity.putInt("eNodeB", hexToDec(eNodeBHex)); - String localCellIdHex = cellIdHexIsEmpty ? "0" : cellIdHex.substring(cellIdHex.length() - 2, cellIdHex.length()); - mapCellIdentity.putInt("localCellId", hexToDec(localCellIdHex)); - - CellSignalStrengthLte cellSignalStrengthLte = ((CellInfoLte) info).getCellSignalStrength(); - - mapCellSignalStrength.putInt("asuLevel", cellSignalStrengthLte.getAsuLevel()); - mapCellSignalStrength.putInt("dBm", cellSignalStrengthLte.getDbm()); - mapCellSignalStrength.putInt("level", cellSignalStrengthLte.getLevel()); - mapCellSignalStrength.putInt("timingAdvance", cellSignalStrengthLte.getTimingAdvance()); - - } else if (info instanceof CellInfoCdma) { - CellIdentityCdma cellIdentity = ((CellInfoCdma) info).getCellIdentity(); - map.putString("connectionType", "CDMA"); - - mapCellIdentity.putInt("basestationId", cellIdentity.getBasestationId()); - mapCellIdentity.putInt("latitude", cellIdentity.getLatitude()); - mapCellIdentity.putInt("longitude", cellIdentity.getLongitude()); - mapCellIdentity.putInt("networkId", cellIdentity.getNetworkId()); - mapCellIdentity.putInt("systemId", cellIdentity.getSystemId()); - CellSignalStrengthCdma cellSignalStrengthCdma = ((CellInfoCdma) info).getCellSignalStrength(); + map.putMap("cellIdentity", mapCellIdentity); + map.putMap("cellSignalStrength", mapCellSignalStrength); - mapCellSignalStrength.putInt("asuLevel", cellSignalStrengthCdma.getAsuLevel()); - mapCellSignalStrength.putInt("cmdaDbm", cellSignalStrengthCdma.getCdmaDbm()); - mapCellSignalStrength.putInt("cmdaEcio", cellSignalStrengthCdma.getCdmaEcio()); - mapCellSignalStrength.putInt("cmdaLevl", cellSignalStrengthCdma.getCdmaLevel()); - mapCellSignalStrength.putInt("dBm", cellSignalStrengthCdma.getDbm()); - mapCellSignalStrength.putInt("evdoDbm", cellSignalStrengthCdma.getEvdoDbm()); - mapCellSignalStrength.putInt("evdoEcio", cellSignalStrengthCdma.getEvdoEcio()); - mapCellSignalStrength.putInt("evdoLevel", cellSignalStrengthCdma.getEvdoLevel()); - mapCellSignalStrength.putInt("evdoSnr", cellSignalStrengthCdma.getEvdoSnr()); - mapCellSignalStrength.putInt("level", cellSignalStrengthCdma.getLevel()); + mapArray.pushMap(map); + i++; } - map.putMap("cellIdentity", mapCellIdentity); - map.putMap("cellSignalStrength", mapCellSignalStrength); - - mapArray.pushMap(map); - i++; + successCallback.invoke(mapArray); + } catch (Exception e) { + failureCallback.invoke(e.getMessage()); } - - successCallback.invoke(mapArray); } @TargetApi(24) @@ -241,51 +251,59 @@ public void getEarfcn(WritableMap mapCellIdentity, CellIdentityLte cellIdentity) } @ReactMethod - public void getPhoneInfo(Callback successCallBack) { - WritableMap mapPhoneInfo = Arguments.createMap(); - - mapPhoneInfo.putString("imsi", telephonyManager.getSubscriberId().toString()); - mapPhoneInfo.putString("imei", telephonyManager.getDeviceId()); - mapPhoneInfo.putString("mdn", telephonyManager.getLine1Number()); - mapPhoneInfo.putString("model", Build.MANUFACTURER - + " " + Build.MODEL + " " + Build.VERSION.RELEASE - + " " + Build.VERSION_CODES.class.getFields()[Build.VERSION.SDK_INT].getName()); - successCallBack.invoke(mapPhoneInfo); + public void getPhoneInfo(Callback successCallBack, Callback failureCallback) { + try { + WritableMap mapPhoneInfo = Arguments.createMap(); + + mapPhoneInfo.putString("imsi", telephonyManager.getSubscriberId().toString()); + mapPhoneInfo.putString("imei", telephonyManager.getDeviceId()); + mapPhoneInfo.putString("mdn", telephonyManager.getLine1Number()); + mapPhoneInfo.putString("model", Build.MANUFACTURER + + " " + Build.MODEL + " " + Build.VERSION.RELEASE + + " " + Build.VERSION_CODES.class.getFields()[Build.VERSION.SDK_INT].getName()); + successCallBack.invoke(mapPhoneInfo); + } catch (Exception e) { + failureCallback.invoke(e.getMessage()); + } } @ReactMethod - public void getNetworkClass(Callback successCallback) { - int networkType = telephonyManager.getNetworkType(); - String network; - - switch (networkType) { - case TelephonyManager.NETWORK_TYPE_GPRS: - case TelephonyManager.NETWORK_TYPE_EDGE: - case TelephonyManager.NETWORK_TYPE_CDMA: - case TelephonyManager.NETWORK_TYPE_1xRTT: - case TelephonyManager.NETWORK_TYPE_IDEN: - network = "2G"; - break; - case TelephonyManager.NETWORK_TYPE_UMTS: - case TelephonyManager.NETWORK_TYPE_EVDO_0: - case TelephonyManager.NETWORK_TYPE_EVDO_A: - case TelephonyManager.NETWORK_TYPE_HSDPA: - case TelephonyManager.NETWORK_TYPE_HSUPA: - case TelephonyManager.NETWORK_TYPE_HSPA: - case TelephonyManager.NETWORK_TYPE_EVDO_B: - case TelephonyManager.NETWORK_TYPE_EHRPD: - case TelephonyManager.NETWORK_TYPE_HSPAP: - network = "3G"; - break; - case TelephonyManager.NETWORK_TYPE_LTE: - network = "4G"; - break; - default: - network = "Unknown"; - break; - } + public void getNetworkClass(Callback successCallback, Callback failureCallback) { + try { + int networkType = telephonyManager.getNetworkType(); + String network; + + switch (networkType) { + case TelephonyManager.NETWORK_TYPE_GPRS: + case TelephonyManager.NETWORK_TYPE_EDGE: + case TelephonyManager.NETWORK_TYPE_CDMA: + case TelephonyManager.NETWORK_TYPE_1xRTT: + case TelephonyManager.NETWORK_TYPE_IDEN: + network = "2G"; + break; + case TelephonyManager.NETWORK_TYPE_UMTS: + case TelephonyManager.NETWORK_TYPE_EVDO_0: + case TelephonyManager.NETWORK_TYPE_EVDO_A: + case TelephonyManager.NETWORK_TYPE_HSDPA: + case TelephonyManager.NETWORK_TYPE_HSUPA: + case TelephonyManager.NETWORK_TYPE_HSPA: + case TelephonyManager.NETWORK_TYPE_EVDO_B: + case TelephonyManager.NETWORK_TYPE_EHRPD: + case TelephonyManager.NETWORK_TYPE_HSPAP: + network = "3G"; + break; + case TelephonyManager.NETWORK_TYPE_LTE: + network = "4G"; + break; + default: + network = "Unknown"; + break; + } - successCallback.invoke(network); + successCallback.invoke(network); + } catch (Exception e) { + failureCallback.invoke(e.getMessage()); + } } @Override From 1f906dd0b2f8274f595aeda9920e1ffe01bf3e56 Mon Sep 17 00:00:00 2001 From: Talia Segev Date: Mon, 16 Apr 2018 13:54:38 +0300 Subject: [PATCH 2/3] 2.0.0 --- package.json | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index b85d6f2..6b8902e 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { - "name": "react-native-telephony", - "version": "1.5.0", - "description": "Android Telephony for React Native", - "main": "index.js", + "name": "react-native-telephony", + "version": "2.0.0", + "description": "Android Telephony for React Native", + "main": "index.js", "scripts": { "test": "jest" }, @@ -24,9 +24,9 @@ "license": "MIT", "devDependencies": { "babel-jest": "20.0.3", - "babel-preset-react-native": "2.0.0", - "eslint": "^4.7.0", - "eslint-plugin-react": "^7.3.0", + "babel-preset-react-native": "2.0.0", + "eslint": "^4.7.0", + "eslint-plugin-react": "^7.3.0", "jest": "^20.0.4", "react-test-renderer": "^16.0.0-rc.2", "react": "^16.0", From 0adfc616360f4a297d088014407c8f9d6b44d71d Mon Sep 17 00:00:00 2001 From: Talia Segev Date: Tue, 1 May 2018 14:40:24 +0300 Subject: [PATCH 3/3] add try catch around telephonyManager addListener --- .../java/com/telephony/TelephonyModule.java | 24 +++++++------ index.js | 36 ++++++++++--------- package.json | 2 +- 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/android/app/src/main/java/com/telephony/TelephonyModule.java b/android/app/src/main/java/com/telephony/TelephonyModule.java index 90ac5ed..732a7d8 100644 --- a/android/app/src/main/java/com/telephony/TelephonyModule.java +++ b/android/app/src/main/java/com/telephony/TelephonyModule.java @@ -96,20 +96,24 @@ private void sendEvent(String eventName, Object params) { @ReactMethod public void startListener(int events) { - telephonyPhoneStateListener = new TelephonyListener(this); - if (telephonyManager != null) { - telephonyManager.listen(telephonyPhoneStateListener, events); - } + try { + telephonyPhoneStateListener = new TelephonyListener(this); + if (telephonyManager != null) { + telephonyManager.listen(telephonyPhoneStateListener, events); + } + } catch (Exception e) {} } @ReactMethod public void stopListener() { - if (telephonyManager != null) { - telephonyManager.listen(telephonyPhoneStateListener, - PhoneStateListener.LISTEN_NONE); - telephonyManager = null; - } - telephonyPhoneStateListener = null; + try { + if (telephonyManager != null) { + telephonyManager.listen(telephonyPhoneStateListener, + PhoneStateListener.LISTEN_NONE); + telephonyManager = null; + } + telephonyPhoneStateListener = null; + } catch (Exception e) {} } @ReactMethod diff --git a/index.js b/index.js index f0e8be4..3d19078 100644 --- a/index.js +++ b/index.js @@ -11,24 +11,26 @@ const PHONE_STATE_LISTENER = 'Telephony-PhoneStateListener' const telephony = RNTelephony -if (Platform.OS === 'android' && telephony) { - telephony.addEventListener = (events, handler) => { - RNTelephony && RNTelephony.startListener(events) - NativeAppEventEmitter.addListener( - PHONE_STATE_LISTENER, - (result) => { - handler(result); - } - ); - } +if (Platform.OS === 'android' && telephony) { + try { + telephony.addEventListener = (events, handler) => { + RNTelephony && RNTelephony.startListener(events) + NativeAppEventEmitter.addListener( + PHONE_STATE_LISTENER, + (result) => { + handler(result); + } + ); + } - telephony.removeEventListener = () => { - RNTelephony && RNTelephony.stopListener() - NativeAppEventEmitter.removeEventListener( - PHONE_STATE_LISTENER, - () => {} - ); - } + telephony.removeEventListener = () => { + RNTelephony && RNTelephony.stopListener() + NativeAppEventEmitter.removeEventListener( + PHONE_STATE_LISTENER, + () => {} + ); + } + } catch (e) { } } export default telephony \ No newline at end of file diff --git a/package.json b/package.json index 6b8902e..5490cbd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-telephony", - "version": "2.0.0", + "version": "2.1.0", "description": "Android Telephony for React Native", "main": "index.js", "scripts": {