summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/location/LocationComponentActivationOptions.java
blob: 3f71209d10fb640c9f08ac41453acac7eb1eb884 (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
package com.mapbox.mapboxsdk.location;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.mapbox.android.core.location.LocationEngine;
import com.mapbox.android.core.location.LocationEngineRequest;
import com.mapbox.mapboxsdk.maps.Style;

/**
 * A class which holds the various options for activating the Maps SDK's {@link LocationComponent} to eventually
 * show the device location on the map.
 */
public class LocationComponentActivationOptions {

  private final Context context;
  private final Style style;
  private final LocationEngine locationEngine;
  private final LocationEngineRequest locationEngineRequest;
  private final LocationComponentOptions locationComponentOptions;
  private final int styleRes;
  private final boolean useDefaultLocationEngine;

  private LocationComponentActivationOptions(@NonNull Context context, @NonNull Style style,
                                             @Nullable LocationEngine locationEngine,
                                             @Nullable LocationEngineRequest locationEngineRequest,
                                             @Nullable LocationComponentOptions locationComponentOptions,
                                             int styleRes,
                                             boolean useDefaultLocationEngine) {
    this.context = context;
    this.style = style;
    this.locationEngine = locationEngine;
    this.locationEngineRequest = locationEngineRequest;
    this.locationComponentOptions = locationComponentOptions;
    this.styleRes = styleRes;
    this.useDefaultLocationEngine = useDefaultLocationEngine;
  }

  /**
   * Convenience method to retrieve a {@link LocationComponentActivationOptions} object which is ready to build with
   *
   * @return a builder object
   */
  @NonNull
  public static Builder builder(@NonNull Context context, @NonNull Style fullyLoadedMapStyle) {
    return new LocationComponentActivationOptions.Builder(context, fullyLoadedMapStyle);
  }

  /**
   * The application's current context
   *
   * @return the application's current context
   */
  @NonNull
  public Context context() {
    return context;
  }

  /**
   * The proxy object for current map style. More info at {@link Style}
   *
   * @return the map's fully loaded Style object
   */
  @NonNull
  public Style style() {
    return style;
  }

  /**
   * The {@link LocationEngine} which the {@link LocationComponent} should use
   *
   * @return the engine
   */
  @Nullable
  public LocationEngine locationEngine() {
    return locationEngine;
  }

  /**
   * The location request which the {@link LocationComponent} should use
   *
   * @return the LocationEngineRequest object
   */
  @Nullable
  public LocationEngineRequest locationEngineRequest() {
    return locationEngineRequest;
  }

  /**
   * A built {@link LocationComponentOptions} object, which holds the various {@link LocationComponent} styling options
   *
   * @return the options for styling the actual LocationComponent
   */
  @Nullable
  public LocationComponentOptions locationComponentOptions() {
    return locationComponentOptions;
  }

  /**
   * The LocationComponent style resource (e.g. R.style.style_name)
   *
   * @return the style resource.
   */
  public int styleRes() {
    return styleRes;
  }

  /**
   * True if you want to initialize and use the built-in location engine or false if there should be no
   * location engine initialized
   *
   * @return whether the default LocationEngine is used
   */
  public boolean useDefaultLocationEngine() {
    return useDefaultLocationEngine;
  }

  /**
   * Builder class for constructing a new instance of {@link LocationComponentActivationOptions}.
   */
  public static class Builder {
    private final Context context;
    private final Style style;
    private LocationEngine locationEngine;
    private LocationEngineRequest locationEngineRequest;
    private LocationComponentOptions locationComponentOptions;
    private int styleRes;

    /**
     * Set to true as the default in case a true/false value isn't declared via the builder's
     * {@link LocationComponentActivationOptions#useDefaultLocationEngine()} method.
     * <p>
     * Please be aware that this activation boolean is ignored when a non-null
     * {@link LocationEngine} is set via the builder's `locationEngine()` method.
     */
    private boolean useDefaultLocationEngine = true;

    /**
     * Constructor for the {@link LocationComponentActivationOptions} builder class.
     * While other activation options are optional, the activation process always requires
     * context and a fully-loaded map {@link Style} object, which is why the two are in this
     * constructor.
     */
    public Builder(@NonNull Context context, @NonNull Style style) {
      this.context = context;
      this.style = style;
    }

    /**
     * Deliver your own {@link LocationEngine} to the LocationComponent.
     * <p>
     * The true/false
     * {@link LocationComponentActivationOptions#builder(Context, Style)#useDefaultLocationEngine}
     * activation option is ignored when a non-null {@link LocationEngine} is set via
     * this `locationEngine()` method.
     *
     * @param locationEngine a {@link LocationEngine} object
     * @return the {@link Builder} object being constructed
     */
    @NonNull
    public Builder locationEngine(@Nullable LocationEngine locationEngine) {
      this.locationEngine = locationEngine;
      return this;
    }

    /**
     * @param locationEngineRequest the location request which the
     *                              {@link LocationComponent} should use
     * @return the {@link Builder} object being constructed
     */
    public Builder locationEngineRequest(LocationEngineRequest locationEngineRequest) {
      this.locationEngineRequest = locationEngineRequest;
      return this;
    }

    /**
     * @param locationComponentOptions a built {@link LocationComponentOptions} object,
     *                                 which holds the various {@link LocationComponent}
     *                                 styling options
     * @return the {@link Builder} object being constructed
     */
    public Builder locationComponentOptions(LocationComponentOptions locationComponentOptions) {
      this.locationComponentOptions = locationComponentOptions;
      return this;
    }

    /**
     * @param styleRes the LocationComponent style resource (e.g. R.style.style_name)
     * @return the {@link Builder} object being constructed
     */
    public Builder styleRes(int styleRes) {
      this.styleRes = styleRes;
      return this;
    }

    /**
     * @param useDefaultLocationEngine true if you want to initialize and use the
     *                                 built-in location engine or false if there
     *                                 should be no location engine initialized
     *                                 This is ignored when null is set as the parameter
     *                                 for {@link LocationComponentActivationOptions#builder
     *                                 (Context, Style)#locationEngine()}.
     * @return the {@link Builder} object being constructed
     */
    public Builder useDefaultLocationEngine(boolean useDefaultLocationEngine) {
      this.useDefaultLocationEngine = useDefaultLocationEngine;
      return this;
    }

    /**
     * Method which actually builds the {@link LocationComponentActivationOptions} object while
     * taking the various options into account.
     *
     * @return a built {@link LocationComponentActivationOptions} object
     */
    public LocationComponentActivationOptions build() {
      if (styleRes != 0 && locationComponentOptions != null) {
        throw new IllegalArgumentException(
            "You've provided both a style resource and a LocationComponentOptions object to the "
                + "LocationComponentActivationOptions builder. You can't use both and "
                + "you must choose one of the two to style the LocationComponent.");
      }
      if (context == null) {
        throw new NullPointerException(
            "Context in LocationComponentActivationOptions is null.");
      }
      if (style == null) {
        throw new NullPointerException(
            "Style in LocationComponentActivationOptions is null. Make sure the "
                + "Style object isn't null. Wait for the map to fully load before passing "
                + "the Style object to LocationComponentActivationOptions.");
      }
      if (!style.isFullyLoaded()) {
        throw new IllegalArgumentException(
            "Style in LocationComponentActivationOptions isn't fully loaded. Wait for the "
                + "map to fully load before passing the Style object to "
                + "LocationComponentActivationOptions.");
      }
      return new LocationComponentActivationOptions(context, style, locationEngine,
          locationEngineRequest, locationComponentOptions, styleRes, useDefaultLocationEngine);
    }
  }
}