1/*
2 * Copyright 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.wifi;
18
19import android.content.Context;
20import android.net.wifi.ScanResult;
21
22import com.android.internal.annotations.VisibleForTesting;
23
24import java.util.Collection;
25
26/**
27 * Evaluates ScanResults for Wifi Wake.
28 */
29public class WakeupEvaluator {
30
31    private final int mThresholdMinimumRssi24;
32    private final int mThresholdMinimumRssi5;
33
34    /**
35     * Constructs a {@link WakeupEvaluator} using the given context.
36     */
37    public static WakeupEvaluator fromContext(Context context) {
38        ScoringParams scoringParams = new ScoringParams(context); // TODO(b/74793980) - replumb
39        return new WakeupEvaluator(scoringParams.getEntryRssi(ScoringParams.BAND2),
40                                   scoringParams.getEntryRssi(ScoringParams.BAND5));
41    }
42
43    @VisibleForTesting
44    WakeupEvaluator(int minimumRssi24, int minimumRssi5) {
45        mThresholdMinimumRssi24 = minimumRssi24;
46        mThresholdMinimumRssi5 = minimumRssi5;
47    }
48
49    /**
50     * Searches ScanResults to find a connectable network.
51     *
52     * <p>This method searches the given ScanResults for one that is present in the given
53     * ScanResultMatchInfos and has a sufficiently high RSSI. If there is no such ScanResult, it
54     * returns null. If there are multiple, it returns the one with the highest RSSI.
55     *
56     * @param scanResults ScanResults to search
57     * @param savedNetworks Network list to compare against
58     * @return The {@link ScanResult} representing an in-range connectable network, or {@code null}
59     *         signifying there is no viable network
60     */
61    public ScanResult findViableNetwork(Collection<ScanResult> scanResults,
62                                        Collection<ScanResultMatchInfo> savedNetworks) {
63        ScanResult selectedScanResult = null;
64
65        for (ScanResult scanResult : scanResults) {
66            if (isBelowThreshold(scanResult)) {
67                continue;
68            }
69            if (savedNetworks.contains(ScanResultMatchInfo.fromScanResult(scanResult))) {
70                if (selectedScanResult == null || selectedScanResult.level < scanResult.level) {
71                    selectedScanResult = scanResult;
72                }
73            }
74        }
75
76        return selectedScanResult;
77    }
78
79    /**
80     * Returns whether the given ScanResult's signal strength is below the selection threshold.
81     */
82    public boolean isBelowThreshold(ScanResult scanResult) {
83        return ((scanResult.is24GHz() && scanResult.level < mThresholdMinimumRssi24)
84                || (scanResult.is5GHz() && scanResult.level < mThresholdMinimumRssi5));
85    }
86}
87