Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] obtain paths to file directories on a worker thread
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasPaczos committed Aug 20, 2018
1 parent 112bbc7 commit ff077e9
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
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;

/**
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private void deleteAmbientDatabase(final Context context) {
@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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public MapSnapshotter(@NonNull Context context, @NonNull Options options) {
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
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;
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;

Expand All @@ -20,6 +25,11 @@
*/
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
Expand Down Expand Up @@ -51,19 +61,20 @@ public interface ResourceTransformCallback {
@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;
Expand Down Expand Up @@ -122,6 +133,89 @@ public static boolean isExternalStorageReadable() {
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;

Expand Down

0 comments on commit ff077e9

Please sign in to comment.