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.ArrayList;
24 import java.util.List;
25 import java.util.concurrent.ConcurrentLinkedQueue;
26 import java.util.concurrent.CountDownLatch;
27 
28 /**
29  * Used for receiving notifications from the LocationManager when the location has changed.
30  */
31 class TestLocationListener implements LocationListener {
32     private volatile boolean mProviderEnabled;
33     private volatile boolean mLocationReceived;
34 
35     // Timeout in sec for count down latch wait
36     private static final int TIMEOUT_IN_SEC = 120;
37     private final CountDownLatch mCountDownLatch;
38     private ConcurrentLinkedQueue<Location> mLocationList = null;
39 
TestLocationListener(int locationToCollect)40     TestLocationListener(int locationToCollect) {
41         mCountDownLatch = new CountDownLatch(locationToCollect);
42         mLocationList = new ConcurrentLinkedQueue<>();
43     }
44 
45     @Override
onLocationChanged(Location location)46     public void onLocationChanged(Location location) {
47         mLocationReceived = true;
48         mLocationList.add(location);
49         mCountDownLatch.countDown();
50     }
51 
52     @Override
onStatusChanged(String s, int i, Bundle bundle)53     public void onStatusChanged(String s, int i, Bundle bundle) {
54     }
55 
56     @Override
onProviderEnabled(String s)57     public void onProviderEnabled(String s) {
58         if (LocationManager.GPS_PROVIDER.equals(s)) {
59             mProviderEnabled = true;
60         }
61     }
62 
63     @Override
onProviderDisabled(String s)64     public void onProviderDisabled(String s) {
65         if (LocationManager.GPS_PROVIDER.equals(s)) {
66             mProviderEnabled = false;
67         }
68     }
69 
await()70     public boolean await() throws InterruptedException {
71         return TestUtils.waitFor(mCountDownLatch, TIMEOUT_IN_SEC);
72     }
73 
await(int timeInSec)74     public boolean await(int timeInSec) throws InterruptedException {
75         return TestUtils.waitFor(mCountDownLatch, timeInSec);
76     }
77 
78     /**
79      * Get the list of locations received.
80      *
81      * Makes a copy of {@code mLocationList}. New locations received after this call is
82      * made are not reflected in the returned list so that the returned list can be safely
83      * iterated without getting a ConcurrentModificationException. Occasionally,
84      * even after calling TestLocationManager.removeLocationUpdates(), the location listener
85      * can receive one or two location updates.
86      */
getReceivedLocationList()87     public List<Location> getReceivedLocationList(){
88         return new ArrayList(mLocationList);
89     }
90 
91     /**
92      * Check if location provider is enabled.
93      *
94      * @return {@code true} if the location provider is enabled and {@code false}
95      *         if location provider is disabled.
96      */
isProviderEnabled()97     public boolean isProviderEnabled() {
98         return mProviderEnabled;
99     }
100 
101     /**
102      * Check if the location is received.
103      *
104      * @return {@code true} if the location is received and {@code false}
105      *         if location is not received.
106      */
isLocationReceived()107     public boolean isLocationReceived() {
108         return mLocationReceived;
109     }
110 }
111