[android] obtain paths to file directories on a worker thread
This commit is contained in:
committed by
Łukasz Paczos
parent
112bbc7ab2
commit
ff077e9ea9
@@ -11,6 +11,9 @@ import com.mapbox.mapboxsdk.constants.MapboxConstants;
|
||||
import com.mapbox.mapboxsdk.exceptions.MapboxConfigurationException;
|
||||
import com.mapbox.mapboxsdk.maps.Telemetry;
|
||||
import com.mapbox.mapboxsdk.net.ConnectivityReceiver;
|
||||
import com.mapbox.mapboxsdk.storage.FileSource;
|
||||
import com.mapbox.mapboxsdk.utils.ThreadUtils;
|
||||
|
||||
import timber.log.Timber;
|
||||
|
||||
/**
|
||||
@@ -42,8 +45,10 @@ public final class Mapbox {
|
||||
*/
|
||||
@UiThread
|
||||
public static synchronized Mapbox getInstance(@NonNull Context context, @Nullable String accessToken) {
|
||||
ThreadUtils.checkThread("Mapbox");
|
||||
if (INSTANCE == null) {
|
||||
Context appContext = context.getApplicationContext();
|
||||
FileSource.initializeFileDirsPaths(appContext);
|
||||
INSTANCE = new Mapbox(appContext, accessToken);
|
||||
if (isAccessTokenValid(accessToken)) {
|
||||
initializeTelemetry();
|
||||
|
||||
@@ -28,7 +28,7 @@ public abstract class MapRenderer implements MapRendererScheduler {
|
||||
public MapRenderer(Context context, String localIdeographFontFamily) {
|
||||
FileSource fileSource = FileSource.getInstance(context);
|
||||
float pixelRatio = context.getResources().getDisplayMetrics().density;
|
||||
String programCacheDir = context.getCacheDir().getAbsolutePath();
|
||||
String programCacheDir = FileSource.getInternalCachePath(context);
|
||||
|
||||
// Initialise native peer
|
||||
nativeInitialize(this, fileSource, pixelRatio, programCacheDir, localIdeographFontFamily);
|
||||
|
||||
@@ -109,7 +109,7 @@ public class OfflineManager {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
String path = context.getCacheDir().getAbsolutePath() + File.separator + "mbgl-cache.db";
|
||||
String path = FileSource.getInternalCachePath(context) + File.separator + "mbgl-cache.db";
|
||||
File file = new File(path);
|
||||
if (file.exists()) {
|
||||
file.delete();
|
||||
|
||||
@@ -217,7 +217,7 @@ public class MapSnapshotter {
|
||||
checkThread();
|
||||
this.context = context.getApplicationContext();
|
||||
FileSource fileSource = FileSource.getInstance(context);
|
||||
String programCacheDir = context.getCacheDir().getAbsolutePath();
|
||||
String programCacheDir = FileSource.getInternalCachePath(context);
|
||||
|
||||
nativeInitialize(this, fileSource, options.pixelRatio, options.width,
|
||||
options.height, options.styleUrl, options.styleJson, options.region, options.cameraPosition,
|
||||
|
||||
@@ -4,6 +4,7 @@ import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.res.AssetManager;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Environment;
|
||||
import android.support.annotation.Keep;
|
||||
import android.support.annotation.NonNull;
|
||||
@@ -11,6 +12,10 @@ import android.support.annotation.UiThread;
|
||||
|
||||
import com.mapbox.mapboxsdk.Mapbox;
|
||||
import com.mapbox.mapboxsdk.constants.MapboxConstants;
|
||||
import com.mapbox.mapboxsdk.utils.ThreadUtils;
|
||||
|
||||
import java.util.concurrent.locks.Lock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import timber.log.Timber;
|
||||
|
||||
@@ -20,6 +25,11 @@ import timber.log.Timber;
|
||||
*/
|
||||
public class FileSource {
|
||||
|
||||
private static String resourcesCachePath;
|
||||
private static String internalCachePath;
|
||||
private static final Lock resourcesCachePathLoaderLock = new ReentrantLock();
|
||||
private static final Lock internalCachePathLoaderLock = new ReentrantLock();
|
||||
|
||||
/**
|
||||
* This callback allows implementors to transform URLs before they are requested
|
||||
* from the internet. This can be used add or remove custom parameters, or reroute
|
||||
@@ -51,19 +61,20 @@ public class FileSource {
|
||||
@UiThread
|
||||
public static synchronized FileSource getInstance(Context context) {
|
||||
if (INSTANCE == null) {
|
||||
String cachePath = getCachePath(context);
|
||||
INSTANCE = new FileSource(cachePath, context.getResources().getAssets());
|
||||
INSTANCE = new FileSource(getResourcesCachePath(context), context.getResources().getAssets());
|
||||
}
|
||||
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the cache path for a context.
|
||||
* Get files directory path for a context.
|
||||
*
|
||||
* @param context the context to derive the cache path from
|
||||
* @return the cache path
|
||||
* @param context the context to derive the files directory path from
|
||||
* @return the files directory path
|
||||
* @deprecated Use {@link #getResourcesCachePath(Context)} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
public static String getCachePath(Context context) {
|
||||
// Default value
|
||||
boolean setStorageExternal = MapboxConstants.DEFAULT_SET_STORAGE_EXTERNAL;
|
||||
@@ -122,6 +133,89 @@ public class FileSource {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes file directories paths.
|
||||
*
|
||||
* @param context the context to derive paths from
|
||||
*/
|
||||
@UiThread
|
||||
public static void initializeFileDirsPaths(Context context) {
|
||||
ThreadUtils.checkThread("FileSource");
|
||||
lockPathLoaders();
|
||||
if (resourcesCachePath == null || internalCachePath == null) {
|
||||
new FileDirsPathsTask().execute(context);
|
||||
}
|
||||
}
|
||||
|
||||
private static class FileDirsPathsTask extends AsyncTask<Context, Void, String[]> {
|
||||
|
||||
@Override
|
||||
protected void onCancelled() {
|
||||
unlockPathLoaders();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String[] doInBackground(Context... contexts) {
|
||||
return new String[] {
|
||||
getCachePath(contexts[0]),
|
||||
contexts[0].getCacheDir().getAbsolutePath()
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(String[] paths) {
|
||||
resourcesCachePath = paths[0];
|
||||
internalCachePath = paths[1];
|
||||
unlockPathLoaders();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get files directory path for a context.
|
||||
*
|
||||
* @param context the context to derive the files directory path from
|
||||
* @return the files directory path
|
||||
*/
|
||||
public static String getResourcesCachePath(Context context) {
|
||||
resourcesCachePathLoaderLock.lock();
|
||||
try {
|
||||
if (resourcesCachePath == null) {
|
||||
resourcesCachePath = getCachePath(context);
|
||||
}
|
||||
return resourcesCachePath;
|
||||
} finally {
|
||||
resourcesCachePathLoaderLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get internal cache path for a context.
|
||||
*
|
||||
* @param context the context to derive the internal cache path from
|
||||
* @return the internal cache path
|
||||
*/
|
||||
public static String getInternalCachePath(Context context) {
|
||||
internalCachePathLoaderLock.lock();
|
||||
try {
|
||||
if (internalCachePath == null) {
|
||||
internalCachePath = context.getCacheDir().getAbsolutePath();
|
||||
}
|
||||
return internalCachePath;
|
||||
} finally {
|
||||
internalCachePathLoaderLock.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
private static void lockPathLoaders() {
|
||||
internalCachePathLoaderLock.lock();
|
||||
resourcesCachePathLoaderLock.lock();
|
||||
}
|
||||
|
||||
private static void unlockPathLoaders() {
|
||||
resourcesCachePathLoaderLock.unlock();
|
||||
internalCachePathLoaderLock.unlock();
|
||||
}
|
||||
|
||||
@Keep
|
||||
private long nativePtr;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user