summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/MyLocationViewSettings.java
blob: ec7c53e1d0e10e84eb0ef5680e561f490c1f54f3 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package com.mapbox.mapboxsdk.maps.widgets;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;

import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.constants.MyLocationTracking;
import com.mapbox.mapboxsdk.maps.FocalPointChangeListener;
import com.mapbox.mapboxsdk.maps.MapboxMapOptions;
import com.mapbox.mapboxsdk.maps.Projection;
import com.mapbox.mapboxsdk.utils.BitmapUtils;

/**
 * Settings to configure the visual appearance of the MyLocationView.
 *
 * @deprecated use location layer plugin from
 * https://github.com/mapbox/mapbox-plugins-android/tree/master/plugins/locationlayer instead.
 */
@Deprecated
public class MyLocationViewSettings {

  private Projection projection;
  private MyLocationView myLocationView;
  private FocalPointChangeListener focalPointChangeListener;

  //
  // State
  //

  private boolean enabled;

  //
  // Foreground
  //

  private Drawable foregroundDrawable;
  private Drawable foregroundBearingDrawable;

  @ColorInt
  private int foregroundTintColor;

  //
  // Background
  //

  private Drawable backgroundDrawable;
  private int[] backgroundOffset = new int[4];

  @ColorInt
  private int backgroundTintColor;

  //
  // Accuracy
  //

  private int accuracyAlpha;
  private float accuracyThreshold = 0f;

  @ColorInt
  private int accuracyTintColor;

  //
  // Padding
  //

  private int[] padding = new int[4];

  /**
   * Creates an instance of MyLocationViewSettings
   * <p>
   *
   * @param myLocationView            the MyLocationView to apply the settings to
   * @param projection                the MapView projection
   * @param focalPointChangedListener the interface to be invoked when focal points changes
   * @see MyLocationView
   */
  public MyLocationViewSettings(MyLocationView myLocationView, Projection projection, FocalPointChangeListener
    focalPointChangedListener) {
    this.myLocationView = myLocationView;
    this.projection = projection;
    this.focalPointChangeListener = focalPointChangedListener;
  }

  /**
   * Initialise this with MapboxMapOptions.
   *
   * @param options the options to initialise this class from
   */
  public void initialise(@NonNull MapboxMapOptions options) {
    CameraPosition position = options.getCamera();
    if (position != null && !position.equals(CameraPosition.DEFAULT)) {
      setTilt(position.tilt);
    }
    setForegroundDrawable(options.getMyLocationForegroundDrawable(), options.getMyLocationForegroundBearingDrawable());
    setForegroundTintColor(options.getMyLocationForegroundTintColor());
    setBackgroundDrawable(options.getMyLocationBackgroundDrawable(), options.getMyLocationBackgroundPadding());
    setBackgroundTintColor(options.getMyLocationBackgroundTintColor());
    setAccuracyAlpha(options.getMyLocationAccuracyAlpha());
    setAccuracyTintColor(options.getMyLocationAccuracyTintColor());
    setAccuracyThreshold(options.getMyLocationAccuracyThreshold());
  }

  public void onSaveInstanceState(Bundle outState) {
    outState.putBoolean(MapboxConstants.STATE_LOCATION_VIEW_ENABLED, isEnabled());
    outState.putByteArray(
      MapboxConstants.STATE_LOCATION_VIEW_FOREGROUND_DRAWABLE,
      BitmapUtils.getByteArrayFromDrawable(getForegroundDrawable())
    );
    outState.putByteArray(
      MapboxConstants.STATE_LOCATION_VIEW_FOREGROUND_BEARING_DRAWABLE,
      BitmapUtils.getByteArrayFromDrawable(getForegroundBearingDrawable())
    );
    outState.putInt(MapboxConstants.STATE_LOCATION_VIEW_FOREGROUND_TINT_COLOR, getForegroundTintColor());
    outState.putByteArray(
      MapboxConstants.STATE_LOCATION_VIEW_BACKGROUND_DRAWABLE,
      BitmapUtils.getByteArrayFromDrawable(getBackgroundDrawable())
    );
    outState.putIntArray(MapboxConstants.STATE_LOCATION_VIEW_BACKGROUND_OFFSET, getBackgroundOffset());
    outState.putInt(MapboxConstants.STATE_LOCATION_VIEW_BACKGROUND_TINT_COLOR, getBackgroundTintColor());
    outState.putInt(MapboxConstants.STATE_LOCATION_VIEW_ACCURACY_ALPHA, getAccuracyAlpha());
    outState.putInt(MapboxConstants.STATE_LOCATION_VIEW_ACCURACY_TINT_COLOR, getAccuracyTintColor());
    outState.putFloat(MapboxConstants.STATE_LOCATION_VIEW_ACCURACY_THRESHOLD, getAccuracyThreshold());
    outState.putIntArray(MapboxConstants.STATE_LOCATION_VIEW_PADDING, getPadding());
  }

  public void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
    setEnabled(savedInstanceState.getBoolean(MapboxConstants.STATE_LOCATION_VIEW_ENABLED));
    setForegroundDrawable(
      BitmapUtils.getDrawableFromByteArray(
        myLocationView.getContext(),
        savedInstanceState.getByteArray(MapboxConstants.STATE_LOCATION_VIEW_FOREGROUND_DRAWABLE)
      ),
      BitmapUtils.getDrawableFromByteArray(
        myLocationView.getContext(),
        savedInstanceState.getByteArray(MapboxConstants.STATE_LOCATION_VIEW_FOREGROUND_BEARING_DRAWABLE)
      )
    );
    setForegroundTintColor(savedInstanceState.getInt(MapboxConstants.STATE_LOCATION_VIEW_FOREGROUND_TINT_COLOR));
    setBackgroundDrawable(
      BitmapUtils.getDrawableFromByteArray(
        myLocationView.getContext(),
        savedInstanceState.getByteArray(MapboxConstants.STATE_LOCATION_VIEW_BACKGROUND_DRAWABLE)
      ),
      savedInstanceState.getIntArray(MapboxConstants.STATE_LOCATION_VIEW_BACKGROUND_OFFSET)
    );
    setBackgroundTintColor(savedInstanceState.getInt(MapboxConstants.STATE_LOCATION_VIEW_BACKGROUND_TINT_COLOR));
    setAccuracyAlpha(savedInstanceState.getInt(MapboxConstants.STATE_LOCATION_VIEW_ACCURACY_ALPHA));
    setAccuracyTintColor(savedInstanceState.getInt(MapboxConstants.STATE_LOCATION_VIEW_ACCURACY_TINT_COLOR));
    setAccuracyThreshold(savedInstanceState.getFloat(MapboxConstants.STATE_LOCATION_VIEW_ACCURACY_THRESHOLD));
    setPadding(savedInstanceState.getIntArray(MapboxConstants.STATE_LOCATION_VIEW_PADDING));
  }

  /**
   * Returns if the MyLocationView is enabled
   *
   * @return true if MyLocationView is enabled,
   */
  public boolean isEnabled() {
    return enabled;
  }

  /**
   * Set the enabled state of MyLocationView
   *
   * @param enabled true shows the MyLocationView on the map
   */
  public void setEnabled(boolean enabled) {
    this.enabled = enabled;
    myLocationView.setEnabled(enabled);
  }

  /**
   * Set the foreground drawable of the MyLocationView
   * <p>
   * The foreground drawable is the image visible on screen
   * </p>
   * It's linked with the foreground tint color
   *
   * @param foregroundDrawable        the drawable to show as foreground without bearing
   * @param foregroundBearingDrawable the drawable to show as foreground when bearing is enabled
   */
  public void setForegroundDrawable(Drawable foregroundDrawable, Drawable foregroundBearingDrawable) {
    this.foregroundDrawable = foregroundDrawable;
    this.foregroundBearingDrawable = foregroundBearingDrawable;
    myLocationView.setForegroundDrawables(foregroundDrawable, foregroundBearingDrawable);
    myLocationView.setForegroundDrawableTint(foregroundTintColor);
  }

  /**
   * Get the foreground drawable when bearing is disabled.
   *
   * @return the drawable used as foreground
   */
  public Drawable getForegroundDrawable() {
    return foregroundDrawable;
  }

  /**
   * Get the foreground drawable when bearing is enabled.
   *
   * @return the bearing drawable used as foreground
   */
  public Drawable getForegroundBearingDrawable() {
    return foregroundBearingDrawable;
  }

  /**
   * Set the foreground tint color.
   * <p>
   * The color will tint both the foreground and the bearing foreground drawable.
   * </p>
   *
   * @param foregroundTintColor the color to tint the foreground drawable or -1 (undefined color) to remove the
   *                            existing foreground tint color
   */
  public void setForegroundTintColor(@ColorInt int foregroundTintColor) {
    this.foregroundTintColor = foregroundTintColor;
    myLocationView.setForegroundDrawableTint(foregroundTintColor);
  }

  /**
   * Get the foreground tint color.
   *
   * @return the foreground tint color
   */
  public int getForegroundTintColor() {
    return foregroundTintColor;
  }

  /**
   * Set the background drawable of MyLocationView
   * <p>
   * Padding can be added to provide an offset to the background
   * </p>
   * It's linked with the background tint color
   *
   * @param backgroundDrawable the drawable to show as background
   * @param padding            the padding added to the background
   */
  public void setBackgroundDrawable(Drawable backgroundDrawable, int[] padding) {
    this.backgroundDrawable = backgroundDrawable;
    this.backgroundOffset = padding;
    if (padding != null && padding.length == 4) {
      myLocationView.setShadowDrawable(backgroundDrawable, padding[0], padding[1], padding[2], padding[3]);
    } else {
      myLocationView.setShadowDrawable(backgroundDrawable);
    }
    myLocationView.setShadowDrawableTint(backgroundTintColor);
  }

  /**
   * Get the background drawable of MyLocationView.
   *
   * @return the drawable used as background
   */
  public Drawable getBackgroundDrawable() {
    return backgroundDrawable;
  }

  /**
   * Set the background tint color.
   *
   * @param backgroundTintColor the color to tint the background drawable or -1 (undefined color) to remove the
   *                            existing background tint color
   */
  public void setBackgroundTintColor(@ColorInt int backgroundTintColor) {
    this.backgroundTintColor = backgroundTintColor;
    myLocationView.setShadowDrawableTint(backgroundTintColor);
  }

  /**
   * Get the background tint color.
   *
   * @return the background tint color
   */
  public int getBackgroundTintColor() {
    return backgroundTintColor;
  }

  /**
   * Get the background offset.
   *
   * @return the background offset
   */
  public int[] getBackgroundOffset() {
    return backgroundOffset;
  }

  /**
   * Set the MyLocationView padding.
   *
   * @param left   the padding left of MyLocationView
   * @param top    the padding top of MyLocationView
   * @param right  the padding right of MyLocationView
   * @param bottom the padding bottom of MyLocaionView
   */
  public void setPadding(int left, int top, int right, int bottom) {
    padding = new int[] {left, top, right, bottom};
    setPadding(padding);
  }

  private void setPadding(int[] padding) {
    myLocationView.setContentPadding(padding);
    projection.invalidateContentPadding(padding);
    invalidateFocalPointForTracking(myLocationView);
  }

  /**
   * Get the MyLocationView padding.
   *
   * @return an array describing the padding in a LTRB manner
   */
  public int[] getPadding() {
    return padding;
  }

  /**
   * Get the alpha value of the accuracy circle of MyLocationView
   *
   * @return the alpha value
   */
  public int getAccuracyAlpha() {
    return accuracyAlpha;
  }

  /**
   * Set the alpha value of the accuracy circle of MyLocationView
   *
   * @param accuracyAlpha the alpha value to set
   */
  public void setAccuracyAlpha(@IntRange(from = 0, to = 255) int accuracyAlpha) {
    this.accuracyAlpha = accuracyAlpha;
    myLocationView.setAccuracyAlpha(accuracyAlpha);
  }

  /**
   * Get the accuracy tint color of MyLocationView.
   *
   * @return the tint color used for accuracy
   */
  public int getAccuracyTintColor() {
    return accuracyTintColor;
  }

  /**
   * Set the accuracy tint color of MyLocationView.
   *
   * @param accuracyTintColor the accuracy tint color
   */
  public void setAccuracyTintColor(@ColorInt int accuracyTintColor) {
    this.accuracyTintColor = accuracyTintColor;
    myLocationView.setAccuracyTint(accuracyTintColor);
  }

  /**
   * Returns current accuracy threshold value (in meters).
   *
   * @return Value of accuracy threshold (in meters), below which circle won't be displayed
   */
  public float getAccuracyThreshold() {
    return accuracyThreshold;
  }

  /**
   * Set accuracy circle threshold. Circle won't be displayed if accuracy is below set value.
   *
   * @param accuracyThreshold Value of accuracy (in meters), below which circle won't be displayed
   */
  public void setAccuracyThreshold(float accuracyThreshold) {
    this.accuracyThreshold = accuracyThreshold;
    myLocationView.setAccuracyThreshold(accuracyThreshold);
  }

  public void setTilt(double tilt) {
    myLocationView.setTilt(tilt);
  }

  private void invalidateFocalPointForTracking(MyLocationView myLocationView) {
    if (!(myLocationView.getMyLocationTrackingMode() == MyLocationTracking.TRACKING_NONE)) {
      focalPointChangeListener.onFocalPointChanged(myLocationView.getCenter());
    } else {
      focalPointChangeListener.onFocalPointChanged(null);
    }
  }
}