summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/maplayout/VisibilityChangeActivity.java
blob: 393abb7cd41c7ae3f75cbb671c771a3805180dfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package com.mapbox.mapboxsdk.testapp.activity.maplayout;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.testapp.R;

/**
 * Test activity showcasing visibility changes to the mapview.
 */
public class VisibilityChangeActivity extends AppCompatActivity {

  private MapView mapView;
  private MapboxMap mapboxMap;
  private Handler handler = new Handler();
  private Runnable runnable;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map_visibility);

    mapView = (MapView) findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(new OnMapReadyCallback() {
      @Override
      public void onMapReady(MapboxMap map) {
        mapboxMap = map;
        mapboxMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
          new LatLng(55.754020, 37.620948), 12), 9000);
      }
    });
  }

  @Override
  protected void onStart() {
    super.onStart();
    mapView.onStart();
    handler.post(runnable = new VisibilityRunner(mapView, findViewById(R.id.viewParent), handler));
  }

  @Override
  protected void onResume() {
    super.onResume();
    mapView.onResume();
  }

  private static class VisibilityRunner implements Runnable {

    private MapView mapView;
    private View viewParent;
    private Handler handler;
    private int currentStep;

    VisibilityRunner(MapView mapView, View viewParent, Handler handler) {
      this.mapView = mapView;
      this.viewParent = viewParent;
      this.handler = handler;
    }

    @Override
    public void run() {
      if (isViewHiearchyReady()) {
        if (isEvenStep()) {
          viewParent.setVisibility(View.VISIBLE);
          mapView.setVisibility(View.VISIBLE);
        } else if (isFirstOrThirdStep()) {
          mapView.setVisibility(getVisibilityForStep());
        } else if (isFifthOrSeventhStep()) {
          viewParent.setVisibility(getVisibilityForStep());
        }
        updateStep();
      }
      handler.postDelayed(this, 1500);
    }

    private void updateStep() {
      if (currentStep == 7) {
        currentStep = 0;
      } else {
        currentStep++;
      }
    }

    private int getVisibilityForStep() {
      return (currentStep == 1 || currentStep == 5) ? View.GONE : View.INVISIBLE;
    }

    private boolean isFifthOrSeventhStep() {
      return currentStep == 5 || currentStep == 7;
    }

    private boolean isFirstOrThirdStep() {
      return currentStep == 1 || currentStep == 3;
    }

    private boolean isEvenStep() {
      return currentStep == 0 || currentStep % 2 == 0;
    }

    private boolean isViewHiearchyReady() {
      return mapView != null && viewParent != null;
    }
  }

  @Override
  protected void onPause() {
    super.onPause();
    mapView.onPause();
  }

  @Override
  protected void onStop() {
    super.onStop();
    if (runnable != null) {
      handler.removeCallbacks(runnable);
      runnable = null;
    }
    mapView.onStop();
  }

  @Override
  public void onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mapView.onSaveInstanceState(outState);
  }
}