1 /* 2 * Copyright (C) 2015 Google Inc. 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; 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.CountDownLatch; 26 import java.util.concurrent.TimeUnit; 27 28 /** 29 * Used for receiving notifications from the LocationManager when the location has changed. 30 */ 31 class TestLocationListener implements LocationListener { 32 private volatile boolean mProviderEnabled; 33 private volatile boolean mLocationReceived; 34 // Timeout in sec for count down latch wait 35 private static final int TIMEOUT_IN_SEC = 120; 36 private final CountDownLatch mCountDownLatch; 37 private List<Location> mLocationList = null; 38 TestLocationListener(int locationToCollect)39 TestLocationListener(int locationToCollect) { 40 mCountDownLatch = new CountDownLatch(locationToCollect); 41 mLocationList = new ArrayList<>(); 42 } 43 44 @Override onLocationChanged(Location location)45 public void onLocationChanged(Location location) { 46 mLocationReceived = true; 47 mLocationList.add(location); 48 mCountDownLatch.countDown(); 49 } 50 51 @Override onStatusChanged(String s, int i, Bundle bundle)52 public void onStatusChanged(String s, int i, Bundle bundle) { 53 } 54 55 @Override onProviderEnabled(String s)56 public void onProviderEnabled(String s) { 57 if (LocationManager.GPS_PROVIDER.equals(s)) { 58 mProviderEnabled = true; 59 } 60 } 61 62 @Override onProviderDisabled(String s)63 public void onProviderDisabled(String s) { 64 if (LocationManager.GPS_PROVIDER.equals(s)) { 65 mProviderEnabled = false; 66 } 67 } 68 await()69 public boolean await() throws InterruptedException { 70 return TestUtils.waitFor(mCountDownLatch, TIMEOUT_IN_SEC); 71 } 72 73 /** 74 * Get the list of locations received 75 * 76 * @return mLocationList, List of {@link Location}. 77 */ getReceivedLocationList()78 public List<Location> getReceivedLocationList(){ 79 return mLocationList; 80 } 81 82 /** 83 * Check if location provider is enabled. 84 * 85 * @return {@code true} if the location provider is enabled and {@code false} 86 * if location provider is disabled. 87 */ isProviderEnabled()88 public boolean isProviderEnabled() { 89 return mProviderEnabled; 90 } 91 92 /** 93 * Check if the location is received. 94 * 95 * @return {@code true} if the location is received and {@code false} 96 * if location is not received. 97 */ isLocationReceived()98 public boolean isLocationReceived() { 99 return mLocationReceived; 100 } 101 } 102