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