1 /*
2  * Copyright (C) 2016 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.gnss;
18 
19 import android.location.GnssStatus;
20 import android.location.cts.common.TestMeasurementUtil;
21 import android.location.cts.common.TestUtils;
22 import android.util.Log;
23 
24 import java.util.HashSet;
25 import java.util.Set;
26 import java.util.concurrent.CountDownLatch;
27 
28 /**
29  * Used for receiving notifications when GNSS status has changed.
30  */
31 public class TestGnssStatusCallback extends GnssStatus.Callback {
32 
33     private final String mTag;
34     private GnssStatus mGnssStatus = null;
35     // Timeout in sec for count down latch wait
36     private static final int TIMEOUT_IN_SEC = 90;
37     private final CountDownLatch mLatchStart;
38     private final CountDownLatch mLatchStatus;
39     private final CountDownLatch mLatchTtff;
40     private final CountDownLatch mLatchStop;
41 
42     // Store list of Satellites including Gnss Band, constellation & SvId
43     private Set<String> mGnssUsedSvStringIds;
44 
45     private final Set<Float> mCarrierFrequencies;
46 
TestGnssStatusCallback(String tag, int gpsStatusCountToCollect)47     public TestGnssStatusCallback(String tag, int gpsStatusCountToCollect) {
48         this.mTag = tag;
49         mLatchStart = new CountDownLatch(1);
50         mLatchStatus = new CountDownLatch(gpsStatusCountToCollect);
51         mLatchTtff = new CountDownLatch(1);
52         mLatchStop = new CountDownLatch(1);
53         mGnssUsedSvStringIds = new HashSet<>();
54         mCarrierFrequencies = new HashSet<>();
55     }
56 
57     @Override
onStarted()58     public void onStarted() {
59         Log.i(mTag, "Gnss Status Listener Started");
60         mLatchStart.countDown();
61     }
62 
63     @Override
onStopped()64     public void onStopped() {
65         Log.i(mTag, "Gnss Status Listener Stopped");
66         mLatchStop.countDown();
67     }
68 
69     @Override
onFirstFix(int ttffMillis)70     public void onFirstFix(int ttffMillis) {
71         Log.i(mTag, "Gnss Status Listener Received TTFF");
72         mLatchTtff.countDown();
73     }
74 
75     @Override
onSatelliteStatusChanged(GnssStatus status)76     public void onSatelliteStatusChanged(GnssStatus status) {
77         Log.i(mTag, "Gnss Status Listener Received Status Update");
78         mGnssStatus = status;
79         for (int i = 0; i < status.getSatelliteCount(); i++) {
80             mCarrierFrequencies.add(status.getCarrierFrequencyHz(i));
81             if (!status.usedInFix(i)) {
82                 continue;
83             }
84             if (status.hasCarrierFrequencyHz(i)) {
85                 mGnssUsedSvStringIds.add(
86                     TestMeasurementUtil.getUniqueSvStringId(status.getConstellationType(i),
87                         status.getSvid(i), status.getCarrierFrequencyHz(i)));
88             } else {
89                 mGnssUsedSvStringIds.add(
90                     TestMeasurementUtil.getUniqueSvStringId(status.getConstellationType(i),
91                         status.getSvid(i)));
92             }
93         }
94         mLatchStatus.countDown();
95     }
96 
97     /**
98      * Returns the list of SV String Ids which were used in fix during the collect
99      *
100      * @return mGnssUsedSvStringIds - Set of SV string Ids
101      */
getGnssUsedSvStringIds()102     public Set<String> getGnssUsedSvStringIds() {
103         return mGnssUsedSvStringIds;
104     }
105 
106     /**
107      * Returns the list of carrier frequencies of the received GnssStatus.
108      *
109      * @return mCarrierFrequencies - a set of carrier frequencies
110      */
getCarrierFrequencies()111     public float[] getCarrierFrequencies() {
112         float[] result = new float[mCarrierFrequencies.size()];
113         int i = 0;
114         for (Float freq : mCarrierFrequencies) {
115             result[i++] = freq;
116         }
117         return result;
118     }
119 
120     /**
121      * Get GNSS Status.
122      *
123      * @return mGnssStatus GNSS Status
124      */
getGnssStatus()125     public GnssStatus getGnssStatus() {
126         return mGnssStatus;
127     }
128 
awaitStart()129     public boolean awaitStart() throws InterruptedException {
130         return TestUtils.waitFor(mLatchStart, TIMEOUT_IN_SEC);
131     }
132 
awaitStatus()133     public boolean awaitStatus() throws InterruptedException {
134         return TestUtils.waitFor(mLatchStatus, TIMEOUT_IN_SEC);
135     }
136 
awaitTtff()137     public boolean awaitTtff() throws InterruptedException {
138         return TestUtils.waitFor(mLatchTtff, TIMEOUT_IN_SEC);
139     }
140 
awaitStop()141     public boolean awaitStop() throws InterruptedException {
142         return TestUtils.waitFor(mLatchStop, TIMEOUT_IN_SEC);
143     }
144 }
145