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

Commit

Permalink
[android] resume file source to complete resources cache path change
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasPaczos committed Apr 30, 2019
1 parent 9d3c18b commit c8ff475
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mapbox.mapboxsdk.storage;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
Expand All @@ -11,6 +12,7 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.support.annotation.WorkerThread;

import com.mapbox.mapboxsdk.MapStrictMode;
import com.mapbox.mapboxsdk.Mapbox;
Expand All @@ -21,6 +23,7 @@

import java.io.File;
import java.lang.ref.WeakReference;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

Expand Down Expand Up @@ -305,33 +308,91 @@ public void onWritePermissionGranted() {
Logger.w(TAG, fileSourceActivatedMessage);
callback.onError(fileSourceActivatedMessage);
} else {
final SharedPreferences.Editor editor =
context.getSharedPreferences(MapboxConstants.MAPBOX_SHARED_PREFERENCES, Context.MODE_PRIVATE).edit();
editor.putString(MAPBOX_SHARED_PREFERENCE_RESOURCES_CACHE_PATH, path);
editor.apply();
setResourcesCachePath(context, path);
callback.onSuccess(path);
new SetResourcesCachePathTask(context, callback).execute(path);
}
}

@Override
public void onError() {
final ResourcesCachePathChangeCallback callback = callbackWeakReference.get();
if (callback != null) {
String message = "Path is not writable: " + path;
Logger.e(TAG, message);
callback.onError(message);
if (callback == null) {
Logger.w(TAG, "Lost callback reference.");
return;
}

String message = "Path is not writable: " + path;
Logger.e(TAG, message);
callback.onError(message);
}
}).execute(new File(path));
}
}

private static void setResourcesCachePath(@NonNull Context context, @NonNull String path) {
private static class SetResourcesCachePathTask extends AsyncTask<String, Void, String> {

private final WeakReference<Context> contextWeakReference;
private final WeakReference<ResourcesCachePathChangeCallback> callbackWeakReference;
private final FileSource fileSource;
private final AtomicBoolean result = new AtomicBoolean();

SetResourcesCachePathTask(Context context, ResourcesCachePathChangeCallback callback) {
this.fileSource = FileSource.getInstance(context);
this.contextWeakReference = new WeakReference<>(context);
this.callbackWeakReference = new WeakReference<>(callback);
}

@Override
protected void onPreExecute() {
fileSource.activate();
}

@Override
protected String doInBackground(String... strings) {
Context context = contextWeakReference.get();
if (context == null) {
String lostContextMessage = "Lost context reference.";
Logger.w(TAG, lostContextMessage);
result.set(false);
return lostContextMessage;
}

String path = strings[0];
setResourcesCachePath(context, fileSource, path);
result.set(true);
return path;
}

@Override
protected void onPostExecute(String message) {
fileSource.deactivate();

ResourcesCachePathChangeCallback callback = callbackWeakReference.get();
if (callback == null) {
Logger.w(TAG, "Lost callback reference.");
return;
}

if (result.get()) {
callback.onSuccess(message);
} else {
Logger.e(TAG, message);
callback.onError(message);
}
}
}

@SuppressLint("ApplySharedPref")
@WorkerThread
private static void setResourcesCachePath(@NonNull Context context, @NonNull FileSource fileSource,
@NonNull String path) {
resourcesCachePathLoaderLock.lock();
resourcesCachePath = path;
fileSource.setResourceCachePath(path);
SharedPreferences.Editor editor =
context.getSharedPreferences(MapboxConstants.MAPBOX_SHARED_PREFERENCES, Context.MODE_PRIVATE).edit();
editor.putString(MAPBOX_SHARED_PREFERENCE_RESOURCES_CACHE_PATH, path);
editor.commit();
resourcesCachePathLoaderLock.unlock();
getInstance(context).setResourceCachePath(path);
}

private static boolean isPathWritable(String path) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mapbox.mapboxsdk.testapp.storage

import android.os.Handler
import android.support.test.annotation.UiThreadTest
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
Expand All @@ -9,7 +8,6 @@ import com.mapbox.mapboxsdk.testapp.activity.FeatureOverviewActivity
import org.junit.*
import org.junit.rules.TestName
import org.junit.runner.RunWith
import java.util.concurrent.CountDownLatch

@RunWith(AndroidJUnit4::class)
class FileSourceStandaloneTest {
Expand Down Expand Up @@ -55,17 +53,6 @@ class FileSourceStandaloneTest {
fileSourceTestUtils.changePath(fileSourceTestUtils.testPath)
Assert.assertEquals(fileSourceTestUtils.testPath, FileSource.getResourcesCachePath(rule.activity))

// workaround for https://github.com/mapbox/mapbox-gl-native/issues/14334
val latch = CountDownLatch(1)
rule.activity.runOnUiThread {
fileSource.activate()
Handler().postDelayed({
fileSource.deactivate()
latch.countDown()
}, 2000)
}
latch.await()

fileSourceTestUtils.changePath(fileSourceTestUtils.originalPath)
Assert.assertEquals(fileSourceTestUtils.originalPath, FileSource.getResourcesCachePath(rule.activity))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class FileSourceTestUtils(private val activity: Activity) {
activity,
path,
object : FileSource.ResourcesCachePathChangeCallback {
override fun onSuccess(path: String?) {
override fun onSuccess(result: String?) {
Assert.assertEquals(path, result)
latch.countDown()
}

Expand Down

0 comments on commit c8ff475

Please sign in to comment.