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 junit.framework.Assert; 20 21 import android.location.GnssNavigationMessage; 22 import android.util.Log; 23 24 import java.util.List; 25 import java.util.concurrent.CopyOnWriteArrayList; 26 import java.util.concurrent.CountDownLatch; 27 import java.util.concurrent.TimeUnit; 28 29 /** 30 * Used for receiving GPS satellite Navigation Messages from the GPS engine. 31 */ 32 class TestGnssNavigationMessageListener extends GnssNavigationMessage.Callback { 33 34 // Timeout in sec for count down latch wait 35 private static final int TIMEOUT_IN_SEC = 90; 36 37 private volatile int mStatus = -1; 38 39 private final String mTag; 40 private final int mEventsToCollect; 41 private final List<GnssNavigationMessage> mEvents; 42 private final CountDownLatch mCountDownLatch; 43 44 TestGnssNavigationMessageListener(String tag, int eventsToCollect) { 45 mTag = tag; 46 mCountDownLatch = new CountDownLatch(1); 47 mEventsToCollect = eventsToCollect; 48 mEvents = new CopyOnWriteArrayList<GnssNavigationMessage>(); 49 } 50 51 @Override 52 public void onGnssNavigationMessageReceived(GnssNavigationMessage event) { 53 mEvents.add(event); 54 if (mEvents.size() > mEventsToCollect) { 55 mCountDownLatch.countDown(); 56 } 57 } 58 59 @Override 60 public void onStatusChanged(int status) { 61 mStatus = status; 62 if (mStatus != GnssNavigationMessage.Callback.STATUS_READY) { 63 mCountDownLatch.countDown(); 64 } 65 } 66 67 public boolean await() throws InterruptedException { 68 Log.i(mTag, "Number of GPS Navigation Message received = " + getEvents().size()); 69 return TestUtils.waitFor(mCountDownLatch, TIMEOUT_IN_SEC); 70 } 71 72 /** 73 * Get GPS Navigation Message Status. 74 * 75 * @return mStatus Gps Navigation Message Status 76 */ 77 public int getStatus() { 78 return mStatus; 79 } 80 81 /** 82 * @return {@code true} if the state of the test ensures that data is expected to be collected, 83 * {@code false} otherwise. 84 */ 85 public boolean verifyState() { 86 switch (getStatus()) { 87 case GnssNavigationMessage.Callback.STATUS_NOT_SUPPORTED: 88 SoftAssert.failAsWarning(mTag, "GnssNavigationMessage is not supported in the" 89 + " device: verifications performed by this test will be skipped."); 90 return false; 91 case GnssNavigationMessage.Callback.STATUS_READY: 92 return true; 93 case GnssNavigationMessage.Callback.STATUS_LOCATION_DISABLED: 94 Log.i(mTag, "Location or GPS is disabled on the device: skipping the test."); 95 return false; 96 default: 97 Assert.fail("GnssNavigationMessage status callback was not received."); 98 } 99 return false; 100 } 101 102 /** 103 * Get list of GPS Navigation Message Events. 104 * 105 * @return mEvents list of GPS Navigation Message Events 106 */ 107 public List<GnssNavigationMessage> getEvents() { 108 return mEvents; 109 } 110 } 111