From 8250000e568683a81001795a644977bac9e84581 Mon Sep 17 00:00:00 2001 From: Fredrik Fornwall Date: Fri, 15 Jun 2018 00:00:15 +0200 Subject: [PATCH] Guard against possible leak pre android-24 From Android Studio lint: On versions prior to Android N (24), initializing the WifiManager via Context#getSystemService can cause a memory leak if the context is not the application context. In many cases, it's not obvious from the code where the Context is coming from (e.g. it might be a parameter to a method, or a field initialized from various method calls). It's possible that the context being passed in is the application context, but to be on the safe side, you should consider changing context.getSystemService(...) to context.getApplicationContext().getSystemService(...). --- app/src/main/java/com/termux/api/WifiAPI.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/termux/api/WifiAPI.java b/app/src/main/java/com/termux/api/WifiAPI.java index 739c5c72..37c77436 100644 --- a/app/src/main/java/com/termux/api/WifiAPI.java +++ b/app/src/main/java/com/termux/api/WifiAPI.java @@ -21,7 +21,7 @@ static void onReceiveWifiConnectionInfo(TermuxApiReceiver apiReceiver, final Con ResultReturner.returnData(apiReceiver, intent, new ResultReturner.ResultJsonWriter() { @Override public void writeJson(JsonWriter out) throws Exception { - WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); + WifiManager manager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); WifiInfo info = manager.getConnectionInfo(); out.beginObject(); if (info == null) { @@ -53,7 +53,7 @@ static void onReceiveWifiScanInfo(TermuxApiReceiver apiReceiver, final Context c ResultReturner.returnData(apiReceiver, intent, new ResultReturner.ResultJsonWriter() { @Override public void writeJson(JsonWriter out) throws Exception { - WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); + WifiManager manager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); List scans = manager.getScanResults(); if (scans == null) { out.beginObject().name("API_ERROR").value("Failed getting scan results").endObject();