summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/offline/OfflineRegion.java
blob: ee6f8aa87ffe97cec1fb9c50a8cac2fd7782270b (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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package com.mapbox.mapboxsdk.offline;

import android.os.Handler;
import android.os.Looper;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.mapbox.mapboxsdk.storage.FileSource;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * An offline region is the basic building block for offline mobile maps.
 * Use {@link com.mapbox.mapboxsdk.offline.OfflineManager.CreateOfflineRegionCallback}
 * to create a new offline region.
 */
public class OfflineRegion {

  //
  // Static methods
  //

  static {
    System.loadLibrary("mapbox-gl");
  }

  // Members

  // Holds the pointer to JNI OfflineRegion
  private long nativePtr;

  // Holds a reference to the FileSource to keep it alive
  private FileSource fileSource;

  //Region id
  private long id;

  // delete status
  private boolean isDeleted;

  private OfflineRegionDefinition definition;

  /**
   * Arbitrary binary region metadata. The contents are opaque to the SDK implementation;
   * it just stores and retrieves a byte[]. Check the `OfflineActivity` in the TestApp
   * for a sample implementation that uses JSON to store an offline region name.
   */
  private byte[] metadata;

  // Makes sure callbacks come back to the main thread
  private Handler handler;

  /**
   * A region can have a single observer, which gets notified whenever a change
   * to the region's status occurs.
   */
  public interface OfflineRegionObserver {
    /**
     * Implement this method to be notified of a change in the status of an
     * offline region. Status changes include any change in state of the members
     * of OfflineRegionStatus.
     * <p>
     * This method will be executed on the main thread.
     * </p>
     *
     * @param status the changed status
     */
    void onStatusChanged(OfflineRegionStatus status);

    /**
     * Implement this method to be notified of errors encountered while downloading
     * regional resources. Such errors may be recoverable; for example the implementation
     * will attempt to re-request failed resources based on an exponential backoff
     * algorithm, or when it detects that network access has been restored.
     * <p>
     * This method will be executed on the main thread.
     * </p>
     *
     * @param error the offline region error message
     */
    void onError(OfflineRegionError error);

    /*
     * Implement this method to be notified when the limit on the number of Mapbox
     * tiles stored for offline regions has been reached.
     *
     * Once the limit has been reached, the SDK will not download further offline
     * tiles from Mapbox APIs until existing tiles have been removed. Contact your
     * Mapbox sales representative to raise the limit.
     *
     * This limit does not apply to non-Mapbox tile sources.
     *
     * This method will be executed on the main thread.
     */
    void mapboxTileCountLimitExceeded(long limit);
  }

  /**
   * This callback receives an asynchronous response containing the OfflineRegionStatus
   * of the offline region, or a {@link String} error message otherwise.
   */
  public interface OfflineRegionStatusCallback {
    /**
     * Receives the status
     *
     * @param status the offline region status
     */
    void onStatus(OfflineRegionStatus status);

    /**
     * Receives the error message
     *
     * @param error the error message
     */
    void onError(String error);
  }

  /**
   * This callback receives an asynchronous response containing a notification when
   * an offline region has been deleted, or a {@link String} error message otherwise.
   */
  public interface OfflineRegionDeleteCallback {
    /**
     * Receives the delete notification
     */
    void onDelete();

    /**
     * Receives the error message
     *
     * @param error the error message
     */
    void onError(String error);
  }

  /**
   * This callback receives an asynchronous response containing the newly update
   * OfflineMetadata in the database, or an error message otherwise.
   */
  public interface OfflineRegionUpdateMetadataCallback {
    /**
     * Receives the newly update offline region metadata.
     *
     * @param metadata the offline metadata to u[date
     */
    void onUpdate(byte[] metadata);

    /**
     * Receives the error message.
     *
     * @param error the error message to be shown
     */
    void onError(String error);
  }

  /**
   * A region is either inactive (not downloading, but previously-downloaded
   * resources are available for use), or active (resources are being downloaded
   * or will be downloaded, if necessary, when network access is available).
   * <p>
   * This state is independent of whether or not the complete set of resources
   * is currently available for offline use. To check if that is the case, use
   * `OfflineRegionStatus.isComplete()`.
   * </p>
   */

  @IntDef( {STATE_INACTIVE, STATE_ACTIVE})
  @Retention(RetentionPolicy.SOURCE)
  public @interface DownloadState {
  }

  public static final int STATE_INACTIVE = 0;
  public static final int STATE_ACTIVE = 1;

  // Keep track of the region state
  private int state = STATE_INACTIVE;

  private boolean deliverInactiveMessages = false;

  /**
   * Gets whether or not the `OfflineRegionObserver` will continue to deliver messages even if
   * the region state has been set as STATE_INACTIVE.
   *
   * @return true if delivering inactive messages
   */
  public boolean isDeliveringInactiveMessages() {
    return deliverInactiveMessages;
  }

  /**
   * When set true, the `OfflineRegionObserver` will continue to deliver messages even if
   * the region state has been set as STATE_INACTIVE (operations happen asynchronously). If set
   * false, the client won't be notified of further messages.
   *
   * @param deliverInactiveMessages true if it should deliver inactive messages
   */
  public void setDeliverInactiveMessages(boolean deliverInactiveMessages) {
    this.deliverInactiveMessages = deliverInactiveMessages;
  }

  private boolean deliverMessages() {
    if (state == STATE_ACTIVE) {
      return true;
    }
    return isDeliveringInactiveMessages();
  }

  /**
   * Constructor
   * <p>
   * For JNI use only, to create a new offline region, use
   * {@link OfflineManager#createOfflineRegion} instead.
   */
  private OfflineRegion(long offlineRegionPtr, FileSource fileSource, long id,
                        OfflineRegionDefinition definition, byte[] metadata) {
    this.fileSource = fileSource;
    this.id = id;
    this.definition = definition;
    this.metadata = metadata;
    initialize(offlineRegionPtr, fileSource);
  }

  /*
   * Getters
   */

  public long getID() {
    return id;
  }

  public OfflineRegionDefinition getDefinition() {
    return definition;
  }

  public byte[] getMetadata() {
    return metadata;
  }

  private Handler getHandler() {
    if (handler == null) {
      handler = new Handler(Looper.getMainLooper());
    }

    return handler;
  }

  /**
   * Register an observer to be notified when the state of the region changes.
   *
   * @param observer the observer to be notified
   */
  public void setObserver(@Nullable final OfflineRegionObserver observer) {
    setOfflineRegionObserver(new OfflineRegionObserver() {
      @Override
      public void onStatusChanged(final OfflineRegionStatus status) {
        if (deliverMessages()) {
          getHandler().post(new Runnable() {
            @Override
            public void run() {
              if (observer != null) {
                observer.onStatusChanged(status);
              }
            }
          });
        }
      }

      @Override
      public void onError(final OfflineRegionError error) {
        if (deliverMessages()) {
          getHandler().post(new Runnable() {
            @Override
            public void run() {
              if (observer != null) {
                observer.onError(error);
              }
            }
          });
        }
      }

      @Override
      public void mapboxTileCountLimitExceeded(final long limit) {
        if (deliverMessages()) {
          getHandler().post(new Runnable() {
            @Override
            public void run() {
              if (observer != null) {
                observer.mapboxTileCountLimitExceeded(limit);
              }
            }
          });
        }
      }
    });
  }

  /**
   * Pause or resume downloading of regional resources.
   *
   * @param state the download state
   */
  public void setDownloadState(@DownloadState int state) {
    this.state = state;
    setOfflineRegionDownloadState(state);
  }

  /**
   * Retrieve the current status of the region. The query will be executed
   * asynchronously and the results passed to the given callback which will be
   * executed on the main thread.
   *
   * @param callback the callback to invoked.
   */
  public void getStatus(@NonNull final OfflineRegionStatusCallback callback) {
    getOfflineRegionStatus(new OfflineRegionStatusCallback() {
      @Override
      public void onStatus(final OfflineRegionStatus status) {
        getHandler().post(new Runnable() {
          @Override
          public void run() {
            callback.onStatus(status);
          }
        });
      }

      @Override
      public void onError(final String error) {
        getHandler().post(new Runnable() {
          @Override
          public void run() {
            callback.onError(error);
          }
        });
      }
    });
  }

  /**
   * Remove an offline region from the database and perform any resources evictions
   * necessary as a result.
   * <p>
   * Eviction works by removing the least-recently requested resources not also required
   * by other regions, until the database shrinks below a certain size.
   * </p>
   * <p>
   * When the operation is complete or encounters an error, the given callback will be
   * executed on the main thread.
   * </p>
   * <p>
   * After you call this method, you may not call any additional methods on this object.
   * </p>
   *
   * @param callback the callback to be invoked
   */
  public void delete(@NonNull final OfflineRegionDeleteCallback callback) {
    if (!isDeleted) {
      isDeleted = true;
      deleteOfflineRegion(new OfflineRegionDeleteCallback() {
        @Override
        public void onDelete() {
          getHandler().post(new Runnable() {
            @Override
            public void run() {
              callback.onDelete();
              OfflineRegion.this.finalize();
            }
          });
        }

        @Override
        public void onError(final String error) {
          getHandler().post(new Runnable() {
            @Override
            public void run() {
              isDeleted = false;
              callback.onError(error);
            }
          });
        }
      });
    }
  }

  /**
   * Update an offline region metadata from the database.
   * <p>
   * When the operation is complete or encounters an error, the given callback will be
   * executed on the main thread.
   * </p>
   * <p>
   * After you call this method, you may not call any additional methods on this object.
   * </p>
   *
   * @param bytes    the metadata in bytes
   * @param callback the callback to be invoked
   */
  public void updateMetadata(@NonNull final byte[] bytes, @NonNull final OfflineRegionUpdateMetadataCallback callback) {
    updateOfflineRegionMetadata(bytes, new OfflineRegionUpdateMetadataCallback() {
      @Override
      public void onUpdate(final byte[] metadata) {
        getHandler().post(new Runnable() {
          @Override
          public void run() {
            OfflineRegion.this.metadata = metadata;
            callback.onUpdate(metadata);
          }
        });
      }

      @Override
      public void onError(final String error) {
        getHandler().post(new Runnable() {
          @Override
          public void run() {
            callback.onError(error);
          }
        });
      }
    });
  }

  private native void initialize(long offlineRegionPtr, FileSource fileSource);

  @Override
  protected native void finalize();

  private native void setOfflineRegionObserver(OfflineRegionObserver callback);

  private native void setOfflineRegionDownloadState(@DownloadState int offlineRegionDownloadState);

  private native void getOfflineRegionStatus(OfflineRegionStatusCallback callback);

  private native void deleteOfflineRegion(OfflineRegionDeleteCallback callback);

  private native void updateOfflineRegionMetadata(byte[] metadata, OfflineRegionUpdateMetadataCallback callback);

}