Skip to content

Commit

Permalink
Guard against possible leak pre android-24
Browse files Browse the repository at this point in the history
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(...).
  • Loading branch information
fornwall committed Jun 14, 2018
1 parent 905650d commit 8250000
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/termux/api/WifiAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<ScanResult> scans = manager.getScanResults();
if (scans == null) {
out.beginObject().name("API_ERROR").value("Failed getting scan results").endObject();
Expand Down

0 comments on commit 8250000

Please sign in to comment.