3
3
import android .app .Activity ;
4
4
import android .support .test .espresso .IdlingResource ;
5
5
6
- import timber .log .Timber ;
7
-
6
+ import com .mapbox .mapboxsdk .maps .MapView ;
8
7
import com .mapbox .mapboxsdk .maps .MapboxMap ;
8
+ import com .mapbox .mapboxsdk .maps .OnMapReadyCallback ;
9
9
10
10
import java .lang .reflect .Field ;
11
11
12
- public class OnMapReadyIdlingResource implements IdlingResource {
12
+ public class OnMapReadyIdlingResource implements IdlingResource , OnMapReadyCallback {
13
13
14
- private final Activity activity ;
15
14
private MapboxMap mapboxMap ;
16
15
private IdlingResource .ResourceCallback resourceCallback ;
16
+ private Activity activity ;
17
17
18
18
public OnMapReadyIdlingResource (Activity activity ) {
19
19
this .activity = activity ;
20
+ try {
21
+ Field field = activity .getClass ().getDeclaredField ("mapView" );
22
+ field .setAccessible (true );
23
+ ((MapView ) field .get (activity )).getMapAsync (this );
24
+ } catch (Exception err ) {
25
+ throw new RuntimeException (err );
26
+ }
27
+
20
28
}
21
29
22
30
@ Override
@@ -26,32 +34,23 @@ public String getName() {
26
34
27
35
@ Override
28
36
public boolean isIdleNow () {
29
- boolean idle = isMapboxMapReady ();
30
- if (idle && resourceCallback != null ) {
31
- resourceCallback .onTransitionToIdle ();
32
- }
33
- return idle ;
37
+ return mapboxMap != null ;
34
38
}
35
39
36
40
@ Override
37
41
public void registerIdleTransitionCallback (ResourceCallback resourceCallback ) {
38
42
this .resourceCallback = resourceCallback ;
39
43
}
40
44
41
- private boolean isMapboxMapReady () {
42
- try {
43
- Field field = activity .getClass ().getDeclaredField ("mapboxMap" );
44
- field .setAccessible (true );
45
- mapboxMap = (MapboxMap ) field .get (activity );
46
- Timber .e ("isMapboxReady called with value %s" , (mapboxMap != null ));
47
- return mapboxMap != null ;
48
- } catch (Exception exception ) {
49
- Timber .e (exception , "could not reflect" );
50
- return false ;
51
- }
52
- }
53
-
54
45
public MapboxMap getMapboxMap () {
55
46
return mapboxMap ;
56
47
}
48
+
49
+ @ Override
50
+ public void onMapReady (MapboxMap mapboxMap ) {
51
+ this .mapboxMap = mapboxMap ;
52
+ if (resourceCallback != null ) {
53
+ resourceCallback .onTransitionToIdle ();
54
+ }
55
+ }
57
56
}
0 commit comments