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 17 package android.location.cts; 18 19 import android.location.GpsSatellite; 20 import android.location.GpsStatus; 21 import android.location.LocationManager; 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 when GPS status has changed. 30 */ 31 class TestGpsStatusListener implements GpsStatus.Listener { 32 33 private volatile boolean mGpsStatusReceived; 34 private GpsStatus mGpsStatus = null; 35 // Timeout in sec for count down latch wait 36 private static final int TIMEOUT_IN_SEC = 90; 37 private final CountDownLatch mCountDownLatch; 38 private final LocationManager mLocationManager; 39 // Store list of Prn for Satellites. 40 private List<List<Integer>> mGpsSatellitePrns; 41 42 TestGpsStatusListener(int gpsStatusCountToCollect, TestLocationManager testLocationManager) { 43 mCountDownLatch = new CountDownLatch(gpsStatusCountToCollect); 44 mLocationManager = testLocationManager.getLocationManager(); 45 mGpsSatellitePrns = new ArrayList<List<Integer>>(); 46 } 47 48 @Override 49 public void onGpsStatusChanged(int event) { 50 if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) { 51 mGpsStatus = mLocationManager.getGpsStatus(mGpsStatus); 52 Iterable<GpsSatellite> satellites = mGpsStatus.getSatellites(); 53 List<Integer> prns = new ArrayList<Integer>(); 54 for (GpsSatellite gpsSatellite : satellites) { 55 prns.add(gpsSatellite.getPrn()); 56 } 57 mGpsSatellitePrns.add(prns); 58 if (!mGpsSatellitePrns.isEmpty()) { 59 mGpsStatusReceived = true; 60 mCountDownLatch.countDown(); 61 } 62 } 63 } 64 65 /** 66 * Returns the list of PRNs (pseudo-random number) for the satellite. 67 * 68 * @return list of PRNs number 69 */ 70 public List<List<Integer>> getGpsSatellitePrns() { 71 return mGpsSatellitePrns; 72 } 73 74 /** 75 * Check if GPS Status is received. 76 * 77 * @return {@code true} if the GPS Status is received and {@code false} 78 * if GPS Status is not received. 79 */ 80 public boolean isGpsStatusReceived() { 81 return mGpsStatusReceived; 82 } 83 84 /** 85 * Get GPS Status. 86 * 87 * @return mGpsStatus GPS Status 88 */ 89 public GpsStatus getGpsStatus() { 90 return mGpsStatus; 91 } 92 93 public boolean await() throws InterruptedException { 94 return TestUtils.waitFor(mCountDownLatch, TIMEOUT_IN_SEC); 95 } 96 } 97