1 /*
2  * Copyright (C) 2022 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 
17 package com.android.server.wifi.scanner;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.net.wifi.IWifiScannerListener;
22 import android.net.wifi.ScanResult;
23 import android.net.wifi.WifiScanner;
24 import android.os.Process;
25 import android.os.WorkSource;
26 import android.util.Log;
27 
28 import com.android.internal.annotations.VisibleForTesting;
29 import com.android.server.wifi.WifiThreadRunner;
30 
31 import java.util.Collections;
32 import java.util.List;
33 
34 /**
35  * WifiScanner manager local system service interface.
36  *
37  * @hide Only for use within the system server.
38  */
39 public abstract class WifiScannerInternal {
40 
41     /**
42      * Local scan listener
43      */
44     public static class ScanListener extends IWifiScannerListener.Stub {
45         private static final String TAG = "WifiScannerInternal";
46         private final WifiScanner.ScanListener mScanListener;
47         private final WifiThreadRunner mWifiThreadRunner;
48 
49         /**
50          * Local scan listener constructor
51          * @param scanListener WifiScanner listener
52          * @param handler handler for the listener
53          */
ScanListener(WifiScanner.ScanListener scanListener, WifiThreadRunner runner)54         public ScanListener(WifiScanner.ScanListener scanListener, WifiThreadRunner runner) {
55             mScanListener = scanListener;
56             mWifiThreadRunner = runner;
57         }
58 
59         /**
60          * Get the WifiScanner listener
61          * @hide
62          */
63         @VisibleForTesting
getWifiScannerListener()64         public WifiScanner.ScanListener getWifiScannerListener() {
65             return mScanListener;
66         }
67 
68         @Override
onSuccess()69         public void onSuccess() {
70             mWifiThreadRunner.post(mScanListener::onSuccess,
71                     TAG + "#onSuccess");
72         }
73 
74         @Override
onFailure(int reason, String description)75         public void onFailure(int reason, String description) {
76             mWifiThreadRunner.post(() -> {
77                 mScanListener.onFailure(reason, description);
78             }, TAG + "#onFailure");
79         }
80 
81         @Override
onResults(WifiScanner.ScanData[] scanDatas)82         public void onResults(WifiScanner.ScanData[] scanDatas) {
83             mWifiThreadRunner.post(() -> {
84                 mScanListener.onResults(scanDatas);
85             }, TAG + "#onResults");
86         }
87 
88         @Override
onFullResult(ScanResult fullScanResult)89         public void onFullResult(ScanResult fullScanResult) {
90             mWifiThreadRunner.post(() -> {
91                 mScanListener.onFullResult(fullScanResult);
92             }, TAG + "#onFullResult");
93         }
94 
95         @Override
onSingleScanCompleted()96         public void onSingleScanCompleted() {
97             // Internal scan listener doesn't need to handle this.
98         }
99 
100         @Override
onPnoNetworkFound(ScanResult[] scanResult)101         public void onPnoNetworkFound(ScanResult[] scanResult) {
102             if (!(mScanListener instanceof WifiScanner.PnoScanListener)) {
103                 Log.wtf(TAG, "Listener is not a PnoScanListener!");
104                 return;
105             }
106             WifiScanner.PnoScanListener pnoScanListener =
107                     (WifiScanner.PnoScanListener) mScanListener;
108             mWifiThreadRunner.post(() -> {
109                 pnoScanListener.onPnoNetworkFound(scanResult);
110             }, TAG + "#onPnoNetworkFound");
111         }
112     }
113 
114     /**
115      * Enable/Disable wifi scanning.
116      *
117      * @param enable set true to enable scanning, false to disable all types of scanning.
118      */
setScanningEnabled(boolean enable)119     public void setScanningEnabled(boolean enable) {
120     }
121 
122     /**
123      * Register a listener that will receive results from all single scans.
124      * @param listener specifies the object to report events to.
125      */
registerScanListener(@onNull ScanListener listener)126     public void registerScanListener(@NonNull ScanListener listener) {
127     }
128 
129     /**
130      * Start a single scan.
131      * @param settings Wifi single scan setting
132      * @param listener listener to the scan
133      */
startScan(WifiScanner.ScanSettings settings, ScanListener listener)134     public void startScan(WifiScanner.ScanSettings settings, ScanListener listener) {
135         startScan(settings, listener, new WorkSource(Process.WIFI_UID));
136     }
137 
138     /**
139      * Start a single scan.
140      * @param settings Wifi single scan setting
141      * @param listener listener to the scan
142      * @param workSource WorkSource to blame for power usage
143      */
startScan(WifiScanner.ScanSettings settings, ScanListener listener, @Nullable WorkSource workSource)144     public void startScan(WifiScanner.ScanSettings settings, ScanListener listener,
145             @Nullable WorkSource workSource) {
146     }
147 
148     /**
149      * Stop a single scan.
150      * @param listener listener to the scan
151      */
stopScan(ScanListener listener)152     public void stopScan(ScanListener listener) {
153     }
154 
155     /**
156      * Start a PNO scan.
157      * @param scanSettings Wifi single scan setting
158      * @param pnoSettings Wifi pno scan setting
159      * @param listener listener to the scan
160      */
startPnoScan(WifiScanner.ScanSettings scanSettings, WifiScanner.PnoSettings pnoSettings, ScanListener listener)161     public void startPnoScan(WifiScanner.ScanSettings scanSettings,
162             WifiScanner.PnoSettings pnoSettings,
163             ScanListener listener) {
164     }
165 
166     /**
167      * Stop a pno scan.
168      * @param listener listener to the scan
169      */
stopPnoScan(ScanListener listener)170     public void stopPnoScan(ScanListener listener) {
171     }
172 
173     /**
174      * Get single scan results.
175      * @return the list of scan results
176      */
getSingleScanResults()177     public List<ScanResult> getSingleScanResults() {
178         return Collections.emptyList();
179     }
180 
181 }
182