1 /* 2 * Copyright (C) 2020 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 package android.location.cts.common; 17 18 import android.location.Location; 19 import android.location.LocationListener; 20 import android.location.LocationManager; 21 import android.os.Bundle; 22 23 import java.util.ArrayList; 24 import java.util.List; 25 import java.util.concurrent.ConcurrentLinkedQueue; 26 import java.util.concurrent.CountDownLatch; 27 28 /** 29 * Used for receiving notifications from the LocationManager when the location has changed. 30 */ 31 public class TestLocationListener implements LocationListener { 32 private volatile boolean mProviderEnabled; 33 private volatile boolean mLocationReceived; 34 35 // Timeout in sec for count down latch wait 36 private static final int TIMEOUT_IN_SEC = 120; 37 private CountDownLatch mCountDownLatch; 38 private final ConcurrentLinkedQueue<Location> mLocationList; 39 TestLocationListener(int locationToCollect)40 public TestLocationListener(int locationToCollect) { 41 mCountDownLatch = new CountDownLatch(locationToCollect); 42 mLocationList = new ConcurrentLinkedQueue<>(); 43 } 44 45 @Override onLocationChanged(Location location)46 public synchronized void onLocationChanged(Location location) { 47 mLocationReceived = true; 48 mLocationList.add(location); 49 mCountDownLatch.countDown(); 50 } 51 52 /** Clears the received locations in {@link #mLocationList} */ clearReceivedLocationsAndResetCounter(int locationToCollect)53 public synchronized void clearReceivedLocationsAndResetCounter(int locationToCollect) { 54 mLocationList.clear(); 55 mCountDownLatch = new CountDownLatch(locationToCollect); 56 } 57 58 @Override onStatusChanged(String s, int i, Bundle bundle)59 public void onStatusChanged(String s, int i, Bundle bundle) { 60 } 61 62 @Override onProviderEnabled(String s)63 public void onProviderEnabled(String s) { 64 if (LocationManager.GPS_PROVIDER.equals(s)) { 65 mProviderEnabled = true; 66 } 67 } 68 69 @Override onProviderDisabled(String s)70 public void onProviderDisabled(String s) { 71 if (LocationManager.GPS_PROVIDER.equals(s)) { 72 mProviderEnabled = false; 73 } 74 } 75 await()76 public boolean await() throws InterruptedException { 77 return TestUtils.waitFor(mCountDownLatch, TIMEOUT_IN_SEC); 78 } 79 await(int timeInSec)80 public boolean await(int timeInSec) throws InterruptedException { 81 return TestUtils.waitFor(mCountDownLatch, timeInSec); 82 } 83 84 /** 85 * Get the list of locations received. 86 * 87 * Makes a copy of {@code mLocationList}. New locations received after this call is 88 * made are not reflected in the returned list so that the returned list can be safely 89 * iterated without getting a ConcurrentModificationException. Occasionally, 90 * even after calling TestLocationManager.removeLocationUpdates(), the location listener 91 * can receive one or two location updates. 92 */ getReceivedLocationList()93 public List<Location> getReceivedLocationList(){ 94 return new ArrayList(mLocationList); 95 } 96 97 /** 98 * Check if location provider is enabled. 99 * 100 * @return {@code true} if the location provider is enabled and {@code false} 101 * if location provider is disabled. 102 */ isProviderEnabled()103 public boolean isProviderEnabled() { 104 return mProviderEnabled; 105 } 106 107 /** 108 * Check if the location is received. 109 * 110 * @return {@code true} if the location is received and {@code false} 111 * if location is not received. 112 */ isLocationReceived()113 public boolean isLocationReceived() { 114 return mLocationReceived; 115 } 116 } 117