From a2eefc11f94d0a89658719324f59bedf3125b783 Mon Sep 17 00:00:00 2001 From: Kesha Antonov Date: Mon, 16 Oct 2017 08:08:46 +0300 Subject: [PATCH 1/2] use wifi info only for 2 funcs --- .../learnium/RNDeviceInfo/RNDeviceModule.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java b/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java index eed7325bb..d4022ed18 100644 --- a/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java +++ b/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java @@ -40,10 +40,6 @@ public RNDeviceModule(ReactApplicationContext reactContext) { super(reactContext); this.reactContext = reactContext; - - - WifiManager manager = (WifiManager) reactContext.getSystemService(Context.WIFI_SERVICE); - this.wifiInfo = manager.getConnectionInfo(); } @Override @@ -51,6 +47,14 @@ public String getName() { return "RNDeviceInfo"; } + private WifiInfo getWifiInfo() { + if ( this.wifiInfo == null ) { + WifiManager manager = (WifiManager) reactContext.getSystemService(Context.WIFI_SERVICE); + this.wifiInfo = manager.getConnectionInfo(); + } + return this.wifiInfo; + } + private String getCurrentLanguage() { Locale current = getReactApplicationContext().getResources().getConfiguration().locale; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { @@ -95,13 +99,13 @@ public void isPinOrFingerprintSet(Callback callback) { @ReactMethod public void getIpAddress(Promise p) { - String ipAddress = Formatter.formatIpAddress(wifiInfo.getIpAddress()); + String ipAddress = Formatter.formatIpAddress(getWifiInfo().getIpAddress()); p.resolve(ipAddress); } @ReactMethod public void getMacAddress(Promise p) { - String macAddress = wifiInfo.getMacAddress(); + String macAddress = getWifiInfo().getMacAddress(); p.resolve(macAddress); } From 88e6ba6452dac3a82f77a3d7549bc9e182e1661d Mon Sep 17 00:00:00 2001 From: Kesha Antonov Date: Mon, 16 Oct 2017 10:58:03 +0300 Subject: [PATCH 2/2] fix: Error: The WIFI_SERVICE must be looked up on the Application context or memory will leak on devices < Android N. Try changing reactContext to reactContext.getApplicationContext() [WifiManagerLeak] WifiManager manager = (WifiManager) reactContext.getSystemService(Context.WIFI_SERVICE); --- .../main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java b/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java index d4022ed18..5fee2e294 100644 --- a/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java +++ b/android/src/main/java/com/learnium/RNDeviceInfo/RNDeviceModule.java @@ -49,7 +49,7 @@ public String getName() { private WifiInfo getWifiInfo() { if ( this.wifiInfo == null ) { - WifiManager manager = (WifiManager) reactContext.getSystemService(Context.WIFI_SERVICE); + WifiManager manager = (WifiManager) reactContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE); this.wifiInfo = manager.getConnectionInfo(); } return this.wifiInfo; @@ -93,7 +93,7 @@ private Boolean isTablet() { @ReactMethod public void isPinOrFingerprintSet(Callback callback) { - KeyguardManager keyguardManager = (KeyguardManager) this.reactContext.getSystemService(Context.KEYGUARD_SERVICE); //api 16+ + KeyguardManager keyguardManager = (KeyguardManager) this.reactContext.getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE); //api 16+ callback.invoke(keyguardManager.isKeyguardSecure()); } @@ -165,7 +165,7 @@ public void getMacAddress(Promise p) { (getCurrentActivity().checkCallingOrSelfPermission(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED || getCurrentActivity().checkCallingOrSelfPermission(Manifest.permission.READ_SMS) == PackageManager.PERMISSION_GRANTED || getCurrentActivity().checkCallingOrSelfPermission("android.permission.READ_PHONE_NUMBERS") == PackageManager.PERMISSION_GRANTED)) { - TelephonyManager telMgr = (TelephonyManager) this.reactContext.getSystemService(Context.TELEPHONY_SERVICE); + TelephonyManager telMgr = (TelephonyManager) this.reactContext.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE); constants.put("phoneNumber", telMgr.getLine1Number()); } return constants;