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