summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/CompassView.java
blob: 5b5999a4a5764b83717b0867e7f108ed478cc23e (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package com.mapbox.mapboxsdk.maps.widgets;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.UiThread;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPropertyAnimatorCompat;
import android.support.v4.view.ViewPropertyAnimatorListenerAdapter;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.UiSettings;

/**
 * UI element overlaid on a map to show the map's bearing when it isn't true north (0.0). Tapping
 * the compass resets the bearing to true north and hides the compass.
 * <p>
 * You can change the behaviour of this View during initialisation with
 * {@link com.mapbox.mapboxsdk.maps.MapboxMapOptions}, and xml attributes. While running you can
 * use {@link com.mapbox.mapboxsdk.maps.UiSettings}.
 * </p>
 */
@SuppressLint("AppCompatCustomView")
@UiThread
public final class CompassView extends ImageView implements Runnable {

  public static final long TIME_WAIT_IDLE = 500;
  public static final long TIME_MAP_NORTH_ANIMATION = 150;
  private static final long TIME_FADE_ANIMATION = TIME_WAIT_IDLE;

  private float rotation = 0.0f;
  private boolean fadeCompassViewFacingNorth = true;
  private ViewPropertyAnimatorCompat fadeAnimator;
  private MapboxMap.OnCompassAnimationListener compassAnimationListener;
  private boolean isAnimating = false;

  private UiSettings uiSettings;

  public CompassView(Context context) {
    super(context);
    initialize(context);
  }

  public CompassView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialize(context);
  }

  public CompassView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initialize(context);
  }

  private void initialize(Context context) {
    // Layout params
    float screenDensity = context.getResources().getDisplayMetrics().density;
    ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams((int) (48 * screenDensity), (int) (48 * screenDensity));
    setLayoutParams(lp);
  }

  public void injectCompassAnimationListener(@NonNull MapboxMap.OnCompassAnimationListener compassAnimationListener) {
    this.compassAnimationListener = compassAnimationListener;
  }

  public void injectUiSettings(@NonNull UiSettings uiSettings) {
    this.uiSettings = uiSettings;
  }

  public void isAnimating(boolean isAnimating) {
    this.isAnimating = isAnimating;
  }

  private void resetAnimation() {
    if (fadeAnimator != null) {
      fadeAnimator.cancel();
    }
    fadeAnimator = null;
  }

  public boolean isHidden() {
    return fadeCompassViewFacingNorth && isFacingNorth();
  }

  public boolean isFacingNorth() {
    // increase range of facing north to more than only 0.0
    return Math.abs(rotation) >= 359.0 || Math.abs(rotation) <= 1.0;
  }

  @Override
  public void setEnabled(boolean enabled) {
    super.setEnabled(enabled);
    if (enabled && !isHidden()) {
      resetAnimation();
      setAlpha(1.0f);
      setVisibility(View.VISIBLE);
    } else {
      resetAnimation();
      setAlpha(0.0f);
      setVisibility(View.INVISIBLE);
    }

    if (uiSettings != null && uiSettings.isCompassEnabled() != enabled) {
      // store the right state in the LiveData object
      uiSettings.setCompassEnabled(enabled);
    }
  }

  /**
   * Updates the direction of the compass.
   *
   * @param bearing the direction value of the map
   * @deprecated Use {@link UiSettings#getCompassRotationObservable()} instead.
   */
  @Deprecated
  public void update(final double bearing) {
    if (uiSettings != null && uiSettings.getCompassRotationObservable().getValue() != bearing) {
      uiSettings.getCompassRotationObservable().setValue(bearing);
    }
  }

  void updateBearing(double bearing) {
    rotation = (float) bearing;

    if (!isEnabled()) {
      return;
    }

    if (isHidden()) {
      if (getVisibility() == View.INVISIBLE || fadeAnimator != null) {
        return;
      }
      postDelayed(this, TIME_WAIT_IDLE);
      return;
    } else {
      resetAnimation();
      setAlpha(1.0f);
      setVisibility(View.VISIBLE);
    }

    notifyCompassAnimationListenerWhenAnimating();
    setRotation(rotation);
  }

  /**
   * @deprecated Use {@link UiSettings#setCompassFadeFacingNorth(boolean)} instead.
   */
  @Deprecated
  public void fadeCompassViewFacingNorth(boolean compassFadeFacingNorth) {
    if (uiSettings != null && uiSettings.isCompassFadeWhenFacingNorth() != compassFadeFacingNorth) {
      // store the right state in the LiveData object
      uiSettings.setCompassFadeFacingNorth(compassFadeFacingNorth);
    }
  }

  /**
   * @deprecated Use {@link UiSettings#isCompassFadeWhenFacingNorth()} instead.
   */
  @Deprecated
  public boolean isFadeCompassViewFacingNorth() {
    return fadeCompassViewFacingNorth;
  }

  void hideCompassViewFacingNorth(boolean compassFadeFacingNorth) {
    fadeCompassViewFacingNorth = compassFadeFacingNorth;
  }

  /**
   * Set the CompassView image.
   *
   * @param compass the drawable to use as compass image
   * @deprecated Use {@link UiSettings#setCompassImage(Drawable)} instead.
   */
  @Deprecated
  public void setCompassImage(Drawable compass) {
    if (uiSettings != null) {
      uiSettings.setCompassImage(compass);
    } else {
      setImageDrawable(compass);
    }
  }

  /**
   * Get the current configured CompassView image.
   *
   * @return the drawable used as compass image
   * @deprecated Use {@link UiSettings#getCompassImage()} instead.
   */
  @Deprecated
  public Drawable getCompassImage() {
    return getDrawable();
  }

  @Override
  public void run() {
    if (isHidden()) {
      compassAnimationListener.onCompassAnimationFinished();
      resetAnimation();
      setLayerType(View.LAYER_TYPE_HARDWARE, null);
      fadeAnimator = ViewCompat.animate(CompassView.this).alpha(0.0f).setDuration(TIME_FADE_ANIMATION);
      fadeAnimator.setListener(new ViewPropertyAnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(View view) {
          setLayerType(LAYER_TYPE_NONE, null);
          setVisibility(View.INVISIBLE);
          resetAnimation();
        }
      });
    }
  }

  private void notifyCompassAnimationListenerWhenAnimating() {
    if (isAnimating) {
      compassAnimationListener.onCompassAnimation();
    }
  }
}