Merge "Made AccessPoint's scan result list thread safe" into qt-dev am: 8e13ea659e

am: 4127f04908

Change-Id: Icbbd5c6404fb9930cc3b2d0d657d1774dee4244c
This commit is contained in:
Quang Luong
2019-06-10 14:50:40 -07:00
committed by android-build-merger

View File

@@ -55,6 +55,7 @@ import android.util.ArraySet;
import android.util.Log; import android.util.Log;
import android.util.Pair; import android.util.Pair;
import androidx.annotation.GuardedBy;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import com.android.internal.annotations.VisibleForTesting; import com.android.internal.annotations.VisibleForTesting;
@@ -108,6 +109,14 @@ public class AccessPoint implements Comparable<AccessPoint> {
/** The key which identifies this AccessPoint grouping. */ /** The key which identifies this AccessPoint grouping. */
private String mKey; private String mKey;
/**
* Synchronization lock for managing concurrency between main and worker threads.
*
* <p>This lock should be held for all modifications to {@link #mScanResults} and
* {@link #mExtraScanResults}.
*/
private final Object mLock = new Object();
@IntDef({Speed.NONE, Speed.SLOW, Speed.MODERATE, Speed.FAST, Speed.VERY_FAST}) @IntDef({Speed.NONE, Speed.SLOW, Speed.MODERATE, Speed.FAST, Speed.VERY_FAST})
@Retention(RetentionPolicy.SOURCE) @Retention(RetentionPolicy.SOURCE)
public @interface Speed { public @interface Speed {
@@ -134,12 +143,14 @@ public class AccessPoint implements Comparable<AccessPoint> {
} }
/** The underlying set of scan results comprising this AccessPoint. */ /** The underlying set of scan results comprising this AccessPoint. */
@GuardedBy("mLock")
private final ArraySet<ScanResult> mScanResults = new ArraySet<>(); private final ArraySet<ScanResult> mScanResults = new ArraySet<>();
/** /**
* Extra set of unused scan results corresponding to this AccessPoint for verbose logging * Extra set of unused scan results corresponding to this AccessPoint for verbose logging
* purposes, such as a set of Passpoint roaming scan results when home scans are available. * purposes, such as a set of Passpoint roaming scan results when home scans are available.
*/ */
@GuardedBy("mLock")
private final ArraySet<ScanResult> mExtraScanResults = new ArraySet<>(); private final ArraySet<ScanResult> mExtraScanResults = new ArraySet<>();
/** /**
@@ -489,8 +500,10 @@ public class AccessPoint implements Comparable<AccessPoint> {
if (isVerboseLoggingEnabled()) { if (isVerboseLoggingEnabled()) {
builder.append(",rssi=").append(mRssi); builder.append(",rssi=").append(mRssi);
builder.append(",scan cache size=").append(mScanResults.size() synchronized (mLock) {
+ mExtraScanResults.size()); builder.append(",scan cache size=").append(mScanResults.size()
+ mExtraScanResults.size());
}
} }
return builder.append(')').toString(); return builder.append(')').toString();
@@ -532,18 +545,20 @@ public class AccessPoint implements Comparable<AccessPoint> {
*/ */
private boolean updateScores(WifiNetworkScoreCache scoreCache, long maxScoreCacheAgeMillis) { private boolean updateScores(WifiNetworkScoreCache scoreCache, long maxScoreCacheAgeMillis) {
long nowMillis = SystemClock.elapsedRealtime(); long nowMillis = SystemClock.elapsedRealtime();
for (ScanResult result : mScanResults) { synchronized (mLock) {
ScoredNetwork score = scoreCache.getScoredNetwork(result); for (ScanResult result : mScanResults) {
if (score == null) { ScoredNetwork score = scoreCache.getScoredNetwork(result);
continue; if (score == null) {
} continue;
TimestampedScoredNetwork timedScore = mScoredNetworkCache.get(result.BSSID); }
if (timedScore == null) { TimestampedScoredNetwork timedScore = mScoredNetworkCache.get(result.BSSID);
mScoredNetworkCache.put( if (timedScore == null) {
result.BSSID, new TimestampedScoredNetwork(score, nowMillis)); mScoredNetworkCache.put(
} else { result.BSSID, new TimestampedScoredNetwork(score, nowMillis));
// Update data since the has been seen in the score cache } else {
timedScore.update(score, nowMillis); // Update data since the has been seen in the score cache
timedScore.update(score, nowMillis);
}
} }
} }
@@ -619,12 +634,14 @@ public class AccessPoint implements Comparable<AccessPoint> {
mIsScoredNetworkMetered |= score.meteredHint; mIsScoredNetworkMetered |= score.meteredHint;
} }
} else { } else {
for (ScanResult result : mScanResults) { synchronized (mLock) {
ScoredNetwork score = scoreCache.getScoredNetwork(result); for (ScanResult result : mScanResults) {
if (score == null) { ScoredNetwork score = scoreCache.getScoredNetwork(result);
continue; if (score == null) {
continue;
}
mIsScoredNetworkMetered |= score.meteredHint;
} }
mIsScoredNetworkMetered |= score.meteredHint;
} }
} }
return oldMetering == mIsScoredNetworkMetered; return oldMetering == mIsScoredNetworkMetered;
@@ -741,8 +758,10 @@ public class AccessPoint implements Comparable<AccessPoint> {
*/ */
public Set<ScanResult> getScanResults() { public Set<ScanResult> getScanResults() {
Set<ScanResult> allScans = new ArraySet<>(); Set<ScanResult> allScans = new ArraySet<>();
allScans.addAll(mScanResults); synchronized (mLock) {
allScans.addAll(mExtraScanResults); allScans.addAll(mScanResults);
allScans.addAll(mExtraScanResults);
}
return allScans; return allScans;
} }
@@ -766,10 +785,12 @@ public class AccessPoint implements Comparable<AccessPoint> {
ScanResult bestResult = null; ScanResult bestResult = null;
int bestRssi = UNREACHABLE_RSSI; int bestRssi = UNREACHABLE_RSSI;
for (ScanResult result : mScanResults) { synchronized (mLock) {
if (result.level > bestRssi) { for (ScanResult result : mScanResults) {
bestRssi = result.level; if (result.level > bestRssi) {
bestResult = result; bestRssi = result.level;
bestResult = result;
}
} }
} }
@@ -1210,9 +1231,11 @@ public class AccessPoint implements Comparable<AccessPoint> {
savedState.putInt(KEY_EAPTYPE, mEapType); savedState.putInt(KEY_EAPTYPE, mEapType);
if (mConfig != null) savedState.putParcelable(KEY_CONFIG, mConfig); if (mConfig != null) savedState.putParcelable(KEY_CONFIG, mConfig);
savedState.putParcelable(KEY_WIFIINFO, mInfo); savedState.putParcelable(KEY_WIFIINFO, mInfo);
savedState.putParcelableArray(KEY_SCANRESULTS, synchronized (mLock) {
mScanResults.toArray(new Parcelable[mScanResults.size() savedState.putParcelableArray(KEY_SCANRESULTS,
+ mExtraScanResults.size()])); mScanResults.toArray(new Parcelable[mScanResults.size()
+ mExtraScanResults.size()]));
}
savedState.putParcelableArrayList(KEY_SCOREDNETWORKCACHE, savedState.putParcelableArrayList(KEY_SCOREDNETWORKCACHE,
new ArrayList<>(mScoredNetworkCache.values())); new ArrayList<>(mScoredNetworkCache.values()));
if (mNetworkInfo != null) { if (mNetworkInfo != null) {
@@ -1291,8 +1314,10 @@ public class AccessPoint implements Comparable<AccessPoint> {
} }
int oldLevel = getLevel(); int oldLevel = getLevel();
mScanResults.clear(); synchronized (mLock) {
mScanResults.addAll(scanResults); mScanResults.clear();
mScanResults.addAll(scanResults);
}
updateBestRssiInfo(); updateBestRssiInfo();
int newLevel = getLevel(); int newLevel = getLevel();
@@ -1323,16 +1348,18 @@ public class AccessPoint implements Comparable<AccessPoint> {
void setScanResultsPasspoint( void setScanResultsPasspoint(
@Nullable Collection<ScanResult> homeScans, @Nullable Collection<ScanResult> homeScans,
@Nullable Collection<ScanResult> roamingScans) { @Nullable Collection<ScanResult> roamingScans) {
mExtraScanResults.clear(); synchronized (mLock) {
if (!CollectionUtils.isEmpty(homeScans)) { mExtraScanResults.clear();
if (!CollectionUtils.isEmpty(roamingScans)) { if (!CollectionUtils.isEmpty(homeScans)) {
mExtraScanResults.addAll(roamingScans); mIsRoaming = false;
if (!CollectionUtils.isEmpty(roamingScans)) {
mExtraScanResults.addAll(roamingScans);
}
setScanResults(homeScans);
} else if (!CollectionUtils.isEmpty(roamingScans)) {
mIsRoaming = true;
setScanResults(roamingScans);
} }
mIsRoaming = false;
setScanResults(homeScans);
} else if (!CollectionUtils.isEmpty(roamingScans)) {
mIsRoaming = true;
setScanResults(roamingScans);
} }
} }