diff options
author | Wilhelm Berg <wb@BergWerk-GIS.at> | 2018-08-22 16:56:32 +0200 |
---|---|---|
committer | Wilhelm Berg <wb@BergWerk-GIS.at> | 2018-08-22 16:56:32 +0200 |
commit | 797f622a26b34db5a30e7f1728d87c6700a2ec1e (patch) | |
tree | 75ea93979db2baf3eb2234c6fbb3d1a9c5b70329 /platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/storage | |
parent | 86fc3f525e42d042f239876b93525ff411ad7182 (diff) | |
parent | 520df7f02049cdbbb9e68041e755d6c3a8d5b21f (diff) | |
download | qtlocation-mapboxgl-797f622a26b34db5a30e7f1728d87c6700a2ec1e.tar.gz |
Merge branch 'master' of github.com:mapbox/mapbox-gl-native into bwg-vs2017
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/storage')
-rw-r--r-- | platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/storage/FileSource.java | 104 |
1 files changed, 99 insertions, 5 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/storage/FileSource.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/storage/FileSource.java index c8d02c05d9..495e425976 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/storage/FileSource.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/storage/FileSource.java @@ -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; |