1 package org.robolectric.shadows; 2 3 import static android.os.Build.VERSION_CODES.P; 4 5 import android.net.wifi.rtt.RangingRequest; 6 import android.net.wifi.rtt.RangingResult; 7 import android.net.wifi.rtt.RangingResultCallback; 8 import android.net.wifi.rtt.WifiRttManager; 9 import java.util.ArrayList; 10 import java.util.List; 11 import java.util.concurrent.Executor; 12 import org.robolectric.annotation.Implementation; 13 import org.robolectric.annotation.Implements; 14 15 /** Shadow for {@link android.net.wifi.rtt.WifiRttManager}. */ 16 @Implements(value = WifiRttManager.class, minSdk = P) 17 public class ShadowWifiRttManager { 18 private List<RangingResult> rangingResults = new ArrayList<>(); 19 20 /** 21 * If there are RangingResults set by the setRangeResults method of this shadow class, this method 22 * will call the onRangingResults method of the callback on the executor thread and pass the list 23 * of RangingResults. If there are no ranging results set, it will pass 24 * RangingResultCallback.STATUS_CODE_FAIL to the onRangingFailure method of the callback, also 25 * called on the executor thread. 26 */ 27 @Implementation(minSdk = P) startRanging( RangingRequest request, Executor executor, RangingResultCallback callback)28 protected void startRanging( 29 RangingRequest request, Executor executor, RangingResultCallback callback) { 30 if (!rangingResults.isEmpty()) { 31 executor.execute(() -> callback.onRangingResults(this.rangingResults)); 32 } else { 33 executor.execute(() -> callback.onRangingFailure(RangingResultCallback.STATUS_CODE_FAIL)); 34 } 35 } 36 37 /** Assumes the WifiRttManager is always available. */ 38 @Implementation(minSdk = P) isAvailable()39 protected boolean isAvailable() { 40 return true; 41 } 42 43 /** 44 * This method sets the RangingResults that are passed to the RangingResultCallback when the 45 * shadow startRanging method is called. 46 */ setRangeResults(List<RangingResult> rangingResults)47 public void setRangeResults(List<RangingResult> rangingResults) { 48 this.rangingResults = rangingResults; 49 } 50 } 51