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.Handler; 25 import android.os.Process; 26 import android.os.WorkSource; 27 import android.util.Log; 28 29 import com.android.internal.annotations.VisibleForTesting; 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 Handler mHandler; 48 49 /** 50 * Local scan listener constructor 51 * @param scanListener WifiScanner listener 52 * @param handler handler for the listener 53 */ ScanListener(WifiScanner.ScanListener scanListener, Handler handler)54 public ScanListener(WifiScanner.ScanListener scanListener, Handler handler) { 55 mScanListener = scanListener; 56 mHandler = handler; 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 mHandler.post(() -> { 71 mScanListener.onSuccess(); 72 }); 73 } 74 75 @Override onFailure(int reason, String description)76 public void onFailure(int reason, String description) { 77 mHandler.post(() -> { 78 mScanListener.onFailure(reason, description); 79 }); 80 } 81 82 @Override onResults(WifiScanner.ScanData[] scanDatas)83 public void onResults(WifiScanner.ScanData[] scanDatas) { 84 mHandler.post(() -> { 85 mScanListener.onResults(scanDatas); 86 }); 87 } 88 89 @Override onFullResult(ScanResult fullScanResult)90 public void onFullResult(ScanResult fullScanResult) { 91 mHandler.post(() -> { 92 mScanListener.onFullResult(fullScanResult); 93 }); 94 } 95 96 @Override onSingleScanCompleted()97 public void onSingleScanCompleted() { 98 // Internal scan listener doesn't need to handle this. 99 } 100 101 @Override onPnoNetworkFound(ScanResult[] scanResult)102 public void onPnoNetworkFound(ScanResult[] scanResult) { 103 if (!(mScanListener instanceof WifiScanner.PnoScanListener)) { 104 Log.wtf(TAG, "Listener is not a PnoScanListener!"); 105 return; 106 } 107 WifiScanner.PnoScanListener pnoScanListener = 108 (WifiScanner.PnoScanListener) mScanListener; 109 mHandler.post(() -> { 110 pnoScanListener.onPnoNetworkFound(scanResult); 111 }); 112 } 113 } 114 115 /** 116 * Enable/Disable wifi scanning. 117 * 118 * @param enable set true to enable scanning, false to disable all types of scanning. 119 */ setScanningEnabled(boolean enable)120 public void setScanningEnabled(boolean enable) { 121 } 122 123 /** 124 * Register a listener that will receive results from all single scans. 125 * @param listener specifies the object to report events to. 126 */ registerScanListener(@onNull ScanListener listener)127 public void registerScanListener(@NonNull ScanListener listener) { 128 } 129 130 /** 131 * Start a single scan. 132 * @param settings Wifi single scan setting 133 * @param listener listener to the scan 134 */ startScan(WifiScanner.ScanSettings settings, ScanListener listener)135 public void startScan(WifiScanner.ScanSettings settings, ScanListener listener) { 136 startScan(settings, listener, new WorkSource(Process.WIFI_UID)); 137 } 138 139 /** 140 * Start a single scan. 141 * @param settings Wifi single scan setting 142 * @param listener listener to the scan 143 * @param workSource WorkSource to blame for power usage 144 */ startScan(WifiScanner.ScanSettings settings, ScanListener listener, @Nullable WorkSource workSource)145 public void startScan(WifiScanner.ScanSettings settings, ScanListener listener, 146 @Nullable WorkSource workSource) { 147 } 148 149 /** 150 * Stop a single scan. 151 * @param listener listener to the scan 152 */ stopScan(ScanListener listener)153 public void stopScan(ScanListener listener) { 154 } 155 156 /** 157 * Start a PNO scan. 158 * @param scanSettings Wifi single scan setting 159 * @param pnoSettings Wifi pno scan setting 160 * @param listener listener to the scan 161 */ startPnoScan(WifiScanner.ScanSettings scanSettings, WifiScanner.PnoSettings pnoSettings, ScanListener listener)162 public void startPnoScan(WifiScanner.ScanSettings scanSettings, 163 WifiScanner.PnoSettings pnoSettings, 164 ScanListener listener) { 165 } 166 167 /** 168 * Stop a pno scan. 169 * @param listener listener to the scan 170 */ stopPnoScan(ScanListener listener)171 public void stopPnoScan(ScanListener listener) { 172 } 173 174 /** 175 * Get single scan results. 176 * @return the list of scan results 177 */ getSingleScanResults()178 public List<ScanResult> getSingleScanResults() { 179 return Collections.emptyList(); 180 } 181 182 } 183