1 /* 2 * Copyright (C) 2021 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 android.location.cts.common; 18 19 import android.content.Context; 20 import android.location.LocationManager; 21 import android.location.provider.ProviderRequest; 22 import android.util.Pair; 23 24 import java.util.concurrent.LinkedBlockingQueue; 25 import java.util.concurrent.TimeUnit; 26 27 /** 28 * A {@link ProviderRequest.ChangedListener} that automatically unregisters itself 29 * when closed. 30 */ 31 public class ProviderRequestChangedListenerCapture implements 32 ProviderRequest.ChangedListener, AutoCloseable { 33 private final LocationManager mLocationManager; 34 private final LinkedBlockingQueue<Pair<String, ProviderRequest>> mProviderRequestChanges; 35 ProviderRequestChangedListenerCapture(Context context)36 public ProviderRequestChangedListenerCapture(Context context) { 37 mLocationManager = context.getSystemService(LocationManager.class); 38 mProviderRequestChanges = new LinkedBlockingQueue<>(); 39 } 40 getNextProviderRequest(long timeoutMs)41 public Pair<String, ProviderRequest> getNextProviderRequest(long timeoutMs) 42 throws InterruptedException { 43 return mProviderRequestChanges.poll(timeoutMs, TimeUnit.MILLISECONDS); 44 } 45 46 @Override onProviderRequestChanged(String provider, ProviderRequest request)47 public void onProviderRequestChanged(String provider, ProviderRequest request) { 48 mProviderRequestChanges.add(new Pair<>(provider, request)); 49 } 50 51 @Override close()52 public void close() throws Exception { 53 mLocationManager.removeProviderRequestChangedListener(this); 54 } 55 } 56