summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/fs/FileSource.java
blob: 1ec6a8db6173c809e30c573f1adf6b265f72f6fb (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
package com.mapbox.mapboxsdk.fs;

import android.content.Context;

import com.mapbox.mapboxsdk.offline.OfflineManager;

import java.lang.ref.WeakReference;

/**
 * Holds a central reference to the core's DefaultFileSource for as long as
 * there are active mapviews / offline managers
 */
public class FileSource {

  // Use weak reference to avoid blocking GC on this reference.
  // Should only block when mapview / Offline manager instances
  // are alive
  private static WeakReference<FileSource> INSTANCE;

  public static synchronized FileSource getInstance(Context context) {
    if (INSTANCE == null || INSTANCE.get() == null) {
      String cachePath = OfflineManager.getDatabasePath(context);
      String apkPath = context.getPackageCodePath();
      INSTANCE = new WeakReference<>(new FileSource(cachePath, apkPath));
    }

    return INSTANCE.get();
  }

  private long nativePtr;

  private FileSource(String cachePath, String apkPath) {
    initialize(cachePath, apkPath);
  }

  private native void initialize(String cachePath, String apkPath);

  @Override
  protected native void finalize() throws Throwable;

}