From a4f3efdb60a5685327463c4e386663e26c2bba7f Mon Sep 17 00:00:00 2001 From: Joe LaPenna Date: Fri, 16 Dec 2016 23:20:41 -0800 Subject: Fix NPE reading scan results. Didn't seem possible at first glance of the code, but it looks like getScanResults might return null on occasion. BUG: 33710559 Test: adb shell am instrument -w com.android.networkrecommendation.tests/android.support.test.runner.AndroidJUnitRunner Change-Id: Ic5d0ef49d0770e6a11d465c44d1c3c0ebe52a340 --- .../DefaultNetworkRecommendationService.java | 64 ++++++++++++---------- 1 file changed, 34 insertions(+), 30 deletions(-) (limited to 'src/com/android') diff --git a/src/com/android/networkrecommendation/DefaultNetworkRecommendationService.java b/src/com/android/networkrecommendation/DefaultNetworkRecommendationService.java index 325ac85..972b35a 100644 --- a/src/com/android/networkrecommendation/DefaultNetworkRecommendationService.java +++ b/src/com/android/networkrecommendation/DefaultNetworkRecommendationService.java @@ -173,37 +173,41 @@ public class DefaultNetworkRecommendationService extends Service { int recommendedScore = Integer.MIN_VALUE; ScanResult[] results = request.getScanResults(); - for (int i = 0; i < results.length; i++) { - final ScanResult scanResult = results[i]; - if (VERBOSE) Log.v(TAG, "Scan: " + scanResult + " " + i); - - // We only want to recommend open networks. This check is taken from - // places like WifiNotificationController and will be extracted to ScanResult in - // a future CL. - if (!"[ESS]".equals(scanResult.capabilities)) { - if (VERBOSE) Log.v(TAG, "Discarding closed network: " + scanResult); - continue; - } - - final NetworkKey networkKey = new NetworkKey( - new WifiKey(quoteSsid(scanResult), scanResult.BSSID)); - if (VERBOSE) Log.v(TAG, "Evaluating network: " + networkKey); - - // We will only score networks we know about. - final ScoredNetwork network = mStorage.get(networkKey); - if (network == null) { - if (VERBOSE) Log.v(TAG, "Discarding unscored network: " + scanResult); - continue; - } - - final int score = network.rssiCurve.lookupScore(scanResult.level); - if (VERBOSE) Log.d(TAG, "Scored " + scanResult + ": " + score); - if (score > recommendedScore) { - recommendedScanResult = scanResult; - recommendedScore = score; - if (VERBOSE) Log.d(TAG, "New recommended network: " + scanResult); - continue; + if (results != null) { + for (int i = 0; i < results.length; i++) { + final ScanResult scanResult = results[i]; + if (VERBOSE) Log.v(TAG, "Scan: " + scanResult + " " + i); + + // We only want to recommend open networks. This check is taken from + // places like WifiNotificationController and will be extracted to ScanResult in + // a future CL. + if (!"[ESS]".equals(scanResult.capabilities)) { + if (VERBOSE) Log.v(TAG, "Discarding closed network: " + scanResult); + continue; + } + + final NetworkKey networkKey = new NetworkKey( + new WifiKey(quoteSsid(scanResult), scanResult.BSSID)); + if (VERBOSE) Log.v(TAG, "Evaluating network: " + networkKey); + + // We will only score networks we know about. + final ScoredNetwork network = mStorage.get(networkKey); + if (network == null) { + if (VERBOSE) Log.v(TAG, "Discarding unscored network: " + scanResult); + continue; + } + + final int score = network.rssiCurve.lookupScore(scanResult.level); + if (VERBOSE) Log.d(TAG, "Scored " + scanResult + ": " + score); + if (score > recommendedScore) { + recommendedScanResult = scanResult; + recommendedScore = score; + if (VERBOSE) Log.d(TAG, "New recommended network: " + scanResult); + continue; + } } + } else { + Log.w(TAG, "Received null scan results in request."); } // If we ended up without a recommendation, recommend the provided configuration -- cgit v1.2.3